对于查询操作,Elasticsearch提供了缓存特性来暂存结果。

对于相同条件的查询请求,在缓存中的数据失效前,响应后续的查询操作时可以直接从缓存中提取结果,有效降低检索操作的时延,提升检索数据时的体验。

提到缓存相关的特性,即要关注如下几点:

  • 缓存的开关
  • 缓存中的数据哪里来
  • 缓存占用的空间
  • 缓存中数据的老化机制
  • 缓存中的数据规模

缓存的开关

创建索引时,默认启用缓存,但可以在创建参数中将参数index.requests.cache.enable指定为false,从而关闭缓存。

命令样例如下:

curl -X PUT "https://localhost:9200/testindex_003?pretty" -H 'Content-Type: application/json' -d'
{
"settings": {
"index.requests.cache.enable": false
}
}
' --cacert $ES_HOME/config/certs/http_ca.crt -u "elastic:ohCxPH=QBE+s5=*lo7F9"

执行结果的样例,如下:

{
"acknowledged" : true,
"shards_acknowledged" : true,
"index" : "testindex_003"
}

检查索引testindex_003的参数,命令样例如下:

curl -X GET "https://localhost:9200/testindex_003/_settings?pretty" --cacert $ES_HOME/config/certs/http_ca.crt -u "elastic:ohCxPH=QBE+s5=*lo7F9"

执行结果的样例,如下:

{
"testindex_003" : {
"settings" : {
"index" : {
"routing" : {
"allocation" : {
"include" : {
"_tier_preference" : "data_content"
}
}
},
"number_of_shards" : "1",
"provided_name" : "testindex_003",
"creation_date" : "1702050678193",
"requests" : {
"cache" : {
"enable" : "false"
}
},
"number_of_replicas" : "1",
"uuid" : "9r8VrKdURhqr1XJM8Z9egQ",
"version" : {
"created" : "8500003"
}
}
}
}
}

在索引创建完毕之后,可以通过Update index settings API修改开关的状态。

例如刚才在创建索引testindex_003时关闭了缓存,可以通过执行命令手工开启缓存。

命令样例如下:

curl -X PUT "https://localhost:9200/testindex_003/_settings?pretty" -H 'Content-Type: application/json' -d'
{
"index.requests.cache.enable": true
}
' --cacert $ES_HOME/config/certs/http_ca.crt -u "elastic:ohCxPH=QBE+s5=*lo7F9"

执行结果的样例,如下:

{
"acknowledged" : true
}

检查索引testindex_003的参数,命令样例如下:

curl -X GET "https://localhost:9200/testindex_003/_settings?pretty" --cacert $ES_HOME/config/certs/http_ca.crt -u "elastic:ohCxPH=QBE+s5=*lo7F9"

执行结果的样例,如下:

{
"testindex_003" : {
"settings" : {
"index" : {
"routing" : {
"allocation" : {
"include" : {
"_tier_preference" : "data_content"
}
}
},
"number_of_shards" : "1",
"provided_name" : "testindex_003",
"creation_date" : "1702050678193",
"requests" : {
"cache" : {
"enable" : "true"
}
},
"number_of_replicas" : "1",
"uuid" : "9r8VrKdURhqr1XJM8Z9egQ",
"version" : {
"created" : "8500003"
}
}
}
}
}

执行检索操作时,依据业务要求指定缓存的开关,比如禁止从缓存中提取数据,命令样例如下:

curl -X GET "https://localhost:9200/testindex_003/_search?request_cache=true&pretty" -H 'Content-Type: application/json' -d'
{
"size": 0,
"aggs": {
"popular_colors": {
"terms": {
"field": "colors"
}
}
}
}
' --cacert $ES_HOME/config/certs/http_ca.crt -u "elastic:ohCxPH=QBE+s5=*lo7F9"

缓存中的数据

假定启用了缓存特性,Elasticsearch节点处理检索请求时,执行如下操作:

  • 处理查询请求时,则使用查询条件作为关键字,从缓存中提取数据。
  • 假如可以从缓存中提取到相关记录,则返回这部分记录,本次处理完毕。
  • 假如没有从缓存中提取到相关记录,则执行检索操作。
  • 检索操作完毕,使用查询条件作为关键字,将处理结果缓存至缓存中。
  • 假如缓存空间不足,则使用LRU算法清理缓存中的记录,直至可用空间足够保存刚才的检索结果。
  • 返回检索结果。

上述为检索操作的一般流程,实际的执行流程以Elasticsearch的实现为准。

缓存占用的空间

缓存可占用的空间,默认值为Elasticsearch进程Java堆空间的%1

在节点的配置文件$ES_HOME/config/elasticsearch.yml中指定,配置样例如下:

indices.requests.cache.size: 2%

配置项indices.requests.cache.expire用于指定缓存中数据的生命周期。

考虑到Elasticsearch后台定期执行的Refresh操作会自动清理缓存中的过期数据,因此一般不需要配置indices.requests.cache.expire

