You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
When a search request is run against an index or against many indices, each
@@ -10,139 +10,16 @@ The shard-level request cache module caches the local results on each shard.
10
10
This allows frequently used (and potentially heavy) search requests to return
11
11
results almost instantly. The requests cache is a very good fit for the logging
12
12
use case, where only the most recent index is being actively updated --
13
-
results from older indices will be served directly from the cache.
13
+
results from older indices will be served directly from the cache. You can use shard request cache settings to control the size and expiration of the cache.
14
14
15
-
[IMPORTANT]
16
-
===================================
17
-
18
-
By default, the requests cache will only cache the results of search requests
19
-
where `size=0`, so it will not cache `hits`,
20
-
but it will cache `hits.total`, <<search-aggregations,aggregations>>, and
21
-
<<search-suggesters,suggestions>>.
22
-
23
-
Most queries that use `now` (see <<date-math>>) cannot be cached.
24
-
25
-
Scripted queries that use the API calls which are non-deterministic, such as
26
-
`Math.random()` or `new Date()` are not cached.
27
-
===================================
28
-
29
-
[discrete]
30
-
==== Cache invalidation
31
-
32
-
The cache is smart -- it keeps the same _near real-time_ promise as uncached
33
-
search.
34
-
35
-
Cached results are invalidated automatically whenever the shard refreshes to
36
-
pick up changes to the documents or when you update the mapping. In other
37
-
words you will always get the same results from the cache as you would for an
38
-
uncached search request.
39
-
40
-
The longer the refresh interval, the longer that cached entries will remain
41
-
valid even if there are changes to the documents. If the cache is full, the
42
-
least recently used cache keys will be evicted.
43
-
44
-
The cache can be expired manually with the <<indices-clearcache,`clear-cache` API>>:
45
-
46
-
[source,console]
47
-
------------------------
48
-
POST /my-index-000001,my-index-000002/_cache/clear?request=true
The `request_cache` query-string parameter can be used to enable or disable
83
-
caching on a *per-request* basis. If set, it overrides the index-level setting:
84
-
85
-
[source,console]
86
-
-----------------------------
87
-
GET /my-index-000001/_search?request_cache=true
88
-
{
89
-
"size": 0,
90
-
"aggs": {
91
-
"popular_colors": {
92
-
"terms": {
93
-
"field": "colors"
94
-
}
95
-
}
96
-
}
97
-
}
98
-
-----------------------------
99
-
// TEST[continued]
100
-
101
-
Requests where `size` is greater than 0 will not be cached even if the request cache is
102
-
enabled in the index settings. To cache these requests you will need to use the
103
-
query-string parameter detailed here.
104
-
105
-
[discrete]
106
-
==== Cache key
107
-
108
-
A hash of the whole JSON body is used as the cache key. This means that if the JSON
109
-
changes -- for instance if keys are output in a different order -- then the
110
-
cache key will not be recognised.
111
-
112
-
TIP: Most JSON libraries support a _canonical_ mode which ensures that JSON
113
-
keys are always emitted in the same order. This canonical mode can be used in
114
-
the application to ensure that a request is always serialized in the same way.
15
+
To learn more about the shard request cache, see <<shard-request-cache>>.
115
16
116
17
[discrete]
117
18
==== Cache settings
118
19
119
-
The cache is managed at the node level, and has a default maximum size of `1%`
120
-
of the heap. This can be changed in the `config/elasticsearch.yml` file with:
121
-
122
-
[source,yaml]
123
-
--------------------------------
124
-
indices.requests.cache.size: 2%
125
-
--------------------------------
126
-
127
-
Also, you can use the +indices.requests.cache.expire+ setting to specify a TTL
128
-
for cached results, but there should be no reason to do so. Remember that
129
-
stale results are automatically invalidated when the index is refreshed. This
130
-
setting is provided for completeness' sake only.
131
-
132
-
[discrete]
133
-
==== Monitoring cache usage
134
-
135
-
The size of the cache (in bytes) and the number of evictions can be viewed
136
-
by index, with the <<indices-stats,`indices-stats`>> API:
137
-
138
-
[source,console]
139
-
------------------------
140
-
GET /_stats/request_cache?human
141
-
------------------------
20
+
indices.requests.cache.size::
21
+
(<<static-cluster-setting,Static>>) The maximum size of the cache, as a percentage of the heap. Default: `1%`.
142
22
143
-
or by node with the <<cluster-nodes-stats,`nodes-stats`>> API:
23
+
indices.requests.cache.expire::
24
+
(<<static-cluster-setting,Static>>) The TTL for cached results. Stale results are automatically invalidated when the index is refreshed, so you shouldn't need to use this setting.
0 commit comments