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.
当针对一个索引或多个索引运行搜索请求时,每个涉及的分片在本地执行搜索并将其本地结果返回给协调节点协调节点将这些分片级别的结果组合成一个“全局”结果集。

The shard-level request cache module caches the local results on each shard. This allows frequently used (and potentially heavy) search requests to return results almost instantly. The requests cache is a very good fit for the logging use case, where only the most recent index is being actively updated — results from older indices will be served directly from the cache.
分片级请求缓存模块在每个分片上缓存本地结果。这允许经常使用(并且可能很重)的搜索请求几乎立即返回结果。请求缓存非常适合日志用例,其中只有最新的索引正在被主动更新——旧索引的结果将直接从缓存中提供。

⚠️
By default, the requests cache will only cache the results of search requests where size=0, so it will not cache hits, but it will cache hits.total, aggregations, and suggestions. Most queries that use now (see Date Math) cannot be cached. Scripted queries that use the API calls which are non-deterministic, such as Math.random() or new Date() are not cached.
默认情况下,请求缓存只会缓存搜索请求size=0的结果,所以它不会缓存hits,但会缓存hits.total、 聚合建议。 大多数使用的查询now(请参阅Date Math)无法缓存。 使用非确定性 API 调用的脚本查询,例如 Math.random()或new Date() 不会被缓存。

缓存失效

The cache is smart — it keeps the same near real-time promise as uncached search.
缓存是智能的——它保持与未缓存搜索相同的近乎实时的承诺。

Cached results are invalidated automatically whenever the shard refreshes to pick up changes to the documents or when you update the mapping. 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 even if there are changes to the documents. If the cache is full, the least recently used cache keys will be evicted.
刷新间隔越长,即使文档发生更改,缓存条目保持有效的时间就越长。如果缓存已满,则将逐出最近最少使用的缓存键。

The cache can be expired manually with the clear-cacheAPI:
缓存可以通过clear-cacheAPI手动过期:

  1. POST /my-index-000001,my-index-000002/_cache/clear?request=true

启用 和 禁用缓存

The cache is enabled by default, but can be disabled when creating a new index as follows:
默认情况下启用缓存,但可以在创建新索引时禁用,如下所示:

  1. PUT /my-index-000001
  2. {
  3. "settings": {
  4. "index.requests.cache.enable": false
  5. }
  6. }

It can also be enabled or disabled dynamically on an existing index with the update-settings API:
也可以使用update-settingsAPI在现有索引上动态启用或禁用它 :

  1. PUT /my-index-000001/_settings
  2. { "index.requests.cache.enable": true }

根据请求启动和禁用缓存

The request_cache query-string parameter can be used to enable or disable caching on a per-request basis. If set, it overrides the index-level setting:
request_cache查询字符串参数可用于基于每个请求启用或禁用缓存。如果设置此参数,会覆盖索引级别的设置:

  1. GET /my-index-000001/_search?request_cache=true
  2. {
  3. "size": 0,
  4. "aggs": {
  5. "popular_colors": {
  6. "terms": {
  7. "field": "colors"
  8. }
  9. }
  10. }
  11. }

Requests where size is greater than 0 will not be cached even if the request cache is enabled in the index settings. To cache these requests you will need to use the query-string parameter detailed here.
size 大于0的请求不会被缓存,即使在索引中设置了。要缓存这些请求,您需要使用此处详述查询字符串参数。

缓存 Key

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.
整个 JSON 主体用作缓存键。这意味着如果 JSON 发生变化——例如,如果键以不同的顺序输出——那么缓存键将不会被识别。

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. 大多数 JSON 库都支持规范模式,以确保 JSON 密钥始终以相同的顺序发出。可以在应用程序中使用这种规范模式来确保请求始终以相同的方式序列化。

缓存设置

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:
缓存在 Node 级别进行管理,并具有默认的最大1% 堆大小。这可以在config/elasticsearch.yml文件中更改:

  1. indices.requests.cache.size: 2%

Also, you can use the indices.requests.cache.expire setting to specify a TTL 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.
此外,您可以使用该indices.requests.cache.expire设置为缓存结果指定 TTL,但没有理由这样做。请记住,刷新索引时,陈旧的结果会自动失效。提供此设置只是为了完整起见。

监控缓存使用情况

The size of the cache (in bytes) and the number of evictions can be viewed by index, with the indices-stats API:
缓存的大小(以字节为单位)和驱逐的数量可以通过索引查看,使用indices-statsAPI:

  1. GET /_stats/request_cache?human

or by node with the nodes-stats API:
或按节点使用nodes-statsAPI:

  1. GET /_nodes/stats/indices/request_cache?human