Files
honeymoose f321dfedf5 同步完成!
**同步结果摘要**:

| 目录 | 同步前 | 同步后 | 官方 |
|------|--------|--------|------|
| aggregations | 40 | 65 | 64 |
| analyzers | 8 | 141 | 140 |
| api-reference | 97 | 186 | 174 |
| benchmark | 34 | 67 | 53 |
| clients | 17 | 18 | 18 |
| dashboards | 38 | 149 | 131 |
| data-prepper | 77 | 134 | 123 |
| im-plugin | 22 | 37 | 37 |
| ingest-pipelines | 27 | 52 | 51 |
| install-and-configure | 37 | 57 | 53 |
| ml-commons-plugin | 81 | 157 | 155 |
| observing-your-data | 39 | 73 | 66 |
| query-dsl | 40 | 77 | 75 |
| search-plugins | 80 | 139 | 95 |
| security-analytics | 22 | 47 | 46 |

**说明**:
- 本地文件数 > 官方:本地有一些官方已删除或重命名的文件
- 新增了约 **740 个英文文档文件**
- 已有文件保持不变(未覆盖)
2026-11-30 10:48:30 -05:00

163 lines
6.1 KiB
Markdown

---
layout: default
title: Index request cache
parent: Caching
grand_parent: Improving search performance
nav_order: 5
---
# Index request cache
The OpenSearch index request cache is a specialized caching mechanism designed to enhance search performance by storing the results of frequently executed search queries at the shard level. This reduces cluster load and improves response times for repeated searches. This cache is enabled by default and is particularly useful for read-heavy workloads where certain queries are executed frequently.
The cache is automatically invalidated at the configured refresh interval. The invalidation includes document updates (including document deletions) and changes to index settings. This ensures that stale results are never returned from the cache. When the cache size exceeds its configured limit, the least recently used entries are evicted to make room for new entries.
Some queries are ineligible for the request cache. These include profiled queries, scroll queries, and search requests with non-deterministic characteristics (such as those using `Math.random()` or DFS queries) or relative times (such as `now` or `new Date()`). By default, only requests with `size=0` are cacheable. In OpenSearch 2.19 and later, this behavior can be changed using `indices.requests.cache.maximum_cacheable_size`.
{: .note}
## Configuring request caching
You can configure the index request cache by setting the parameters in the `opensearch.yml` configuration file or using the REST API. For more information, see [Index settings]({{site.url}}{{site.baseurl}}/install-and-configure/configuring-opensearch/index-settings/).
### Settings
The following table lists the index request cache settings. For more information about dynamic settings, see [Index settings]({{site.url}}{{site.baseurl}}/install-and-configure/configuring-opensearch/index-settings/).
Setting | Data type | Default | Level | Static/Dynamic | Description
:--- |:-----------|:--------| :--- | :--- | :---
`indices.cache.cleanup_interval` | Time unit | `1m` (1 minute) | Cluster | Static | Schedules a recurring background task that cleans up expired entries from the cache at the specified interval.
`indices.requests.cache.size` | Percentage | `1%` | Cluster | Static | The cache size as a percentage of the heap size (for example, to use 1% of the heap, specify `1%`).
`index.requests.cache.enable` | Boolean | `true` | Index | Dynamic | Enables or disables the request cache.
`indices.requests.cache.maximum_cacheable_size` | Integer | `0` | Cluster | Dynamic | Sets the maximum `size` of queries to be added to the request cache.
### Example
To disable the request cache for an index, send the following request:
```json
PUT /my_index/_settings
{
"index.requests.cache.enable": false
}
```
{% include copy-curl.html %}
## Caching specific requests
In addition to providing index-level or cluster-level settings for the request cache, you can also cache specific search requests selectively by setting the `request_cache` query parameter to `true`:
```json
GET /students/_search?request_cache=true
{
"query": {
"match": {
"name": "doe john"
}
}
}
```
{% include copy-curl.html %}
## Matching requests to cached results
The request cache stores results for each shard. To find a cached result, OpenSearch compares the parsed search request, so requests that differ only in formatting share a cached result. For example, requests that differ only in white space or in the order of JSON keys return the same cached result. So do two `terms` aggregations that differ only in whether they specify the default `size` of `10`. Requests that differ in content, such as an aggregation with a different name, are cached separately.
## Monitoring the request cache
Monitoring cache usage and performance is crucial to maintaining an efficient caching strategy. OpenSearch provides several APIs to help monitor the cache.
### Retrieving cache statistics for all nodes
The [Nodes Stats API]({{site.url}}{{site.baseurl}}/api-reference/nodes-apis/nodes-stats/) returns cache statistics for all nodes in a cluster:
```json
GET /_nodes/stats/indices/request_cache
```
{% include copy-curl.html %}
The response contains the request cache statistics:
```json
{
"nodes": {
"T7aqO6zaQX-lt8XBWBYLsA": {
"indices": {
"request_cache": {
"memory_size_in_bytes": 10240,
"evictions": 0,
"hit_count": 50,
"miss_count": 10
}
}
}
}
}
```
{% include copy-curl.html %}
### Retrieving cache statistics for a specific index
The [Index Stats API]({{site.url}}{{site.baseurl}}/api-reference/index-apis/stats/) returns cache statistics for a specific index:
```json
GET /my_index/_stats/request_cache
```
{% include copy-curl.html %}
The response contains the request cache statistics:
```json
{
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"_all": {
"primaries": {
"request_cache": {
"memory_size_in_bytes": 2048,
"evictions": 1,
"hit_count": 30,
"miss_count": 5
}
},
"total": {
"request_cache": {
"memory_size_in_bytes": 4096,
"evictions": 2,
"hit_count": 60,
"miss_count": 10
}
}
},
"indices": {
"my_index": {
"primaries": {
"request_cache": {
"memory_size_in_bytes": 2048,
"evictions": 1,
"hit_count": 30,
"miss_count": 5
}
},
"total":{
"request_cache": {
"memory_size_in_bytes": 4096,
"evictions": 2,
"hit_count": 60,
"miss_count": 10
}
}
}
}
}
```
## Best practices
When using the index request cache, consider the following best practices:
- **Appropriate cache size**: Configure the cache size based on your query patterns. A larger cache can store more results but may consume significant resources.
- **Query optimization**: Ensure that frequently executed queries are optimized so that they can benefit from caching.
- **Monitoring**: Regularly monitor cache hit and cache miss rates to understand cache efficiency and make necessary adjustments.