elasticsearch常用语法


7.x之后版本

https://developer.aliyun.com/article/742308


创建my_store 索引
curl -X PUT http://127.0.0.1:9200/my_store -H 'Content-Type: application/json' -d '
{
    "settings": {
        "number_of_shards": 1
    },
    "mappings": {
        "properties": {
            "productName": {
                "type": "text"
            },
            "size": {
                "type": "keyword"
            }
        }
    }
}'

导入数据
curl -X PUT "http://127.0.0.1:9200/_bulk" -H 'Content-Type: application/json' --data-binary @testdata.json

testdata.json 数据如下
{"index": {"_index":"my_store","_type":"products"}}
{"productName": "2019秋装新款文艺衬衫女装","size": "M"}
{"index": {"_index":"my_store","_type":"products"}}
{"productName": "2019秋装新款文艺衬衫女装","size": "L"}

{"index":{"_index":"zhouls","_type":"emp","_id":"10"}}
{ "name":"jack", "age" :18}
{"index":{"_index":"zhouls","_type":"emp","_id":"11"}}
{"name":"tom", "age":27}
{"update":{"_index":"zhouls","_type":"emp", "_id":"2"}}
{"doc":{"age" :22}}
{"delete":{"_index":"zhouls","_type":"emp","_id":"1"}}

查询
curl -XGET 'http://192.168.80.200:9200/zhouls/emp/1?pretty'


查看ES集群的工作状态:
curl -X GET 'http://node101.yinzhengjie.org.cn:9200/_cat'                #查看ES可以查看的方法
curl -X GET 'http://node101.yinzhengjie.org.cn:9200/_cat/nodes'           #查看各个node节点信息,不显示头部信息
curl -X GET 'http://node101.yinzhengjie.org.cn:9200/_cat/nodes?v'        #查看各个node节点信息,显示头部信息    
curl -X GET 'http://node101.yinzhengjie.org.cn:9200/_cat/master?v'        #显示主节点信息,显示头部信息    
curl -X GET 'http://node101.yinzhengjie.org.cn:9200/_cat/health?v'        #查看集群的节点状态
            
            
            
//更新
POST /website/blog/1/_update
{
  "doc" : {
     "tags" : [ "testing" ],
     "views": 0
  }
}
            
            
1. 集群支持的选项
curl -XGET "http://172.0.0.52:9200/_cat"
2. 查看节点信息
curl -XGET "http://172.0.0.52:9200/_cat/nodes?v"
3. 查看master节点信息
curl -XGET "http://172.0.0.52:9200/_cat/master?v"
4.查看所有节点上的热点线程
curl -XGET "http://172.0.0.52:9200/_nodes/hot_threads"
5.查看有问题的分片或索引
curl -XGET "http://172.0.0.52:9200/_cluster/allocation/explain?pretty"
6.查看线程池设置
curl -XGET "http://172.0.0.52:9200/_nodes/thread_pool/"
7.统计全部信息
curl -XGET "http://172.0.0.52:9200/_cluster/stats?human&pretty"
8.查看集群状态
curl -XGET "http://172.0.0.52:9200/_cluster/health?pretty"
9.查看ES信息
curl -XGET "http://172.0.0.52:9200/"
10.获取所有索引的信息
curl -XGET "http://172.0.0.52:9200/_cat/indices?v&pretty"
curl -XGET "http://172.0.0.52:9200/_cat/nodes?v"
curl -XGET "http://172.0.0.52:9200/_cat/segments?v&h=shard,segment,size,size.memory"
11.获取所有文档数量
curl -XGET "http://172.0.0.52:9200/_count?pretty" -H 'Content-Type: application/json' -d'
{

    "query": {

        "match_all": {}

    }

}'
12. 查看集群的健康状态
green:所有功能都是完好的;
yellow:所有数据是可用的,但是一些副本还没有被分配;
red代表一些数据由于某些原因已经不可用。
注意,尽管一个集群是red状态,它仍然可以提供部分服务(比如,它会继续从可用的切片数据里搜索),但是在失去部分数据后,需要尽快去修复。
curl -XGET "http://172.0.0.52:9200/_cat/health?v"
13. 创建索引
test_one 索引名
pretty 参数表示输出格式良好的JSON响应(如果存在)
curl -XPUT "http://172.0.0.52:9200/test_one?pretty"
14. 查看索引列表
curl -XGET "http://172.0.0.52:9200/_cat/indices?v"
curl -XGET "http://172.0.0.52:9200/test_one"
15. 删除索引
根据索引名称删除
curl -XDELETE "http://172.0.0.52:9200/test_one?pretty"
可以一次删除多个索引(以逗号间隔)删除所有索引_all或通配符 *
16. 创建文档
向es中插入文档的时候,必须要指定一个类型(type)
16.1.使用PUT来创建文档,需要指定id
索引 index:test_one
类型 type:test_type
_id:1
curl -XPUT "http://172.0.0.52:9200/test_one/test_type/1?pretty" -H 'Content-Type: application/json' -d'
{"name": "ghl", "age": 24, "sex": "male"}'
16.2. 使用POST来创建文档,可以不指定id(不指定时随机生成id)
curl -XPOST "http://172.0.0.52:9200/test_one/test_type?pretty" -H 'Content-Type: application/json' -d'
{"name": "Jack"}'
17. 查看文档
curl -XGET "http://172.0.0.52:9200/test_one/test_type/1?pretty"
18. 替换文档
使用PUT并指定id时,es会使用新的文档替换原文档
curl -XPUT "http://172.0.0.52:9200/test_one/test_type/1?pretty" -H 'Content-Type: application/json' -d'
{"name": "Jack"}'
19. 更新文档
curl -XPOST "http://172.0.0.52:9200/test_one/test_type/1/_update?pretty" -H 'Content-Type: application/json' -d'
{"doc":{"name": "Lucy"}}'
20. 删除文档
curl -XDELETE "http://172.0.0.52:9200/test_one/test_type/1?pretty"
21. 索引的增删改查有一个类似的格式下:
curl -XGET "http://172.0.0.52:9200/<Index>/<Type>/<ID>"
REST Verb:REST风格的语法谓词;
Node:节点ip;
port:节点端口号,默认9200;
Index:索引名;
Type:索引类型;
ID:操作对象的ID号;
22. 判断索引是否存在
curl -XHEAD "http://172.0.0.52:9200/test_one"
23. 查看索引模板
curl -XGET "http://172.0.0.52:9200/_template/template_1"
curl -XGET "http://172.0.0.52:9200/_template/temp*"
curl -XGET "http://172.0.0.52:9200/_template/template_1,template_2"
curl -XGET "http://172.0.0.52:9200/_template"
24. 删除模板
curl -XDELETE "http://172.0.0.52:9200/_template/template_1"
25. 打开/关闭索引
curl -XPOST "http://172.0.0.52:9200/test_one/_close"
curl -XPOST "http://172.0.0.52:9200/test_one/_open"
curl -XGET "http://172.0.0.52:9200/_cat/indices?v"
26.查看索引状态信息
curl -XGET "http://172.0.0.52:9200/_stats"
curl -XGET "http://172.0.0.52:9200/logstash-nginx-access-2019.08.07,test_one/_stats"
27.查看索引段信息
curl -XGET "http://172.0.0.52:9200/test_one/_segments"
curl -XGET "http://172.0.0.52:9200/logstash-nginx-access-2019.08.07,test_one/_segments"
curl -XGET "http://172.0.0.52:9200/_segments"            
            
            
            
            
            
            
            
            
            
            
            

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

zhou_bo88

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值