@@ -55,12 +55,12 @@ For general information on how to use yii's ActiveRecord please refer to the [gu
For defining an elasticsearch ActiveRecord class your record class needs to extend from `yii\elasticsearch\ActiveRecord` and
implement at least the `attributes()` method to define the attributes of the record.
The primary key (the `_id` field in elasticsearch terms) is represented by `getId()` and `setId()` and can not be changed.
The primary key is not part of the attributes.
primary key can be defined via [[primaryKey()]] which defaults to `id` if not specified.
The primaryKey needs to be part of the attributes so make sure you have an `id` attribute defined if you do
not specify your own primary key.
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](http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/mapping-id-field.html)
for the `_id` field to be part of the attributes.
See [elasticsearch docs](http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/mapping-id-field.html) on how to define it.
The `_id` field of a document/record can be accessed using [[ActiveRecord::getPrimaryKey()]] and [[ActiveRecord::setPrimaryKey()]].
When path mapping is defined, the attribute name can be defined using the [[primaryKey()]] method.
The following is an example model called `Customer`:
...
...
@@ -72,6 +72,7 @@ class Customer extends \yii\elasticsearch\ActiveRecord
@@ -105,25 +106,23 @@ It supports the same interface and features except the following limitations and
and [type](http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/glossary.html#glossary-type) to query against.
-`select()` has been replaced with `fields()` which basically does the same but `fields` is more elasticsearch terminology.
It defines the fields to retrieve from a document.
-`via`-relations can not be defined via a table as there are not tables in elasticsearch. You can only define relations via other records.
- As elasticsearch is a data storage and search engine there is of course support added for search your records.
-`via`-relations can not be defined via a table as there are no tables in elasticsearch. You can only define relations via other records.
- As elasticsearch is not only a data storage but also a search engine there is of course support added for search your records.
There are `query()`, `filter()` and `addFacets()` methods that allows to compose an elasticsearch query.
See the usage example below on how they work and check out the [Query DSL](http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/query-dsl.html)
on how to compose `query` and `filter` parts.
- It is also possible to define relations from elasticsearch ActiveRecords to normal ActiveRecord classes and vice versa.
Elasticsearch separates primary key from attributes. You need to set the `id` property of the record to set its primary key.
Usage example:
```php
$customer=newCustomer();
$customer->id=1;
$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::get([1,2,3]);// get a records multiple by pk
$customers=Customer::mget([1,2,3]);// get multiple records by pk
$customer=Customer::find()->where(['name'=>'test'])->one();// find by query
$customers=Customer::find()->active()->all();// find all by query (using the `active` scope)
...
...
@@ -152,10 +151,11 @@ Using the elasticsearch DebugPanel
----------------------------------
The yii2 elasticsearch extensions provides a `DebugPanel` that can be integrated with the yii debug module
an shows the executed elasticsearch queries. It also allows to run these queries on different cluster nodes
an view the results.
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:
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):
```php
// ...
...
...
@@ -173,4 +173,18 @@ Add the following to you application config to enable it:
// ...
```
![elasticsearch DebugPanel](README-debug.png)
\ No newline at end of file
![elasticsearch DebugPanel](README-debug.png)
Relation definitions with records whose primary keys are not part of attributes