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
}
}
评论 (0)