elasticsearch/docs/reference/mapping/params/search-analyzer.asciidoc
James Rodewig 5c75d004fa
[DOCS] Replace put with create or update in API names (#70330)
Co-authored-by: debadair <debadair@elastic.co>
Co-authored-by: Lisa Cawley <lcawley@elastic.co>
Co-authored-by: Elastic Machine <elasticmachine@users.noreply.github.com>
2021-03-15 14:49:44 -04:00

80 lines
2.2 KiB
Text

[[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 or when using search-time synonyms.
By default, queries will use the `analyzer` defined in the field mapping, but
this can be overridden with the `search_analyzer` setting:
[source,console]
--------------------------------------------------
PUT my-index-000001
{
"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": {
"properties": {
"text": {
"type": "text",
"analyzer": "autocomplete", <2>
"search_analyzer": "standard" <2>
}
}
}
}
PUT my-index-000001/_doc/1
{
"text": "Quick Brown Fox" <3>
}
GET my-index-000001/_search
{
"query": {
"match": {
"text": {
"query": "Quick Br", <4>
"operator": "and"
}
}
}
}
--------------------------------------------------
<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.
TIP: The `search_analyzer` setting can be updated on existing fields
using the <<indices-put-mapping,update mapping API>>. Note, that in order to do so,
any existing "analyzer" setting and "type" need to be repeated in the updated field definition.