2017 © Pedro Peláez
 

yii2-extension yii2-elasticsearch-behavior

Yii2 behavior to support Elasticsearch indexing

image

borales/yii2-elasticsearch-behavior

Yii2 behavior to support Elasticsearch indexing

  • Friday, January 30, 2015
  • by borales
  • Repository
  • 5 Watchers
  • 18 Stars
  • 5,443 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 8 Forks
  • 5 Open issues
  • 2 Versions
  • 4 % Grown

The README.md

9X" /> , (*1)

Elasticsearch Behavior for Yii 2

Latest Stable Version Total Downloads Latest Unstable Version License, (*2)

Yii2 AR behavior to support Elasticsearch auto-indexing., (*3)

image, (*4)

Installation

The preferred way to install this extension is through composer., (*5)

Either run, (*6)

$ php composer.phar require "borales/yii2-elasticsearch-behavior" "*"

or add, (*7)

"borales/yii2-elasticsearch-behavior": "*"

to the require section of your composer.json file., (*8)

Configuring

Configure model as follows (for "command" mode):, (*9)

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: command or model. Default is command) - 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 up elasticIndex and elasticType params. - in case of model - it is required to set up elasticClass parameter with the value of model class name (specified class must extend the \yii\elasticsearch\ActiveRecord model 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->attributes dynamic property of the \yii\db\ActiveRecord class (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\ActiveRecord model or anonymous functions (callbacks)., (*10)

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., (*11)

The Versions

30/01 2015

dev-master

9999999-dev https://github.com/Borales/yii2-elasticsearch-behavior

Yii2 behavior to support Elasticsearch indexing

  Sources   Download

BSD-3-Clause

The Requires

 

by Borales (Alexandr Bordun)

yii2 elasticsearch behavior es yii2-behavior

14/12 2014

0.0.1

0.0.1.0 https://github.com/Borales/yii2-elasticsearch-behavior

Yii2 behavior to support Elasticsearch indexing

  Sources   Download

BSD-3-Clause

The Requires

 

by Borales (Alexandr Bordun)

yii2 elasticsearch behavior es yii2-behavior