PHPackages                             borales/yii2-elasticsearch-behavior - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Search &amp; Filtering](/categories/search)
4. /
5. borales/yii2-elasticsearch-behavior

ActiveYii2-extension[Search &amp; Filtering](/categories/search)

borales/yii2-elasticsearch-behavior
===================================

Yii2 behavior to support Elasticsearch indexing

0.0.1(11y ago)2110.2k↓50%6[3 issues](https://github.com/Borales/yii2-elasticsearch-behavior/issues)[1 PRs](https://github.com/Borales/yii2-elasticsearch-behavior/pulls)BSD-3-ClausePHP

Since Dec 14Pushed 10y ago5 watchersCompare

[ Source](https://github.com/Borales/yii2-elasticsearch-behavior)[ Packagist](https://packagist.org/packages/borales/yii2-elasticsearch-behavior)[ Docs](https://github.com/Borales/yii2-elasticsearch-behavior)[ RSS](/packages/borales-yii2-elasticsearch-behavior/feed)WikiDiscussions master Synced 1mo ago

READMEChangelogDependencies (1)Versions (2)Used By (0)

Elasticsearch Behavior for Yii 2
================================

[](#elasticsearch-behavior-for-yii-2)

[![Latest Stable Version](https://camo.githubusercontent.com/76c71157a7449ea89bdab97497e3c42768f46ef55999f5c25b8502cc1d9070c5/68747470733a2f2f706f7365722e707567782e6f72672f626f72616c65732f796969322d656c61737469637365617263682d6265686176696f722f762f737461626c652e737667)](https://packagist.org/packages/borales/yii2-elasticsearch-behavior)[![Total Downloads](https://camo.githubusercontent.com/d1136c7b5258c7ff5eba53d63e90b45e5166481ac9ba6ec1ee990d321d22bc14/68747470733a2f2f706f7365722e707567782e6f72672f626f72616c65732f796969322d656c61737469637365617263682d6265686176696f722f646f776e6c6f6164732e737667)](https://packagist.org/packages/borales/yii2-elasticsearch-behavior)[![Latest Unstable Version](https://camo.githubusercontent.com/6997dd1e3816022d15b8a0ba1861f4dd82c1acaa5be355c2283335766cc6f399/68747470733a2f2f706f7365722e707567782e6f72672f626f72616c65732f796969322d656c61737469637365617263682d6265686176696f722f762f756e737461626c652e737667)](https://packagist.org/packages/borales/yii2-elasticsearch-behavior)[![License](https://camo.githubusercontent.com/c195486db18a66a3f9c570f17fc57f46a265afbb16784ce7771960ae34b99962/68747470733a2f2f706f7365722e707567782e6f72672f626f72616c65732f796969322d656c61737469637365617263682d6265686176696f722f6c6963656e73652e737667)](https://packagist.org/packages/borales/yii2-elasticsearch-behavior)

Yii2 AR behavior to support [Elasticsearch](http://www.elasticsearch.org/) auto-indexing.

[![image](https://cloud.githubusercontent.com/assets/1118933/5841840/63c39bb0-a1a0-11e4-9a6b-df0911203ba5.png)](https://cloud.githubusercontent.com/assets/1118933/5841840/63c39bb0-a1a0-11e4-9a6b-df0911203ba5.png)

Installation
------------

[](#installation)

The preferred way to install this extension is through [composer](http://getcomposer.org/download/).

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
-----------

[](#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: `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](https://github.com/yiisoft/yii2/blob/master/docs/guide/structure-models.md#data-exporting-)). 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).

### Example of using "model" `mode`

[](#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](https://github.com/yiisoft/yii2-elasticsearch#using-the-activerecord).

###  Health Score

32

—

LowBetter than 72% of packages

Maintenance17

Infrequent updates — may be unmaintained

Popularity33

Limited adoption so far

Community12

Small or concentrated contributor base

Maturity53

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Unknown

Total

1

Last Release

4167d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/4a7700b2f0b477c2b60e3624bfd4aac000d5cb1d9e75e45a7f99ede46c6fe1e2?d=identicon)[borales](/maintainers/borales)

---

Top Contributors

[![Borales](https://avatars.githubusercontent.com/u/1118933?v=4)](https://github.com/Borales "Borales (12 commits)")

---

Tags

elasticsearchyii2Behavioresyii2-behavior

### Embed Badge

![Health badge](/badges/borales-yii2-elasticsearch-behavior/health.svg)

```
[![Health](https://phpackages.com/badges/borales-yii2-elasticsearch-behavior/health.svg)](https://phpackages.com/packages/borales-yii2-elasticsearch-behavior)
```

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
