filtered-query.asciidoc 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. [[query-dsl-filtered-query]]
  2. === Filtered Query
  3. deprecated[2.0.0, Use the `bool` query instead with a `must` clause for the query and a `filter` clause for the filter]
  4. The `filtered` query is used to combine a query which will be used for
  5. scoring with another query which will only be used for filtering the result
  6. set.
  7. TIP: Exclude as many document as you can with a filter, then query just the
  8. documents that remain.
  9. [source,js]
  10. --------------------------------------------------
  11. {
  12. "filtered": {
  13. "query": {
  14. "match": { "tweet": "full text search" }
  15. },
  16. "filter": {
  17. "range": { "created": { "gte": "now - 1d / d" }}
  18. }
  19. }
  20. }
  21. --------------------------------------------------
  22. The `filtered` query can be used wherever a `query` is expected, for instance,
  23. to use the above example in search request:
  24. [source,js]
  25. --------------------------------------------------
  26. curl -XGET localhost:9200/_search -d '
  27. {
  28. "query": {
  29. "filtered": { <1>
  30. "query": {
  31. "match": { "tweet": "full text search" }
  32. },
  33. "filter": {
  34. "range": { "created": { "gte": "now - 1d / d" }}
  35. }
  36. }
  37. }
  38. }
  39. '
  40. --------------------------------------------------
  41. <1> The `filtered` query is passed as the value of the `query`
  42. parameter in the search request.
  43. ==== Filtering without a query
  44. If a `query` is not specified, it defaults to the
  45. <<query-dsl-match-all-query,`match_all` query>>. This means that the
  46. `filtered` query can be used to wrap just a filter, so that it can be used
  47. wherever a query is expected.
  48. [source,js]
  49. --------------------------------------------------
  50. curl -XGET localhost:9200/_search -d '
  51. {
  52. "query": {
  53. "filtered": { <1>
  54. "filter": {
  55. "range": { "created": { "gte": "now - 1d / d" }}
  56. }
  57. }
  58. }
  59. }
  60. '
  61. --------------------------------------------------
  62. <1> No `query` has been specified, so this request applies just the filter,
  63. returning all documents created since yesterday.
  64. ===== Multiple filters
  65. Multiple filters can be applied by wrapping them in a
  66. <<query-dsl-bool-query,`bool` query>>, for example:
  67. [source,js]
  68. --------------------------------------------------
  69. {
  70. "filtered": {
  71. "query": { "match": { "tweet": "full text search" }},
  72. "filter": {
  73. "bool": {
  74. "must": { "range": { "created": { "gte": "now - 1d / d" }}},
  75. "should": [
  76. { "term": { "featured": true }},
  77. { "term": { "starred": true }}
  78. ],
  79. "must_not": { "term": { "deleted": false }}
  80. }
  81. }
  82. }
  83. }
  84. --------------------------------------------------
  85. ===== Filter strategy
  86. You can control how the filter and query are executed with the `strategy`
  87. parameter:
  88. [source,js]
  89. --------------------------------------------------
  90. {
  91. "filtered" : {
  92. "query" : { ... },
  93. "filter" : { ... },
  94. "strategy": "leap_frog"
  95. }
  96. }
  97. --------------------------------------------------
  98. IMPORTANT: This is an _expert-level_ setting. Most users can simply ignore it.
  99. The `strategy` parameter accepts the following options:
  100. [horizontal]
  101. `leap_frog_query_first`::
  102. Look for the first document matching the query, and then alternatively
  103. advance the query and the filter to find common matches.
  104. `leap_frog_filter_first`::
  105. Look for the first document matching the filter, and then alternatively
  106. advance the query and the filter to find common matches.
  107. `leap_frog`::
  108. Same as `leap_frog_query_first`.
  109. `query_first`::
  110. If the filter supports random access, then search for documents using the
  111. query, and then consult the filter to check whether there is a match.
  112. Otherwise fall back to `leap_frog_query_first`.
  113. `random_access_${threshold}`::
  114. If the filter supports random access and if the number of documents in the
  115. index divided by the cardinality of the filter is greater than ${threshold},
  116. then apply the filter first.
  117. Otherwise fall back to `leap_frog_query_first`. `${threshold}` must be
  118. greater than or equal to `1`.
  119. `random_access_always`::
  120. Apply the filter first if it supports random access. Otherwise fall back
  121. to `leap_frog_query_first`.
  122. The default strategy is to use `query_first` on filters that are not
  123. advanceable such as geo filters and script filters, and `random_access_100` on
  124. other filters.