curl operate elasticsearch

来源:互联网 发布:百视通网络电视要密码 编辑:程序博客网 时间:2024/06/16 07:56
系统自带变量名(如_index、_source)
{"_index":"dept","_type":"employee","_id":"1","_version":1,"_shards":{"total":2,"successful":1,"failed":0},"created":true}
{
  "took" : 5,
  "timed_out" : false,
  "_shards" : {
    "total" : 5,
    "successful" : 5,
    "failed" : 0
  },
  "hits" : {
    "total" : 4,
    "max_score" : 1.0,
    "hits" : [ {
      "_index" : "dept",
      "_type" : "employee",
      "_id" : "2",
      "_score" : 1.0,
      "_source":{"empname": "emp2","gender": "female"}
    }, {
      "_index" : "dept",
      "_type" : "employee",
      "_id" : "1",
      "_score" : 1.0,
      "_source":{"empname": "emp1","age": 25}
    } ]
  }
}

总体
# curl相当于curl -XGET或curl -X GET
查看版本:curl "http://localhost:9200"
查看索引情况:curl 'localhost:9200/_cat/indices?v'
查看健康情况:
(1)curl 'localhost:9200/_cat/health?v';
(2)curl "localhost:9200/_cluster/health?pretty"
(3)curl 'localhost:9200/_cat/nodes?v'

变更
# PUT/POST对应http put和http post,在Elasticsearch这里都可以
curl -XPUT 'http://localhost:9200/dept/employee/1' -d '{"empname": "emp1","age": 25}'
curl -XPUT 'http://localhost:9200/dept/employee/2' -d '{"empname": "emp2","gender": "female"}'
curl -XPOST 'http://localhost:9200/dept/employee/3' -d '{"empname": "emp3","gender": "male"}'
curl -X DELETE "http://localhost:9200/dept/employee/1"

Batch变更
# Sample:https://github.com/bly2k/files/blob/master/accounts.zip?raw=true
curl -XPOST 'localhost:9200/dept/accounts/_bulk?pretty' --data-binary @/mk/test/accounts.json
curl 'localhost:9200/_cat/indices?v'
curl 'localhost:9200/dept/accounts/2?pretty'

查看
# pretty排版json格式
curl "http://localhost:9200/dept/employee/1"

搜索
curl "http://localhost:9200/dept/employee/_search?pretty"
curl "http://localhost:9200/dept/employee/_search?pretty&_source=age"
curl "http://localhost:9200/dept/employee/_search?q=gender:*male&pretty"
简易搜索:curl 'centos1:9200/dept/employee/_search?search_type=count&pretty'
简易搜索:curl 'centos1:9200/dept/employee/_count?pretty'
- took —— Elasticsearch执行这个搜索的耗时,以毫秒为单位
- timed_out —— 指明这个搜索是否超时
- _shards —— 指出多少个分片被搜索了,同时也指出了成功/失败的被搜索的shards的数量
- hits —— 搜索结果
- hits.total —— 能够匹配我们查询标准的文档的总数目
- hits.hits —— 真正的搜索结果数据(默认只显示前10个文档)

ES-HEAD
curl -XHEAD "http://localhost:9200/dept/employee/1" (要先装elasticsearch-head插件,http://mobz.github.io/elasticsearch-head/)

使用请求体方法
curl -X GET "$elasticsearchwebaddress/megacorp/employee/_search" -d '
{
    "query" : {
        "filtered" : {
            "filter" : {
                "range" : {
                    "age" : { "gt" : 30 } 
                     }
                },
            "query" : {
                "match" : {
                    "last_name" : "smith" 
                      }
               }
          }
     }
}'
curl -XPOST 'localhost:9200/bank/_search?pretty' -d '
            {
              "query": { "match_all": {} }
            }'
聚集搜索:curl 'centos1:9200/dept/employee/_search?pretty' -d '
{
  "aggs": {
    "all_ages": {
      "terms": { "field": "age" }
    }
  }
} '
Null搜索:curl 'centos1:9200/dept/employee/_count?pretty' -d '
{
    "query" : {
        "constant_score" : {
            "filter" : {
                "bool": {
                    "must": {"exists": {"field": "age"}},
                    "must_not": {"term": {"<your_field_name_here>": ""}}
                }
            }
        }
    }
}
'
区间搜索:curl 'centos1:9200/dept/employee/_count?pretty' -d '
{
    "query": {
      "filtered": {
        "query": { "match_all": {} },
        "filter": {
          "range": {
            "age": {
              "gte": 25,
              "lt": 26
            }
          }
        }
      }
    }
  }
'

部分参考:

http://www.cnblogs.com/zhangchenliang/p/4193518.html

http://blog.csdn.net/cnweike/article/details/33736429

0 0