match-query.asciidoc 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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. {
  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. [[query-dsl-match-query-boolean]]
  16. ==== match
  17. The `match` query is of type `boolean`. It means that the text
  18. provided is analyzed and the analysis process constructs a boolean query
  19. from the provided text. The `operator` flag can be set to `or` or `and`
  20. to control the boolean clauses (defaults to `or`). The minimum number of
  21. optional `should` clauses to match can be set using the
  22. <<query-dsl-minimum-should-match,`minimum_should_match`>>
  23. parameter.
  24. The `analyzer` can be set to control which analyzer will perform the
  25. analysis process on the text. It defaults to the field explicit mapping
  26. definition, or the default search analyzer.
  27. The `lenient` parameter can be set to `true` to ignore exceptions caused by
  28. data-type mismatches, such as trying to query a numeric field with a text
  29. query string. Defaults to `false`.
  30. [[query-dsl-match-query-fuzziness]]
  31. ===== Fuzziness
  32. `fuzziness` allows _fuzzy matching_ based on the type of field being queried.
  33. See <<fuzziness>> for allowed settings.
  34. The `prefix_length` and
  35. `max_expansions` can be set in this case to control the fuzzy process.
  36. If the fuzzy option is set the query will use `top_terms_blended_freqs_${max_expansions}`
  37. as its <<query-dsl-multi-term-rewrite,rewrite
  38. method>> the `fuzzy_rewrite` parameter allows to control how the query will get
  39. rewritten.
  40. Fuzzy transpositions (`ab` -> `ba`) are allowed by default but can be disabled
  41. by setting `fuzzy_transpositions` to `false`.
  42. Here is an example when providing additional parameters (note the slight
  43. change in structure, `message` is the field name):
  44. [source,js]
  45. --------------------------------------------------
  46. {
  47. "match" : {
  48. "message" : {
  49. "query" : "this is a test",
  50. "operator" : "and"
  51. }
  52. }
  53. }
  54. --------------------------------------------------
  55. [[query-dsl-match-query-zero]]
  56. ===== Zero terms query
  57. If the analyzer used removes all tokens in a query like a `stop` filter
  58. does, the default behavior is to match no documents at all. In order to
  59. change that the `zero_terms_query` option can be used, which accepts
  60. `none` (default) and `all` which corresponds to a `match_all` query.
  61. [source,js]
  62. --------------------------------------------------
  63. {
  64. "match" : {
  65. "message" : {
  66. "query" : "to be or not to be",
  67. "operator" : "and",
  68. "zero_terms_query": "all"
  69. }
  70. }
  71. }
  72. --------------------------------------------------
  73. [[query-dsl-match-query-cutoff]]
  74. ===== Cutoff frequency
  75. The match query supports a `cutoff_frequency` that allows
  76. specifying an absolute or relative document frequency where high
  77. frequency terms are moved into an optional subquery and are only scored
  78. if one of the low frequency (below the cutoff) terms in the case of an
  79. `or` operator or all of the low frequency terms in the case of an `and`
  80. operator match.
  81. This query allows handling `stopwords` dynamically at runtime, is domain
  82. independent and doesn't require a stopword file. It prevents scoring /
  83. iterating high frequency terms and only takes the terms into account if a
  84. more significant / lower frequency term matches a document. Yet, if all
  85. of the query terms are above the given `cutoff_frequency` the query is
  86. automatically transformed into a pure conjunction (`and`) query to
  87. ensure fast execution.
  88. The `cutoff_frequency` can either be relative to the total number of
  89. documents if in the range `[0..1)` or absolute if greater or equal to
  90. `1.0`.
  91. Here is an example showing a query composed of stopwords exclusively:
  92. [source,js]
  93. --------------------------------------------------
  94. {
  95. "match" : {
  96. "message" : {
  97. "query" : "to be or not to be",
  98. "cutoff_frequency" : 0.001
  99. }
  100. }
  101. }
  102. --------------------------------------------------
  103. IMPORTANT: The `cutoff_frequency` option operates on a per-shard-level. This means
  104. that when trying it out on test indexes with low document numbers you
  105. should follow the advice in {defguide}/relevance-is-broken.html[Relevance is broken].
  106. .Comparison to query_string / field
  107. **************************************************
  108. The match family of queries does not go through a "query parsing"
  109. process. It does not support field name prefixes, wildcard characters,
  110. or other "advanced" features. For this reason, chances of it failing are
  111. very small / non existent, and it provides an excellent behavior when it
  112. comes to just analyze and run that text as a query behavior (which is
  113. usually what a text search box does). Also, the `phrase_prefix` type can
  114. provide a great "as you type" behavior to automatically load search
  115. results.
  116. **************************************************