elasticsearch 缓存 配置

本文介绍了Elasticsearch中的缓存管理,包括过滤器缓存(Filter Cache)和字段数据缓存(Field Data Cache)。过滤器缓存负责缓存过滤后的结果,支持不同类型的缓存策略如节点过滤器缓存等。字段数据缓存主要用于排序或分面搜索,通过加载字段值到内存提高访问速度。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

缓存
索引有不同的内置缓存模块。 它们包括 过滤器(filter), 字段(field) 和其它。
过滤器缓存
过滤器缓存负责缓存过滤后的结果(在查询中使用)。 默认实现过滤器缓存(和一个推荐使用在几乎所有的情况下) 的是 节点过滤器缓存 类型。
节点过滤器缓存
这个 节点过滤器缓存 可以被配置为使用总内存的一定比例分配给进程或一个指定数量的内存。 所有的索引分片只能出现在一个节点共享一个单一的节点缓存(这就是为什么它叫 节点 ”)。 这个缓存工具实现了一个叫做 LRU 数据删除策略:当缓存区满了以后,最近最少使用的数据被删除,以腾出空间给新的数据存放。
这个设置允许控制过滤器缓存占多少内存大小, indices.cache.filter.size ,该属性的默认值 20% 。 注意 ,这是不一个索引级别的设置,而是节点级别设置(可以在节点上进行配置)。indices.cache.filter.size  可以接受一个百分比值,如:30% ,或一个精确值,如:512
mb 。

索引过滤器缓存
该过滤器缓存是在索引等级上面的(每一个节点上)。 一般来说,不建议使用该缓存,因为缓存内存使用量取决于每个节点上面的分片,这个很难难预测。 这个类型分为: 常驻,  软件 和 弱 (resident, soft and weak.)。
所有类型支持以下设置:
Setting
Description
index.cache.filter.max_size
这个是设置缓存(每次查询在一个分片里的缓存)的最大值(计数,而不是字节大小)。默认是不设置(-1),一般用于 soft 缓存或者是适合缓存的过滤器。
index.cache.filter.expire
设置缓存的数据处于非活动状态多长时间后过期。
默认值为(-1)。例如,可以设置为 5m,表示缓存的数据5分钟后过期。
字段数据缓存
字段数据缓存的主要用在当以某一个字段排序或faceting上。 它将所有字段的数值加载到内存中为了提供快速的访问这些值。 字段数据缓存可能很昂贵为一个字段,所以建议要有足够的内存来分配它,并使它加载。
默认该缓存的类型使用 resident(常驻) (因为重建的代码太大)。 其他类型可以使用 soft。
Setting
Description
index.cache.field.max_size
这个是设置缓存(每次查询在一个分片里的缓存)的最大值(计数,而不是字节大小)。默认是不设置(-1)
index.cache.field.expire
设置缓存的数据处于非活动状态多长时间到期。
默认值为(-1)。例如,可以设置为 5m,表示缓存的数据5分钟后过期。

——————————————————————————————————————————–
英文原文:
Cache

There are different caching inner modules associated with an index. They include filter,field and others.
Filter Cache
The filter cache is responsible for caching the results of filters (used in the query). The default implementation of a filter cache (and the one recommended
to use in almost all cases) is the node filter cache type.

Node Filter Cache
The node filter cache may be configured to use either a percentage of the total memory allocated to the process or an specific amount of memory. All shards
present on a node share a single node cache (thats why its called node`). The cache implements an LRUeviction policy: when a cache becomes full, the least recently used data is evicted to make way for new data.

The setting that allows one to control the memory size for the filter cache isindices.cache.filter.size, which defaults to 20%. Note, this is not an index
level setting but a node level setting (can be configured in the node configuration).

indices.cache.filter.size can accept either a percentage value, like 30%, or an exact value, like 512mb.
Index Filter Cache
A filter cache that exists on the index level (on each node). Generally, not recommended for use since its memory usage depends on which shards are allocated
on each node and its hard to predict it. The types are: resident, soft and weak.

All types support the following settings:



Setting
Description
index.cache.filter.max_size

The max size (count, not byte size) of the cache (per search segment in a shard). Defaults to not set (-1), which is usually fine with soft cache and
proper cacheable filters.



index.cache.filter.expire
A time based setting that expires filters after a certain time of inactivity. Defaults to -1. For example, can be set to 5m for a 5 minute expiry.

Field Data Cache
The field data cache is used mainly when sorting on or faceting on a field. It loads all the field values to memory in order to provide fast document based
access to those values. The field data cache can be expensive to build for a field, so its recommended to have enough memory to allocate it, and keep it loaded.

The default type for the field data cache is resident (because of the cost of rebuilding it). Other types include soft.

Setting
Description

index.cache.field.max_size
The max size (count, not byte size) of the cache (per search segment in a shard). Defaults to not set (-1).

index.cache.field.expire
A time based setting that expires filters after a certain time of inactivity. Defaults to -1. For example, can be set to 5m for a 5 minute expiry

### Elasticsearch 缓存机制概述 Elasticsearch缓存机制旨在提升查询性能,减少资源消耗并加快响应时间。其主要通过三种类型的缓存实现这一目标:节点查询缓存、分片请求缓存以及字段数据缓存。 #### 节点查询缓存 (Node Query Cache) 节点查询缓存是一种基于内存的缓存方式,用于存储布尔查询中的过滤器部分的结果集。当接收到相同查询时,可以直接从缓存中获取结果而无需重新执行计算过程。这种缓存适用于只读场景下的精确匹配操作,例如 `term` 或 `terms` 过滤条件[^1]。需要注意的是,此缓存仅针对特定结构化查询有效,并且会在每次更新文档后失效以保持一致性。 ```json GET /_stats/query_cache { "query_cache": { "_shards": {...}, ... "memory_size_in_bytes": ..., "total_count": ..., "hit_count": ..., "miss_count": ... } } ``` 上述命令可以用来查看当前集群内的节点查询缓存状态信息[^5]。 #### 分片请求缓存 (Shard Request Cache) 相比节点级别的查询缓存,分片请求缓存则更加关注于保存整个搜索请求及其对应的聚合运算结果。它特别适合处理那些具有较高重复率但不需要最新数据支持的历史数据分析任务。默认情况下,只有设置了 `"size=0"` 参数(即不返回具体命中记录列表)的搜索才会被纳入到此类缓存之中;同时还可以借助 `index.requests.cache.size` 来调节可用空间比例,默认占堆栈总容量的百分之一][^[^23]。 如果希望自定义启用规则,则可以在创建索引或者修改现有索引设置时加入如下配置项: ```json PUT my_index/_settings { "index.requests.cache.enable": true/false } ``` 此外,在实际部署过程中也应考虑适当延长刷新间隔周期(refresh interval),这样有助于进一步减轻因频繁写入引发的数据变动所带来的影响,进而增强整体系统的稳定性与效率[^4]。 --- ###
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值