date: 2020-05-28title: es集群常用查询 #标题
tags: es查询 #标签
categories: elastic stack # 分类
es集群常用查询指令
$ curl http://192.168.20.2:9200/_cat # 查看所有可查看项
=^.^=
/_cat/allocation
/_cat/shards
/_cat/shards/{index}
/_cat/master
/_cat/nodes
/_cat/tasks
/_cat/indices
/_cat/indices/{index}
/_cat/segments
/_cat/segments/{index}
/_cat/count
/_cat/count/{index}
/_cat/recovery
/_cat/recovery/{index}
/_cat/health
/_cat/pending_tasks
/_cat/aliases
/_cat/aliases/{alias}
/_cat/thread_pool
/_cat/thread_pool/{thread_pools}
/_cat/plugins
/_cat/fielddata
/_cat/fielddata/{fields}
/_cat/nodeattrs
/_cat/repositories
/_cat/snapshots/{repository}
/_cat/templates
集群相关查询
显示master详细信息
$ curl http://192.168.20.2:9200/_cat/master?v
id host ip node
JdRF40BnRhewUqHlSgyCTQ 192.168.20.2 192.168.20.2 es1
输出可以显示的列 ?help
$ curl http://192.168.20.2:9200/_cat/master?help
id | | node id
host | h | host name
ip | | ip address
node | n | node name
指定输出的列 ?h
$ curl http://192.168.20.2:9200/_cat/master?h=ip,node
192.168.20.2 es1
查询集群中所有节点信息
$ curl http://192.168.20.2:9200/_cat/nodes?v
ip heap.percent ram.percent cpu load_1m load_5m load_15m node.role master name
192.168.20.2 11 98 0 0.00 0.03 0.05 ilm * es1
192.168.20.4 10 97 0 0.01 0.03 0.05 dilm - es3
192.168.20.3 19 97 0 0.00 0.03 0.05 dilm - es2
查询设置集群状态
$ curl -XGET http://192.168.20.2:9200/_cluster/health?pretty=true
{
"cluster_name" : "my-es",
"status" : "green",
"timed_out" : false,
"number_of_nodes" : 3,
"number_of_data_nodes" : 2,
"active_primary_shards" : 0,
"active_shards" : 0,
"relocating_shards" : 0,
"initializing_shards" : 0,
"unassigned_shards" : 0,
"delayed_unassigned_shards" : 0,
"number_of_pending_tasks" : 0,
"number_of_in_flight_fetch" : 0,
"task_max_waiting_in_queue_millis" : 0,
"active_shards_percent_as_number" : 100.0
}
显示集群系统信息,包括cpu jvm等
$ curl -XGET http://192.168.20.2:9200/_cluster/stats?pretty=true
查询集群的详细信息,包括节点、分片等
$ curl -XGET http://192.168.20.2:9200/_cluster/state?pretty=true
查询集群堆积的任务
$ curl -XGET http://192.168.20.2:9200/_cluster/pending_tasks?pretty=true
索引相关指令
创建索引
$ curl -XPUT http://192.168.20.2:9200/kgctest1?pretty # 创建的索引名为kgctest1
{
"acknowledged" : true,
"shards_acknowledged" : true,
"index" : "kgctest1"
}
查看所有索引
$ curl http://192.168.20.2:9200/_cat/indices?v
health status index uuid pri rep docs.count docs.deleted store.size pri.store.size
green open kgctest1 2B-LDy4rTyqwYuWsS7K8TA 1 1 0 0 460b 230b
关闭索引
$ curl -XPOST http://192.168.20.2:9200/kgctest1/_close?pretty
{
"acknowledged" : true,
"shards_acknowledged" : true,
"indices" : {
"kgctest1" : {
"closed" : true
}
}
}
开启索引
$ curl -XPOST http://192.168.20.2:9200/kgctest1/_open?pretty
{
"acknowledged" : true,
"shards_acknowledged" : true
}
插入数据
# 插入的数据类型为fulltext,数据的id为1
$ curl -XPUT http://192.168.20.2:9200/kgctest1/fulltext/1?pretty -H 'Content-Type: application/json' -d'
> {
> "name": "lvjianzhao"
> }'
{
"_index" : "kgctest1",
"_type" : "fulltext",
"_id" : "1",
"_version" : 1,
"result" : "created",
"_shards" : {
"total" : 2,
"successful" : 2,
"failed" : 0
},
"_seq_no" : 0,
"_primary_term" : 3
}
查询fulltext类型的id为1的数据
$ curl -XGET http://192.168.20.2:9200/kgctest1/fulltext/1?pretty
{
"_index" : "kgctest1",
"_type" : "fulltext",
"_id" : "1",
"_version" : 1,
"_seq_no" : 0,
"_primary_term" : 3,
"found" : true,
"_source" : {
"name" : "lvjianzhao"
}
}
更新文档
$ curl -XPOST http://192.168.20.2:9200/kgctest1/fulltext/1/_update?pretty -H 'Content-Type:application/json' -d'
> {
> "doc": {"name": "ray"}
> }'
{
"_index" : "kgctest1",
"_type" : "fulltext",
"_id" : "1",
"_version" : 2,
"result" : "updated",
"_shards" : {
"total" : 2,
"successful" : 2,
"failed" : 0
},
"_seq_no" : 1,
"_primary_term" : 3
}
查询所有记录
$ curl -XPOST 'http://192.168.20.2:9200/kgctest1/fulltext/_search?pretty' -H 'Content-Type:application/json' -d'{
> "query": {"match_all":{}}
> }'
{
"took" : 9,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 1,
"relation" : "eq"
},
"max_score" : 1.0,
"hits" : [
{
"_index" : "kgctest1",
"_type" : "fulltext",
"_id" : "1",
"_score" : 1.0,
"_source" : {
"name" : "ray"
}
}
]
}
}
查询符合条件的记录
$ curl -XPOST http://192.168.20.2:9200/kgctest1/fulltext/_search?pretty -H 'Content-Type:application/json' -d'{
> "query": {"match":{"name":"ray"}}
> }'
{
"took" : 4,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 1,
"relation" : "eq"
},
"max_score" : 0.2876821,
"hits" : [
{
"_index" : "kgctest1",
"_type" : "fulltext",
"_id" : "1",
"_score" : 0.2876821,
"_source" : {
"name" : "ray"
}
}
]
}
}
清空内存中的缓存
$ curl -XPOST http://192.168.20.2:9200/kgctest1/_cache/
flush和refresh(强制刷新数据到磁盘)
$ curl -XPOST 'http://192.168.20.2:9200/kgctest1/_flush?pretty' -H 'Content-Type:application/json'
"_shards" : {
"total" : 10,
"successful" : 10,
"failed" : 0
}
}
$ curl -XPOST http://192.168.20.2:9200/kgctest1/_refresh?pretty -H 'Content-Type:application/json'
{
"_shards" : {
"total" : 10,
"successful" : 10,
"failed" : 0
}
}
refresh和flush的区别
当一个文档进入ES的初期, 文档是被存储到内存里的,默认经过1s之后, 会被写入文件系统缓存,这样该文档就可以被搜索到了,注意,此时该索引数据被没有最终写入到磁盘上。如果你对这1s的时间间隔还不满意, 调用_refresh就可以立即实现内存->文件系统缓存, 从而使文档可以立即被搜索到。 ES为了数据的安全, 在接受写入的文档的时候, 在写入内存buffer的同时, 会写一份translog日志,从而在出现程序故障/磁盘异常时, 保证数据的完整和安全。flush会触发lucene commit,并清空translog日志文件。 translog的flush是ES在后台自动执行的,默认情况下ES每隔5s会去检测要不要flush translog,默认条件是:每 30 分钟主动进行一次 flush,或者当 translog 文件大小大于 512MB主动进行一次 flush。
删除文档
$ curl -XDELETE http://192.168.20.2:9200/kgctest1/fulltext/1?pretty
{
"_index" : "kgctest1",
"_type" : "fulltext",
"_id" : "1",
"_version" : 3,
"result" : "deleted",
"_shards" : {
"total" : 2,
"successful" : 2,
"failed" : 0
},
"_seq_no" : 2,
"_primary_term" : 3
}
删除索引
$ curl -XDELETE http://192.168.20.2:9200/kgctest1?pretty
{
"acknowledged" : true
}