Browse Source

Add support for elasticsearch suggestions

close #4505
tags/2.0.0-rc
tvdavid 10 years ago committed by Carsten Brandt
parent
commit
259b8c901f
  1. 2
      extensions/elasticsearch/CHANGELOG.md
  2. 29
      extensions/elasticsearch/Command.php

2
extensions/elasticsearch/CHANGELOG.md

@ -12,7 +12,7 @@ Yii Framework 2 elasticsearch extension Change Log
- 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)
- Enh: Added support for query suggesters (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)

29
extensions/elasticsearch/Command.php

@ -38,15 +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 = [];
/**
* Sends a request to the _search API and returns the result
* @param array $options
* @return mixed
*/
@ -69,6 +65,29 @@ class Command extends Component
}
/**
* Sends a request to the _suggest API and returns the result
* @param string|array $suggester the suggester body
* @param array $options
* @return mixed
* @see http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/search-suggesters.html
*/
public function suggest($suggester, $options = [])
{
if (empty($suggester)) {
$suggester = '{}';
}
if (is_array($suggester)) {
$suggester = Json::encode($suggester);
}
$url = [
$this->index !== null ? $this->index : '_all',
'_suggest'
];
return $this->db->post($url, array_merge($this->options, $options), $suggester);
}
/**
* Inserts a document into an index
* @param string $index
* @param string $type

Loading…
Cancel
Save