2013-08-28 19:24:34 -04:00
|
|
|
[[search-suggesters]]
|
|
|
|
== Suggesters
|
|
|
|
|
|
|
|
The suggest feature suggests similar looking terms based on a provided
|
2013-09-03 15:27:49 -04:00
|
|
|
text by using a suggester. Parts of the suggest feature are still under
|
2013-08-28 19:24:34 -04:00
|
|
|
development.
|
|
|
|
|
2016-12-14 21:49:53 -05:00
|
|
|
The suggest request part is defined alongside the query part in a `_search`
|
2018-11-28 06:01:27 -05:00
|
|
|
request. If the query part is left out, only suggestions are returned.
|
2016-12-14 21:49:53 -05:00
|
|
|
|
|
|
|
NOTE: `_suggest` endpoint has been deprecated in favour of using suggest via
|
|
|
|
`_search` endpoint. In 5.0, the `_search` endpoint has been optimized for
|
|
|
|
suggest only search requests.
|
2013-08-28 19:24:34 -04:00
|
|
|
|
|
|
|
[source,js]
|
|
|
|
--------------------------------------------------
|
2016-07-22 18:51:36 -04:00
|
|
|
POST twitter/_search
|
|
|
|
{
|
2013-08-28 19:24:34 -04:00
|
|
|
"query" : {
|
2016-07-22 18:51:36 -04:00
|
|
|
"match": {
|
|
|
|
"message": "tring out Elasticsearch"
|
|
|
|
}
|
2013-08-28 19:24:34 -04:00
|
|
|
},
|
|
|
|
"suggest" : {
|
2016-07-22 18:51:36 -04:00
|
|
|
"my-suggestion" : {
|
2018-05-01 18:16:28 -04:00
|
|
|
"text" : "tring out Elasticsearch",
|
2016-07-22 18:51:36 -04:00
|
|
|
"term" : {
|
|
|
|
"field" : "message"
|
|
|
|
}
|
|
|
|
}
|
2013-08-28 19:24:34 -04:00
|
|
|
}
|
2016-07-22 18:51:36 -04:00
|
|
|
}
|
2013-08-28 19:24:34 -04:00
|
|
|
--------------------------------------------------
|
2016-07-22 18:51:36 -04:00
|
|
|
// CONSOLE
|
|
|
|
// TEST[setup:twitter]
|
2013-08-28 19:24:34 -04:00
|
|
|
|
|
|
|
Several suggestions can be specified per request. Each suggestion is
|
|
|
|
identified with an arbitrary name. In the example below two suggestions
|
|
|
|
are requested. Both `my-suggest-1` and `my-suggest-2` suggestions use
|
|
|
|
the `term` suggester, but have a different `text`.
|
|
|
|
|
|
|
|
[source,js]
|
|
|
|
--------------------------------------------------
|
2016-12-14 21:49:53 -05:00
|
|
|
POST _search
|
2016-07-22 18:51:36 -04:00
|
|
|
{
|
2016-12-14 21:49:53 -05:00
|
|
|
"suggest": {
|
|
|
|
"my-suggest-1" : {
|
|
|
|
"text" : "tring out Elasticsearch",
|
|
|
|
"term" : {
|
|
|
|
"field" : "message"
|
|
|
|
}
|
|
|
|
},
|
|
|
|
"my-suggest-2" : {
|
|
|
|
"text" : "kmichy",
|
|
|
|
"term" : {
|
|
|
|
"field" : "user"
|
|
|
|
}
|
2013-08-28 19:24:34 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
--------------------------------------------------
|
2016-07-22 18:51:36 -04:00
|
|
|
// CONSOLE
|
|
|
|
// TEST[setup:twitter]
|
2013-08-28 19:24:34 -04:00
|
|
|
|
|
|
|
The below suggest response example includes the suggestion response for
|
|
|
|
`my-suggest-1` and `my-suggest-2`. Each suggestion part contains
|
|
|
|
entries. Each entry is effectively a token from the suggest text and
|
|
|
|
contains the suggestion entry text, the original start offset and length
|
|
|
|
in the suggest text and if found an arbitrary number of options.
|
|
|
|
|
|
|
|
[source,js]
|
|
|
|
--------------------------------------------------
|
|
|
|
{
|
2016-07-22 18:51:36 -04:00
|
|
|
"_shards": ...
|
2016-12-14 21:49:53 -05:00
|
|
|
"hits": ...
|
|
|
|
"took": 2,
|
|
|
|
"timed_out": false,
|
|
|
|
"suggest": {
|
|
|
|
"my-suggest-1": [ {
|
|
|
|
"text": "tring",
|
|
|
|
"offset": 0,
|
|
|
|
"length": 5,
|
|
|
|
"options": [ {"text": "trying", "score": 0.8, "freq": 1 } ]
|
|
|
|
}, {
|
|
|
|
"text": "out",
|
|
|
|
"offset": 6,
|
|
|
|
"length": 3,
|
|
|
|
"options": []
|
|
|
|
}, {
|
|
|
|
"text": "elasticsearch",
|
|
|
|
"offset": 10,
|
|
|
|
"length": 13,
|
|
|
|
"options": []
|
|
|
|
} ],
|
|
|
|
"my-suggest-2": ...
|
|
|
|
}
|
2013-08-28 19:24:34 -04:00
|
|
|
}
|
|
|
|
--------------------------------------------------
|
2016-07-22 18:51:36 -04:00
|
|
|
// TESTRESPONSE[s/"_shards": \.\.\./"_shards": "$body._shards",/]
|
2016-12-14 21:49:53 -05:00
|
|
|
// TESTRESPONSE[s/"hits": .../"hits": "$body.hits",/]
|
|
|
|
// TESTRESPONSE[s/"took": 2,/"took": "$body.took",/]
|
|
|
|
// TESTRESPONSE[s/"my-suggest-2": \.\.\./"my-suggest-2": "$body.suggest.my-suggest-2"/]
|
2016-07-22 18:51:36 -04:00
|
|
|
|
2013-08-28 19:24:34 -04:00
|
|
|
|
|
|
|
Each options array contains an option object that includes the
|
|
|
|
suggested text, its document frequency and score compared to the suggest
|
|
|
|
entry text. The meaning of the score depends on the used suggester. The
|
|
|
|
term suggester's score is based on the edit distance.
|
|
|
|
|
|
|
|
[float]
|
2013-09-25 12:17:40 -04:00
|
|
|
[[global-suggest]]
|
2013-08-28 19:24:34 -04:00
|
|
|
=== Global suggest text
|
|
|
|
|
|
|
|
To avoid repetition of the suggest text, it is possible to define a
|
|
|
|
global text. In the example below the suggest text is defined globally
|
|
|
|
and applies to the `my-suggest-1` and `my-suggest-2` suggestions.
|
|
|
|
|
|
|
|
[source,js]
|
|
|
|
--------------------------------------------------
|
2016-12-14 21:49:53 -05:00
|
|
|
POST _search
|
2016-07-22 18:51:36 -04:00
|
|
|
{
|
2016-12-14 21:49:53 -05:00
|
|
|
"suggest": {
|
|
|
|
"text" : "tring out Elasticsearch",
|
|
|
|
"my-suggest-1" : {
|
|
|
|
"term" : {
|
|
|
|
"field" : "message"
|
|
|
|
}
|
|
|
|
},
|
|
|
|
"my-suggest-2" : {
|
|
|
|
"term" : {
|
|
|
|
"field" : "user"
|
|
|
|
}
|
2013-08-28 19:24:34 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
--------------------------------------------------
|
2016-07-22 18:51:36 -04:00
|
|
|
// CONSOLE
|
2013-08-28 19:24:34 -04:00
|
|
|
|
|
|
|
The suggest text can in the above example also be specified as
|
|
|
|
suggestion specific option. The suggest text specified on suggestion
|
|
|
|
level override the suggest text on the global level.
|
|
|
|
|
|
|
|
include::suggesters/term-suggest.asciidoc[]
|
|
|
|
|
|
|
|
include::suggesters/phrase-suggest.asciidoc[]
|
|
|
|
|
|
|
|
include::suggesters/completion-suggest.asciidoc[]
|
|
|
|
|
2013-10-08 07:55:25 -04:00
|
|
|
include::suggesters/context-suggest.asciidoc[]
|
2017-02-10 04:53:38 -05:00
|
|
|
|
|
|
|
include::suggesters/misc.asciidoc[]
|