Elasticsearch In Yii2 Installation Throws Class 'customer' Not Found Error

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,




$query = Customer::find()->where(['address' => 'Chennai city'])->one(); // find by query

var_dump($query);



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?

You probably forgot to namespace your class.

Either use \app\models\Customer, or add "use \app\models\Customer" at the beginning of controller

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?

The settings you show are for mysql, not elasticsearch… however the default config seems to work in your case.

There is no such thing as a tableName in elasticsearch, please read the README and API docs for how to define AR class correctly.

http://stuff.cebe.cc/yii2docs/ext_elasticsearch_index.html#using-the-activerecord

http://stuff.cebe.cc/yii2docs/yii_elasticsearch_activerecord.html

Please paste the complete error message including the details about elasticsearch response.

Make sure your index and type are existing when doing a find call.

@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



And I get the following error,




Setting read-only property: app\models\Customer::primaryKey



Do I miss something?

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.

Are you sure you extend your class from yii\elasticsearch\ActiveRecord instead of yii\db\ActiveRecord and do you use the latest version?

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,


Setting read-only property: app\models\Customer::primaryKey

error. Any workaround for this issue?

Hi,

Have you tried to override the index and the type (in your case optional) in your model?

something like that:




...

public static function index() {

  return 'my_index'

}


public static function type() {

  return 'customer'

}


public function attributes() {

  ...

}

...



Hello,

I am using same method for elasticsearch but getting the Unknown component ID: elasticsearch errors.

if u have any suggestion for same then let me know.