Skip to content
Xiaoyu's Blog
Go back

ElasticSearch curl命令

😀
又一次在工作中使用到了elasticserach,在此记录一下常用的curl命令。curl elasticsearch api,可以快速帮我们验证问题和检查数据/索引是否正确。

CURL命令:

-X :指定http的请求方式,有HEAD、GET、POST、PUT、DELETE

-d :指定要传输的数据

-H :指定http的请求头信息

其中,-d传输的数据是json格式的,需要指定 -H或者--header 'Content-Type: application/json' ,并且 json数据在命令行环境下不支持换行,所以建议是在json.cn上把数据压缩成一行再执行命令

📝 索引相关

1.新建索引

curl -u user:password -XPUT 'http://ip:port/索引名?pretty'

2.索引查询

2.1 查询所有索引

curl -u user:password "http://ip:port/_cat/indices?v"

2.2 查询索引结构

curl -u user:password 'http://ip:port/索引名/_mapping?pretty=true'

2.2 查询索引全部数据

curl -u user:password 'http://ip:port/索引名/_search?pretty=true'

3.修改索引

3.1 索引结构增加字段

curl -u username:password --header 'Content-Type: application/json' -XPUT 'http://ip:port/索引名/_mapping' -d '{"properties": {"yourField": {"type": "fieldType"}}}'

4.查询数据

4.1 条件查询

curl -u mirbox:889c2940309edcc330c0623285195c4f -XGET 'http://ip:port/索引名/_search" -d'
{
"query": { "match_all": {} },
"_source": ["account_number", "balance"],
"sort": { "balance": { "order": "desc" },
"from": 10,
"size": 10
}
'

其中

query:里面为查询条件此处为全部,不做限制,

_source:为要显示的那些字段

sort:为排序字段 from为从第10条开始,size:取10条

例如:

测试环境,查询所有status = 1 的数据
curl  --header 'Content-Type: application/json' -u user:password -XGET "http://ip:port/索引名/_search?pretty" -d'{"query":{"match":{"status":1}}}'
 
测试环境,查询所有status字段不存在的数据
curl  --header 'Content-Type: application/json' -u user:password -XGET "http://ip:port/索引名/_search?pretty" -d'{"query":{"bool":{"must_not":[{"exists":{"field":"status"}}]}}}'


Share this post:

Previous Post
上传自己的项目到maven中央仓库
Next Post
一句鸡汤文