query_dsl.asciidoc 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. [[breaking_20_query_dsl_changes]]
  2. === Query DSL changes
  3. ==== Queries and filters merged
  4. Queries and filters have been merged -- all filter clauses are now query
  5. clauses. Instead, query clauses can now be used in _query context_ or in
  6. _filter context_:
  7. Query context::
  8. A query used in query context will calculate relevance scores and will not be
  9. cacheable. Query context is used whenever filter context does not apply.
  10. Filter context::
  11. +
  12. --
  13. A query used in filter context will not calculate relevance scores, and will
  14. be cacheable. Filter context is introduced by:
  15. * the `constant_score` query
  16. * the `must_not` and (newly added) `filter` parameter in the `bool` query
  17. * the `filter` and `filters` parameters in the `function_score` query
  18. * any API called `filter`, such as the `post_filter` search parameter, or in
  19. aggregations or index aliases
  20. --
  21. ==== `terms` query and filter
  22. The `execution` option of the `terms` filter is now deprecated and is ignored
  23. if provided. Similarly, the `terms` query no longer supports the
  24. `minimum_should_match` parameter.
  25. ==== `or` and `and` now implemented via `bool`
  26. The `or` and `and` filters previously had a different execution pattern to the
  27. `bool` filter. It used to be important to use `and`/`or` with certain filter
  28. clauses, and `bool` with others.
  29. This distinction has been removed: the `bool` query is now smart enough to
  30. handle both cases optimally. As a result of this change, the `or` and `and`
  31. filters are now sugar syntax which are executed internally as a `bool` query.
  32. These filters may be removed in the future.
  33. ==== `filtered` query and `query` filter deprecated
  34. The `query` filter is deprecated as is it no longer needed -- all queries can
  35. be used in query or filter context.
  36. The `filtered` query is deprecated in favour of the `bool` query. Instead of
  37. the following:
  38. [source,js]
  39. -------------------------
  40. GET _search
  41. {
  42. "query": {
  43. "filtered": {
  44. "query": {
  45. "match": {
  46. "text": "quick brown fox"
  47. }
  48. },
  49. "filter": {
  50. "term": {
  51. "status": "published"
  52. }
  53. }
  54. }
  55. }
  56. }
  57. -------------------------
  58. move the query and filter to the `must` and `filter` parameters in the `bool`
  59. query:
  60. [source,js]
  61. -------------------------
  62. GET _search
  63. {
  64. "query": {
  65. "bool": {
  66. "must": {
  67. "match": {
  68. "text": "quick brown fox"
  69. }
  70. },
  71. "filter": {
  72. "term": {
  73. "status": "published"
  74. }
  75. }
  76. }
  77. }
  78. }
  79. -------------------------
  80. ==== Filter auto-caching
  81. It used to be possible to control which filters were cached with the `_cache`
  82. option and to provide a custom `_cache_key`. These options are deprecated
  83. and, if present, will be ignored.
  84. Query clauses used in filter context are now auto-cached when it makes sense
  85. to do so. The algorithm takes into account the frequency of use, the cost of
  86. query execution, and the cost of building the filter.
  87. The `terms` filter lookup mechanism no longer caches the values of the
  88. document containing the terms. It relies on the filesystem cache instead. If
  89. the lookup index is not too large, it is recommended to replicate it to all
  90. nodes by setting `index.auto_expand_replicas: 0-all` in order to remove the
  91. network overhead as well.
  92. ==== Numeric queries use IDF for scoring
  93. Previously, term queries on numeric fields were deliberately prevented from
  94. using the usual Lucene scoring logic and this behaviour was undocumented and,
  95. to some, unexpected.
  96. Single `term` queries on numeric fields now score in the same way as string
  97. fields, using IDF and norms (if enabled).
  98. To query numeric fields without scoring, the query clause should be used in
  99. filter context, e.g. in the `filter` parameter of the `bool` query, or wrapped
  100. in a `constant_score` query:
  101. [source,js]
  102. ----------------------------
  103. GET _search
  104. {
  105. "query": {
  106. "bool": {
  107. "must": [
  108. {
  109. "match": { <1>
  110. "numeric_tag": 5
  111. }
  112. }
  113. ],
  114. "filter": [
  115. {
  116. "match": { <2>
  117. "count": 5
  118. }
  119. }
  120. ]
  121. }
  122. }
  123. }
  124. ----------------------------
  125. <1> This clause would include IDF in the relevance score calculation.
  126. <2> This clause would have no effect on the relevance score.
  127. ==== Fuzziness and fuzzy-like-this
  128. Fuzzy matching used to calculate the score for each fuzzy alternative, meaning
  129. that rare misspellings would have a higher score than the more common correct
  130. spellings. Now, fuzzy matching blends the scores of all the fuzzy alternatives
  131. to use the IDF of the most frequently occurring alternative.
  132. Fuzziness can no longer be specified using a percentage, but should instead
  133. use the number of allowed edits:
  134. * `0`, `1`, `2`, or
  135. * `AUTO` (which chooses `0`, `1`, or `2` based on the length of the term)
  136. The `fuzzy_like_this` and `fuzzy_like_this_field` queries used a very
  137. expensive approach to fuzzy matching and have been removed.
  138. ==== More Like This
  139. The More Like This (`mlt`) API and the `more_like_this_field` (`mlt_field`)
  140. query have been removed in favor of the
  141. <<query-dsl-mlt-query, `more_like_this`>> query.
  142. The parameter `percent_terms_to_match` has been removed in favor of
  143. `minimum_should_match`.
  144. ==== `limit` filter deprecated
  145. The `limit` filter is deprecated and becomes a no-op. You can achieve similar
  146. behaviour using the <<search-request-body,terminate_after>> parameter.
  147. ==== Java plugins registering custom queries
  148. Java plugins that register custom queries can do so by using the
  149. `IndicesQueriesModule#addQuery(Class<? extends QueryParser>)` method. Other
  150. ways to register custom queries are not supported anymore.