最近项目碰到需求,用curl给es发指令,看看返回结果,记录部分如下。
使用sql对es进行查询
curl
-X POST "http://127.0.0.1:9201/_sql?format=json"
-H 'Content-Type: application/json; charset=UTF-8'
-H 'Authorization: Basic sadhsakjdfhsakjfhkjsadas'
-d '{
"query":
"
select count(1)
from \"test1\"
where id > 3000 and age >= 6000
"
}'
curl
-X POST "http://10.5.189.226:9201/_sql?format=json"
-H 'Content-Type: application/json; charset=UTF-8'
-H 'Authorization: Basic ZGF0YWJhbms6RG52aXNlQCE3'
-d '{
"query":
"
select count(1)
from (
select *
from \"test\"
where age > 30
) t
where age > 2000
"
}'
注意:经过多次测试和百度,es sql中,表名是需要使用引号括起来的,但是子查询不需要。
查看索引信息
curl
-X GET "http://127.0.0.1:9202/test?pretty"
-H 'Content-Type: application/json; charset=UTF-8'
-H 'Authorization: Basic dsfgtsdfsdfddfsadsadasdd'
删除索引全部数据
# 删除 test索引的全部数据
curl
-X POST "http://127.0.0.1:9202/test/_delete_by_query"
-H 'Content-Type: application/json; charset=UTF-8'
-H 'Authorization: Basic asafdsfdsfgdsgfddfgdfgfd'
-d '{
"query": {
"match_all": {}
}
}'
删除索引
curl
-X DELETE "http://127.0.0.1:9202/test"
-H 'Content-Type: application/json; charset=UTF-8'
-H 'Authorization: Basic sdfgsdfsdfdsfsdfdsfsdfds'
新增索引
curl
-H 'Content-Type: application/json; charset=UTF-8'
-X POST "https://127.0.0.1:9202/test?pretty"
-H 'Content-Type: application/json'
注意事项
-H 'Authorization: Basic sadhsakjdfhsakjfhkjsadas'
这个是用来登录验证的。
为了看起来方便,所以我加了回车,实际不应该有回车的。