search_analyzer

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:

  1. PUT my-index-000001
  2. {
  3. "settings": {
  4. "analysis": {
  5. "filter": {
  6. "autocomplete_filter": {
  7. "type": "edge_ngram",
  8. "min_gram": 1,
  9. "max_gram": 20
  10. }
  11. },
  12. "analyzer": {
  13. "autocomplete": {
  14. "type": "custom",
  15. "tokenizer": "standard",
  16. "filter": [
  17. "lowercase",
  18. "autocomplete_filter"
  19. ]
  20. }
  21. }
  22. }
  23. },
  24. "mappings": {
  25. "properties": {
  26. "text": {
  27. "type": "text",
  28. "analyzer": "autocomplete",
  29. "search_analyzer": "standard"
  30. }
  31. }
  32. }
  33. }
  34. PUT my-index-000001/_doc/1
  35. {
  36. "text": "Quick Brown Fox"
  37. }
  38. GET my-index-000001/_search
  39. {
  40. "query": {
  41. "match": {
  42. "text": {
  43. "query": "Quick Br",
  44. "operator": "and"
  45. }
  46. }
  47. }
  48. }

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.