* @param bool $runValidation whether to perform validation before saving the record.
* If the validation fails, the record will not be inserted into the database.
* @param array $attributeNames list of attribute names that need to be saved. Defaults to null,
* meaning all attributes that are loaded from DB will be saved.
* @param array $options options given in this parameter are passed to elasticsearch
* as request URI parameters. These are among others:
*
* - `routing` define shard placement of this record.
* - `parent` by giving the primaryKey of another record this defines a parent-child relation
* - `timeout` timeout waiting for a shard to become available.
* - `replication` the replication type for the delete/index operation (sync or async).
* - `consistency` the write consistency of the index/delete operation.
* - `refresh` refresh the relevant primary and replica shards (not the whole index) immediately after the operation occurs, so that the updated document appears in search results immediately.
* - `detect_noop` this parameter will become part of the request body and will prevent the index from getting updated when nothing has changed.
*
* Please refer to the [elasticsearch documentation](http://www.elastic.co/guide/en/elasticsearch/reference/current/docs-update.html#_parameters_3)
* for more details on these options.
*
* The following parameters are Yii specific:
*
* - `optimistic_locking` set this to `true` to enable optimistic locking, avoid updating when the record has changed since it
* has been loaded from the database. Yii will set the `version` parameter to the value stored in [[version]].
* See the [elasticsearch documentation](http://www.elastic.co/guide/en/elasticsearch/guide/current/optimistic-concurrency-control.html) for details.
*
* Make sure the record has been fetched with a [[version]] before. This is only the case
* for records fetched via [[get()]] and [[mget()]] by default. For normal queries, the `_version` field has to be fetched explicitly.
*
* @return int|bool the number of rows affected, or false if validation fails
* or [[beforeSave()]] stops the updating process.
* @throws StaleObjectException if optimistic locking is enabled and the data being updated is outdated.
* @throws InvalidParamException if no [[version]] is available and optimistic locking is enabled.
thrownewInvalidParamException('Unable to use optimistic locking on a record that has no version set. Refer to the docs of ActiveRecord::update() for details.');
}
$options['version']=$this->_version;
unset($options['optimistic_locking']);
}
try{
$result=static::getDb()->createCommand()->update(
static::index(),
static::type(),
$this->getOldPrimaryKey(false),
$values,
$options
);
}catch(Exception$e){
// HTTP 409 is the response in case of failed optimistic locking
thrownewException(__METHOD__.' failed updating records counters.',$errors);
}
return$n;
}
/**
* @inheritdoc
*
* @param array $options options given in this parameter are passed to elasticsearch
* as request URI parameters. These are among others:
*
* - `routing` define shard placement of this record.
* - `parent` by giving the primaryKey of another record this defines a parent-child relation
* - `timeout` timeout waiting for a shard to become available.
* - `replication` the replication type for the delete/index operation (sync or async).
* - `consistency` the write consistency of the index/delete operation.
* - `refresh` refresh the relevant primary and replica shards (not the whole index) immediately after the operation occurs, so that the updated document appears in search results immediately.
*
* Please refer to the [elasticsearch documentation](http://www.elastic.co/guide/en/elasticsearch/reference/current/docs-delete.html)
* for more details on these options.
*
* The following parameters are Yii specific:
*
* - `optimistic_locking` set this to `true` to enable optimistic locking, avoid updating when the record has changed since it
* has been loaded from the database. Yii will set the `version` parameter to the value stored in [[version]].
* See the [elasticsearch documentation](http://www.elastic.co/guide/en/elasticsearch/reference/current/docs-delete.html#delete-versioning) for details.
*
* Make sure the record has been fetched with a [[version]] before. This is only the case
* for records fetched via [[get()]] and [[mget()]] by default. For normal queries, the `_version` field has to be fetched explicitly.
*
* @return int|bool the number of rows deleted, or false if the deletion is unsuccessful for some reason.
* Note that it is possible the number of rows deleted is 0, even though the deletion execution is successful.
* @throws StaleObjectException if optimistic locking is enabled and the data being deleted is outdated.
thrownewInvalidParamException('Unable to use optimistic locking on a record that has no version set. Refer to the docs of ActiveRecord::delete() for details.');
}
$options['version']=$this->_version;
unset($options['optimistic_locking']);
}
try{
$result=static::getDb()->createCommand()->delete(
static::index(),
static::type(),
$this->getOldPrimaryKey(false),
$options
);
}catch(Exception$e){
// HTTP 409 is the response in case of failed optimistic locking
- Enh #47: Added support for post_filter option in search queries (mxkh)
- Enh #60: Minor updates to guide (devypt, beowulfenator)
- Enh #82: Support HTTPS protocol (dor-denis, beowulfenator)
- Enh #83: Support for "gt", ">", "gte", ">=", "lt", "<", "lte", "<=" operators in query (i-lie, beowulfenator)
- Enh #119: Added support for explanation on query (kyle-mccarthy)
- Enh #150: Explicitily send `Content-Type` header in HTTP requests to elasticsearch (lubobill1990)
- Enh: Bulk API implemented and used in AR (tibee, beowulfenator)
- Enh: Deserialization of raw response when text/plain is supported (Tezd)
- Enh: Added ability to work with aliases through Command class (Tezd)
2.0.4 March 17, 2016
--------------------
- Bug #8: Fixed issue with running out of sockets when running a large number of requests by reusing curl handles (cebe)
- Bug #13: Fixed wrong API call for getting all types or searching all types, `_all` works only for indexes (cebe)
- Bug #19: `DeleteAll` now deletes all entries, not first 10 (beowulfenator)
- Bug #48: `UpdateAll` now updates all entries, not first 10 (beowulfenator)
- Bug #65: Fixed warning `array to string conversion` when parsing error response (rhertogh, silverfire)
- Bug #73: Fixed debug panel exception when no data was recorded for elasticsearch panel (jafaripur)
- Enh #2: Added `min_score` option to query (knut)
- Enh #28: AWS Elasticsearch service compatibility (andrey-bahrachev)
- Enh #33: Implemented `Command::updateSettings()` and `Command::updateAnalyzers()` (githubjeka)
- Enh #50: Implemented HTTP auth (silverfire)
- Enh #62: Added support for scroll API in `batch()` and `each()` (beowulfenator, 13leaf)
- Enh #70: `Query` and `ActiveQuery` now have `$options` attribute that is passed to commands generated by queries (beowulfenator)
- Enh: Unified model creation from result set in `Query` and `ActiveQuery` with `populate()` (beowulfenator)
2.0.3 March 01, 2015
--------------------
- no changes in this release.
2.0.2 January 11, 2015
----------------------
- Enh: Added `ActiveFixture` class for testing fixture support for elasticsearch (cebe, viilveer)
2.0.1 December 07, 2014
-----------------------
- Bug #5662: Elasticsearch AR updateCounters() now uses explicitly `groovy` script for updating making it compatible with ES >1.3.0 (cebe)
- Bug #6065: `ActiveRecord::unlink()` was failing in some situations when working with relations via array valued attributes (cebe)
- Enh #5758: Allow passing custom options to `ActiveRecord::update()` and `::delete()` including support for routing needed for updating records with parent relation (cebe)
- Enh: Add support for optimistic locking (cebe)
2.0.0 October 12, 2014
----------------------
- Enh #3381: Added ActiveRecord::arrayAttributes() to define attributes that should be treated as array when retrieved via `fields` (cebe)
2.0.0-rc September 27, 2014
---------------------------
- Bug #3587: Fixed an issue with storing empty records (cebe)
- Bug #4187: Elasticsearch dynamic scripting is disabled in 1.2.0, so do not use it in query builder (cebe)
- Enh #3527: Added `highlight` property to Query and ActiveRecord. (Borales)
- Enh #4048: Added `init` event to `ActiveQuery` classes (qiangxue)
- Enh #4086: changedAttributes of afterSave Event now contain old values (dizews)
- Enh: Make error messages more readable in HTML output (cebe)
- Enh: Added support for query stats (cebe)
- Enh: Added support for query suggesters (cebe, tvdavid)
- Enh: Added support for delete by query (cebe, tvdavid)
- Chg #4451: Removed support for facets and replaced them with aggregations (cebe, tadaszelvys)
- Chg: asArray in ActiveQuery is now equal to using the normal Query. This means, that the output structure has changed and `with` is supported anymore. (cebe)
- Chg: Deletion of a record is now also considered successful if the record did not exist. (cebe)
- Chg: Requirement changes: Yii now requires elasticsearch version 1.0 or higher (cebe)
2.0.0-beta April 13, 2014
-------------------------
- Bug #1993: afterFind event in AR is now called after relations have been populated (cebe, creocoder)
- Bug #2324: Fixed QueryBuilder bug when building a query with "query" option (mintao)
- Enh #1313: made index and type available in `ActiveRecord::instantiate()` to allow creating records based on elasticsearch type when doing cross index/type search (cebe)
- Enh #1382: Added a debug toolbar panel for elasticsearch (cebe)
- Enh #1765: Added support for primary key path mapping, pk can now be part of the attributes when mapping is defined (cebe)
- Enh #2002: Added filterWhere() method to yii\elasticsearch\Query to allow easy addition of search filter conditions by ignoring empty search fields (samdark, cebe)
- Enh #2892: ActiveRecord dirty attributes are now reset after call to `afterSave()` so information about changed attributes is available in `afterSave`-event (cebe)
- Chg #1765: Changed handling of ActiveRecord primary keys, removed getId(), use getPrimaryKey() instead (cebe)
- Chg #2281: Renamed `ActiveRecord::create()` to `populateRecord()` and changed signature. This method will not call instantiate() anymore (cebe)
- Chg #2146: Removed `ActiveRelation` class and moved the functionality to `ActiveQuery`.
All relational queries are now directly served by `ActiveQuery` allowing to use
// Protocol is not a standard ES node property, so we add it manually
$node['protocol']=$this->defaultProtocol;
}
if(!empty($nodes)){
$this->nodes=array_values($nodes);
}else{
curl_close($this->_curl);
thrownewException('Cluster autodetection did not find any active node. Make sure a GET /_nodes reguest on the hosts defined in the config returns the "http_address" field for each node.');
}
}
/**
* select active node randomly
*/
protectedfunctionselectActiveNode()
{
$keys=array_keys($this->nodes);
$this->activeNode=$keys[rand(0,count($keys)-1)];
}
/**
* Closes the currently active DB connection.
* It does nothing if the connection is already closed.
*/
publicfunctionclose()
{
if($this->activeNode===null){
return;
}
Yii::trace('Closing connection to elasticsearch. Active node was: '
<h1align="center">Elasticsearch Query and ActiveRecord for Yii 2</h1>
<br>
</p>
This extension provides the [elasticsearch](https://www.elastic.co/products/elasticsearch) integration for the [Yii framework 2.0](http://www.yiiframework.com).
It includes basic querying/search support and also implements the `ActiveRecord` pattern that allows you to store active
records in elasticsearch.
For license information check the [LICENSE](LICENSE.md)-file.
Documentation is at [docs/guide/README.md](docs/guide/README.md).