borales / yii2-elasticsearch-behavior
Yii2 behavior to support Elasticsearch indexing
Installs: 9 987
Dependents: 0
Suggesters: 0
Security: 0
Stars: 21
Watchers: 5
Forks: 6
Open Issues: 5
Type:yii2-extension
pkg:composer/borales/yii2-elasticsearch-behavior
Requires
This package is auto-updated.
Last update: 2025-10-19 00:54:42 UTC
README
Yii2 AR behavior to support Elasticsearch auto-indexing.
Installation
The preferred way to install this extension is through composer.
Either run
$ php composer.phar require "borales/yii2-elasticsearch-behavior" "*"
or add
"borales/yii2-elasticsearch-behavior": "*"
to the require section of your composer.json file.
Configuring
Configure model as follows (for "command" mode):
class Post extends \yii\db\ActiveRecord { public function behaviors() { return [ ... 'elasticsearch' => [ 'class' => \borales\behaviors\elasticsearch\Behavior::className(), 'mode' => 'command', 'elasticIndex' => 'project-index', 'elasticType' => 'posts', 'dataMap' => [ 'id' => 'id', 'title' => 'name', 'body' => function() { return strip_tags($this->body); }, 'date_publish' => function() { return date('U', strtotime($this->date_create)); }, 'author' => function() { return ucfirst($this->author->name); } ] ], ]; } ... }
Configuration values of the behavior:
mode(possible values:commandormodel. Default iscommand) - it is the option, which controls how to interact with Elasticsearch:- in case of
command- the behavior use\Yii::$app->elasticsearch->createCommand()way to execute commands. In this mode - it is required to set upelasticIndexandelasticTypeparams. - in case of
model- it is required to set upelasticClassparameter with the value of model class name (specified class must extend the\yii\elasticsearch\ActiveRecordmodel class). In this case behavior will communicate with Elasticsearch through the specified model class. dataMap- this is an optional parameter. By default - the behavior will use$this->owner->attributesdynamic property of the\yii\db\ActiveRecordclass (you can learn more how to set up this property here). Otherwise - this is a key-value array, where the keys are the field names for the Elasticsearch entry and the values are the field names of the current\yii\db\ActiveRecordmodel or anonymous functions (callbacks).
Example of using "model" mode
class Post extends \yii\db\ActiveRecord { public function behaviors() { return [ ... 'elasticsearch' => [ 'class' => \borales\behaviors\elasticsearch\Behavior::className(), 'mode' => 'model', 'elasticClass' => \common\models\elastic\PostElastic::className(), 'dataMap' => [ 'id' => 'id', 'title' => 'name', 'body' => function() { return strip_tags($this->body); }, 'date_publish' => function() { return date('U', strtotime($this->date_create)); }, 'author' => function() { return ucfirst($this->author->name); } ] ], ]; } ... } ... class PostElastic extends \yii\elasticsearch\ActiveRecord { /** * @return array the list of attributes for this record */ public function attributes() { // path mapping for '_id' is setup to field 'id' return ['id', 'title', 'body', 'date_publish', 'author']; } }
More details and features about Elasticsearch ActiveRecord you will find here.
