elasticsearch curl使用

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

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值