1.curl格式
curl -X<REST Verb> <Node>:<Port>/<Index>/<Type>/<ID>
<REST Verb>:REST风格的语法谓词
<Node>:节点ip
<port>:节点端口号,默认9200
<Index>:索引名
<Type>:索引类型
<ID>:操作对象的ID号
2.Example
查看集群是否健康
curl 'localhost:9200/_cat/health?v'
绿色表示一切正常, 黄色表示所有的数据可用但是部分副本还没有分配,红色表示部分数据因为某些原因不可用
新建一个索引
curl -XPUT 'localhost:9200/customer?pretty'
查看所有索引
curl 'localhost:9200/_cat/indices?v'
删除一个索引
curl -XDELETE 'localhost:9200/customer?pretty'
插入索引数据
curl -XPUT 'localhost:9200/customer/external/1?pretty' -d '
{
"name": "John Doe"
}'
查看索引数据
curl -XGET 'localhost:9200/customer/external/1?pretty'
更新数据
curl -XPOST 'localhost:9200/customer/external/1/_update?pretty' -d '
{
"doc": { "name": "Jane Doe", "age": 20 }
}'
curl -XPOST 'localhost:9200/customer/external/_bulk?pretty' -d '
{"update":{"_id":"1"}}
{"doc": { "name": "John Doe becomes Jane Doe" } }
{"delete":{"_id":"2"}}
'
插入json数据
curl -XPOST 'localhost:9200/bank/account/_bulk?pretty' --data-binary "@accounts.json"
accounts.json是json文件。
查看文件内容
//使用关键字_mget,可以一次获取多个文档,而且这些文档可以跨索引、跨类型。
curl 'localhost:9200/_mget?pretty' -d '
{
"docs":[{"_index":"bank","type":"account","_id":208},{"_index":"ik_test_index","type":"external","_id":1}]
}'
//返回所有bank中的索引数据。其中 q=* 表示匹配索引中所有的数据。
curl 'localhost:9200/bank/_search?q=*&pretty'
//返回一条数据
curl -XPOST 'localhost:9200/bank/_search?pretty' -d '
{"query":{"match_all":{}},"size":1}'
//10条数据
curl -XPOST 'localhost:9200/bank/_search?pretty' -d '
{
"query": { "match_all": {} },
"from": 10,
"size": 10
}'
//匹配数据"account_number": 20
curl -XPOST 'localhost:9200/bank/_search?pretty' -d '
{
"query": { "match": { "account_number": 20 } }
}'
//聚合
curl -XPOST 'localhost:9200/bank/_search?pretty' -d '
{
"size": 0,
"aggs": {
"group_by_state": {
"terms": {
"field": "state"
}
}
}
}'
//过滤
curl -XPOST 'localhost:9200/bank/_search?pretty' -d '
{
"query": {
"bool": {
"must": { "match_all": {} },
"filter": {
"range": {
"balance": {
"gte": 20000,
"lte": 30000
}
}
}
}
}
}'
参考博客:
http://blog.csdn.net/lisonglisonglisong/article/details/50728256
https://www.cnblogs.com/pilihaotian/p/5830754.html