match-query.asciidoc 5.1 KB

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