search-analyzer.asciidoc 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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.
  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,js]
  12. --------------------------------------------------
  13. PUT my_index
  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. "my_type": {
  38. "properties": {
  39. "text": {
  40. "type": "text",
  41. "analyzer": "autocomplete", <2>
  42. "search_analyzer": "standard" <2>
  43. }
  44. }
  45. }
  46. }
  47. }
  48. PUT my_index/my_type/1
  49. {
  50. "text": "Quick Brown Fox" <3>
  51. }
  52. GET my_index/_search
  53. {
  54. "query": {
  55. "match": {
  56. "text": {
  57. "query": "Quick Br", <4>
  58. "operator": "and"
  59. }
  60. }
  61. }
  62. }
  63. --------------------------------------------------
  64. // CONSOLE
  65. <1> Analysis settings to define the custom `autocomplete` analyzer.
  66. <2> The `text` field uses the `autocomplete` analyzer at index time, but the `standard` analyzer at search time.
  67. <3> This field is indexed as the terms: [ `q`, `qu`, `qui`, `quic`, `quick`, `b`, `br`, `bro`, `brow`, `brown`, `f`, `fo`, `fox` ]
  68. <4> The query searches for both of these terms: [ `quick`, `br` ]
  69. See {defguide}/_index_time_search_as_you_type.html[Index time search-as-you-
  70. type] for a full explanation of this example.
  71. TIP: The `search_analyzer` setting must have the same setting for fields of
  72. the same name in the same index. Its value can be updated on existing fields
  73. using the <<indices-put-mapping,PUT mapping API>>.