search-analyzer.asciidoc 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. [[search-analyzer]]
  2. === `search_analyzer`
  3. Usually, the same <<analyzer,analyzer>> should be applied at index time and at
  4. search time, to ensure that the terms in the query are in the same format as
  5. the terms in the inverted index.
  6. Sometimes, though, it can make sense to use a different analyzer at search
  7. time, such as when using the <<analysis-edgengram-tokenizer,`edge_ngram`>>
  8. tokenizer for autocomplete or when using search-time synonyms.
  9. By default, queries will use the `analyzer` defined in the field mapping, but
  10. this can be overridden with the `search_analyzer` setting:
  11. [source,console]
  12. --------------------------------------------------
  13. PUT my-index-000001
  14. {
  15. "settings": {
  16. "analysis": {
  17. "filter": {
  18. "autocomplete_filter": {
  19. "type": "edge_ngram",
  20. "min_gram": 1,
  21. "max_gram": 20
  22. }
  23. },
  24. "analyzer": {
  25. "autocomplete": { <1>
  26. "type": "custom",
  27. "tokenizer": "standard",
  28. "filter": [
  29. "lowercase",
  30. "autocomplete_filter"
  31. ]
  32. }
  33. }
  34. }
  35. },
  36. "mappings": {
  37. "properties": {
  38. "text": {
  39. "type": "text",
  40. "analyzer": "autocomplete", <2>
  41. "search_analyzer": "standard" <2>
  42. }
  43. }
  44. }
  45. }
  46. PUT my-index-000001/_doc/1
  47. {
  48. "text": "Quick Brown Fox" <3>
  49. }
  50. GET my-index-000001/_search
  51. {
  52. "query": {
  53. "match": {
  54. "text": {
  55. "query": "Quick Br", <4>
  56. "operator": "and"
  57. }
  58. }
  59. }
  60. }
  61. --------------------------------------------------
  62. <1> Analysis settings to define the custom `autocomplete` analyzer.
  63. <2> The `text` field uses the `autocomplete` analyzer at index time, but the `standard` analyzer at search time.
  64. <3> This field is indexed as the terms: [ `q`, `qu`, `qui`, `quic`, `quick`, `b`, `br`, `bro`, `brow`, `brown`, `f`, `fo`, `fox` ]
  65. <4> The query searches for both of these terms: [ `quick`, `br` ]
  66. See {defguide}/_index_time_search_as_you_type.html[Index time search-as-you-
  67. type] for a full explanation of this example.
  68. TIP: The `search_analyzer` setting can be updated on existing fields
  69. using the <<indices-put-mapping,update mapping API>>. Note, that in order to do so,
  70. any existing "analyzer" setting and "type" need to be repeated in the updated field definition.