filtered-query.asciidoc 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. [[query-dsl-filtered-query]]
  2. === Filtered Query
  3. deprecated[2.0.0-beta1, 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. --------------------------------------------------