match-query.asciidoc 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. [[query-dsl-match-query]]
  2. === Match query
  3. ++++
  4. <titleabbrev>Match</titleabbrev>
  5. ++++
  6. `match` queries accept text/numerics/dates, analyzes
  7. them, and constructs a query. For example:
  8. [source,js]
  9. --------------------------------------------------
  10. GET /_search
  11. {
  12. "query": {
  13. "match" : {
  14. "message" : "this is a test"
  15. }
  16. }
  17. }
  18. --------------------------------------------------
  19. // CONSOLE
  20. Note, `message` is the name of a field, you can substitute the name of
  21. any field instead.
  22. [[query-dsl-match-query-boolean]]
  23. ==== match
  24. The `match` query is of type `boolean`. It means that the text
  25. provided is analyzed and the analysis process constructs a boolean query
  26. from the provided text. The `operator` flag can be set to `or` or `and`
  27. to control the boolean clauses (defaults to `or`). The minimum number of
  28. optional `should` clauses to match can be set using the
  29. <<query-dsl-minimum-should-match,`minimum_should_match`>>
  30. parameter.
  31. Here is an example when providing additional parameters (note the slight
  32. change in structure, `message` is the field name):
  33. [source,js]
  34. --------------------------------------------------
  35. GET /_search
  36. {
  37. "query": {
  38. "match" : {
  39. "message" : {
  40. "query" : "this is a test",
  41. "operator" : "and"
  42. }
  43. }
  44. }
  45. }
  46. --------------------------------------------------
  47. // CONSOLE
  48. The `analyzer` can be set to control which analyzer will perform the
  49. analysis process on the text. It defaults to the field explicit mapping
  50. definition, or the default search analyzer.
  51. The `lenient` parameter can be set to `true` to ignore exceptions caused by
  52. data-type mismatches, such as trying to query a numeric field with a text
  53. query string. Defaults to `false`.
  54. [[query-dsl-match-query-fuzziness]]
  55. ===== Fuzziness
  56. `fuzziness` allows _fuzzy matching_ based on the type of field being queried.
  57. See <<fuzziness>> for allowed settings.
  58. The `prefix_length` and
  59. `max_expansions` can be set in this case to control the fuzzy process.
  60. If the fuzzy option is set the query will use `top_terms_blended_freqs_${max_expansions}`
  61. as its <<query-dsl-multi-term-rewrite,rewrite
  62. method>> the `fuzzy_rewrite` parameter allows to control how the query will get
  63. rewritten.
  64. Fuzzy transpositions (`ab` -> `ba`) are allowed by default but can be disabled
  65. by setting `fuzzy_transpositions` to `false`.
  66. NOTE: Fuzzy matching is not applied to terms with synonyms or in cases where the
  67. analysis process produces multiple tokens at the same position. Under the hood
  68. these terms are expanded to a special synonym query that blends term frequencies,
  69. which does not support fuzzy expansion.
  70. [source,js]
  71. --------------------------------------------------
  72. GET /_search
  73. {
  74. "query": {
  75. "match" : {
  76. "message" : {
  77. "query" : "this is a testt",
  78. "fuzziness": "AUTO"
  79. }
  80. }
  81. }
  82. }
  83. --------------------------------------------------
  84. // CONSOLE
  85. [[query-dsl-match-query-zero]]
  86. ===== Zero terms query
  87. If the analyzer used removes all tokens in a query like a `stop` filter
  88. does, the default behavior is to match no documents at all. In order to
  89. change that the `zero_terms_query` option can be used, which accepts
  90. `none` (default) and `all` which corresponds to a `match_all` query.
  91. [source,js]
  92. --------------------------------------------------
  93. GET /_search
  94. {
  95. "query": {
  96. "match" : {
  97. "message" : {
  98. "query" : "to be or not to be",
  99. "operator" : "and",
  100. "zero_terms_query": "all"
  101. }
  102. }
  103. }
  104. }
  105. --------------------------------------------------
  106. // CONSOLE
  107. [[query-dsl-match-query-synonyms]]
  108. ===== Synonyms
  109. The `match` query supports multi-terms synonym expansion with the <<analysis-synonym-graph-tokenfilter,
  110. synonym_graph>> token filter. When this filter is used, the parser creates a phrase query for each multi-terms synonyms.
  111. For example, the following synonym: `"ny, new york" would produce:`
  112. `(ny OR ("new york"))`
  113. It is also possible to match multi terms synonyms with conjunctions instead:
  114. [source,js]
  115. --------------------------------------------------
  116. GET /_search
  117. {
  118. "query": {
  119. "match" : {
  120. "message": {
  121. "query" : "ny city",
  122. "auto_generate_synonyms_phrase_query" : false
  123. }
  124. }
  125. }
  126. }
  127. --------------------------------------------------
  128. // CONSOLE
  129. The example above creates a boolean query:
  130. `(ny OR (new AND york)) city`
  131. that matches documents with the term `ny` or the conjunction `new AND york`.
  132. By default the parameter `auto_generate_synonyms_phrase_query` is set to `true`.
  133. .Comparison to query_string / field
  134. **************************************************
  135. The match family of queries does not go through a "query parsing"
  136. process. It does not support field name prefixes, wildcard characters,
  137. or other "advanced" features. For this reason, chances of it failing are
  138. very small / non existent, and it provides an excellent behavior when it
  139. comes to just analyze and run that text as a query behavior (which is
  140. usually what a text search box does).
  141. **************************************************