match-query.asciidoc 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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. The `analyzer` can be set to control which analyzer will perform the
  29. analysis process on the text. It defaults to the field explicit mapping
  30. definition, or the default search analyzer.
  31. The `lenient` parameter can be set to `true` to ignore exceptions caused by
  32. data-type mismatches, such as trying to query a numeric field with a text
  33. query string. Defaults to `false`.
  34. [[query-dsl-match-query-fuzziness]]
  35. ===== Fuzziness
  36. `fuzziness` allows _fuzzy matching_ based on the type of field being queried.
  37. See <<fuzziness>> for allowed settings.
  38. The `prefix_length` and
  39. `max_expansions` can be set in this case to control the fuzzy process.
  40. If the fuzzy option is set the query will use `top_terms_blended_freqs_${max_expansions}`
  41. as its <<query-dsl-multi-term-rewrite,rewrite
  42. method>> the `fuzzy_rewrite` parameter allows to control how the query will get
  43. rewritten.
  44. Fuzzy transpositions (`ab` -> `ba`) are allowed by default but can be disabled
  45. by setting `fuzzy_transpositions` to `false`.
  46. Here is an example when providing additional parameters (note the slight
  47. change in structure, `message` is the field name):
  48. [source,js]
  49. --------------------------------------------------
  50. GET /_search
  51. {
  52. "query": {
  53. "match" : {
  54. "message" : {
  55. "query" : "this is a test",
  56. "operator" : "and"
  57. }
  58. }
  59. }
  60. }
  61. --------------------------------------------------
  62. // CONSOLE
  63. [[query-dsl-match-query-zero]]
  64. ===== Zero terms query
  65. If the analyzer used removes all tokens in a query like a `stop` filter
  66. does, the default behavior is to match no documents at all. In order to
  67. change that the `zero_terms_query` option can be used, which accepts
  68. `none` (default) and `all` which corresponds to a `match_all` query.
  69. [source,js]
  70. --------------------------------------------------
  71. GET /_search
  72. {
  73. "query": {
  74. "match" : {
  75. "message" : {
  76. "query" : "to be or not to be",
  77. "operator" : "and",
  78. "zero_terms_query": "all"
  79. }
  80. }
  81. }
  82. }
  83. --------------------------------------------------
  84. // CONSOLE
  85. [[query-dsl-match-query-cutoff]]
  86. ===== Cutoff frequency
  87. The match query supports a `cutoff_frequency` that allows
  88. specifying an absolute or relative document frequency where high
  89. frequency terms are moved into an optional subquery and are only scored
  90. if one of the low frequency (below the cutoff) terms in the case of an
  91. `or` operator or all of the low frequency terms in the case of an `and`
  92. operator match.
  93. This query allows handling `stopwords` dynamically at runtime, is domain
  94. independent and doesn't require a stopword file. It prevents scoring /
  95. iterating high frequency terms and only takes the terms into account if a
  96. more significant / lower frequency term matches a document. Yet, if all
  97. of the query terms are above the given `cutoff_frequency` the query is
  98. automatically transformed into a pure conjunction (`and`) query to
  99. ensure fast execution.
  100. The `cutoff_frequency` can either be relative to the total number of
  101. documents if in the range `[0..1)` or absolute if greater or equal to
  102. `1.0`.
  103. Here is an example showing a query composed of stopwords exclusively:
  104. [source,js]
  105. --------------------------------------------------
  106. GET /_search
  107. {
  108. "query": {
  109. "match" : {
  110. "message" : {
  111. "query" : "to be or not to be",
  112. "cutoff_frequency" : 0.001
  113. }
  114. }
  115. }
  116. }
  117. --------------------------------------------------
  118. // CONSOLE
  119. IMPORTANT: The `cutoff_frequency` option operates on a per-shard-level. This means
  120. that when trying it out on test indexes with low document numbers you
  121. should follow the advice in {defguide}/relevance-is-broken.html[Relevance is broken].
  122. [[query-dsl-match-query-synonyms]]
  123. ===== Synonyms
  124. The `match` query supports multi-terms synonym expansion with the <<analysis-synonym-graph-tokenfilter,
  125. synonym_graph>> token filter. When this filter is used, the parser creates a phrase query for each multi-terms synonyms.
  126. For example, the following synonym: `"ny, new york" would produce:`
  127. `(ny OR ("new york"))`
  128. It is also possible to match multi terms synonyms with conjunctions instead:
  129. [source,js]
  130. --------------------------------------------------
  131. GET /_search
  132. {
  133. "query": {
  134. "match" : {
  135. "message": {
  136. "query" : "ny city",
  137. "auto_generate_synonyms_phrase_query" : false
  138. }
  139. }
  140. }
  141. }
  142. --------------------------------------------------
  143. // CONSOLE
  144. The example above creates a boolean query:
  145. `(ny OR (new AND york)) city)`
  146. that matches documents with the term `ny` or the conjunction `new AND york`.
  147. By default the parameter `auto_generate_synonyms_phrase_query` is set to `true`.
  148. .Comparison to query_string / field
  149. **************************************************
  150. The match family of queries does not go through a "query parsing"
  151. process. It does not support field name prefixes, wildcard characters,
  152. or other "advanced" features. For this reason, chances of it failing are
  153. very small / non existent, and it provides an excellent behavior when it
  154. comes to just analyze and run that text as a query behavior (which is
  155. usually what a text search box does). Also, the <<query-dsl-match-query-phrase-prefix,`match_phrase_prefix`>>
  156. type can provide a great "as you type" behavior to automatically load search results.
  157. **************************************************