Yii2 framework backup
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
Carsten Brandt f099616967 improved IDE autocompletion for AR::find() 10 years ago
..
images fixed some urls in guide; moved elasticsearch png file to proper location, to be seen by apidoc generator 11 years ago
ActiveQuery.php Fixes #4048: Added `init` event to `ActiveQuery` classes 10 years ago
ActiveRecord.php improved IDE autocompletion for AR::find() 10 years ago
CHANGELOG.md Fixes #4048: Added `init` event to `ActiveQuery` classes 10 years ago
Command.php property code style extensions 10 years ago
Connection.php property code style extensions 10 years ago
DebugAction.php property code style extensions 10 years ago
DebugPanel.php property code style extensions 10 years ago
Exception.php Reformat code te be PSR-2 compatible 11 years ago
LICENSE.md psr-4 change. 11 years ago
Query.php property code style extensions 10 years ago
QueryBuilder.php fixed broken NOT condition in elasticsearch 10 years ago
README.md Update README.md 10 years ago
composer.json replaced keyword "yii" with "yii2" in composer.json files (fixes #2557) 11 years ago

README.md

Elasticsearch Query and ActiveRecord for Yii 2

This extension provides the elasticsearch integration for the Yii2 framework. It includes basic querying/search support and also implements the ActiveRecord pattern that allows you to store active records in elasticsearch.

To use this extension, you have to configure the Connection class in your application configuration:

return [
    //....
    'components' => [
        'elasticsearch' => [
            'class' => 'yii\elasticsearch\Connection',
            'nodes' => [
                ['http_address' => '127.0.0.1:9200'],
                // configure more hosts if you have a cluster
            ],
        ],
    ]
];

Requirements

elasticsearch version 1.0 or higher is required.

Installation

The preferred way to install this extension is through composer.

Either run

php composer.phar require --prefer-dist yiisoft/yii2-elasticsearch "*"

or add

"yiisoft/yii2-elasticsearch": "*"

to the require section of your composer.json.

Using the Query

TBD

NOTE: elasticsearch limits the number of records returned by any query to 10 records by default. If you expect to get more records you should specify limit explicitly in relation definition.

Using the ActiveRecord

For general information on how to use yii's ActiveRecord please refer to the guide.

For defining an elasticsearch ActiveRecord class your record class needs to extend from yii\elasticsearch\ActiveRecord and implement at least the yii\elasticsearch\ActiveRecord::attributes() method to define the attributes of the record. The handling of primary keys is different in elasticsearch as the primary key (the _id field in elasticsearch terms) is not part of the attributes by default. However it is possible to define a path mapping for the _id field to be part of the attributes. See elasticsearch docs on how to define it. The _id field of a document/record can be accessed using yii\elasticsearch\ActiveRecord::getPrimaryKey() and yii\elasticsearch\ActiveRecord::setPrimaryKey(). When path mapping is defined, the attribute name can be defined using the yii\elasticsearch\ActiveRecord::primaryKey() method.

The following is an example model called Customer:

class Customer 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', 'name', 'address', 'registration_date'];
    }

    /**
     * @return ActiveQuery defines a relation to the Order record (can be in other database, e.g. redis or sql)
     */
    public function getOrders()
    {
        return $this->hasMany(Order::className(), ['customer_id' => 'id'])->orderBy('id');
    }

    /**
     * Defines a scope that modifies the `$query` to return only active(status = 1) customers
     */
    public static function active($query)
    {
        $query->andWhere(['status' => 1]);
    }
}

You may override yii\elasticsearch\ActiveRecord::index() and yii\elasticsearch\ActiveRecord::type() to define the index and type this record represents.

The general usage of elasticsearch ActiveRecord is very similar to the database ActiveRecord as described in the guide. It supports the same interface and features except the following limitations and additions(!):

NOTE: elasticsearch limits the number of records returned by any query to 10 records by default. If you expect to get more records you should specify limit explicitly in query and also relation definition. This is also important for relations that use via() so that if via records are limited to 10 the relations records can also not be more than 10.

Usage example:

$customer = new Customer();
$customer->primaryKey = 1; // in this case equivalent to $customer->id = 1;
$customer->attributes = ['name' => 'test'];
$customer->save();

$customer = Customer::get(1); // get a record by pk
$customers = Customer::mget([1,2,3]); // get multiple records by pk
$customer = Customer::find()->where(['name' => 'test'])->one(); // find by query, note that you need to configure mapping for this field in order to find records properly
$customers = Customer::find()->active()->all(); // find all by query (using the `active` scope)

// http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/query-dsl-match-query.html
$result = Article::find()->query(["match" => ["title" => "yii"]])->all(); // articles whose title contains "yii"

// http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/query-dsl-flt-query.html
$query = Article::find()->query([
    "fuzzy_like_this" => [
        "fields" => ["title", "description"],
        "like_text" => "This query will return articles that are similar to this text :-)",
        "max_query_terms" => 12
    ]
]);

$query->all(); // gives you all the documents
// you can add facets to your search:
$query->addStatisticalFacet('click_stats', ['field' => 'visit_count']);
$query->search(); // gives you all the records + stats about the visit_count field. e.g. mean, sum, min, max etc...

And there is so much more in it. "it’s endless what you can build"¹

Using the elasticsearch DebugPanel

The yii2 elasticsearch extensions provides a DebugPanel that can be integrated with the yii debug module and shows the executed elasticsearch queries. It also allows to run these queries and view the results.

Add the following to you application config to enable it (if you already have the debug module enabled, it is sufficient to just add the panels configuration):

    // ...
    'bootstrap' => ['debug'],
    'modules' => [
        'debug' => [
            'class' => 'yii\\debug\\Module',
            'panels' => [
                'elasticsearch' => [
                    'class' => 'yii\\elasticsearch\\DebugPanel',
                ],
            ],
        ],
    ],
    // ...

elasticsearch DebugPanel

Relation definitions with records whose primary keys are not part of attributes

TODO

Patterns

Fetching records from different indexes/types

TODO