Skip to content

Instantly share code, notes, and snippets.

@rkaldung
Created November 23, 2013 08:19
Show Gist options
  • Save rkaldung/7612148 to your computer and use it in GitHub Desktop.
Save rkaldung/7612148 to your computer and use it in GitHub Desktop.
Autocomplete w/ Elasticsearch && Elastica
/**
* $q query term
* 'field' => 'suggest'> Suggest is the fieldname used during indexing
* see http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/search-suggesters-completion.html
*/
$elasticaClient = new \Elastica\Client();
$elasticaIndex = $elasticaClient->getIndex('myindex');
$search = new \Elastica\Search($elasticaClient);
$suggest = new \Elastica\Suggest\Term();
$suggest->addTerm('suggest', ['text' => $q, 'completion' => ['field' => 'suggest']]);
$search->addIndex($elasticaIndex);
$search->addSuggest($suggest);
$result = $search->search();
if ($result->countSuggests()) {
$suggestions = [];
foreach ($result->getSuggests()['suggest']['options'] as $suggestion) {
$suggestions[] = $suggestion['text'];
}
}
Copy link

ghost commented Mar 21, 2014

Which version of elastica did you use? This is not working for 0.9.

Warning: Missing argument 1 for Elastica\Suggest\AbstractSuggest::__construct(), called in ProductsController.php on line 119 and defined in /Elastica/Suggest/AbstractSuggest.php line 28

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment