query_dsl.asciidoc 5.4 KB

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