PHPackages                             dimvic/yii-elastic - 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. [Utility &amp; Helpers](/categories/utility)
4. /
5. dimvic/yii-elastic

AbandonedArchivedYii-extension[Utility &amp; Helpers](/categories/utility)

dimvic/yii-elastic
==================

Yii 1.1 elasticsearch tools

71452[1 PRs](https://github.com/dimvic/yii-elastic/pulls)PHP

Since Nov 21Pushed 6y ago1 watchersCompare

[ Source](https://github.com/dimvic/yii-elastic)[ Packagist](https://packagist.org/packages/dimvic/yii-elastic)[ RSS](/packages/dimvic-yii-elastic/feed)WikiDiscussions master Synced 2mo ago

READMEChangelogDependenciesVersions (1)Used By (0)

yii-elastic: elasticsearch tools for Yii 1.1
============================================

[](#yii-elastic-elasticsearch-tools-for-yii-11)

[![Packagist package](https://camo.githubusercontent.com/91af0539e6a0168e39ebcd03eeadcd543a91f72a96b73febd569578ccebdfa67/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f7061636b61676973742d6465762d6c69676874677265792e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/dimvic/yii-elastic)[![License](https://camo.githubusercontent.com/d4f0021391edf3dee3d3aafffdfd079c3a9be768dac39da5b96920eca37c298b/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f6c2f64696d7669632f7969692d656c61737469632e7376673f7374796c653d666c61742d737175617265)](LICENSE.md)

Set of tools for working with elasticsearch in Yii 1.1 projects. Includes a `CActiveDataProvider` compatible data provider, a `CActiveRecordBehavior` and an elasticsearch query helper.

Apart for enabling an application for speedy searches using elasticsearch, the tools also enable for searches inside relations with near zero configuration.

Features
--------

[](#features)

- No elasticsearch background required
- ElasticActiveRecordBehavior
    - Automatically indexed (documents are added and deleted from elasticsearch when a record is inserted/updated/deleted)
    - Automatic indexing of relations and even their relations at any depth (minimal configuration required)
- ElasticDataProvider
    - Compatible with CGridView, CListView, etc. Use `$model->elasticSearch()` instead of `$model->search()`
    - Fully compatible with CActiveDataProvider, all pagination and sorting is supported
    - Search supports all operands it is expected to (all operands supported by `CDbCriteria->compare())`, &gt;, &lt;, &gt;=, &lt;&gt;, etc)
    - Returns CActiveRecord\[\], not elasticsearch documents, for maximum compatibility
- ElasticQueryHelper
    - Build elasticsearch queries (implements CDbCriteria-&gt;compare(), works for relations)
    - Search inside relations' fields (`category.products.price>10` just works)

Caveats
-------

[](#caveats)

- This will only work for `CActiveRecord` implementations with one single primary key (any type), composite primary keys are not supported.
- `ElasticDataProvider` uses elasticsearch to search but returns `CActiveRecord[]`, NOT an array of elasticsearch documents. It fetches the primary keys for the matched records and in turn queries by primary key in order to return `CActiveRecord[]` for maximum compatibility with Yii 1.1 widgets/extensions/components/plugins.
- Searching inside relation fields currently only works for a full match ("imba" will not match "imbalanced" as you might expect)

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

[](#installation)

```
$ composer require dimvic/yii-elastic:dev-master
$ composer update

```

Then Add to your configuration:

```
return [
	...
	'components' => [
		'elastica' => [
			'class' => 'extensions.yii-elastic.Elastica',
			'host' => '127.0.0.1',
			'port' => '9200',
			'debug' => YII_DEBUG,
		],
		...
	],
	...
];
```

Getting started
---------------

[](#getting-started)

### Attach `ElasticActiveRecordBehavior` to a `CActiveRecord`:

[](#attach-elasticactiverecordbehavior-to-a-cactiverecord)

```
class Post extends CActiveRecord
{
    ...
	public function Behaviors()
	{
		return array_replace(parent::behaviors(), [
            [
                'class'=>'ext.yii-elastic.ElasticActiveRecordBehavior',
                'elastic_index'=>null,   //defaults to parsing db name from $this->getDbConnection()
                'elastic_type'=>null,    //defaults to $model->tableName()
                'elastic_raw_cols'=>null,//the columns that will be used for aggregations, defaults to ['caption', 'slug', 'label', 'name']
                'elastic_relations'=>[   //the relations you want indexed, can be nested to any depth
                    'author',
                    'author.group',
                ],
            ],
            ...
		]);
	}
    ...
]
```

### Index the model's data in elastic

[](#index-the-models-data-in-elastic)

This will create the index and type in elasticsearch, and it will delete them first if they already exist! It will also index all data, so have a look at the method's parameters if you intend to run it on big data sets.

```
Post::model()->elasticRebuild();
```

### Ready

[](#ready)

In your controller:

```
public function actionGrid() {
    $model = new Post('search');

    $this->render('grid', [
        'model' => $model,
    ]);
}
```

In your view:

```
$this->widget('zii.widgets.grid.CGridView', [
    'dataProvider'=>$model->elasticSearch(),
    'filter'=>$model,
    'columns' => [
        ['name'=>'id'],
        ['name'=>'title'],
        ['name'=>'author.name', 'filter'=>$model->nestedFilterInput('author.name')],
        ['name'=>'author.group.name', 'filter'=>$model->nestedFilterInput('author.group.name')],
    ],
    'ajaxUpdate'=>false,
]);
```

And you can already search by the post author's group name.

Usage
-----

[](#usage)

Although the default functionality should be enough for most cases, you can handle more complex scenarios with ease.

You will probably want to have a look at:

- `ElasticActiveRecordBehavior->elasticSearch()` if you want to run custom queries
- `ElasticActiveRecordBehavior->createElasticType()` and `ElasticActiveRecordBehavior->elasticProperties()` if you want to index your data in a specific way (you will probably want to override `ElasticActiveRecordBehavior->createElasticDocument()` which prepares the documents as well)
- `ElasticQueryHelper` to get an idea of how queries are build Playing with the parameters of the methods should be enough for most cases, overriding is always an option.

`ElasticActiveDataProvider->getResultSet()` will give you access to the `\Elastica\ResultSet` for every query, in case you need more insight on the search you perform.

Thanks
------

[](#thanks)

- [ruflin/Elastica](https://github.com/ruflin/Elastica)
- [anasAsh/Yii-Elastica](https://github.com/anasAsh/Yii-Elastica)

Todo
----

[](#todo)

- Add `$model->relations()` to index by default
- `ElasticaQueryHelper::compare()` dates support
- Create a schema cache for the relational fields, as they will always be searched for exact matches ATM
- Support aggregations for model and relation fields
- Documentation

###  Health Score

22

—

LowBetter than 22% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity17

Limited adoption so far

Community9

Small or concentrated contributor base

Maturity35

Early-stage or recently created project

 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.

### Community

Maintainers

![](https://www.gravatar.com/avatar/70f09cf662c407690fe7f5d05fb2b73b1a51da1d5d4ca576920204f29736d4b1?d=identicon)[dimvic](/maintainers/dimvic)

---

Top Contributors

[![dimvic](https://avatars.githubusercontent.com/u/386233?v=4)](https://github.com/dimvic "dimvic (49 commits)")

### Embed Badge

![Health badge](/badges/dimvic-yii-elastic/health.svg)

```
[![Health](https://phpackages.com/badges/dimvic-yii-elastic/health.svg)](https://phpackages.com/packages/dimvic-yii-elastic)
```

PHPackages © 2026

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