2013-08-28 19:24:34 -04:00
|
|
|
[[search-suggesters]]
|
2019-07-19 14:35:36 -04:00
|
|
|
=== Suggesters
|
2013-08-28 19:24:34 -04:00
|
|
|
|
2019-09-25 08:38:47 -04:00
|
|
|
Suggests similar looking terms based on a provided text by using a suggester.
|
|
|
|
Parts of the suggest feature are still under development.
|
2013-08-28 19:24:34 -04:00
|
|
|
|
2019-09-09 12:35:50 -04:00
|
|
|
[source,console]
|
2013-08-28 19:24:34 -04:00
|
|
|
--------------------------------------------------
|
2020-08-04 14:16:38 -04:00
|
|
|
POST my-index-000001/_search
|
2016-07-22 18:51:36 -04:00
|
|
|
{
|
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
|
|
|
--------------------------------------------------
|
2020-08-04 14:16:38 -04:00
|
|
|
// TEST[setup:messages]
|
2013-08-28 19:24:34 -04:00
|
|
|
|
2019-09-25 08:38:47 -04:00
|
|
|
|
|
|
|
[[search-suggesters-api-request]]
|
|
|
|
==== {api-request-title}
|
|
|
|
|
|
|
|
The suggest feature suggests similar looking terms based on a provided text by
|
|
|
|
using a suggester. The suggest request part is defined alongside the query part
|
|
|
|
in a `_search` request. If the query part is left out, only suggestions are
|
|
|
|
returned.
|
|
|
|
|
|
|
|
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.
|
|
|
|
|
|
|
|
|
|
|
|
[[search-suggesters-api-example]]
|
|
|
|
==== {api-examples-title}
|
|
|
|
|
|
|
|
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`.
|
2013-08-28 19:24:34 -04:00
|
|
|
|
2019-09-09 12:35:50 -04:00
|
|
|
[source,console]
|
2013-08-28 19:24:34 -04:00
|
|
|
--------------------------------------------------
|
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" : {
|
2020-08-04 14:16:38 -04:00
|
|
|
"field" : "user.id"
|
2016-12-14 21:49:53 -05:00
|
|
|
}
|
2013-08-28 19:24:34 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
--------------------------------------------------
|
2020-08-04 14:16:38 -04:00
|
|
|
// TEST[setup:messages]
|
|
|
|
// TEST[s/^/PUT my-index-000001\/_mapping\n{"properties":{"user":{"properties":{"id":{"type":"keyword"}}}}}\n/]
|
2019-09-25 08:38:47 -04:00
|
|
|
|
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.
|
|
|
|
|
2019-09-06 16:09:09 -04:00
|
|
|
[source,console-result]
|
2013-08-28 19:24:34 -04:00
|
|
|
--------------------------------------------------
|
|
|
|
{
|
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.
|
|
|
|
|
2020-07-23 12:42:33 -04:00
|
|
|
[discrete]
|
2013-09-25 12:17:40 -04:00
|
|
|
[[global-suggest]]
|
2019-09-25 08:38:47 -04:00
|
|
|
===== Global suggest text
|
2013-08-28 19:24:34 -04:00
|
|
|
|
|
|
|
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.
|
|
|
|
|
2019-09-09 12:35:50 -04:00
|
|
|
[source,console]
|
2013-08-28 19:24:34 -04:00
|
|
|
--------------------------------------------------
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
--------------------------------------------------
|
|
|
|
|
|
|
|
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[]
|