request_cache.asciidoc 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. [[shard-request-cache]]
  2. === Shard request cache settings
  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. Scripted queries that use the API calls which are non-deterministic, such as
  20. `Math.random()` or `new Date()` are not cached.
  21. ===================================
  22. [discrete]
  23. ==== Cache invalidation
  24. The cache is smart -- it keeps the same _near real-time_ promise as uncached
  25. search.
  26. Cached results are invalidated automatically whenever the shard refreshes, but
  27. only if the data in the shard has actually changed. In other words, you will
  28. always get the same results from the cache as you would for an uncached search
  29. request.
  30. The longer the refresh interval, the longer that cached entries will remain
  31. valid. If the cache is full, the least recently used cache keys will be
  32. evicted.
  33. The cache can be expired manually with the <<indices-clearcache,`clear-cache` API>>:
  34. [source,console]
  35. ------------------------
  36. POST /my-index-000001,my-index-000002/_cache/clear?request=true
  37. ------------------------
  38. // TEST[s/^/PUT my-index-000001\nPUT my-index-000002\n/]
  39. [discrete]
  40. ==== Enabling and disabling caching
  41. The cache is enabled by default, but can be disabled when creating a new
  42. index as follows:
  43. [source,console]
  44. -----------------------------
  45. PUT /my-index-000001
  46. {
  47. "settings": {
  48. "index.requests.cache.enable": false
  49. }
  50. }
  51. -----------------------------
  52. It can also be enabled or disabled dynamically on an existing index with the
  53. <<indices-update-settings,`update-settings`>> API:
  54. [source,console]
  55. -----------------------------
  56. PUT /my-index-000001/_settings
  57. { "index.requests.cache.enable": true }
  58. -----------------------------
  59. // TEST[continued]
  60. [discrete]
  61. ==== Enabling and disabling caching per request
  62. The `request_cache` query-string parameter can be used to enable or disable
  63. caching on a *per-request* basis. If set, it overrides the index-level setting:
  64. [source,console]
  65. -----------------------------
  66. GET /my-index-000001/_search?request_cache=true
  67. {
  68. "size": 0,
  69. "aggs": {
  70. "popular_colors": {
  71. "terms": {
  72. "field": "colors"
  73. }
  74. }
  75. }
  76. }
  77. -----------------------------
  78. // TEST[continued]
  79. Requests where `size` is greater than 0 will not be cached even if the request cache is
  80. enabled in the index settings. To cache these requests you will need to use the
  81. query-string parameter detailed here.
  82. [discrete]
  83. ==== Cache key
  84. The whole JSON body is used as the cache key. This means that if the JSON
  85. changes -- for instance if keys are output in a different order -- then the
  86. cache key will not be recognised.
  87. TIP: Most JSON libraries support a _canonical_ mode which ensures that JSON
  88. keys are always emitted in the same order. This canonical mode can be used in
  89. the application to ensure that a request is always serialized in the same way.
  90. [discrete]
  91. ==== Cache settings
  92. The cache is managed at the node level, and has a default maximum size of `1%`
  93. of the heap. This can be changed in the `config/elasticsearch.yml` file with:
  94. [source,yaml]
  95. --------------------------------
  96. indices.requests.cache.size: 2%
  97. --------------------------------
  98. Also, you can use the +indices.requests.cache.expire+ setting to specify a TTL
  99. for cached results, but there should be no reason to do so. Remember that
  100. stale results are automatically invalidated when the index is refreshed. This
  101. setting is provided for completeness' sake only.
  102. [discrete]
  103. ==== Monitoring cache usage
  104. The size of the cache (in bytes) and the number of evictions can be viewed
  105. by index, with the <<indices-stats,`indices-stats`>> API:
  106. [source,console]
  107. ------------------------
  108. GET /_stats/request_cache?human
  109. ------------------------
  110. or by node with the <<cluster-nodes-stats,`nodes-stats`>> API:
  111. [source,console]
  112. ------------------------
  113. GET /_nodes/stats/indices/request_cache?human
  114. ------------------------