Skip to content

This content is for v5.0. Switch to the latest version for up-to-date documentation.

Index-model field mapping

Define your index-model’s field mapping and embedded relationships to fine-tune your indexed data

By default, the Index Model will be built with all the fields it finds from the Base Model during synchronisation.

However, you can customize this by defining a fieldMap() method in your Index Model.

use PDPhilip\ElasticLens\Builder\IndexBuilder;
use PDPhilip\ElasticLens\Builder\IndexField;
class IndexedUser extends IndexModel
{
protected $baseModel = User::class;
public function fieldMap(): IndexBuilder
{
return IndexBuilder::map(User::class, function (IndexField $field) {
$field->text('first_name');
$field->text('last_name');
$field->text('email');
$field->bool('is_active'); //See attributes as fields
$field->type('state', UserState::class); //Maps enum
$field->text('created_at');
$field->text('updated_at');
});
}