ElasticSearch之CURL操作

2018-11-29  本文已影响0人  simonsgj
curl -X指定http请求的方法(如HEAD GET POST PUT DELETE)-d  '指定要传输的数据'

例子:

建立索引库company(PUT和POST都可以,索引库名必须小写):

curl -XPUT 'http://localhost:9200/company'

索引库名称必须要全部小写,不能以下划线开头,也不能包含逗号

创建索引,其中employee是type,1是document,-d是指定要传输的数据(遵循JSON格式):

curl -XPOST http://localhost:9200/company/employee/1 -d 
'{
"first_name" : "John",
"last_name" : "Smith",
"age" : 25,
"about" : "I love to go rock climbing",
"interests": [ "sports", "music" ]
}'

创建索引

a.put创建
curl -XPUT http://localhost:9200/shb01/student/1 -d '{"name":"jack","age":30,"info":"hello elasticsearch"}'

返回:{"_index":"shb01","_type":"student","_id":"1","_version":1,"created":true}
执行put后有返回值
_index索引名称
_type类型名
_version版本号
created:true表示是新创建的。
上面的命令每执行一次version就会加1,-XPUT必须制定id。

b.post创建
curl -XPOST http://localhost:9200/shb01/student -d'{"name":"tom","age":21,"info":"tom"}'

返回:{"_index":"shb01","_type":"student","_id":"AVadzuNgxskBS1Rg2tdp","_version":1,"created":true}

http://127.0.0.1:9200/_cat/health?v  #查看es集群状态 
http://127.0.0.1:9200/_cat/nodes?v #集群节点健康查看
curl -XGET '127.0.0.1:9200/_cat/indices?v&pretty' #查询所有索引,pretty:格式化

查询返回最近10条

curl -XPOST 'localhost:9200/logstash-zhifubao-2018.09.18/_search?pretty' -d '{"query": { "match_all": {} },"from": 10,"size": 10}'

查询索引状态

curl -XGET http://localhost:9200/logstash-zhifubao-2018.08.15/_status

查询某一个索引

curl -XGET http://localhost:9200/logstash-zhifubao-2018.08.15/message/0ea1b2df-caa4-457c-8cc1-294f5e9284c7/_search?pretty

根据business_no查询,多个条件用逗号拼接

curl -XGET http://localhost:9200/logstash-zhifubao-2018.08.15/_search?q=message:0ea1b2df-caa4-457c-8cc1-294f5e9284c7

根据business_no查询,只返回特定字段

curl -XGET http://localhost:9200/logstash-zhifubao-2018.08.15/_search?q=message:0ea1b2df-caa4-457c-8cc1-294f5e9284c7?_source=message

查询集群状态

Curl –XGET http://localhost:9200/_cluster/health?pretty
http://localhost:9200/_cluster/health?pretty

多索引,多类型查询,分页查询,超时

Curl:curl -XGET http://localhost:9200/shb01,shb02/stu,tea/_search?pretty
curl -XGET http://localhost:9200/_all/stu,tea/_search?pretty

分页

curl -XGET http://localhost:9200/shb01/stu/_search?size=2&from=0

更新部分字段

crul –XPUT http:localhost:9200/shb01/student/1/_update?version=1
–d ‘{“doc”:{“name”:”updatename”}

根据id删除

curl -XDELETE http://localhost:9200/shb01/student/AVad05EExskBS1Rg2tdq

删除所有的索引库中名称为tom的文档

curl -XDELETE http://localhost:9200/_all/_query?q=name:tom

批处理

{"index":{"_index":"shb01","_type":"student","_id":"1"}}
{"name":"st01","age":"10","info":"st01"}
{"create":{"_index":"shb100","_type":"student","_id":"2"}}
{"name":"tea01","age":"10","info":"tea01"}
{"delete":{"_index":"shb01","_type":"student","_id":"AVadzuNgxskBS1Rg2tdp"}
{"update":{"_index":"shb02","_type":"tea","_id":"1"}}
{"doc":{"name":"zszszszs"}}
curl -XPUThttp://localhost:9200/_bulk --data-binary @/usr/local/t

_cluster系列

查询设置集群状态

curl  -XGET localhost:9200/_cluster/health?pretty=true
curl -XGET localhost:9200/_cluster/stats?pretty=true  # 显示集群系统信息,包括CPU JVM等等 
curl -XGET localhost:9200/_cluster/pending_tasks?pretty=true  #获取集群堆积的任务

pretty=true表示格式化输出
level=indices 表示显示索引状态
level=shards 表示显示分片信息

索引参数相关

URL 说明

集群参数相关

URL 说明

Nodes参数相关

URL 说明

以上这些操作和可以通过如

上一篇 下一篇

猜你喜欢

热点阅读