date: 2020-05-28title: es集群常用查询 #标题
tags: es查询 #标签
categories: elastic stack # 分类

用于记录下es集群中的常用查询指令。

es集群常用查询指令

  1. $ curl http://192.168.20.2:9200/_cat # 查看所有可查看项
  2. =^.^=
  3. /_cat/allocation
  4. /_cat/shards
  5. /_cat/shards/{index}
  6. /_cat/master
  7. /_cat/nodes
  8. /_cat/tasks
  9. /_cat/indices
  10. /_cat/indices/{index}
  11. /_cat/segments
  12. /_cat/segments/{index}
  13. /_cat/count
  14. /_cat/count/{index}
  15. /_cat/recovery
  16. /_cat/recovery/{index}
  17. /_cat/health
  18. /_cat/pending_tasks
  19. /_cat/aliases
  20. /_cat/aliases/{alias}
  21. /_cat/thread_pool
  22. /_cat/thread_pool/{thread_pools}
  23. /_cat/plugins
  24. /_cat/fielddata
  25. /_cat/fielddata/{fields}
  26. /_cat/nodeattrs
  27. /_cat/repositories
  28. /_cat/snapshots/{repository}
  29. /_cat/templates

集群相关查询

显示master详细信息
  1. $ curl http://192.168.20.2:9200/_cat/master?v
  2. id host ip node
  3. JdRF40BnRhewUqHlSgyCTQ 192.168.20.2 192.168.20.2 es1

输出可以显示的列 ?help

  1. $ curl http://192.168.20.2:9200/_cat/master?help
  2. id | | node id
  3. host | h | host name
  4. ip | | ip address
  5. node | n | node name

指定输出的列 ?h
  1. $ curl http://192.168.20.2:9200/_cat/master?h=ip,node
  2. 192.168.20.2 es1

查询集群中所有节点信息
  1. $ curl http://192.168.20.2:9200/_cat/nodes?v
  2. ip heap.percent ram.percent cpu load_1m load_5m load_15m node.role master name
  3. 192.168.20.2 11 98 0 0.00 0.03 0.05 ilm * es1
  4. 192.168.20.4 10 97 0 0.01 0.03 0.05 dilm - es3
  5. 192.168.20.3 19 97 0 0.00 0.03 0.05 dilm - es2

查询设置集群状态
  1. $ curl -XGET http://192.168.20.2:9200/_cluster/health?pretty=true
  2. {
  3. "cluster_name" : "my-es",
  4. "status" : "green",
  5. "timed_out" : false,
  6. "number_of_nodes" : 3,
  7. "number_of_data_nodes" : 2,
  8. "active_primary_shards" : 0,
  9. "active_shards" : 0,
  10. "relocating_shards" : 0,
  11. "initializing_shards" : 0,
  12. "unassigned_shards" : 0,
  13. "delayed_unassigned_shards" : 0,
  14. "number_of_pending_tasks" : 0,
  15. "number_of_in_flight_fetch" : 0,
  16. "task_max_waiting_in_queue_millis" : 0,
  17. "active_shards_percent_as_number" : 100.0
  18. }

显示集群系统信息,包括cpu jvm等
  1. $ curl -XGET http://192.168.20.2:9200/_cluster/stats?pretty=true

查询集群的详细信息,包括节点、分片等
  1. $ curl -XGET http://192.168.20.2:9200/_cluster/state?pretty=true

查询集群堆积的任务
  1. $ curl -XGET http://192.168.20.2:9200/_cluster/pending_tasks?pretty=true

索引相关指令

创建索引
  1. $ curl -XPUT http://192.168.20.2:9200/kgctest1?pretty # 创建的索引名为kgctest1
  2. {
  3. "acknowledged" : true,
  4. "shards_acknowledged" : true,
  5. "index" : "kgctest1"
  6. }

查看所有索引
  1. $ curl http://192.168.20.2:9200/_cat/indices?v
  2. health status index uuid pri rep docs.count docs.deleted store.size pri.store.size
  3. green open kgctest1 2B-LDy4rTyqwYuWsS7K8TA 1 1 0 0 460b 230b

关闭索引
  1. $ curl -XPOST http://192.168.20.2:9200/kgctest1/_close?pretty
  2. {
  3. "acknowledged" : true,
  4. "shards_acknowledged" : true,
  5. "indices" : {
  6. "kgctest1" : {
  7. "closed" : true
  8. }
  9. }
  10. }

开启索引
  1. $ curl -XPOST http://192.168.20.2:9200/kgctest1/_open?pretty
  2. {
  3. "acknowledged" : true,
  4. "shards_acknowledged" : true
  5. }