清理缓存

手工清理缓存,命令样例如下:

curl -X POST "https://localhost:9200/testindex_001/_cache/clear?request=true&pretty" --cacert $ES_HOME/config/certs/http_ca.crt -u "elastic:ohCxPH=QBE+s5=*lo7F9"

执行结果的样例,如下:

{
"_shards" : {
"total" : 2,
"successful" : 1,
"failed" : 0
}
}

缓存的使用情况

可以使用如下API来获取使用情况。

Index stats API

基于索引的粒度统计缓存的使用情况。

命令样例,如下:

curl -X GET "https://localhost:9200/_stats/request_cache?human&pretty" --cacert $ES_HOME/config/certs/http_ca.crt -u "elastic:ohCxPH=QBE+s5=*lo7F9"

执行结果的样例,如下:

{
"_shards" : {
"total" : 13,
"successful" : 5,
"failed" : 0
},
"_all" : {
"primaries" : {
"request_cache" : {
"memory_size" : "0b",
"memory_size_in_bytes" : 0,
"evictions" : 0,
"hit_count" : 0,
"miss_count" : 0
}
},
"total" : {
"request_cache" : {
"memory_size" : "0b",
"memory_size_in_bytes" : 0,
"evictions" : 0,
"hit_count" : 0,
"miss_count" : 0
}
}
},
"indices" : {
"cloned-testindex_002" : {
"uuid" : "Lrz1DOsWRY6OTt0Veawo-w",
"health" : "yellow",
"status" : "open",
"primaries" : {
"request_cache" : {
"memory_size" : "0b",
"memory_size_in_bytes" : 0,
"evictions" : 0,
"hit_count" : 0,
"miss_count" : 0
}
},
"total" : {
"request_cache" : {
"memory_size" : "0b",
"memory_size_in_bytes" : 0,
"evictions" : 0,
"hit_count" : 0,
"miss_count" : 0
}
}
},
"testindex_002" : {
"uuid" : "k6twq9y9Qtmcs2AHK-USEQ",
"health" : "yellow",
"status" : "open",
"primaries" : {
"request_cache" : {
"memory_size" : "0b",
"memory_size_in_bytes" : 0,
"evictions" : 0,
"hit_count" : 0,
"miss_count" : 0
}
},
"total" : {
"request_cache" : {
"memory_size" : "0b",
"memory_size_in_bytes" : 0,
"evictions" : 0,
"hit_count" : 0,
"miss_count" : 0
}
}
},
"testindex_001" : {
"uuid" : "7iGJRFfxRd2jD3qP-KDRmQ",
"health" : "yellow",
"status" : "open",
"primaries" : {
"request_cache" : {
"memory_size" : "0b",
"memory_size_in_bytes" : 0,
"evictions" : 0,
"hit_count" : 0,
"miss_count" : 0
}
},
"total" : {
"request_cache" : {
"memory_size" : "0b",
"memory_size_in_bytes" : 0,
"evictions" : 0,
"hit_count" : 0,
"miss_count" : 0
}
}
}
}
}

Nodes stats API

基于集群中节点的粒度统计缓存的使用情况。

命令样例,如下:

curl -X GET "https://localhost:9200/_nodes/stats/indices/request_cache?human&pretty" --cacert $ES_HOME/config/certs/http_ca.crt -u "elastic:ohCxPH=QBE+s5=*lo7F9"

执行结果的样例,如下:

{
"_nodes" : {
"total" : 1,
"successful" : 1,
"failed" : 0
},
"cluster_name" : "elasticsearch",
"nodes" : {
"aKgBu7LgS9a6iPYH8n2JPw" : {
"timestamp" : 1702049373653,
"name" : "jackie-ubuntu",
"transport_address" : "127.0.0.1:9300",
"host" : "127.0.0.1",
"ip" : "127.0.0.1:9300",
"roles" : [
"data",
"data_cold",
"data_content",
"data_frozen",
"data_hot",
"data_warm",
"ingest",
"master",
"ml",
"remote_cluster_client",
"transform"
],
"attributes" : {
"transform.config_version" : "10.0.0",
"ml.machine_memory" : "4040318976",
"ml.allocated_processors" : "4",
"ml.allocated_processors_double" : "4.0",
"ml.max_jvm_size" : "2021654528",
"ml.config_version" : "11.0.0",
"xpack.installed" : "true"
},
"indices" : {
"request_cache" : {
"memory_size" : "0b",
"memory_size_in_bytes" : 0,
"evictions" : 0,
"hit_count" : 0,
"miss_count" : 0
}
}
}
}
}

相关资料

