diff --git a/extensions/elasticsearch/ActiveRecord.php b/extensions/elasticsearch/ActiveRecord.php index 6ff1095..be6a745 100644 --- a/extensions/elasticsearch/ActiveRecord.php +++ b/extensions/elasticsearch/ActiveRecord.php @@ -42,6 +42,8 @@ use yii\helpers\StringHelper; * * @property float $score Returns the score of this record when it was retrieved via a [[find()]] query. This * property is read-only. + * @property array $highlight Returns a list of arrays with highlighted excerpts indexed by field names. This + * property is read-only. * * @author Carsten Brandt <mail@cebe.cc> * @since 2.0 @@ -51,6 +53,7 @@ class ActiveRecord extends BaseActiveRecord private $_id; private $_score; private $_version; + private $_highlight; /** * Returns the database connection used by this AR class. @@ -176,6 +179,14 @@ class ActiveRecord extends BaseActiveRecord } /** + * @return array|null A list of arrays with highlighted excerpts indexed by field names. + */ + public function getHighlight() + { + return $this->_highlight; + } + + /** * Sets the primary key * @param mixed $value * @throws \yii\base\InvalidCallException when record is not new @@ -302,6 +313,7 @@ class ActiveRecord extends BaseActiveRecord if ($pk === '_id') { $record->_id = $row['_id']; } + $record->_highlight = isset($row['highlight']) ? $row['highlight'] : null; $record->_score = isset($row['_score']) ? $row['_score'] : null; $record->_version = isset($row['_version']) ? $row['_version'] : null; // TODO version should always be available... } diff --git a/extensions/elasticsearch/CHANGELOG.md b/extensions/elasticsearch/CHANGELOG.md index 3710c97..92595c4 100644 --- a/extensions/elasticsearch/CHANGELOG.md +++ b/extensions/elasticsearch/CHANGELOG.md @@ -5,8 +5,9 @@ Yii Framework 2 elasticsearch extension Change Log -------------------------- - 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 successfull if the record did not exist. (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) +- Enh #3527: Added `highlight` property to Query and ActiveRecord. (Borales) 2.0.0-beta April 13, 2014 diff --git a/extensions/elasticsearch/Command.php b/extensions/elasticsearch/Command.php index f1313ad..7528f0f 100644 --- a/extensions/elasticsearch/Command.php +++ b/extensions/elasticsearch/Command.php @@ -38,6 +38,11 @@ class Command extends Component * @var array list of arrays or json strings that become parts of a query */ public $queryParts; + /** + * @var array list of arrays to highlight search results on one or more fields + * @see http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/search-request-highlighting.html + */ + public $highlight; public $options = []; diff --git a/extensions/elasticsearch/Query.php b/extensions/elasticsearch/Query.php index 535e1c8..9c4f8e6 100644 --- a/extensions/elasticsearch/Query.php +++ b/extensions/elasticsearch/Query.php @@ -132,6 +132,11 @@ class Query extends Component implements QueryInterface * the elasticsearch [Query DSL](http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/query-dsl.html). */ public $filter; + /** + * @var array The highlight part of this search query. This is an array that allows to highlight search results + * on one or more fields. + */ + public $highlight; public $facets = []; @@ -526,6 +531,18 @@ class Query extends Component implements QueryInterface } /** + * Sets a highlight parameters to retrieve from the documents. + * @param array $highlight array of parameters to highlight results. + * @return static the query object itself + * @see http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/search-request-highlighting.html + */ + public function highlight($highlight) + { + $this->highlight = $highlight; + return $this; + } + + /** * Sets the source filtering, specifying how the `_source` field of the document should be returned. * @param array $source the source patterns to be selected. * @return static the query object itself diff --git a/extensions/elasticsearch/QueryBuilder.php b/extensions/elasticsearch/QueryBuilder.php index 951e6f6..96aa374 100644 --- a/extensions/elasticsearch/QueryBuilder.php +++ b/extensions/elasticsearch/QueryBuilder.php @@ -97,6 +97,10 @@ class QueryBuilder extends \yii\base\Object $parts['filter'] = $whereFilter; } + if($query->highlight) { + $parts['highlight'] = $query->highlight; + } + $sort = $this->buildOrderBy($query->orderBy); if (!empty($sort)) { $parts['sort'] = $sort;