Skip to content

Index-model migrations

Elasticsearch auto-indexes new fields, but it doesn’t always guess right. Take control with migrationMap().

Elasticsearch indexes fields automatically, but it may not type them the way you need. A field you want as keyword might get mapped as text, or vice versa. The migrationMap() gives you explicit control.

Since the Index Model is built on Laravel-Elasticsearch, use the Index Blueprint to define your schema:

use PDPhilip\Elasticsearch\Schema\Blueprint;
class IndexedUser extends IndexModel
{
//......
public function migrationMap(): callable
{
return function (Blueprint $index) {
$index->text('name');
$index->keyword('first_name');
$index->text('first_name');
$index->keyword('last_name');
$index->text('last_name');
$index->keyword('email');
$index->text('email');
$index->text('avatar')->indexField(false);
$index->keyword('type');
$index->text('type');
$index->keyword('state');
$index->text('state');
//...etc
};
}
Terminal window
php artisan lens:migrate User

This deletes the existing index, runs the migration, and bulk rebuilds all records.

ElasticLens Migrate