match-query.asciidoc 8.1 KB

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