Search analyzers
Search analyzers are specified at query time and are used to analyze the query string when you run a full-text query on a text field.
Determining which search analyzer to use
To determine which analyzer to use for a query string at query time, OpenSearch examines the following parameters in order:
- The
analyzer
parameter of the query - The
search_analyzer
mapping parameter of the field - The
analysis.analyzer.default_search
index setting - The
analyzer
mapping parameter of the field - The
standard
analyzer (default)
In most cases, specifying a search analyzer that is different from the index analyzer is not necessary and could negatively impact search result relevance or lead to unexpected search results.
For information about verifying which analyzer is associated with which field, see Verifying analyzer settings.
Specifying a search analyzer for a query string
Specify the name of the analyzer you want to use at query time in the analyzer
field:
GET shakespeare/_search
{
"query": {
"match": {
"text_entry": {
"query": "speak the truth",
"analyzer": "english"
}
}
}
}
For more information about supported analyzers, see Analyzers.
Specifying a search analyzer for a field
When creating index mappings, you can provide the search_analyzer
parameter for each text field. When providing the search_analyzer
, you must also provide the analyzer
parameter, which specifies the index analyzer to be used at indexing time.
For example, the following request specifies the simple
analyzer as the index analyzer and the whitespace
analyzer as the search analyzer for the text_entry
field:
PUT testindex
{
"mappings": {
"properties": {
"text_entry": {
"type": "text",
"analyzer": "simple",
"search_analyzer": "whitespace"
}
}
}
}
Specifying the default search analyzer for an index
If you want to analyze all query strings at search time with the same analyzer, you can specify the search analyzer in the analysis.analyzer.default_search
setting. When providing the analysis.analyzer.default_search
, you must also provide the analysis.analyzer.default
parameter, which specifies the index analyzer to be used at indexing time.
For example, the following request specifies the simple
analyzer as the index analyzer and the whitespace
analyzer as the search analyzer for the testindex
index:
PUT testindex
{
"settings": {
"analysis": {
"analyzer": {
"default": {
"type": "simple"
},
"default_search": {
"type": "whitespace"
}
}
}
}
}