Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I add filter to Completion Suggester in ElasticSearch?

Tags:

Does anyone know how to add filter to suggester?

This works very well:

{ "headline-suggest" : {     "text" : "n",     "completion" : {         "field" : "headline_suggest"     } } 

but I want force ElasticSearch to build list of suggestions from subset of data, like:

{ "headline-suggest" : {     "text" : "n",     "completion" : {         "field" : "headline_suggest"     } }, "filter" : {     "term" : { "mydifferentfield" : "someword" } } } 

How can I achieve it?

(I'm using elasticsearch 0.90.5)

like image 535
zuko Avatar asked Oct 17 '13 18:10

zuko


People also ask

How does Elasticsearch implement autocomplete?

Autocomplete can be achieved by changing match queries to prefix queries. While match queries work on token (indexed) to token (search query tokens) match, prefix queries (as their name suggests) match all the tokens starting with search tokens, hence the number of documents (results) matched is high.

What is a suggester in Elasticsearch?

The term suggester suggests terms based on edit distance. The provided suggest text is analyzed before terms are suggested. The suggested terms are provided per analyzed suggest text token. The term suggester doesn't take the query into account that is part of request.

How does filter work in Elasticsearch?

Frequently used filters will be cached automatically by Elasticsearch, to speed up performance. Filter context is in effect whenever a query clause is passed to a filter parameter, such as the filter or must_not parameters in the bool query, the filter parameter in the constant_score query, or the filter aggregation.


2 Answers

As per version 1.2.0, you can add context to your suggester and obtain filtered suggestions.

Introductory blog post Introductory blog post

Full Docs Full Docs

like image 60
dg6 Avatar answered Dec 06 '22 16:12

dg6


i am not really sure about that, but i think that you can not filter a suggest request in elasticsearch.

from what i read this is due to the kind of data structure it is stored in. elasticsearch calculates statistical data on the terms to suggest at index time and uses those for the suggest operations.

did you try performing a normal query in combination with a suggest? maybe there is a different outcome to that?

curl -s -XPOST 'localhost:9200/_search' -d '{   "query" : {     ...   },   "suggest" : {     ...   } }' 
like image 21
phoet Avatar answered Dec 06 '22 14:12

phoet