request_cache.asciidoc 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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. For now, 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. 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. curl -XPOST 'localhost:9200/kimchy,elasticsearch/_cache/clear?request_cache=true'
  35. ------------------------
  36. [float]
  37. ==== Enabling caching by default
  38. The cache is not enabled by default, but can be enabled when creating a new
  39. index as follows:
  40. [source,js]
  41. -----------------------------
  42. curl -XPUT localhost:9200/my_index -d'
  43. {
  44. "settings": {
  45. "index.requests.cache.enable": true
  46. }
  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,js]
  53. -----------------------------
  54. curl -XPUT localhost:9200/my_index/_settings -d'
  55. { "index.requests.cache.enable": true }
  56. '
  57. -----------------------------
  58. [float]
  59. ==== Enabling caching per request
  60. The `query_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,js]
  63. -----------------------------
  64. curl 'localhost:9200/my_index/_search?request_cache=true' -d'
  65. {
  66. "size": 0,
  67. "aggs": {
  68. "popular_colors": {
  69. "terms": {
  70. "field": "colors"
  71. }
  72. }
  73. }
  74. }
  75. '
  76. -----------------------------
  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. [float]
  81. ==== Cache key
  82. The whole JSON body is used as the cache key. This means that if the JSON
  83. changes -- for instance if keys are output in a different order -- then the
  84. cache key will not be recognised.
  85. TIP: Most JSON libraries support a _canonical_ mode which ensures that JSON
  86. keys are always emitted in the same order. This canonical mode can be used in
  87. the application to ensure that a request is always serialized in the same way.
  88. [float]
  89. ==== Cache settings
  90. The cache is managed at the node level, and has a default maximum size of `1%`
  91. of the heap. This can be changed in the `config/elasticsearch.yml` file with:
  92. [source,yaml]
  93. --------------------------------
  94. indices.requests.cache.size: 2%
  95. --------------------------------
  96. Also, you can use the +indices.requests.cache.expire+ setting to specify a TTL
  97. for cached results, but there should be no reason to do so. Remember that
  98. stale results are automatically invalidated when the index is refreshed. This
  99. setting is provided for completeness' sake only.
  100. [float]
  101. ==== Monitoring cache usage
  102. The size of the cache (in bytes) and the number of evictions can be viewed
  103. by index, with the <<indices-stats,`indices-stats`>> API:
  104. [source,js]
  105. ------------------------
  106. curl 'localhost:9200/_stats/request_cache?pretty&human'
  107. ------------------------
  108. or by node with the <<cluster-nodes-stats,`nodes-stats`>> API:
  109. [source,js]
  110. ------------------------
  111. curl 'localhost:9200/_nodes/stats/indices/request_cache?pretty&human'
  112. ------------------------