插入数据
  1. # 插入的数据类型为fulltext,数据的id为1
  2. $ curl -XPUT http://192.168.20.2:9200/kgctest1/fulltext/1?pretty -H 'Content-Type: application/json' -d'
  3. > {
  4. > "name": "lvjianzhao"
  5. > }'
  6. {
  7. "_index" : "kgctest1",
  8. "_type" : "fulltext",
  9. "_id" : "1",
  10. "_version" : 1,
  11. "result" : "created",
  12. "_shards" : {
  13. "total" : 2,
  14. "successful" : 2,
  15. "failed" : 0
  16. },
  17. "_seq_no" : 0,
  18. "_primary_term" : 3
  19. }

查询fulltext类型的id为1的数据
  1. $ curl -XGET http://192.168.20.2:9200/kgctest1/fulltext/1?pretty
  2. {
  3. "_index" : "kgctest1",
  4. "_type" : "fulltext",
  5. "_id" : "1",
  6. "_version" : 1,
  7. "_seq_no" : 0,
  8. "_primary_term" : 3,
  9. "found" : true,
  10. "_source" : {
  11. "name" : "lvjianzhao"
  12. }
  13. }

更新文档
  1. $ curl -XPOST http://192.168.20.2:9200/kgctest1/fulltext/1/_update?pretty -H 'Content-Type:application/json' -d'
  2. > {
  3. > "doc": {"name": "ray"}
  4. > }'
  5. {
  6. "_index" : "kgctest1",
  7. "_type" : "fulltext",
  8. "_id" : "1",
  9. "_version" : 2,
  10. "result" : "updated",
  11. "_shards" : {
  12. "total" : 2,
  13. "successful" : 2,
  14. "failed" : 0
  15. },
  16. "_seq_no" : 1,
  17. "_primary_term" : 3
  18. }

查询所有记录
  1. $ curl -XPOST 'http://192.168.20.2:9200/kgctest1/fulltext/_search?pretty' -H 'Content-Type:application/json' -d'{
  2. > "query": {"match_all":{}}
  3. > }'
  4. {
  5. "took" : 9,
  6. "timed_out" : false,
  7. "_shards" : {
  8. "total" : 1,
  9. "successful" : 1,
  10. "skipped" : 0,
  11. "failed" : 0
  12. },
  13. "hits" : {
  14. "total" : {
  15. "value" : 1,
  16. "relation" : "eq"
  17. },
  18. "max_score" : 1.0,
  19. "hits" : [
  20. {
  21. "_index" : "kgctest1",
  22. "_type" : "fulltext",
  23. "_id" : "1",
  24. "_score" : 1.0,
  25. "_source" : {
  26. "name" : "ray"
  27. }
  28. }
  29. ]
  30. }
  31. }

查询符合条件的记录
  1. $ curl -XPOST http://192.168.20.2:9200/kgctest1/fulltext/_search?pretty -H 'Content-Type:application/json' -d'{
  2. > "query": {"match":{"name":"ray"}}
  3. > }'
  4. {
  5. "took" : 4,
  6. "timed_out" : false,
  7. "_shards" : {
  8. "total" : 1,
  9. "successful" : 1,
  10. "skipped" : 0,
  11. "failed" : 0
  12. },
  13. "hits" : {
  14. "total" : {
  15. "value" : 1,
  16. "relation" : "eq"
  17. },
  18. "max_score" : 0.2876821,
  19. "hits" : [
  20. {
  21. "_index" : "kgctest1",
  22. "_type" : "fulltext",
  23. "_id" : "1",
  24. "_score" : 0.2876821,
  25. "_source" : {
  26. "name" : "ray"
  27. }
  28. }
  29. ]
  30. }
  31. }

清空内存中的缓存
  1. $ curl -XPOST http://192.168.20.2:9200/kgctest1/_cache/

flush和refresh(强制刷新数据到磁盘)
  1. $ curl -XPOST 'http://192.168.20.2:9200/kgctest1/_flush?pretty' -H 'Content-Type:application/json'
  2. "_shards" : {
  3. "total" : 10,
  4. "successful" : 10,
  5. "failed" : 0
  6. }
  7. }
  8. $ curl -XPOST http://192.168.20.2:9200/kgctest1/_refresh?pretty -H 'Content-Type:application/json'
  9. {
  10. "_shards" : {
  11. "total" : 10,
  12. "successful" : 10,
  13. "failed" : 0
  14. }
  15. }

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。

删除文档
  1. $ curl -XDELETE http://192.168.20.2:9200/kgctest1/fulltext/1?pretty
  2. {
  3. "_index" : "kgctest1",
  4. "_type" : "fulltext",
  5. "_id" : "1",
  6. "_version" : 3,
  7. "result" : "deleted",
  8. "_shards" : {
  9. "total" : 2,
  10. "successful" : 2,
  11. "failed" : 0
  12. },
  13. "_seq_no" : 2,
  14. "_primary_term" : 3
  15. }

删除索引
  1. $ curl -XDELETE http://192.168.20.2:9200/kgctest1?pretty
  2. {
  3. "acknowledged" : true
  4. }