ElasticSearch下的REST接口

来源:互联网 发布:dvd菜单制作软件 编辑:程序博客网 时间:2024/05/11 18:41

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


curl -XDELETE 'http://172.16.1.16:9200/logstash-2013.03.*' 


健康度:


get http://nj02-rp-6yuebu-recommend-personal31.nj02:8200/_cat/health?v
epoch      timestamp cluster               status node.total node.data shards pri relo init unassign 
1433854015 20:46:55  elasticsearch-sandbox green           4         4     44  22    0    0        0 


获得节集群中的节点列表:
get http://nj02-rp-6yuebu-recommend-personal31.nj02:8200/_cat/nodes?v
host                                               ip            heap.percent ram.percent load node.role master name          
nj02-rp-6yuebu-recommend-personal30.nj02.baidu.com 10.205.132.43            2                  d         *      es-personal30 
nj02-rp-6yuebu-recommend-personal32.nj02.baidu.com 10.205.132.45            2                  d         m      es-personal32 
nj02-rp-6yuebu-recommend-personal31.nj02.baidu.com 10.205.132.44            1                  d         m      es-personal31 
nj02-rp-6yuebu-recommend-personal33.nj02.baidu.com 10.205.132.46            1                  d         m      es-personal33 


列出所有的索引:
get http://nj02-rp-6yuebu-recommend-personal31.nj02:8200/_cat/indices?v
health index        pri rep docs.count docs.deleted store.size pri.store.size 
green  15             5   1      10000            0     56.7mb         28.2mb 
green  es_api_proxy   5   1          2            0     11.6kb          5.8kb 
green  2              1   1     331775       268050    722.8mb        361.4mb 
green  3              1   1      87744         4780    132.3mb         64.1mb 
green  42             5   1      10000            0       57mb         28.5mb 
green  41             5   1      10000            0     55.7mb         27.9mb  
解释:
es_api_proxy 索引有5个主分片,有一份复制,包含2个文件;


创建索引:
post http://nj02-rp-6yuebu-recommend-personal31.nj02:8200/my_index?pretty
创建my_index索引
返回
{
    "acknowledged": true
}


索引中添加文档或者替换文档:需指明哪个索引、哪个类型type下,customer索引、“external”类型中,这个文档的ID是1
post http://nj02-rp-6yuebu-recommend-personal31.nj02:8200/my_index/external/1?pretty
{"name": "John Doe"}
返回:
{
    "_index": "my_index",
    "_type": "external",
    "_id": "1",
    "_version": 1,
    "created": true
}




索引中更新文档:
post http://nj02-rp-6yuebu-recommend-personal31.nj02:8200/my_index/external/1/_update?pretty
{
"doc": { "name": "Jane Doe", "age": 7 }
}
****
post http://nj02-rp-6yuebu-recommend-personal31.nj02:8200/my_index/external/1/_update?pretty
{
"doc": { "age": 27 }
}


从索引中查询文件:
get http://nj02-rp-6yuebu-recommend-personal31.nj02:8200/my_index/external/1?pretty
返回:
{
    "_index": "my_index",
    "_type": "external",
    "_id": "1",
    "_version": 1,
    "found": true,
    "_source": {
        "name": "John Doe"
    }
}


删除索引:
delete http://nj02-rp-6yuebu-recommend-personal31.nj02:8200/my_index?pretty
返回:
{
  "acknowledged" : true
}




curl -<REST Verb> <Node>:<Port>/<Index>/<Type><ID>


在索引的时候,ID部分是可选的。添加的时候可选,查询的时候必选?
如果不指定,Elasticsearch将产生一个随机的ID来索引这个文档。
Elasticsearch生成的ID会作为索引API调用的一部分被返回。
post http://nj02-rp-6yuebu-recommend-personal31.nj02:8200/my_index/external?pretty
{"name": "John Doe"}
返回:
{
    "_index": "my_index",
    "_type": "external",
    "_id": "dCExdE07RcCgHHIkZwxvtQ",
    "_version": 1,
    "created": true
}




文件批量上传,
accounts.json文件内容:
{"index":{"_id":"1"}}
{"account_number":1,"balance":39225,.....
{"index":{"_id":"6"}}
{"account_number":6,"balance":5686,......
curl -XPOST 'nj02-rp-6yuebu-recommend-personal31.nj02:8200/bank/account/_bulk?pretty' --data-binary @accounts.json
index为bank,type为account,id在文件中


查询
get http://nj02-rp-6yuebu-recommend-personal31.nj02:8200/my_index/_search?q=*&pretty=


请求体查询
使用请求体方法的等价搜索是:
curl -XPOST 'localhost:9200/bank/_search?pretty' -d '{"query": { "match_all": {} }}'


Elasticsearch提供一种JSON风格的特定领域语言,利用它你可以执行查询。
这杯称为查询DSL。这个查询语言相当全面,第一眼看上去可能有些咄咄逼人,但是最好的学习方法就是以几个基础的例子来开始。
{
"query": { "match_all": {} },
{
"query": { "match_all": {} },
"from": 10,
"size": 1
}


{
"query": { "match_all": {} },
"sort": { "balance": { "order": "desc" } }
}
下面这个例子返回账户编号为20的文档:
    
        curl -XPOST 'localhost:9200/bank/_search?pretty' -d '
        {
          "query": { "match": { "account_number": 20 } }
        }'
下面这个例子返回地址中包含“mill”的所有账户:
    
        curl -XPOST 'localhost:9200/bank/_search?pretty' -d '
        {
          "query": { "match": { "address": "mill" } }
        }'
下面这个例子返回地址中包含“mill”或者包含“lane”的账户:
    
       curl -XPOST 'localhost:9200/bank/_search?pretty' -d '
        {
          "query": { "match": { "address": "mill lane" } }
        }' 
下面这个例子是match的变体(match_phrase),它会去匹配短语“mill lane”:
    
        curl -XPOST 'localhost:9200/bank/_search?pretty' -d '
        {
          "query": { "match_phrase": { "address": "mill lane" } }
        }'
现在这个例子组合了两个match查询,这个组合查询返回包含“mill”和“lane”的所有的账户:
    
        curl -XPOST 'localhost:9200/bank/_search?pretty' -d '
        {
          "query": {
            "bool": {
              "must": [
                { "match": { "address": "mill" } },
                { "match": { "address": "lane" } }
              ]
            }
          }
        }'
相反的,下面的例子组合了两个match查询,它返回的是地址中包含“mill”或者“lane”的所有的账户:
    
        curl -XPOST 'localhost:9200/bank/_search?pretty' -d '
        {
          "query": {
            "bool": {
              "should": [
                { "match": { "address": "mill" } },
                { "match": { "address": "lane" } }
              ]
            }
          }
        }'
我们可以在一个bool查询里一起使用must、should、must_not。此外,我们可以将bool查询放到这样的bool语句中来模拟复杂的、多等级的布尔逻辑。


执行过滤器
这个例子使用一个被过滤的查询,其返回值是越在20000到30000之间(闭区间)的账户。换句话说,我们想要找到越大于等于20000并且小于等于30000的账户。
    
        curl -XPOST 'localhost:9200/bank/_search?pretty' -d '
        {
          "query": {
            "filtered": {
              "query": { "match_all": {} },
              "filter": {
                "range": {
                  "balance": {
                    "gte": 20000,
                    "lte": 30000
                  }
                }
              }
            }
          }
        }'
        
执行聚合


over








0 0
原创粉丝点击