Created
November 23, 2013 08:19
-
-
Save rkaldung/7612148 to your computer and use it in GitHub Desktop.
Autocomplete w/ Elasticsearch && Elastica
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* $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']; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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