2015-08-06 11:24:29 -04:00
|
|
|
[[search-analyzer]]
|
|
|
|
=== `search_analyzer`
|
|
|
|
|
|
|
|
Usually, the same <<analyzer,analyzer>> should be applied at index time and at
|
|
|
|
search time, to ensure that the terms in the query are in the same format as
|
|
|
|
the terms in the inverted index.
|
|
|
|
|
|
|
|
Sometimes, though, it can make sense to use a different analyzer at search
|
|
|
|
time, such as when using the <<analysis-edgengram-tokenizer,`edge_ngram`>>
|
|
|
|
tokenizer for autocomplete.
|
|
|
|
|
|
|
|
By default, queries will use the `analyzer` defined in the field mapping, but
|
|
|
|
this can be overridden with the `search_analyzer` setting:
|
|
|
|
|
|
|
|
[source,js]
|
|
|
|
--------------------------------------------------
|
2019-01-14 16:08:01 -05:00
|
|
|
PUT my_index?include_type_name=true
|
2015-08-06 11:24:29 -04:00
|
|
|
{
|
|
|
|
"settings": {
|
|
|
|
"analysis": {
|
|
|
|
"filter": {
|
|
|
|
"autocomplete_filter": {
|
|
|
|
"type": "edge_ngram",
|
|
|
|
"min_gram": 1,
|
|
|
|
"max_gram": 20
|
|
|
|
}
|
|
|
|
},
|
|
|
|
"analyzer": {
|
|
|
|
"autocomplete": { <1>
|
|
|
|
"type": "custom",
|
|
|
|
"tokenizer": "standard",
|
|
|
|
"filter": [
|
|
|
|
"lowercase",
|
|
|
|
"autocomplete_filter"
|
|
|
|
]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
"mappings": {
|
2017-12-14 11:47:53 -05:00
|
|
|
"_doc": {
|
2015-08-06 11:24:29 -04:00
|
|
|
"properties": {
|
|
|
|
"text": {
|
2016-03-18 12:01:27 -04:00
|
|
|
"type": "text",
|
2015-08-06 11:24:29 -04:00
|
|
|
"analyzer": "autocomplete", <2>
|
|
|
|
"search_analyzer": "standard" <2>
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-14 11:47:53 -05:00
|
|
|
PUT my_index/_doc/1
|
2015-08-06 11:24:29 -04:00
|
|
|
{
|
|
|
|
"text": "Quick Brown Fox" <3>
|
|
|
|
}
|
|
|
|
|
|
|
|
GET my_index/_search
|
|
|
|
{
|
|
|
|
"query": {
|
|
|
|
"match": {
|
|
|
|
"text": {
|
|
|
|
"query": "Quick Br", <4>
|
|
|
|
"operator": "and"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
--------------------------------------------------
|
2016-05-09 09:42:23 -04:00
|
|
|
// CONSOLE
|
2015-08-06 11:24:29 -04:00
|
|
|
|
|
|
|
<1> Analysis settings to define the custom `autocomplete` analyzer.
|
|
|
|
<2> The `text` field uses the `autocomplete` analyzer at index time, but the `standard` analyzer at search time.
|
|
|
|
<3> This field is indexed as the terms: [ `q`, `qu`, `qui`, `quic`, `quick`, `b`, `br`, `bro`, `brow`, `brown`, `f`, `fo`, `fox` ]
|
|
|
|
<4> The query searches for both of these terms: [ `quick`, `br` ]
|
|
|
|
|
|
|
|
See {defguide}/_index_time_search_as_you_type.html[Index time search-as-you-
|
|
|
|
type] for a full explanation of this example.
|
2015-08-12 15:21:37 -04:00
|
|
|
|
2017-07-05 06:30:19 -04:00
|
|
|
TIP: The `search_analyzer` setting can be updated on existing fields
|
2015-08-12 15:21:37 -04:00
|
|
|
using the <<indices-put-mapping,PUT mapping API>>.
|