elasearch命令

一、索引分片和副本

1、settings

#elasticsearch 创建索引,分片数量,必须在索引创建时指定,创建后无法修改
curl -H 'Content-Type: application/json' -XPUT 'http://192.168.180.35:9200/customer?pretty' -d '{
  "settings" : {
      "number_of_shards" : 3,
      "number_of_replicas" : 1
  }
}'

#查看索引(索引分片,副本数,创建时间等信息):
curl -XGET 'http://192.168.180.35:9200/customer/_settings?pretty'

#修改副本数为0,即没有副本,_all是修改所有索引,可换成具体的索引,如customer,副本数可以随时修改。
curl -H 'Content-Type: application/json' -XPUT 'http://192.168.180.35:9200/_all/_settings' -d ' {"index.number_of_replicas":"0"}'

#elasticsearch  删除索引
curl -XDELETE 'http://192.168.180.35:9200/customer?pretty' 

#打开或关闭索引,说明:关闭的索引只能够显示索引元数据,不能够进行读写操作。
#关闭
curl -H 'Content-Type: application/json' XPOST 'http://192.168.180.35:9200/customer?_close'
#打开
curl -H 'Content-Type: application/json' XPOST 'http://192.168.180.35:9200/customer/_open'

二、其他命令


#elasticsearch 查看集群统计信息
curl -XGET 'http://192.168.180.35:9200/_cluster/stats?pretty'    

#elasticsearch 查看所有索引
curl 'http://192.168.180.35:9200/_cat/indices?v'        

#elasticsearch 查看集群的节点列表
curl 'http://192.168.180.35:9200/_cat/nodes?v'  

#elasticsearch 检测集群是否健康
curl 'http://192.168.180.35:9200/_cat/health?v' 

#elasticsearch 创建索引
curl -XPUT 'http://192.168.180.35:9200/customer?pretty'

#elasticsearch 插入数据
curl -H "Content-Type: application/json" -XPUT 'http://192.168.180.35:9200/customer/external/1?pretty' -d '
{
  "name": "John Doe"
}'

#列出每个 Index 所包含的 Type
curl 'http://192.168.180.35:9200/_mapping?pretty=true'

#elasticsearch 获取数据
#获取customer索引下类型为external,id为1的数据,pretty参数表示返回结果格式美观。
curl -XGET 'http://192.168.180.35:9200/customer/external/1?pretty'
  
#查看某个索引下全部记录
curl http://192.168.180.35:9200/customer/_search
 
#elasticsearch  删除索引
curl -XDELETE 'http://192.168.180.35:9200/customer?pretty' 

#elasticsearch 修改数据
#先新增id为1,name为John Doe的数据,然后将id为1的name修改为Jane Doe。
curl -H "Content-Type: application/json" -XPUT 'http://192.168.180.35:9200/customer/external/2?pretty' -d '
{
  "name": "John Doe"
}'
  
curl -H "Content-Type: application/json" -XPUT 'http://192.168.180.35:9200/customer/external/2?pretty' -d '
{
  "name": "Jane Doe"
}'
     
#elasticsearch 更新数据
curl -H "Content-Type: application/json" -XPOST 'http://192.168.180.35:9200/customer/external/1/_update?pretty' -d '
{
  "doc": { "name": "Jane Doe" }
}'

curl -H "Content-Type: application/json" -XPOST 'http://192.168.180.35:9200/customer/external/1/_update?pretty' -d '
{
  "doc": { "name": "Jane Doe", "age": 20 }
}'


#elasticsearch 删除数据
#将执行删除Customer中ID为2的数据
curl -XDELETE 'http://192.168.180.35:9200/customer/external/2?pretty'

三、_cat

查看具体命令 curl 192.168.180.35:9200/_cat
** ?v 打印出表头信息
** ?pretty 美化输出

  #1.集群健康状态
 curl 192.168.180.35:9200/_cat/health?v
  #2.资源信息
  curl 192.168.180.35:9200/_cat/allocation?v
  #3.查看文档总数
  curl 192.168.180.35:9200/_cat/count?v
  #4.查看具体索引的文档总数
  curl 192.168.180.35:9200/_cat/count/{index}
  #5.查看fielddata占用内存情况(查询时es会把fielddata信息load进内存)
  curl 192.168.180.35:9200/_cat/fielddata
  #6.针对某一字段进行查看
  curl 192.168.180.35:9200/_cat/fielddata/{fields}
  #7.查看数据恢复状态
  curl 192.168.180.35:9200/_cat/recovery
  #8.查看具体索引的数据恢复状态
  curl 192.168.180.35:9200/_cat/recovery/{index}
  #9.查看存储片段信息
  curl 192.168.180.35:9200/_cat/segments
  #10.查看具体索引的存储片段信息
  curl 192.168.180.35:9200/_cat/segments/{index}
  #11.查看节点状态
  curl 192.168.180.35:9200/_cat/nodes
  #12.查看别名信息
  curl 192.168.180.35:9200/_cat/aliases
  #13.指定别名查看信息
  curl 192.168.180.35:9200/_cat/aliases/{alias}
  #14.查看线程池信息
  curl 192.168.180.35:9200/_cat/thread_pool
  #15.查看线程池下插件
  curl 192.168.180.35:9200/_cat/thread_pool/{thread_pools}/_cat/plugins
  #16.查看主节点
  curl 192.168.180.35:9200/_cat/master
  #17.查看快照库
  curl 192.168.180.35:9200/_cat/snapshots/{repository}
  #18.查看模板
  curl 192.168.180.35:9200/_cat/templates
  #19.查看存储库
  curl 192.168.180.35:9200/_cat/repositories
  #20.查看索引信息
  curl 192.168.180.35:9200/_cat/indices
  #21.查看具体索引信息
  curl 192.168.180.35:9200/_cat/indices/{index}
  #22.查看任务
  curl 192.168.180.35:9200/_cat/tasks
  #23.查看待处理任务
  curl 192.168.180.35:9200/_cat/pending_tasks
  #24.查看分片信息
  curl 192.168.180.35:9200/_cat/shards
  #25.查看具体索引的分片信息
  curl 192.168.180.35:9200/_cat/shards/{index}
  #26.查看节点的自定义属性
  curl 192.168.180.35:9200/_cat/nodeattrs
  #27.查看插件信息
  curl 192.168.180.35:9200/_cat/plugins