ElasticSearch之Shard request cache settings的更多相关文章

  1. [转] KVM storage performance and cache settings on Red Hat Enterprise Linux 6.2

    Almost one year ago, I checked how different cache settings affected KVM storage subsystem performan ...

  2. hystrix中request cache请求缓存

    有一个概念,叫做reqeust context,请求上下文,一般来说,在一个web应用中, 我们会在一个filter里面,对每一个请求都施加一个请求上下文,就是说,tomcat容器内,每一次请求,就是 ...

  3. Elasticsearch系列---shard内部原理

    概要 本篇我们来看看shard内部的一些操作原理,了解一下人家是怎么玩的. 倒排索引 倒排索引的结构,是非常适合用来做搜索的,Elasticsearch会为索引的每个index为analyzed的字段 ...

  4. Elasticsearch 模块 - Shard Allocation 机制

    原文 1. 背景 shard allocation 意思是分片分配, 是一个将分片分配到节点的过程; 可能发生该操作的过程包括: 初始恢复(initial recovery) 副本分配(replica ...

  5. 【ElasticSearch】异常 Request cannot be executed; I/O reactor status: STOPPED

    Caused by: java.lang.RuntimeException: Request cannot be executed; I/O reactor status: STOPPED at or ...

  6. Java访问Elasticsearch报错Request cannot be executed; I/O reactor status: STOPPED

    简介 使用ES过程中遇到一个Request cannot be executed; I/O reactor status: STOPPED 的异常,大概意思是和server端的连接异常终止了.开始以为 ...

  7. elasticsearch:shard 和 replica 机制

    shard 和 replica 机制: index包含多个shard 每个shard都是一个最小工作单元,承载部分数据,lucene实例,完整的建立索引和处理请求的能力 增减节点时,shard会自动在 ...

  8. Elasticsearch:shard 分配感知

  9. Elasticsearch相关配置(二)

    一.关于elasticsearch的基本概念 term 索引词,在elasticsearch中索引词(term)是一个能够被索引的精确值.foo,Foo Foo几个单词是不相同的索引词.索引词(ter ...

  10. Elasticsearch 通关教程(七): Elasticsearch 的性能优化

    硬件选择 Elasticsearch(后文简称 ES)的基础是 Lucene,所有的索引和文档数据是存储在本地的磁盘中,具体的路径可在 ES 的配置文件../config/elasticsearch. ...

随机推荐

  1. MindSpore简要性能分析

    技术背景 在之前的一篇博客中,我们介绍过MindInsight的安装与使用.不过在前面的文章中,我们主要介绍的是MindInsight与SummaryCollector的配合使用,更多的是用于对结果进 ...

  2. 在线问诊 Python、FastAPI、Neo4j — 创建 疾病节点

    目录 疾病数据 创建节点 根据检查结果.医生的临床经验得出疾病 疾病数据 disease_data.csv 建议值用""引起来.避免中间有,号造成误识别 疾病 "干眼&q ...

  3. 兴达易控Modbus转Profinet网关连接三菱A800变频器配置案例

    兴达易控Modbus转Profinet网关连接1200Profinet转modbus接三菱A800变频器 下面介绍A800 变频器通过兴达易控modbus转profinet网关,使1200plc无需编 ...

  4. git Failed to connect to 127.0.0.1 port xxxx: Connection refused 的问题。

    问题描述在使用 git 拉取.提交代码的时候,会出现 git Failed to connect to 127.0.0.1 port xxxx: Connection refused 的问题. 原因: ...

  5. C#学习笔记---异常捕获和变量

    异常捕获 使用异常捕获可以捕获出现异常的代码块,防止因为异常抛出造成的程序卡死的情况发生. try{}catch{}finally{}结构 //异常捕获 try { string str=Consol ...

  6. Java 魔法值处理的四种方法

    Java 魔法值处理方案 魔法值的定义 方法一 静态常量(不推荐) 方法二 接口中定义 方法三 定义在实体类 方法四 使用枚举类 enum 总结 魔法值的定义 魔法值是Java中突兀出现在代码中的常量 ...

  7. 初探富文本之React实时预览

    初探富文本之React实时预览 在前文中我们探讨了很多关于富文本引擎和协同的能力,在本文中我们更偏向具体的应用组件实现.在一些场景中比如组件库的文档编写时,我们希望能够有实时预览的能力,也就是用户可以 ...

  8. 如何使用Python将PDF转为图片

    将PDF转为图片能方便我们将文档内容上传至社交媒体平台进行分享.此外,转换为图片后,还可以对图像进行进一步的裁剪.调整大小或添加标记等操作. 用Python将PDF文件转JPG/ PNG图片可能是大家 ...

  9. Godot - 创建翻译文件(常量表)

    版本 Godot 3.1.2 背景 Godot的UI系统封装的很难受, 一些东西很难改动, 比如这个AcceptDialog的"确定""取消"按钮, 特别是在编 ...

  10. 博弈论nim游戏

    nim游戏 给定n堆物品,第i堆物品有Ai个,两名玩家轮流行动,每次可以任选一堆,取走任意多个物品,可把一堆取光,但不能不取.取走最后一件物品的人获胜. 定理:nim游戏先手必胜,当且仅当A1 xor ...