match-query.asciidoc 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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. Here is an example when providing additional parameters (note the slight
  41. change in structure, `message` is the field name):
  42. [source,js]
  43. --------------------------------------------------
  44. {
  45. "match" : {
  46. "message" : {
  47. "query" : "this is a test",
  48. "operator" : "and"
  49. }
  50. }
  51. }
  52. --------------------------------------------------
  53. [[query-dsl-match-query-zero]]
  54. ===== Zero terms query
  55. If the analyzer used removes all tokens in a query like a `stop` filter
  56. does, the default behavior is to match no documents at all. In order to
  57. change that the `zero_terms_query` option can be used, which accepts
  58. `none` (default) and `all` which corresponds to a `match_all` query.
  59. [source,js]
  60. --------------------------------------------------
  61. {
  62. "match" : {
  63. "message" : {
  64. "query" : "to be or not to be",
  65. "operator" : "and",
  66. "zero_terms_query": "all"
  67. }
  68. }
  69. }
  70. --------------------------------------------------
  71. [[query-dsl-match-query-cutoff]]
  72. ===== Cutoff frequency
  73. The match query supports a `cutoff_frequency` that allows
  74. specifying an absolute or relative document frequency where high
  75. frequency terms are moved into an optional subquery and are only scored
  76. if one of the low frequency (below the cutoff) terms in the case of an
  77. `or` operator or all of the low frequency terms in the case of an `and`
  78. operator match.
  79. This query allows handling `stopwords` dynamically at runtime, is domain
  80. independent and doesn't require a stopword file. It prevents scoring /
  81. iterating high frequency terms and only takes the terms into account if a
  82. more significant / lower frequency term matches a document. Yet, if all
  83. of the query terms are above the given `cutoff_frequency` the query is
  84. automatically transformed into a pure conjunction (`and`) query to
  85. ensure fast execution.
  86. The `cutoff_frequency` can either be relative to the total number of
  87. documents if in the range `[0..1)` or absolute if greater or equal to
  88. `1.0`.
  89. Here is an example showing a query composed of stopwords exclusively:
  90. [source,js]
  91. --------------------------------------------------
  92. {
  93. "match" : {
  94. "message" : {
  95. "query" : "to be or not to be",
  96. "cutoff_frequency" : 0.001
  97. }
  98. }
  99. }
  100. --------------------------------------------------
  101. IMPORTANT: The `cutoff_frequency` option operates on a per-shard-level. This means
  102. that when trying it out on test indexes with low document numbers you
  103. should follow the advice in {defguide}/relevance-is-broken.html[Relevance is broken].
  104. .Comparison to query_string / field
  105. **************************************************
  106. The match family of queries does not go through a "query parsing"
  107. process. It does not support field name prefixes, wildcard characters,
  108. or other "advanced" features. For this reason, chances of it failing are
  109. very small / non existent, and it provides an excellent behavior when it
  110. comes to just analyze and run that text as a query behavior (which is
  111. usually what a text search box does). Also, the `phrase_prefix` type can
  112. provide a great "as you type" behavior to automatically load search
  113. results.
  114. **************************************************