I followed the Github documentation for Yii2 ElasticSearch Installation. I have configured my settings as given below:
I added the elasticSearch extension to vendor/yiisoft/yii2-elasticsearch using composer correctly.
In web.php, I added the db settings:
/* added db config for elasticsearch example*/
'db' => [
'class' => 'yii\db\Connection',
'dsn' => 'mysql:host=localhost;dbname=yii2',
'username' => 'root',
'password' => '',
'enableSchemaCache' => true,
// Duration of schema cache.
'schemaCacheDuration' => 3600,
// Name of the cache component used. Default is 'cache'.
'schemaCache' => 'cache',
],
/* added db config for elasticsearch example*/
In Customer Model, I have extended "\yii\elasticsearch\ActiveRecord" elasticSearch extension.
namespace app\models;
/**
* This is the model class for table "customer".
*
* @property integer $id
* @property string $name
* @property string $address
* @property string $registration_date
*/
//class Customer extends \yii\db\ActiveRecord
class Customer extends \yii\elasticsearch\ActiveRecord
{
public function attributes()
{
// path mapping for '_id' is setup to field 'id'
return ['id', 'name', 'address', 'registration_date'];
}
/**
* @inheritdoc
*/
public static function tableName()
{
return 'customer';
}
}
In my view page, I call the Customer model that extends elasticSearch server as follows,
When I remove “\yii\elasticsearch\ActiveRecord” from Customer model, the application works. If I include it, I get this error. Why do I get “Class ‘Customer’ not found” error?
Thanks, ORey. There is no mention about this inclusion in documentation. Those new to elasticSearch should notice this. Okay, after adding the line as you suggested, I get a new error "Elasticsearch request failed with code 400.". Is my configuration incorrect?
@ceBe, I have removed method "tableName" and now I used the same coding found in API documentation. These are the ones I use now, I call this in a view page.
$customer = new Customer();
$customer->primaryKey = 1; // in this case equivalent to $customer->id = 1;
$customer->attributes = ['name' => 'test'];
$customer->save();
$query = Customer::get(1); // get a record by pk
I believe installation is correct but something is missing or wrong which makes it not work. Provide a solution quicker, it a bit urgent. There is no response from IRC too.
Yes, as you can see the question of this thread, my class extends “yii\elasticsearch\ActiveRecord” correctly and I downloaded the latest yii2 version for utilizing ElasticSearch. But it doesn’t work and I am still getting,