filters.asciidoc 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. [[query-dsl-filters]]
  2. == Filters
  3. As a general rule, filters should be used instead of queries:
  4. * for binary yes/no searches
  5. * for queries on exact values
  6. [float]
  7. [[caching]]
  8. === Filters and Caching
  9. Filters can be a great candidate for caching. Caching the result of a
  10. filter does not require a lot of memory, and will cause other queries
  11. executing against the same filter (same parameters) to be blazingly
  12. fast.
  13. However the cost of caching is not the same for all filters. For
  14. instance some filters are already fast out of the box while caching could
  15. add significant overhead, and some filters produce results that are already
  16. cacheable so caching them is just a matter of putting the result in the
  17. cache.
  18. The default caching policy, `_cache: auto`, tracks the 1000 most recently
  19. used filters on a per-index basis and makes decisions based on their
  20. frequency.
  21. [float]
  22. ==== Filters that read directly the index structure
  23. Some filters can directly read the index structure and potentially jump
  24. over large sequences of documents that are not worth evaluating (for
  25. instance when these documents do not match the query). Caching these
  26. filters introduces overhead given that all documents that the filter
  27. matches need to be consumed in order to be loaded into the cache.
  28. These filters, which include the <<query-dsl-term-filter,term>> and
  29. <<query-dsl-term-query,query>> filters, are only cached after they
  30. appear 5 times or more in the history of the 1000 most recently used
  31. filters.
  32. [float]
  33. ==== Filters that produce results that are already cacheable
  34. Some filters produce results that are already cacheable, and the difference
  35. between caching and not caching them is the act of placing the result in
  36. the cache or not. These filters, which include the
  37. <<query-dsl-terms-filter,terms>>,
  38. <<query-dsl-prefix-filter,prefix>>, and
  39. <<query-dsl-range-filter,range>> filters, are by default cached after they
  40. appear twice or more in the history of the most 1000 recently used filters.
  41. [float]
  42. ==== Computational filters
  43. Some filters need to run some computation in order to figure out whether
  44. a given document matches a filter. These filters, which include the geo and
  45. <<query-dsl-script-filter,script>> filters, but also the
  46. <<query-dsl-terms-filter,terms>> and <<query-dsl-range-filter,range>>
  47. filters when using the `fielddata` execution mode are never cached by default,
  48. as it would require to evaluate the filter on all documents in your indices
  49. while they can otherwise be only evaluated on documents that match the query.
  50. [float]
  51. ==== Compound filters
  52. The last type of filters are those working with other filters, and includes
  53. the <<query-dsl-bool-filter,bool>>,
  54. <<query-dsl-and-filter,and>>,
  55. <<query-dsl-not-filter,not>> and
  56. <<query-dsl-or-filter,or>> filters.
  57. There is no general rule about these filters. Depending on the filters that
  58. they wrap, they will sometimes return a filter that dynamically evaluates the
  59. sub filters and sometimes evaluate the sub filters eagerly in order to return
  60. a result that is already cacheable, so depending on the case, these filters
  61. will be cached after they appear 2+ or 5+ times in the history of the most
  62. 1000 recently used filters.
  63. [float]
  64. ==== Overriding the default behaviour
  65. All filters allow to set `_cache` element on them to explicitly control
  66. caching. It accepts 3 values: `true` in order to cache the filter, `false`
  67. to make sure that the filter will not be cached, and `auto`, which is the
  68. default and will decide on whether to cache the filter based on the cost
  69. to cache it and how often it has been used as explained above.
  70. Filters also allow to set `_cache_key` which will be used as the
  71. caching key for that filter. This can be handy when using very large
  72. filters (like a terms filter with many elements in it).
  73. include::filters/and-filter.asciidoc[]
  74. include::filters/bool-filter.asciidoc[]
  75. include::filters/exists-filter.asciidoc[]
  76. include::filters/geo-bounding-box-filter.asciidoc[]
  77. include::filters/geo-distance-filter.asciidoc[]
  78. include::filters/geo-distance-range-filter.asciidoc[]
  79. include::filters/geo-polygon-filter.asciidoc[]
  80. include::filters/geo-shape-filter.asciidoc[]
  81. include::filters/geohash-cell-filter.asciidoc[]
  82. include::filters/has-child-filter.asciidoc[]
  83. include::filters/has-parent-filter.asciidoc[]
  84. include::filters/ids-filter.asciidoc[]
  85. include::filters/indices-filter.asciidoc[]
  86. include::filters/limit-filter.asciidoc[]
  87. include::filters/match-all-filter.asciidoc[]
  88. include::filters/missing-filter.asciidoc[]
  89. include::filters/nested-filter.asciidoc[]
  90. include::filters/not-filter.asciidoc[]
  91. include::filters/or-filter.asciidoc[]
  92. include::filters/prefix-filter.asciidoc[]
  93. include::filters/query-filter.asciidoc[]
  94. include::filters/range-filter.asciidoc[]
  95. include::filters/regexp-filter.asciidoc[]
  96. include::filters/script-filter.asciidoc[]
  97. include::filters/term-filter.asciidoc[]
  98. include::filters/terms-filter.asciidoc[]
  99. include::filters/type-filter.asciidoc[]