request_cache.asciidoc 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. [[shard-request-cache]]
  2. === Shard request cache
  3. When a search request is run against an index or against many indices, each
  4. involved shard executes the search locally and returns its local results to
  5. the _coordinating node_, which combines these shard-level results into a
  6. ``global'' result set.
  7. The shard-level request cache module caches the local results on each shard.
  8. This allows frequently used (and potentially heavy) search requests to return
  9. results almost instantly. The requests cache is a very good fit for the logging
  10. use case, where only the most recent index is being actively updated --
  11. results from older indices will be served directly from the cache.
  12. [IMPORTANT]
  13. ===================================
  14. By default, the requests cache will only cache the results of search requests
  15. where `size=0`, so it will not cache `hits`,
  16. but it will cache `hits.total`, <<search-aggregations,aggregations>>, and
  17. <<search-suggesters,suggestions>>.
  18. Most queries that use `now` (see <<date-math>>) cannot be cached.
  19. ===================================
  20. [float]
  21. ==== Cache invalidation
  22. The cache is smart -- it keeps the same _near real-time_ promise as uncached
  23. search.
  24. Cached results are invalidated automatically whenever the shard refreshes, but
  25. only if the data in the shard has actually changed. In other words, you will
  26. always get the same results from the cache as you would for an uncached search
  27. request.
  28. The longer the refresh interval, the longer that cached entries will remain
  29. valid. If the cache is full, the least recently used cache keys will be
  30. evicted.
  31. The cache can be expired manually with the <<indices-clearcache,`clear-cache` API>>:
  32. [source,console]
  33. ------------------------
  34. POST /kimchy,elasticsearch/_cache/clear?request=true
  35. ------------------------
  36. // TEST[s/^/PUT kimchy\nPUT elasticsearch\n/]
  37. [float]
  38. ==== Enabling and disabling caching
  39. The cache is enabled by default, but can be disabled when creating a new
  40. index as follows:
  41. [source,console]
  42. -----------------------------
  43. PUT /my_index
  44. {
  45. "settings": {
  46. "index.requests.cache.enable": false
  47. }
  48. }
  49. -----------------------------
  50. It can also be enabled or disabled dynamically on an existing index with the
  51. <<indices-update-settings,`update-settings`>> API:
  52. [source,console]
  53. -----------------------------
  54. PUT /my_index/_settings
  55. { "index.requests.cache.enable": true }
  56. -----------------------------
  57. // TEST[continued]
  58. [float]
  59. ==== Enabling and disabling caching per request
  60. The `request_cache` query-string parameter can be used to enable or disable
  61. caching on a *per-request* basis. If set, it overrides the index-level setting:
  62. [source,console]
  63. -----------------------------
  64. GET /my_index/_search?request_cache=true
  65. {
  66. "size": 0,
  67. "aggs": {
  68. "popular_colors": {
  69. "terms": {
  70. "field": "colors"
  71. }
  72. }
  73. }
  74. }
  75. -----------------------------
  76. // TEST[continued]
  77. IMPORTANT: If your query uses a script whose result is not deterministic (e.g.
  78. it uses a random function or references the current time) you should set the
  79. `request_cache` flag to `false` to disable caching for that request.
  80. Requests where `size` is greater than 0 will not be cached even if the request cache is
  81. enabled in the index settings. To cache these requests you will need to use the
  82. query-string parameter detailed here.
  83. [float]
  84. ==== Cache key
  85. The whole JSON body is used as the cache key. This means that if the JSON
  86. changes -- for instance if keys are output in a different order -- then the
  87. cache key will not be recognised.
  88. TIP: Most JSON libraries support a _canonical_ mode which ensures that JSON
  89. keys are always emitted in the same order. This canonical mode can be used in
  90. the application to ensure that a request is always serialized in the same way.
  91. [float]
  92. ==== Cache settings
  93. The cache is managed at the node level, and has a default maximum size of `1%`
  94. of the heap. This can be changed in the `config/elasticsearch.yml` file with:
  95. [source,yaml]
  96. --------------------------------
  97. indices.requests.cache.size: 2%
  98. --------------------------------
  99. Also, you can use the +indices.requests.cache.expire+ setting to specify a TTL
  100. for cached results, but there should be no reason to do so. Remember that
  101. stale results are automatically invalidated when the index is refreshed. This
  102. setting is provided for completeness' sake only.
  103. [float]
  104. ==== Monitoring cache usage
  105. The size of the cache (in bytes) and the number of evictions can be viewed
  106. by index, with the <<indices-stats,`indices-stats`>> API:
  107. [source,console]
  108. ------------------------
  109. GET /_stats/request_cache?human
  110. ------------------------
  111. or by node with the <<cluster-nodes-stats,`nodes-stats`>> API:
  112. [source,console]
  113. ------------------------
  114. GET /_nodes/stats/indices/request_cache?human
  115. ------------------------