原英文版地址: https://www.elastic.co/guide/en/elasticsearch/reference/7.7/search-analyzer.html, 原文档版权归 www.elastic.co 所有
本地英文版地址: ../en/search-analyzer.html

search_analyzeredit

Usually, the same 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 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:

PUT my_index
{
  "settings": {
    "analysis": {
      "filter": {
        "autocomplete_filter": {
          "type": "edge_ngram",
          "min_gram": 1,
          "max_gram": 20
        }
      },
      "analyzer": {
        "autocomplete": { 
          "type": "custom",
          "tokenizer": "standard",
          "filter": [
            "lowercase",
            "autocomplete_filter"
          ]
        }
      }
    }
  },
  "mappings": {
    "properties": {
      "text": {
        "type": "text",
        "analyzer": "autocomplete", 
        "search_analyzer": "standard" 
      }
    }
  }
}

PUT my_index/_doc/1
{
  "text": "Quick Brown Fox" 
}

GET my_index/_search
{
  "query": {
    "match": {
      "text": {
        "query": "Quick Br", 
        "operator": "and"
      }
    }
  }
}

Analysis settings to define the custom autocomplete analyzer.

The text field uses the autocomplete analyzer at index time, but the standard analyzer at search time.

This field is indexed as the terms: [ q, qu, qui, quic, quick, b, br, bro, brow, brown, f, fo, fox ]

The query searches for both of these terms: [ quick, br ]

See Index time search-as-you- type for a full explanation of this example.

The search_analyzer setting can be updated on existing fields using the PUT mapping API.