Completion field type
Create a mapping with a completion field:
copy
Index suggestions into OpenSearch:
{
"suggestions": {
"input": ["Books on openings", "Books on endgames"],
"weight" : 10
}
}
copy
Parameters
The following table lists the parameters accepted by completion fields.
Multiple suggestions can be indexed as follows:
PUT chess_store/_doc/2
{
"suggestions": [
{
"input": "Chess set",
"weight": 20
},
{
"input": "Chess pieces",
"weight": 10
},
{
"input": "Chess board",
"weight": 5
}
]
}
copy
As an alternative, you can use the following shorthand notation (note that you cannot provide the weight
parameter in this notation):
To query completion field types, specify the prefix that you want to search for and the name of the field in which to look for suggestions.
Query the index for suggestions that start with the word “chess”:
GET chess_store/_search
{
"suggest": {
"product-suggestions": {
"prefix": "chess",
"completion": {
"field": "suggestions"
}
}
}
}
copy
The response contains autocomplete suggestions:
{
"took" : 3,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 0,
"relation" : "eq"
},
"max_score" : null,
"hits" : [ ]
},
"suggest" : {
"product-suggestions" : [
{
"text" : "chess",
"offset" : 0,
"length" : 5,
"options" : [
{
"text" : "Chess set",
"_index" : "chess_store",
"_type" : "_doc",
"_id" : "2",
"_score" : 20.0,
"_source" : {
"suggestions" : [
{
"input" : "Chess set",
"weight" : 20
{
"input" : "Chess pieces",
"weight" : 10
},
{
"input" : "Chess board",
"weight" : 5
}
]
}
},
{
"_index" : "chess_store",
"_type" : "_doc",
"_id" : "3",
"_score" : 1.0,
"_source" : {
"suggestions" : [
"Chess clock",
"Chess timer"
]
}
}
]
}
]
}
}
In the response, the _score
field contains the value of the weight
parameter that was set up at index time. The text
field is populated with the suggestion’s input
parameter.
By default, the response contains the whole document, including the _source
field, which may impact performance. To return only the suggestions
field, you can specify that in the _source
parameter. You can also restrict the number of returned suggestions by specifying the size
parameter.
copy
The response contains the suggestions:
{
"took" : 5,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 0,
"relation" : "eq"
},
"max_score" : null,
"hits" : [ ]
},
"suggest" : {
"product-suggestions" : [
{
"text" : "chess",
"offset" : 0,
"length" : 5,
"options" : [
{
"text" : "Chess set",
"_index" : "chess_store",
"_type" : "_doc",
"_id" : "2",
"_score" : 20.0,
"_source" : {
"suggestions" : [
{
"input" : "Chess set",
"weight" : 20
},
{
"input" : "Chess pieces",
"weight" : 10
},
{
"input" : "Chess board",
"weight" : 5
}
]
}
},
"text" : "Chess clock",
"_index" : "chess_store",
"_type" : "_doc",
"_id" : "3",
"_score" : 1.0,
"_source" : {
"suggestions" : [
"Chess clock",
"Chess timer"
]
}
}
]
}
]
}
Completion query parameters
The following table lists the parameters accepted by the completion suggester query.
To allow for fuzzy matching, you can specify the fuzziness
parameter for the completion query. In this case, even if the user mistypes a search term, the completion query still returns results. Additionally, the longer the prefix that matches the query, the higher the document’s score.
GET chess_store/_search
{
"suggest": {
"product-suggestions": {
"prefix": "chesc",
"completion": {
"field": "suggestions",
"size" : 3,
"fuzzy" : {
"fuzziness" : "AUTO"
}
}
}
}
}
copy
To use all default fuzziness options, specify "fuzzy": {}
or "fuzzy": true
.
The following table lists the parameters accepted by the fuzzy completion suggester query. All of the parameters are optional.
Regex queries
You can use a regular expression to define the prefix for the completion suggester query.
For example, to search for strings that start with “a” and have a “d” later on, use the following query:
copy
{
"took" : 2,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 0,
"relation" : "eq"
},
"max_score" : null,
"hits" : [ ]
},
"suggest" : {
"product-suggestions" : [
{
"text" : "a.*d",
"offset" : 0,
"length" : 4,
"options" : [
{
"text" : "abcde",
"_index" : "chess_store",
"_type" : "_doc",
"_id" : "2",
"_score" : 20.0,
"_source" : {
"suggestions" : [
{
"input" : "abcde",
"weight" : 20
}
]
}
}
]
}
]
}