2015-06-26 10:31:38 -04:00
|
|
|
[[shard-request-cache]]
|
2020-06-01 09:44:32 -04:00
|
|
|
=== Shard request cache settings
|
2014-08-06 05:54:51 -04:00
|
|
|
|
|
|
|
When a search request is run against an index or against many indices, each
|
|
|
|
involved shard executes the search locally and returns its local results to
|
|
|
|
the _coordinating node_, which combines these shard-level results into a
|
|
|
|
``global'' result set.
|
|
|
|
|
2015-06-26 10:31:38 -04:00
|
|
|
The shard-level request cache module caches the local results on each shard.
|
2014-08-06 05:54:51 -04:00
|
|
|
This allows frequently used (and potentially heavy) search requests to return
|
2015-06-26 10:31:38 -04:00
|
|
|
results almost instantly. The requests cache is a very good fit for the logging
|
2014-08-06 05:54:51 -04:00
|
|
|
use case, where only the most recent index is being actively updated --
|
|
|
|
results from older indices will be served directly from the cache.
|
|
|
|
|
|
|
|
[IMPORTANT]
|
2015-06-22 17:49:45 -04:00
|
|
|
===================================
|
2014-08-06 05:54:51 -04:00
|
|
|
|
2016-07-18 08:33:59 -04:00
|
|
|
By default, the requests cache will only cache the results of search requests
|
2015-01-14 05:19:32 -05:00
|
|
|
where `size=0`, so it will not cache `hits`,
|
2014-08-06 05:54:51 -04:00
|
|
|
but it will cache `hits.total`, <<search-aggregations,aggregations>>, and
|
|
|
|
<<search-suggesters,suggestions>>.
|
|
|
|
|
2016-08-11 09:25:34 -04:00
|
|
|
Most queries that use `now` (see <<date-math>>) cannot be cached.
|
2019-12-19 18:30:34 -05:00
|
|
|
|
|
|
|
Scripted queries that use the API calls which are non-deterministic, such as
|
|
|
|
`Math.random()` or `new Date()` are not cached.
|
2015-06-22 17:49:45 -04:00
|
|
|
===================================
|
2014-08-06 05:54:51 -04:00
|
|
|
|
2020-07-23 12:42:33 -04:00
|
|
|
[discrete]
|
2015-06-22 17:49:45 -04:00
|
|
|
==== Cache invalidation
|
2014-08-06 05:54:51 -04:00
|
|
|
|
|
|
|
The cache is smart -- it keeps the same _near real-time_ promise as uncached
|
|
|
|
search.
|
|
|
|
|
|
|
|
Cached results are invalidated automatically whenever the shard refreshes, but
|
|
|
|
only if the data in the shard has actually changed. In other words, you will
|
|
|
|
always get the same results from the cache as you would for an uncached search
|
|
|
|
request.
|
|
|
|
|
|
|
|
The longer the refresh interval, the longer that cached entries will remain
|
|
|
|
valid. If the cache is full, the least recently used cache keys will be
|
|
|
|
evicted.
|
|
|
|
|
|
|
|
The cache can be expired manually with the <<indices-clearcache,`clear-cache` API>>:
|
|
|
|
|
2019-09-09 12:35:50 -04:00
|
|
|
[source,console]
|
2014-08-06 05:54:51 -04:00
|
|
|
------------------------
|
2020-08-03 13:31:19 -04:00
|
|
|
POST /my-index-000001,my-index-000002/_cache/clear?request=true
|
2014-08-06 05:54:51 -04:00
|
|
|
------------------------
|
2020-08-03 13:31:19 -04:00
|
|
|
// TEST[s/^/PUT my-index-000001\nPUT my-index-000002\n/]
|
2014-08-06 05:54:51 -04:00
|
|
|
|
2020-07-23 12:42:33 -04:00
|
|
|
[discrete]
|
2016-08-11 09:25:34 -04:00
|
|
|
==== Enabling and disabling caching
|
2014-08-06 05:54:51 -04:00
|
|
|
|
2016-08-11 09:25:34 -04:00
|
|
|
The cache is enabled by default, but can be disabled when creating a new
|
2014-08-06 05:54:51 -04:00
|
|
|
index as follows:
|
|
|
|
|
2019-09-09 12:35:50 -04:00
|
|
|
[source,console]
|
2014-08-06 05:54:51 -04:00
|
|
|
-----------------------------
|
2020-07-27 15:58:26 -04:00
|
|
|
PUT /my-index-000001
|
2014-08-06 05:54:51 -04:00
|
|
|
{
|
|
|
|
"settings": {
|
2016-08-11 09:25:34 -04:00
|
|
|
"index.requests.cache.enable": false
|
2014-08-06 05:54:51 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
-----------------------------
|
|
|
|
|
|
|
|
It can also be enabled or disabled dynamically on an existing index with the
|
|
|
|
<<indices-update-settings,`update-settings`>> API:
|
|
|
|
|
2019-09-09 12:35:50 -04:00
|
|
|
[source,console]
|
2014-08-06 05:54:51 -04:00
|
|
|
-----------------------------
|
2020-07-27 15:58:26 -04:00
|
|
|
PUT /my-index-000001/_settings
|
2015-06-26 10:31:38 -04:00
|
|
|
{ "index.requests.cache.enable": true }
|
2014-08-06 05:54:51 -04:00
|
|
|
-----------------------------
|
2016-09-21 09:27:18 -04:00
|
|
|
// TEST[continued]
|
|
|
|
|
2014-08-06 05:54:51 -04:00
|
|
|
|
2020-07-23 12:42:33 -04:00
|
|
|
[discrete]
|
2016-08-11 09:25:34 -04:00
|
|
|
==== Enabling and disabling caching per request
|
2014-08-06 05:54:51 -04:00
|
|
|
|
2015-07-27 06:59:45 -04:00
|
|
|
The `request_cache` query-string parameter can be used to enable or disable
|
2015-06-26 10:31:38 -04:00
|
|
|
caching on a *per-request* basis. If set, it overrides the index-level setting:
|
2014-08-06 05:54:51 -04:00
|
|
|
|
2019-09-09 12:35:50 -04:00
|
|
|
[source,console]
|
2014-08-06 05:54:51 -04:00
|
|
|
-----------------------------
|
2020-07-27 15:58:26 -04:00
|
|
|
GET /my-index-000001/_search?request_cache=true
|
2014-08-06 05:54:51 -04:00
|
|
|
{
|
2015-01-14 05:19:32 -05:00
|
|
|
"size": 0,
|
2014-08-06 05:54:51 -04:00
|
|
|
"aggs": {
|
|
|
|
"popular_colors": {
|
|
|
|
"terms": {
|
|
|
|
"field": "colors"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
-----------------------------
|
2016-09-21 09:27:18 -04:00
|
|
|
// TEST[continued]
|
2014-08-06 05:54:51 -04:00
|
|
|
|
2017-06-28 08:31:03 -04:00
|
|
|
Requests where `size` is greater than 0 will not be cached even if the request cache is
|
2016-07-18 08:33:59 -04:00
|
|
|
enabled in the index settings. To cache these requests you will need to use the
|
|
|
|
query-string parameter detailed here.
|
|
|
|
|
2020-07-23 12:42:33 -04:00
|
|
|
[discrete]
|
2015-06-22 17:49:45 -04:00
|
|
|
==== Cache key
|
2014-08-06 05:54:51 -04:00
|
|
|
|
|
|
|
The whole JSON body is used as the cache key. This means that if the JSON
|
|
|
|
changes -- for instance if keys are output in a different order -- then the
|
|
|
|
cache key will not be recognised.
|
|
|
|
|
|
|
|
TIP: Most JSON libraries support a _canonical_ mode which ensures that JSON
|
|
|
|
keys are always emitted in the same order. This canonical mode can be used in
|
|
|
|
the application to ensure that a request is always serialized in the same way.
|
|
|
|
|
2020-07-23 12:42:33 -04:00
|
|
|
[discrete]
|
2015-06-22 17:49:45 -04:00
|
|
|
==== Cache settings
|
2014-08-06 05:54:51 -04:00
|
|
|
|
|
|
|
The cache is managed at the node level, and has a default maximum size of `1%`
|
|
|
|
of the heap. This can be changed in the `config/elasticsearch.yml` file with:
|
|
|
|
|
|
|
|
[source,yaml]
|
|
|
|
--------------------------------
|
2015-06-26 10:31:38 -04:00
|
|
|
indices.requests.cache.size: 2%
|
2014-08-06 05:54:51 -04:00
|
|
|
--------------------------------
|
|
|
|
|
2015-06-26 10:31:38 -04:00
|
|
|
Also, you can use the +indices.requests.cache.expire+ setting to specify a TTL
|
2014-08-06 05:54:51 -04:00
|
|
|
for cached results, but there should be no reason to do so. Remember that
|
|
|
|
stale results are automatically invalidated when the index is refreshed. This
|
|
|
|
setting is provided for completeness' sake only.
|
|
|
|
|
2020-07-23 12:42:33 -04:00
|
|
|
[discrete]
|
2015-06-22 17:49:45 -04:00
|
|
|
==== Monitoring cache usage
|
2014-08-06 05:54:51 -04:00
|
|
|
|
|
|
|
The size of the cache (in bytes) and the number of evictions can be viewed
|
|
|
|
by index, with the <<indices-stats,`indices-stats`>> API:
|
|
|
|
|
2019-09-09 12:35:50 -04:00
|
|
|
[source,console]
|
2014-08-06 05:54:51 -04:00
|
|
|
------------------------
|
2016-09-21 09:27:18 -04:00
|
|
|
GET /_stats/request_cache?human
|
2014-08-06 05:54:51 -04:00
|
|
|
------------------------
|
|
|
|
|
|
|
|
or by node with the <<cluster-nodes-stats,`nodes-stats`>> API:
|
|
|
|
|
2019-09-09 12:35:50 -04:00
|
|
|
[source,console]
|
2014-08-06 05:54:51 -04:00
|
|
|
------------------------
|
2016-09-21 09:27:18 -04:00
|
|
|
GET /_nodes/stats/indices/request_cache?human
|
2014-08-06 05:54:51 -04:00
|
|
|
------------------------
|