OpenSearch/docs/reference/search/suggesters/misc.asciidoc

86 lines
2.3 KiB
Plaintext

[[return-suggesters-type]]
==== Returning the type of the suggester
Sometimes you need to know the exact type of a suggester in order to parse its results. The `typed_keys` parameter
can be used to change the suggester's name in the response so that it will be prefixed by its type.
Considering the following example with two suggesters `term` and `phrase`:
[source,console]
--------------------------------------------------
POST _search?typed_keys
{
"suggest": {
"text" : "some test mssage",
"my-first-suggester" : {
"term" : {
"field" : "message"
}
},
"my-second-suggester" : {
"phrase" : {
"field" : "message"
}
}
}
}
--------------------------------------------------
// TEST[setup:messages]
In the response, the suggester names will be changed to respectively `term#my-first-suggester` and
`phrase#my-second-suggester`, reflecting the types of each suggestion:
[source,console-result]
--------------------------------------------------
{
"suggest": {
"term#my-first-suggester": [ <1>
{
"text": "some",
"offset": 0,
"length": 4,
"options": []
},
{
"text": "test",
"offset": 5,
"length": 4,
"options": []
},
{
"text": "mssage",
"offset": 10,
"length": 6,
"options": [
{
"text": "message",
"score": 0.8333333,
"freq": 4
}
]
}
],
"phrase#my-second-suggester": [ <2>
{
"text": "some test mssage",
"offset": 0,
"length": 16,
"options": [
{
"text": "some test message",
"score": 0.030227963
}
]
}
]
},
...
}
--------------------------------------------------
// TESTRESPONSE[s/\.\.\./"took": "$body.took", "timed_out": false, "_shards": "$body._shards", "hits": "$body.hits"/]
// TESTRESPONSE[s/"score": 0.8333333/"score": $body.suggest.term#my-first-suggester.2.options.0.score/]
// TESTRESPONSE[s/"score": 0.030227963/"score": $body.suggest.phrase#my-second-suggester.0.options.0.score/]
<1> The name `my-first-suggester` now contains the `term` prefix.
<2> The name `my-second-suggester` now contains the `phrase` prefix.