match-query.asciidoc 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. [[query-dsl-match-query]]
  2. === Match query
  3. ++++
  4. <titleabbrev>Match</titleabbrev>
  5. ++++
  6. Returns documents that match a provided text, number, date or boolean value. The
  7. provided text is analyzed before matching.
  8. The `match` query is the standard query for performing a full-text search,
  9. including options for fuzzy matching.
  10. [[match-query-ex-request]]
  11. ==== Example request
  12. [source,console]
  13. --------------------------------------------------
  14. GET /_search
  15. {
  16. "query": {
  17. "match": {
  18. "message": {
  19. "query": "this is a test"
  20. }
  21. }
  22. }
  23. }
  24. --------------------------------------------------
  25. [[match-top-level-params]]
  26. ==== Top-level parameters for `match`
  27. `<field>`::
  28. (Required, object) Field you wish to search.
  29. [[match-field-params]]
  30. ==== Parameters for `<field>`
  31. `query`::
  32. +
  33. --
  34. (Required) Text, number, boolean value or date you wish to find in the provided
  35. `<field>`.
  36. The `match` query <<analysis,analyzes>> any provided text before performing a
  37. search. This means the `match` query can search <<text,`text`>> fields for
  38. analyzed tokens rather than an exact term.
  39. --
  40. `analyzer`::
  41. (Optional, string) <<analysis,Analyzer>> used to convert the text in the `query`
  42. value into tokens. Defaults to the <<specify-index-time-analyzer,index-time
  43. analyzer>> mapped for the `<field>`. If no analyzer is mapped, the index's
  44. default analyzer is used.
  45. `auto_generate_synonyms_phrase_query`::
  46. +
  47. --
  48. (Optional, boolean) If `true`, <<query-dsl-match-query-phrase,match phrase>>
  49. queries are automatically created for multi-term synonyms. Defaults to `true`.
  50. See <<query-dsl-match-query-synonyms,Use synonyms with match query>> for an
  51. example.
  52. --
  53. `fuzziness`::
  54. (Optional, string) Maximum edit distance allowed for matching. See <<fuzziness>>
  55. for valid values and more information. See <<query-dsl-match-query-fuzziness>>
  56. for an example.
  57. `max_expansions`::
  58. (Optional, integer) Maximum number of terms to which the query will
  59. expand. Defaults to `50`.
  60. `prefix_length`::
  61. (Optional, integer) Number of beginning characters left unchanged for fuzzy
  62. matching. Defaults to `0`.
  63. `fuzzy_transpositions`::
  64. (Optional, boolean) If `true`, edits for fuzzy matching include
  65. transpositions of two adjacent characters (ab → ba). Defaults to `true`.
  66. `fuzzy_rewrite`::
  67. +
  68. --
  69. (Optional, string) Method used to rewrite the query. See the
  70. <<query-dsl-multi-term-rewrite, `rewrite` parameter>> for valid values and more
  71. information.
  72. If the `fuzziness` parameter is not `0`, the `match` query uses a `rewrite`
  73. method of `top_terms_blended_freqs_${max_expansions}` by default.
  74. --
  75. `lenient`::
  76. (Optional, boolean) If `true`, format-based errors, such as providing a text
  77. `query` value for a <<number,numeric>> field, are ignored. Defaults to `false`.
  78. `operator`::
  79. +
  80. --
  81. (Optional, string) Boolean logic used to interpret text in the `query` value.
  82. Valid values are:
  83. `OR` (Default)::
  84. For example, a `query` value of `capital of Hungary` is interpreted as `capital
  85. OR of OR Hungary`.
  86. `AND`::
  87. For example, a `query` value of `capital of Hungary` is interpreted as `capital
  88. AND of AND Hungary`.
  89. --
  90. `minimum_should_match`::
  91. +
  92. --
  93. (Optional, string) Minimum number of clauses that must match for a document to
  94. be returned. See the <<query-dsl-minimum-should-match, `minimum_should_match`
  95. parameter>> for valid values and more information.
  96. --
  97. `zero_terms_query`::
  98. +
  99. --
  100. (Optional, string) Indicates whether no documents are returned if the `analyzer`
  101. removes all tokens, such as when using a `stop` filter. Valid values are:
  102. `none` (Default)::
  103. No documents are returned if the `analyzer` removes all tokens.
  104. `all`::
  105. Returns all documents, similar to a <<query-dsl-match-all-query,`match_all`>>
  106. query.
  107. See <<query-dsl-match-query-zero>> for an example.
  108. --
  109. [[match-query-notes]]
  110. ==== Notes
  111. [[query-dsl-match-query-short-ex]]
  112. ===== Short request example
  113. You can simplify the match query syntax by combining the `<field>` and `query`
  114. parameters. For example:
  115. [source,console]
  116. ----
  117. GET /_search
  118. {
  119. "query": {
  120. "match": {
  121. "message": "this is a test"
  122. }
  123. }
  124. }
  125. ----
  126. [[query-dsl-match-query-boolean]]
  127. ===== How the match query works
  128. The `match` query is of type `boolean`. It means that the text
  129. provided is analyzed and the analysis process constructs a boolean query
  130. from the provided text. The `operator` parameter can be set to `or` or `and`
  131. to control the boolean clauses (defaults to `or`). The minimum number of
  132. optional `should` clauses to match can be set using the
  133. <<query-dsl-minimum-should-match,`minimum_should_match`>>
  134. parameter.
  135. Here is an example with the `operator` parameter:
  136. [source,console]
  137. --------------------------------------------------
  138. GET /_search
  139. {
  140. "query": {
  141. "match": {
  142. "message": {
  143. "query": "this is a test",
  144. "operator": "and"
  145. }
  146. }
  147. }
  148. }
  149. --------------------------------------------------
  150. The `analyzer` can be set to control which analyzer will perform the
  151. analysis process on the text. It defaults to the field explicit mapping
  152. definition, or the default search analyzer.
  153. The `lenient` parameter can be set to `true` to ignore exceptions caused by
  154. data-type mismatches, such as trying to query a numeric field with a text
  155. query string. Defaults to `false`.
  156. [[query-dsl-match-query-fuzziness]]
  157. ===== Fuzziness in the match query
  158. `fuzziness` allows _fuzzy matching_ based on the type of field being queried.
  159. See <<fuzziness>> for allowed settings.
  160. The `prefix_length` and
  161. `max_expansions` can be set in this case to control the fuzzy process.
  162. If the fuzzy option is set the query will use `top_terms_blended_freqs_${max_expansions}`
  163. as its <<query-dsl-multi-term-rewrite,rewrite
  164. method>> the `fuzzy_rewrite` parameter allows to control how the query will get
  165. rewritten.
  166. Fuzzy transpositions (`ab` -> `ba`) are allowed by default but can be disabled
  167. by setting `fuzzy_transpositions` to `false`.
  168. NOTE: Fuzzy matching is not applied to terms with synonyms or in cases where the
  169. analysis process produces multiple tokens at the same position. Under the hood
  170. these terms are expanded to a special synonym query that blends term frequencies,
  171. which does not support fuzzy expansion.
  172. [source,console]
  173. --------------------------------------------------
  174. GET /_search
  175. {
  176. "query": {
  177. "match": {
  178. "message": {
  179. "query": "this is a testt",
  180. "fuzziness": "AUTO"
  181. }
  182. }
  183. }
  184. }
  185. --------------------------------------------------
  186. [[query-dsl-match-query-zero]]
  187. ===== Zero terms query
  188. If the analyzer used removes all tokens in a query like a `stop` filter
  189. does, the default behavior is to match no documents at all. In order to
  190. change that the `zero_terms_query` option can be used, which accepts
  191. `none` (default) and `all` which corresponds to a `match_all` query.
  192. [source,console]
  193. --------------------------------------------------
  194. GET /_search
  195. {
  196. "query": {
  197. "match": {
  198. "message": {
  199. "query": "to be or not to be",
  200. "operator": "and",
  201. "zero_terms_query": "all"
  202. }
  203. }
  204. }
  205. }
  206. --------------------------------------------------
  207. [[query-dsl-match-query-synonyms]]
  208. ===== Synonyms
  209. The `match` query supports multi-terms synonym expansion with the <<analysis-synonym-graph-tokenfilter,
  210. synonym_graph>> token filter. When this filter is used, the parser creates a phrase query for each multi-terms synonyms.
  211. For example, the following synonym: `"ny, new york"` would produce:
  212. `(ny OR ("new york"))`
  213. It is also possible to match multi terms synonyms with conjunctions instead:
  214. [source,console]
  215. --------------------------------------------------
  216. GET /_search
  217. {
  218. "query": {
  219. "match" : {
  220. "message": {
  221. "query" : "ny city",
  222. "auto_generate_synonyms_phrase_query" : false
  223. }
  224. }
  225. }
  226. }
  227. --------------------------------------------------
  228. The example above creates a boolean query:
  229. `(ny OR (new AND york)) city`
  230. that matches documents with the term `ny` or the conjunction `new AND york`.
  231. By default the parameter `auto_generate_synonyms_phrase_query` is set to `true`.