filtered-query.asciidoc 4.1 KB

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