Search-as-you-type field type
Mapping a search-as-you-type field creates n-gram subfields of this field, where n is in the range [2, ]. Additionally, it creates an index prefix subfield.
Create a mapping with a search-as-you-type field:
copy
In addition to the suggestions
field, this creates suggestions._2gram
, suggestions._3gram
, and suggestions._index_prefix
fields.
Index a document with a search-as-you-type field:
PUT books/_doc/1
{
"suggestions": "one two three four"
}
To match terms in any order, use a bool_prefix or multi-match query. These queries rank the documents in which search terms are in the specified order higher than the documents in which terms are out of order.
copy
The response contains the matching document:
{
"took" : 13,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 1,
"relation" : "eq"
},
"max_score" : 1.0,
"hits" : [
{
"_index" : "books",
"_type" : "_doc",
"_id" : "1",
"_score" : 1.0,
"_source" : {
"suggestions" : "one two three four"
}
]
}
}
To match terms in order, use a match_phrase_prefix query:
copy
{
"took" : 23,
"timed_out" : false,
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 1,
"relation" : "eq"
},
"max_score" : 0.4793051,
"hits" : [
{
"_index" : "books",
"_type" : "_doc",
"_id" : "1",
"_score" : 0.4793051,
"_source" : {
"suggestions" : "one two three four"
}
}
}
}
To match the last terms exactly, use a match_phrase query:
copy
Response:
{
"took" : 2,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 1,
"relation" : "eq"
},
"max_score" : 0.2876821,
{
"_index" : "books",
"_type" : "_doc",
"_id" : "1",
"_score" : 0.2876821,
"_source" : {
"suggestions" : "one two three four"
}
}
]
}
Parameters
The following table lists the parameters accepted by search-as-you-type field types. All parameters are optional.