共计 1856 个字符,预计需要花费 5 分钟才能阅读完成。
ElasticSearch 作为海量数据存储和搜索查询工具,有着大量的 API 操作,这里总结下常用的 API 查询语句,方便使用。
状态查询
查询索引节点分片状态
根据索引名称 my_index
查询索引对应的分片和节点存储分布情况:
GET _cat/shards?index=my_index&s=node,store:desc&v
假设你的集群有 3 个节点,分片设计为 3 个主分片 1 个副分片,返回结果:
index | shard | prirep | state | docs | store | ip | node |
---|---|---|---|---|---|---|---|
my_index | 0 | p | STARTED | 74648 | 30.7gb | 127.0.0.1 | 1648036502000024932 |
my_index | 0 | r | STARTED | 76042 | 30.5gb | 127.0.0.1 | 1648036502000024932 |
my_index | 1 | p | STARTED | 73648 | 30.7gb | 127.0.0.1 | 164803650200003421 |
my_index | 1 | r | STARTED | 72042 | 30.5gb | 127.0.0.1 | 164803650200003421 |
my_index | 2 | p | STARTED | 71648 | 30.7gb | 127.0.0.1 | 164803650200001833 |
my_index | 2 | r | STARTED | 70042 | 30.5gb | 127.0.0.1 | 164803650200001833 |
其中 index 代表索引名称,shard 代表分片,prirep 代表分片类型,p 是主分片,r 是副分片。
state 表示分片状态,docs 代表文档数,store 代表存储的数据大小,ip 是机器的 IP,node 代表集群节点编号。
通过这个查询可以了解索引的分片和节点分布是否均衡。从分片设计的角度来说,每个分片的存储大小最好在 30~50G 左右,并保证每个节点的分片能均匀分布。
查看索引信息
查看具体某个索引 my_index
的信息状态:
GET _cat/indices?index=my_index&v
返回的结果:
health | status | index | uuid | pri | rep | docs.count | docs.deleted | store.size | pri.store.size |
---|---|---|---|---|---|---|---|---|---|
green | open | my_index | sxfaef232_U312344DO034233Q | 9 | 1 | 238432 | 0 | 876gb | 438gb |
其中特别关注的有 health,green 代表索引状态正常,red 代表不可用,yellow 代表有异常。
缓存清除
清除全部缓存:
POST /_cache/clear
清除特定索引的缓存:
POST /my_index/_cache/clear
POST /my_index1,my_index2/_cache/clear
清除特定类型缓存:
通过设置 fielddata
,query
,request
参数为 true
来清除特定类型的缓存
POST /my-index/_cache/clear?fielddata=true
POST /my-index/_cache/clear?query=true
POST /my-index/_cache/clear?request=true
获取分片分配原因
获取有关分片分配问题的信息:
GET _cluster/allocation/explain?pretty
重新分片
对未分配的分片进行重新分片:
POST _cluster/reroute?retry_failed=true
聚合查询
去重统计
统计某个字段去重后的数量,主要用到 cardinality
函数。
这里举个例子,统计 uid 去重后的数量。
GET /example_index/_search
{
"size": 0,
"aggs": {
"uid_dictinct_count": {
"cardinality": {"field": "uid"}
}
}
}
返回结果:
"aggregations" : {
"uid_dictinct_count" : {"value" : 4634}
}
这里统计出来的 4634
就是 example_index
这个 index 中 uid
去重后的数量。
统计数值
对某个字段数据进行总数、最大值、最小值、平均值、总和的统计。
GET /example_index/_search
{
"size": 0,
"aggs": {
"length_stats": {
"stats": {"field": "length"}
}
}
}
返回的结果:
"aggregations" : {
"length_stats" : {
"count" : 15232,
"min" : 6.0,
"max" : 132.0,
"avg" : 67.52,
"sum" : 534211.0
}
}
正文完