4、ElasticSearch restful实操

rest接口

 

 

es 创建索引库

 

/test/user/1      索引库/类型/文档                  即一条记录

curl  -XPUT  'http://192.168.81.131:9200/test/'               # PUT \POST都可以
{"acknowledged":true,"shards_acknowledged":true,"index":"test"}
curl  -H "Content-Type: application/json" -XPOST  'http://192.168.81.131:9200/test/user/1' -d'{"name":"jack","age":28}'
{"_index":"test","_type":"user","_id":"1","_version":1,"result":"created","_shards":{"total":2,"successful":1,"failed":0},"_seq_no":0,"_primary_term":1}
put是幂等方法,用于更新
post不是    ,用于新增操作
put,delete(幂等性)

 注意事项

1、索引库名称必须要全部小写,不能以下划线开头,也不能包含逗号
2、如果没有明确指定索引数据的ID,那么es会自动生成一个随机的ID,需要使用post参数
3、创建全新内容的两种方式:
    使用自增id(post)
    在url后面添加参数
curl -H 'xx' -XPOST http://192.168.81.131:9200/test/user/2  -d'{"name":"xx"}' 
curl -H 'xx' -XPUT http://192.168.81.131:9200/test/user/2?op_type=create -d'{"name":"xx"}'
curl -H 'XX' -XPUT http://192.168.81.131:9200/test/user/3/_create -d'{"name":"xx"}'

 

 

es 查询所有get
1、根据ID查询
  curl -XGET 'http://192.168.81.131:9200/test/user/1'

2、在任意的查询字符串中添加pretty参数,es可以得到易于识别的json结果
  curl -XGET 'http://192.168.81.131:9200/test/user/3?_source=name&pretty'

3、根据条件进行查询
  curl -XGET 'http://192.168.81.131:9200/test/user/_search?q=name:jack&pretty=true'
  curl -XGET 'http://192.168.81.131:9200/test/user/_search?q=name:jack&pretty'
es dsl查询
Domain Specific  Language领域特定语言
 
curl -H '
Content-Type: application/json' -XPUT http://192.168.81.131:9200/test/user/3/_create -d'{"name":"xx"}'
curl -H 'Content-Type: application/json' -XGET http://192.168.81.131:9200/test/user/_search-d'{"query":{"match":{"name":"jack"}}}'

 

es mget查询

 

获取多条文档  (在同一个index,type中)
curl -H 'Content-Type: application/json' -XGET http://192.168.81.131:9200/_mget?pretty 
-d'{"docs":[{"_index":"test","_type":"user","_id":2,"_source":"name"},]}' # 指定字段

如果需要的文档在同一个_index或者同一个_type中,你就可以在URL中指定一个默认的/_index或者/_index/_type
            /test/user/_mget?pretty -d'{"docs":[{"_id":1},{"_id":2}]}'

如果所有的文档拥有相同的_index 以及_type,直接在请求中添加ids的数组即可
           
/test/user/_mget?pretty -d'{"ids":["1","2"]}'

 

es head使用

 

知识想检查一下文档是否存在

可以用head来代替get方法,这样只会返回http头文件

curl -i -XHEAD  'http://192.168.81.131:9200/test/user/1'
HTTP/1.1 200 OK
content-type: application/json; charset=UTF-8
content-length: 145
HTTP/1.1 404 Not Found
content-type: application/json; charset=UTF-8
content-length: 57

 

es 更新操作

 

 

全部替换
curl -H 'Content-Type: application/json' -XPOST 'http://192.168.81.131:9200/test/user/KbCHT2sBh4SzoEBjbZiu'
                          -d'{"docs":{"name":"bady","age":27}}'
局部更新,必须使用POST
curl -H 'Content-Type: application/json' -XPOST 'http://192.168.81.131:9200/test/user/1/_update?version=2'  -d'{"docs":{"name":"bady","age":27}}'

  

es 删除操作
curl -XDELETE 'http://192.168.81.131:9200/test/user/1'

 

 es批量操作  bulk

 

 

curl -H 'Content-Type: application/json' -XPOST 'http://192.168.81.131:9200/_bulk' --data-binary @requests; 
{"index":{"_index":"test","_type":"user","_id":1}}
{"name":"mayun","age":21}                # 替换原有数据
{"update":{"_index":"test","_type":"user","_id":6}}
{"doc":{"age":52}}              # 局部更新

 

 乐观锁
读写锁


 

posted @ 2019-06-13 12:51  慕沁  阅读(216)  评论(0)    收藏  举报