四_cluster

  #1.查看集群状态
  curl 192.168.180.35:9200/_cluster/health
  #2.查看集群系统信息
  curl 192.168.180.35:9200/_cluster/stats?pretty=true
  #3.查看集群详细信息
  curl 192.168.180.35:9200/_cluster/state?pretty=true
  #4.查看集群堆积的任务
  curl 192.168.180.35:9200/_cluster/pending_tasks?pretty=true
  #5.修改集群配置(-XPUT),** transient 表示临时的,persistent表示永久的
  curl -H "Content-Type: application/json" -XPUT 192.168.180.35:9200/_cluster/settings -d '{ 
  “persistent” : { 
        “discovery.zen.minimum_master_nodes” : 2 
   } 
  }'
  
  #6.对shard的手动控制(-XPOST)
  curl -H "Content-Type: application/json" -XPOST192.168.180.35:9200/_cluster/reroute’ -d ‘xxxxxx’
  #7.关闭节点(-XPOST)
  #a.关闭指定127.0.0.1节点 
  curl -H "Content-Type: application/json" -XPOST 192.168.180.35:9200/_cluster/nodes/_local/_shutdown’ 
  
  curl -H "Content-Type: application/json" -XPOST 192.168.180.35:9200/_cluster/nodes/192.168.1.1/_shutdown’ 
  
  #b.关闭主节点 
  curl -H "Content-Type: application/json" -XPOST 192.168.180.35:9200/_cluster/nodes/_master/_shutdown’ 
  
  #c.关闭整个集群 
  curl -H "Content-Type: application/json" -XPOST 192.168.180.35:9200/_shutdown?delay=10s’ 
  curl -H "Content-Type: application/json" -XPOST 192.168.180.35:9200/_cluster/nodes/_shutdown’ 
  curl -H "Content-Type: application/json" -XPOST 192.168.180.35:9200/_cluster/nodes/_all/_shutdown’ 
  ** delay=10s表示延迟10秒关闭

五、_nodes

 #1.集群JVM状态
  curl 192.168.180.35:9200/_nodes/stats/jvm
  #2.查询节点状态
  curl 192.168.180.35:9200/_nodes/stats?pretty=true’ 
  curl 192.168.180.35:9200/_nodes/192.168.1.2/stats?pretty=true’ 
  curl 192.168.180.35:9200/_nodes/process’ 
  curl 192.168.180.35:9200/_nodes/_all/process’ 
  curl 192.168.180.35:9200/_nodes/192.168.1.2,192.168.1.3/jvm,process’ 
  curl 192.168.180.35:9200/_nodes/192.168.1.2,192.168.1.3/info/jvm,process’ 
  curl 192.168.180.35:9200/_nodes/192.168.1.2,192.168.1.3/_all 
  curl 192.168.180.35:9200/_nodes/hot_threads

六、CURD

 #1.查询数据
  curl -XGET 'http://localhost:9200/{index}/{type}/{id}'
  #2.索引(插入)数据
  curl -XPOST 'http://localhost:9200/{index}/{type}/{id}' -d '{“key”:”value”,“key”:”value”}'
  #3.批量导入数据(在a.json文件所在当前文件下)
  curl -XPOST 'localhost:9200/{index}/{type}/_bulk' --data-binary "@a.json"
  #4.删除数据
  curl -XDELETE 'http://localhost:9200/{index}/{type}/{id}'
  5.按照查询结果删除数据
  curl -XPOST 'localhost:9200/{index}/{type}/_delete_by_query?pretty' -d'
  {"query": {"query_string": {"message": "some message"}}}'

七、es服务启动自动分片失败,报出集群状态为red或yellow

 #1.查看未分配分片
   curl -XGET 'http://localhost:9200/_cat/shards' | grep UNASSIGNED
 #2.手动分片
  curl -XPOST 'localhost:9200/_cluster/reroute' -d '{
  "commands": [{
     "allocate": {
     "index": "index-name",
     "shard": 0,
     "node": "node-0",
     "allow_primary": false
      }
     }]
   }'
上一篇:Go 中的 gRPC 入门详解


下一篇:容器的日志管理ELK