match-query.asciidoc 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. [[query-dsl-match-query]]
  2. === Match Query
  3. A family of `match` queries that accept text/numerics/dates, analyzes
  4. it, and constructs a query out of it. For example:
  5. [source,js]
  6. --------------------------------------------------
  7. {
  8. "match" : {
  9. "message" : "this is a test"
  10. }
  11. }
  12. --------------------------------------------------
  13. Note, `message` is the name of a field, you can substitute the name of
  14. any field (including `_all`) instead.
  15. [float]
  16. ==== Types of Match Queries
  17. [float]
  18. ===== boolean
  19. The default `match` query is of type `boolean`. It means that the text
  20. provided is analyzed and the analysis process constructs a boolean query
  21. from the provided text. The `operator` flag can be set to `or` or `and`
  22. to control the boolean clauses (defaults to `or`). The minimum number of
  23. should clauses to match can be set using the
  24. <<query-dsl-minimum-should-match,`minimum_should_match`>>
  25. parameter.
  26. The `analyzer` can be set to control which analyzer will perform the
  27. analysis process on the text. It default to the field explicit mapping
  28. definition, or the default search analyzer.
  29. `fuzziness` can be set to a value (depending on the relevant type, for
  30. string types it should be a value between `0.0` and `1.0`) to constructs
  31. fuzzy queries for each term analyzed. The `prefix_length` and
  32. `max_expansions` can be set in this case to control the fuzzy process.
  33. If the fuzzy option is set the query will use `constant_score_rewrite`
  34. as its <<query-dsl-multi-term-rewrite,rewrite
  35. method>> the `rewrite` parameter allows to control how the query will get
  36. rewritten.
  37. Here is an example when providing additional parameters (note the slight
  38. change in structure, `message` is the field name):
  39. [source,js]
  40. --------------------------------------------------
  41. {
  42. "match" : {
  43. "message" : {
  44. "query" : "this is a test",
  45. "operator" : "and"
  46. }
  47. }
  48. }
  49. --------------------------------------------------
  50. .zero_terms_query
  51. If the analyzer used removes all tokens in a query like a `stop` filter
  52. does, the default behavior is to match no documents at all. In order to
  53. change that the `zero_terms_query` option can be used, which accepts
  54. `none` (default) and `all` which corresponds to a `match_all` query.
  55. [source,js]
  56. --------------------------------------------------
  57. {
  58. "match" : {
  59. "message" : {
  60. "query" : "to be or not to be",
  61. "operator" : "and",
  62. "zero_terms_query": "all"
  63. }
  64. }
  65. }
  66. --------------------------------------------------
  67. .cutoff_frequency
  68. The match query supports a `cutoff_frequency` that allows
  69. specifying an absolute or relative document frequency where high
  70. frequent terms are moved into an optional subquery and are only scored
  71. if one of the low frequent (below the cutoff) terms in the case of an
  72. `or` operator or all of the low frequent terms in the case of an `and`
  73. operator match.
  74. This query allows handling `stopwords` dynamically at runtime, is domain
  75. independent and doesn't require on a stopword file. It prevent scoring /
  76. iterating high frequent terms and only takes the terms into account if a
  77. more significant / lower frequent terms match a document. Yet, if all of
  78. the query terms are above the given `cutoff_frequency` the query is
  79. automatically transformed into a pure conjunction (`and`) query to
  80. ensure fast execution.
  81. The `cutoff_frequency` can either be relative to the number of documents
  82. in the index if in the range `[0..1)` or absolute if greater or equal to
  83. `1.0`.
  84. Here is an example showing a query composed of stopwords exclusivly:
  85. [source,js]
  86. --------------------------------------------------
  87. {
  88. "match" : {
  89. "message" : {
  90. "query" : "to be or not to be",
  91. "cutoff_frequency" : 0.001
  92. }
  93. }
  94. }
  95. --------------------------------------------------
  96. [float]
  97. ===== phrase
  98. The `match_phrase` query analyzes the text and creates a `phrase` query
  99. out of the analyzed text. For example:
  100. [source,js]
  101. --------------------------------------------------
  102. {
  103. "match_phrase" : {
  104. "message" : "this is a test"
  105. }
  106. }
  107. --------------------------------------------------
  108. Since `match_phrase` is only a `type` of a `match` query, it can also be
  109. used in the following manner:
  110. [source,js]
  111. --------------------------------------------------
  112. {
  113. "match" : {
  114. "message" : {
  115. "query" : "this is a test",
  116. "type" : "phrase"
  117. }
  118. }
  119. }
  120. --------------------------------------------------
  121. A phrase query maintains order of the terms up to a configurable `slop`
  122. (which defaults to 0).
  123. The `analyzer` can be set to control which analyzer will perform the
  124. analysis process on the text. It default to the field explicit mapping
  125. definition, or the default search analyzer, for example:
  126. [source,js]
  127. --------------------------------------------------
  128. {
  129. "match_phrase" : {
  130. "message" : {
  131. "query" : "this is a test",
  132. "analyzer" : "my_analyzer"
  133. }
  134. }
  135. }
  136. --------------------------------------------------
  137. [float]
  138. ===== match_phrase_prefix
  139. The `match_phrase_prefix` is the same as `match_phrase`, except that it
  140. allows for prefix matches on the last term in the text. For example:
  141. [source,js]
  142. --------------------------------------------------
  143. {
  144. "match_phrase_prefix" : {
  145. "message" : "this is a test"
  146. }
  147. }
  148. --------------------------------------------------
  149. Or:
  150. [source,js]
  151. --------------------------------------------------
  152. {
  153. "match" : {
  154. "message" : {
  155. "query" : "this is a test",
  156. "type" : "phrase_prefix"
  157. }
  158. }
  159. }
  160. --------------------------------------------------
  161. It accepts the same parameters as the phrase type. In addition, it also
  162. accepts a `max_expansions` parameter that can control to how many
  163. prefixes the last term will be expanded. It is highly recommended to set
  164. it to an acceptable value to control the execution time of the query.
  165. For example:
  166. [source,js]
  167. --------------------------------------------------
  168. {
  169. "match_phrase_prefix" : {
  170. "message" : {
  171. "query" : "this is a test",
  172. "max_expansions" : 10
  173. }
  174. }
  175. }
  176. --------------------------------------------------
  177. [float]
  178. ==== Comparison to query_string / field
  179. The match family of queries does not go through a "query parsing"
  180. process. It does not support field name prefixes, wildcard characters,
  181. or other "advance" features. For this reason, chances of it failing are
  182. very small / non existent, and it provides an excellent behavior when it
  183. comes to just analyze and run that text as a query behavior (which is
  184. usually what a text search box does). Also, the `phrase_prefix` type can
  185. provide a great "as you type" behavior to automatically load search
  186. results.