Commit 2e7ae380 by Borales

[WIP] #3527 Improving Elasticsearch extension

parent 5771dfc4
...@@ -42,6 +42,8 @@ use yii\helpers\StringHelper; ...@@ -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 float $score Returns the score of this record when it was retrieved via a [[find()]] query. This
* property is read-only. * 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> * @author Carsten Brandt <mail@cebe.cc>
* @since 2.0 * @since 2.0
...@@ -51,6 +53,7 @@ class ActiveRecord extends BaseActiveRecord ...@@ -51,6 +53,7 @@ class ActiveRecord extends BaseActiveRecord
private $_id; private $_id;
private $_score; private $_score;
private $_version; private $_version;
private $_highlight;
/** /**
* Returns the database connection used by this AR class. * Returns the database connection used by this AR class.
...@@ -176,6 +179,14 @@ class ActiveRecord extends BaseActiveRecord ...@@ -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 * Sets the primary key
* @param mixed $value * @param mixed $value
* @throws \yii\base\InvalidCallException when record is not new * @throws \yii\base\InvalidCallException when record is not new
...@@ -302,6 +313,7 @@ class ActiveRecord extends BaseActiveRecord ...@@ -302,6 +313,7 @@ class ActiveRecord extends BaseActiveRecord
if ($pk === '_id') { if ($pk === '_id') {
$record->_id = $row['_id']; $record->_id = $row['_id'];
} }
$record->_highlight = isset($row['highlight']) ? $row['highlight'] : null;
$record->_score = isset($row['_score']) ? $row['_score'] : null; $record->_score = isset($row['_score']) ? $row['_score'] : null;
$record->_version = isset($row['_version']) ? $row['_version'] : null; // TODO version should always be available... $record->_version = isset($row['_version']) ? $row['_version'] : null; // TODO version should always be available...
} }
......
...@@ -5,8 +5,9 @@ Yii Framework 2 elasticsearch extension Change Log ...@@ -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: 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) - 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 2.0.0-beta April 13, 2014
......
...@@ -38,6 +38,11 @@ class Command extends Component ...@@ -38,6 +38,11 @@ class Command extends Component
* @var array list of arrays or json strings that become parts of a query * @var array list of arrays or json strings that become parts of a query
*/ */
public $queryParts; 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 = []; public $options = [];
......
...@@ -132,6 +132,11 @@ class Query extends Component implements QueryInterface ...@@ -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). * the elasticsearch [Query DSL](http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/query-dsl.html).
*/ */
public $filter; 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 = []; public $facets = [];
...@@ -526,6 +531,18 @@ class Query extends Component implements QueryInterface ...@@ -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. * Sets the source filtering, specifying how the `_source` field of the document should be returned.
* @param array $source the source patterns to be selected. * @param array $source the source patterns to be selected.
* @return static the query object itself * @return static the query object itself
......
...@@ -97,6 +97,10 @@ class QueryBuilder extends \yii\base\Object ...@@ -97,6 +97,10 @@ class QueryBuilder extends \yii\base\Object
$parts['filter'] = $whereFilter; $parts['filter'] = $whereFilter;
} }
if($query->highlight) {
$parts['highlight'] = $query->highlight;
}
$sort = $this->buildOrderBy($query->orderBy); $sort = $this->buildOrderBy($query->orderBy);
if (!empty($sort)) { if (!empty($sort)) {
$parts['sort'] = $sort; $parts['sort'] = $sort;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment