How to index database table in yii2 using elasticsearch

When adding record to elasticsearch one-by-one using the following code it works:


$elastic = new Elastic();

$elastic->name  = 'Jhon Doe';

$elastic->email = 'johndoe@email.com';

if ($elastic->insert()) {

    echo "Added Successfully";

} else {

    echo "Error";

}

But when I tried to index complete table using the following code it is not work


$elastic = Elastic::createIndex();

if (isset($elastic)) {

    echo "Index created Successfully";

} else {

    echo "Error";

}

This is the createIndex() method inside Elastic model


public static function createIndex(){

    $db = static::getDb();

    $command = $db->createCommand();

    $command->createIndex(static::index(), ['mappings' => static::mapping()]);

}

Please tell me how can I index complete table using elastic search in yii2. My table has three fields ‘id’, ‘name’ and ‘email’. I’m using yii2-elasticsearch extension.

In the first block of code you created/updated a document in ElasticSearch, so the index is already created.

How many indexes do you have?

I think you are confusing the Elastic Search´s Indexes with index/create/update a document.

Can you please share the correct code how to index a database table that has three fields ‘id’, ‘name’ & ‘email’ in elastic search.