request_cache.asciidoc 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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,js]
  33. ------------------------
  34. POST /kimchy,elasticsearch/_cache/clear?request=true
  35. ------------------------
  36. // CONSOLE
  37. // TEST[s/^/PUT kimchy\nPUT elasticsearch\n/]
  38. [float]
  39. ==== Enabling and disabling caching
  40. The cache is enabled by default, but can be disabled when creating a new
  41. index as follows:
  42. [source,js]
  43. -----------------------------
  44. PUT /my_index
  45. {
  46. "settings": {
  47. "index.requests.cache.enable": false
  48. }
  49. }
  50. -----------------------------
  51. // CONSOLE
  52. It can also be enabled or disabled dynamically on an existing index with the
  53. <<indices-update-settings,`update-settings`>> API:
  54. [source,js]
  55. -----------------------------
  56. PUT /my_index/_settings
  57. { "index.requests.cache.enable": true }
  58. -----------------------------
  59. // CONSOLE
  60. // TEST[continued]
  61. [float]
  62. ==== Enabling and disabling caching per request
  63. The `request_cache` query-string parameter can be used to enable or disable
  64. caching on a *per-request* basis. If set, it overrides the index-level setting:
  65. [source,js]
  66. -----------------------------
  67. GET /my_index/_search?request_cache=true
  68. {
  69. "size": 0,
  70. "aggs": {
  71. "popular_colors": {
  72. "terms": {
  73. "field": "colors"
  74. }
  75. }
  76. }
  77. }
  78. -----------------------------
  79. // CONSOLE
  80. // TEST[continued]
  81. IMPORTANT: If your query uses a script whose result is not deterministic (e.g.
  82. it uses a random function or references the current time) you should set the
  83. `request_cache` flag to `false` to disable caching for that request.
  84. Requests where `size` is greater than 0 will not be cached even if the request cache is
  85. enabled in the index settings. To cache these requests you will need to use the
  86. query-string parameter detailed here.
  87. [float]
  88. ==== Cache key
  89. The whole JSON body is used as the cache key. This means that if the JSON
  90. changes -- for instance if keys are output in a different order -- then the
  91. cache key will not be recognised.
  92. TIP: Most JSON libraries support a _canonical_ mode which ensures that JSON
  93. keys are always emitted in the same order. This canonical mode can be used in
  94. the application to ensure that a request is always serialized in the same way.
  95. [float]
  96. ==== Cache settings
  97. The cache is managed at the node level, and has a default maximum size of `1%`
  98. of the heap. This can be changed in the `config/elasticsearch.yml` file with:
  99. [source,yaml]
  100. --------------------------------
  101. indices.requests.cache.size: 2%
  102. --------------------------------
  103. Also, you can use the +indices.requests.cache.expire+ setting to specify a TTL
  104. for cached results, but there should be no reason to do so. Remember that
  105. stale results are automatically invalidated when the index is refreshed. This
  106. setting is provided for completeness' sake only.
  107. [float]
  108. ==== Monitoring cache usage
  109. The size of the cache (in bytes) and the number of evictions can be viewed
  110. by index, with the <<indices-stats,`indices-stats`>> API:
  111. [source,js]
  112. ------------------------
  113. GET /_stats/request_cache?human
  114. ------------------------
  115. // CONSOLE
  116. or by node with the <<cluster-nodes-stats,`nodes-stats`>> API:
  117. [source,js]
  118. ------------------------
  119. GET /_nodes/stats/indices/request_cache?human
  120. ------------------------
  121. // CONSOLE