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:

  1. The analyzer parameter of the query
  2. The search_analyzer mapping parameter of the field
  3. The analysis.analyzer.default_search index setting
  4. The analyzer mapping parameter of the field
  5. 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:

  1. GET shakespeare/_search
  2. {
  3. "query": {
  4. "match": {
  5. "text_entry": {
  6. "query": "speak the truth",
  7. "analyzer": "english"
  8. }
  9. }
  10. }
  11. }

copy

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:

  1. PUT testindex
  2. {
  3. "mappings": {
  4. "properties": {
  5. "text_entry": {
  6. "type": "text",
  7. "analyzer": "simple",
  8. "search_analyzer": "whitespace"
  9. }
  10. }
  11. }
  12. }

copy

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:

  1. PUT testindex
  2. {
  3. "settings": {
  4. "analysis": {
  5. "analyzer": {
  6. "default": {
  7. "type": "simple"
  8. },
  9. "default_search": {
  10. "type": "whitespace"
  11. }
  12. }
  13. }
  14. }
  15. }

copy