Elasticsearch CURL命令

1、查看集群状态

curl '10.18.37.223:9200/_cat/health?v'
绿色表示一切正常, 黄色表示所有的数据可用但是部分副本还没有分配,红色表示部分数据因为某些原因不可用

2、获取集群节点列表

curl '10.18.37.223:9200/_cat/nodes?v'

3、查看所有index

curl -X GET 'http://10.18.37.223:9200/_cat/indices?v'

4、查询所有的index包含其所有的type

curl '10.18.37.223:9200/_mapping?pretty=true'

5、查询某个index下的所有type

curl '10.18.37.223:9200/test/_mapping?pretty=true' 查询test下的所有type

6、查询某个index的所有数据

curl '10.18.37.223:9200/test/_search?pretty=true'

7、查询index下某个type类型的数据

curl '10.18.37.223:9200/test/test_topic/_search?pretty=true'
其中:根据规划,Elastic 6.x 版只允许每个 Index 包含一个 Type,7.x 版将会彻底移
除 Type, index=test type=test_topic 注意自己使用的版本

8、查询index下某个type下id确定的数据

curl '10.18.37.223:9200/test/test_topic/3525?pretty=true'
index = test type= test_topic id = 3525

9、和sql一样的查询数据

curl "10.18.37.223:9200/test/_search" -d'
{
"query": { "match_all": {} },
"_source": ["account_number", "balance"],
"sort": { "balance": { "order": "desc" },
"from": 10,
"size": 10
}
'
注:-d之后的内容使用回车输入,不能使用换行符,es不能识别
query:里面为查询条件此处为全部,不做限制,_source:为要显示的那些字段
sort:为排序字段 from为从第10条开始,size:取10条
除此之外还有:布尔匹配,or匹配。包含匹配。范围匹配。更多查询请去官网查看:
官网查询API地址

10、创建索引(index)

curl -X PUT '10.18.37.223:9200/test?pretty'
OR
curl -X PUT '10.18.37.223:9200/test'
创建一个名为test的索引
注:索引只能是小写,不能以下划线开头,也不能包含逗号
如果没有明确指定索引数据的ID,那么es会自动生成一个随机的ID,需要使用POST参数

11、往index里面插入数据

curl -X PUT '10.18.37.223:9200/test/test_zhang/1?pretty' -d '
{"name":"tom","age":18}'
往es中插入index=test,type=test_zhang id = 1的数据为
{"name":"tom","age":18}的数据。
-X POST也即可

12、修改数据

curl -X PUT '10.18.37.223:9200/test/test_zhang/1?pretty' -d '{"name":"pete","age":20}'
注:修改 index = test type=test_zhang id = 1 数据: {"name":"tom","age":18}
为{"name":"pete","age":20} 成功之后执行查看数据命令可看到最新数据,且
version 会增加一个版本

13、更新数据同时新增数据,在一个index,type中

curl -X POST '10.18.37.223:9200/test/test_zhang/1/_update?pretty' -d '{"doc":{"name":"Alice","age":18,"addr":"beijing"}}'
注:修改了名字,年龄,同时新增了字段addr=beijing

14、利用script更新数据

curl -X POST '10.18.37.223:9200/test/test_zhang/1/_update?pretty' -d '{"script": "ctx._source.age += 5"}'
注:将年龄加5
从ES 1.4.3以后, inline script默认是被禁止的
要打开, 需要在config/elasticsearch.yml中添加如下配置:
script.inline:true
script.indexed:true 然后重启 (如果是集群模式:需要每个节点都添加 然后重启)

15、删除记录

curl -X DELETE '10.18.37.223:9200/test/test_zhang/1'
注:删除index = test type = test_zhang id = 1 的数据

16、删除index

curl -X DELETE '10.18.37.223:9200/test'
删除index=test的数据

17、批量操作

curl -X POST '10.18.37.223:9200/test/test_zhang/_bulk?pretty' -d '
{"index":{"_id":"2"}}
{"name":"zhangsan","age":12}
{"index":{"_id":"3"}}
{"name":"lisi"}
'
注:在index = test type = test_zhang下
新增id= 2 和 id=3 的两条数据

curl -X POST '10.18.37.223:9200/test/test_zhang/_bulk?pretty' -d '
{"update":{"_id":"2"}}
{"doc":{"name":"wangwu"}}
{"delete":{"_id":"3"}}'
注: 修改id = 2 的数据 并且同时删除掉id=3的数据
在index = test type = test_zhang下

18、根据条件删除

curl -X POST "10.18.37.223:9200/test/_delete_by_query" -d'
{
"query": {
"match": {
"name": "pete"
}
}
}'
使用es的_delete_by_query,此插件在es2.0版本以后被移除掉,要使用此命令。
需要自己安装_delete_by_query插件:
在es安装目录下。bin目录下,执行:
./plugin install delete-by-query 安装插件
如果是集群模式,则每个节点都需要安装然后重启

原文地址:https://www.cnblogs.com/xiaodf/p/10623266.html