Clear Cache API (캐시 비우기 API)
Clear Cache API는 하나 이상의 인덱스의 캐시를 비우는 API예요. 데이터 스트림의 경우에는 해당 스트림의 백킹 인덱스(backing index) 캐시를 비웁니다.
Security 플러그인을 사용한다면 manage index 권한이 있어야 합니다.
출처: OpenSearch 문서
본문
Endpoints
POST /{target}/_cache/clear
Path parameters
| Parameter | Data type | Description | | target | String | Comma-delimited list of data streams, indexes, and index aliases to which cache clearing is applied. Wildcard expressions (*) are supported. To target all data streams and indexes in a cluster, omit this parameter or use _all or *. Optional. |
Query parameters
모든 쿼리 파라미터는 선택 사항입니다.
| Parameter | Data type | Description | | allow_no_indices | Boolean | Whether to ignore wildcards, index aliases, or _all target (target path parameter) values that don’t match any indexes. If false, the request returns an error if any wildcard expression, index alias, or _all target value doesn’t match any indexes. This behavior also applies if the request targets include other open indexes. For example, a request where the target is fig*,app* returns an error if an index starts with fig but no index starts with app. Defaults to true. | | expand_wildcards | String | Determines the index types that wildcard expressions can expand to. Accepts multiple values separated by a comma, such as open,hidden. Valid values are: all – Expand to open, closed, and hidden indexes.open – Expand only to open indexes.closed – Expand only to closed indexeshidden – Expand to include hidden indexes. Must be combined with open, closed, or both.none – Expansions are not accepted. Defaults to open. | | fielddata | Boolean | If true, clears the fields cache. Use the fields parameter to clear specific fields’ caches. Defaults to true. | | fields | String | Used in conjunction with the fielddata parameter. Comma-delimited list of field names that are cleared out of the cache. Does not support objects or field aliases. Defaults to all fields. | | file | Boolean | If true, clears the unused entries from the file cache on nodes with the Search role. Defaults to false. | | index | String | Comma-delimited list of index names that are cleared out of the cache. | | ignore_unavailable | Boolean | If true, OpenSearch ignores missing or closed indexes. Defaults to false. | | query | Boolean | If true, clears the query cache. Defaults to true. | | request | Boolean | If true, clears the request cache. Defaults to true. |
Example requests
다음 예제 요청들은 clear cache API의 다양한 사용법을 보여줍니다.
Clear a specific cache
다음 요청은 필드 캐시만 비웁니다.
REST Python
POST /my-index/_cache/clear?fielddata=true
Copy Copy as cURL
response = client.indices.clear_cache(
index = "my-index",
params = { "fielddata": "true" }
)
Copy 다음 요청은 쿼리 캐시만 비웁니다.
REST Python
POST /my-index/_cache/clear?query=true
Copy Copy as cURL
response = client.indices.clear_cache(
index = "my-index",
params = { "query": "true" }
)
Copy 다음 요청은 요청 캐시만 비웁니다.
REST Python
POST /my-index/_cache/clear?request=true
Copy Copy as cURL
response = client.indices.clear_cache(
index = "my-index",
params = { "request": "true" }
)
Copy
Clear the cache for specific fields
다음 요청은 fielda와 fieldb의 필드 캐시를 비웁니다.
REST Python
POST /my-index/_cache/clear?fields=fielda,fieldb
Copy Copy as cURL
response = client.indices.clear_cache(
index = "my-index",
params = { "fields": "fielda,fieldb" }
)
Copy
Clear caches for specific data streams or indexes
다음 요청은 두 개의 특정 인덱스에 대한 캐시를 비웁니다.
REST Python
POST /my-index,my-index2/_cache/clear
Copy Copy as cURL
response = client.indices.clear_cache(
index = "my-index,my-index2"
)
Copy
Clear caches for all data streams and indexes
다음 요청은 모든 데이터 스트림과 인덱스의 캐시를 비웁니다.
REST Python
POST /_cache/clear
Copy Copy as cURL
response = client.indices.clear_cache()
Copy
Clear unused entries from the cache on search-capable nodes
REST Python
POST /*/_cache/clear?file=true
Copy Copy as cURL
response = client.indices.clear_cache(
index = "*",
params = { "file": "true" }
)
Copy
Example response
POST /books,hockey/_cache/clear 요청은 다음 필드들을 반환합니다.
{
"_shards" : {
"total" : 4,
"successful" : 2,
"failed" : 0
}
}
Response body fields
POST /books,hockey/_cache/clear 요청은 다음 응답 필드들을 반환합니다.
| Field | Data type | Description | | _shards | Object | Shard information. | | total | Integer | Total number of shards. | | successful | Integer | Number of index shards with caches successfully cleared. | | failed | Integer | Number of index shards with caches that failed to clear. |
Required permissions
Security 플러그인을 사용한다면 indices:admin/cache/clear 권한이 있는지 확인하세요.
더 알아보기 (Learn more)
캐시는 읽기 성능을 높여주지만, 문제를 디버깅하거나 메모리를 회수할 때 비울 필요가 있어요. 필드, 쿼리, 요청 캐시를 각각 따로, 또는 전체를 비울 수 있으니 상황에 맞게 캐시 유형을 골라 사용하면 됩니다. 대규모 클러스터에서는 캐시를 자주 비우면 오히려 성능이 떨어질 수 있으므로 신중하게 운영하세요.