美文网首页
常用Elasticsearch API接口

常用Elasticsearch API接口

作者: sknfie | 来源:发表于2021-06-21 13:34 被阅读0次

    查看节点信息

    curl http://192.168.2.191:9200/_cat/nodes?v
    ip            heap.percent ram.percent cpu load_1m load_5m load_15m node.role  master name
    192.168.2.192           24          28   1    0.00    0.06     0.10 cdhilmrstw -      node-b
    192.168.2.191           32          25   1    0.00    0.08     0.10 cdhilmrstw *      node-a
    192.168.2.193           25          25   1    0.03    0.07     0.07 cdhilmrstw -      node-c
    

    查看索引信息

    curl http://192.168.2.191:9200/_cat/indices?v
    health status index uuid pri rep docs.count docs.deleted store.size pri.store.size
    默认没有索引信息
    

    新增索引

    curl -X PUT http://192.168.2.191:9200/nginx_access_log
    {"acknowledged":true,"shards_acknowledged":true,"index":"nginx_access_log"}
    

    查看新增的索引:

    curl http://192.168.2.191:9200/_cat/indices?v
    health status index            uuid                   pri rep docs.count docs.deleted store.size pri.store.size
    green  open   nginx_access_log m-F620aATl2fomepOZeR4g   1   1          0            0       416b           208b
    
    索引

    green:所有的主分片和副本分片都已分配。你的集群是100%可用的。
    yellow:所有的主分片已经分片了,但至少还有一个副本是缺失的。不会有数据丢失,所以搜索结果依然是完整的。
    不过,你的高可用性在某种程度上被弱化。如果 更多的 分片消失,你就会丢数据了。把 yellow 想象成一个需要及时调查的警告。
    red:至少一个主分片(以及它的全部副本)都在缺失中。这意味着你在缺少数据:搜索只能返回部分数据,而分配到这个分片上的写入请求会返回一个异常。

    删除索引

    curl -X DELETE http://192.168.2.191:9200/nginx_access_log
    {"acknowledged":true}
    

    导入数据

    1.下载待导入文件

    wget https://raw.githubusercontent.com/elastic/elasticsearch/master/docs/src/test/resources/accounts.json
    {"index":{"_id":"1"}}
    {"account_number":1,"balance":39225,"firstname":"Amber","lastname":"Duke","age":32,"gender":"M","address":"880 Holmes Lane","employer":"Pyrami","email":"amberduke@pyrami.com","city":"Brogan","state":"IL"}
    {"index":{"_id":"6"}}
    {"account_number":6,"balance":5686,"firstname":"Hattie","lastname":"Bond","age":36,"gender":"M","address":"671 Bristol Street","employer":"Netagy","email":"hattiebond@netagy.com","city":"Dante","state":"TN"}
    {"index":{"_id":"13"}}
    {"account_number":13,"balance":32838,"firstname":"Nanette","lastname":"Bates","age":28,"gender":"F","address":"789 Madison Street","employer":"Quility","email":"nanettebates@quility.com","city":"Nogal","state":"VA"}
    {"index":{"_id":"18"}}
    {"account_number":18,"balance":4180,"firstname":"Dale","lastname":"Adams","age":33,"gender":"M","address":"467 Hutchinson Court","employer":"Boink","email":"daleadams@boink.com","city":"Orick","state":"MD"}
    {"index":{"_id":"20"}}
    {"account_number":20,"balance":16418,"firstname":"Elinor","lastname":"Ratliff","age":36,"gender":"M","address":"282 Kings Place","employer":"Scentric","email":"elinorratliff@scentric.com","city":"Ribera","state":"WA"}
    {"index":{"_id":"25"}}
    {"account_number":25,"balance":40540,"firstname":"Virginia","lastname":"Ayala","age":39,"gender":"F","address":"171 Putnam Avenue","employer":"Filodyne","email":"virginiaayala@filodyne.com","city":"Nicholson","state":"PA"}
    {"index":{"_id":"32"}}
    {"account_number":32,"balance":48086,"firstname":"Dillard","lastname":"Mcpherson","age":34,"gender":"F","address":"702 Quentin Street","employer":"Quailcom","email":"dillardmcpherson@quailcom.com","city":"Veguita","state":"IN"}
    {"index":{"_id":"37"}}
    {"account_number":37,"balance":18612,"firstname":"Mcgee","lastname":"Mooney","age":39,"gender":"M","address":"826 Fillmore Place","employer":"Reversus","email":"mcgeemooney@reversus.com","city":"Tooleville","state":"OK"}
    

    3.导入到elasticsearch

    curl -H "Content-Type: application/json" -XPOST "192.168.2.191:9200/bank/_doc/_bulk?pretty&refresh" --data-binary "@accounts.json"
    

    3.查询确认

    curl http://192.168.2.191:9200/_cat/indices?v
    health status index uuid                   pri rep docs.count docs.deleted store.size pri.store.size
    green  open   bank  WlRqZi6ARdeMVa74e2iiVw   1   1       1000         1000      1.5mb        777.4kb
    

    4.查询bank索引的数据(使用查询字符串进行查询)

    curl -X GET "192.168.2.191:9200/bank/_search?q=*&sort=account_number:asc&pretty"
    说明:
    默认结果为10条
    search 属于一类API, 用于执行查询操作
    q=* ES批量索引中的所有文档
    sort=account_number: asc 表示根据account_number按升序对结果排序
    pretty调整显示格式
    

    5.查询bank索引的数据 (使用json格式进行查询)

    curl -X GET "192.168.2.191:9200/bank/_search?pretty" -H 'Content-Type:application/json' -d'
    {
    "query": { "match_all": {} },
    "sort": [
    { "account_number": "asc" }
    ]
    }'
    
    • 匹配所有文档match_all
      查询所有,默认只返回10个文档
    curl -X GET "192.168.2.191:9200/bank/_search?pretty" -H 'Content-Type:application/json' -d'
    {
    "query": { "match_all": {} }
    }'
    

    query告诉我们查询什么
    match_al l 是我们查询的类型
    match_al l 查询仅仅在指定的索引的所有文件进行搜索

    • 指定位置与查询条数from, size
      除了query参数外,还可以传递其他参数,比如sort或者size
      (1). 查询一条数据:
    curl -X GET "192.168.2.191:9200/bank/_search?pretty" -H 'Content-Type:application/json' -d'
    {
    "query": { "match_all": {} },
    "size": 1
    }'
    

    (2)指定位置与查询条数

    curl -X GET "192.168.2.191:9200/bank/_search?pretty" -H 'Content-Type:application/json' -d'
    {
    "query": { "match_all": {} },
    "sort": [
    { "account_number": "asc" }
    ] ,
    "from": 2,
    "size": 2
    }'
    

    from 0表示从第1个开始
    size 指定查询的个数

    • 查询account_number从第201 条到210条的数据
    curl -X GET "192.168.2.191:9200/bank/_search?pretty" -H 'Content-Type:application/json' -d'
    > {
    > "query": { "match_all": {} },
    > "sort": [
    > { "account_number": "asc" }
    > ] ,
    > "from": 200,
    > "size": 10
    > }' 2>/dev/nul l | grep account_number
              "account_number" : 200,
              "account_number" : 201,
              "account_number" : 202,
              "account_number" : 203,
              "account_number" : 204,
              "account_number" : 205,
              "account_number" : 206,
              "account_number" : 207,
              "account_number" : 208,
              "account_number" : 209,
    
    • 匹配查询字段,返回_source字段中的片段字段
    curl -X GET "192.168.2.191:9200/bank/_search?pretty" -H 'Content-Type:application/json' -d'
    {
    "query": { "match_all": {} },
    "_source": ["account_number", "balance"]
    }'
    
    • match基本搜索查询,针对特定字段或字段集合进行搜索
      查询编号为20的账户
    curl -X GET "192.168.2.191:9200/bank/_search?pretty" -H 'Content-Type:application/json' -d'
    {
    "query": { "match": { "account_number": 20}}
    }'
    

    返回地址中包含mill的账户

    curl -X GET "192.168.2.191:9200/bank/_search?pretty" -H 'Content-Type:application/json' -d'
    {
    "query": { "match": { "address": "mill"}}
    }'
    

    空格就是或的关系

    curl -X GET "192.168.2.191:9200/bank/_search?pretty" -H 'Content-Type:application/json' -d'
    {
    "query": { "match": { "address": "mill lane"}}
    }'
    
    • bool查询
      bool must 查询的字段必须同时存在
      查询包含mi l l 和l ane的所有账户
    curl -X GET "192.168.2.191:9200/bank/_search?pretty" -H 'Content-Type:application/json' -d'
    {
    "query": {
    "bool": {
    "must": [
    { "match": { "address": "mill" } },
    { "match": { "address": "lane" } }
    ]
    }
    }
    }'
    

    bool shoul d 查询的字段仅存在一即可
    查询包含mi l l 或l ane的所有账户

    curl -X GET "192.168.2.191:9200/bank/_search?pretty" -H 'Content-Type:application/json' -d'
    {
    "query": {
    "bool": {
    "should": [
    { "match": { "address": "mill " } },
    { "match": { "address": "lane" } }
    ]
    }
    }
    }'
    
    • range指定区间内的数字或者时间
      操作符: gt大于, gte大于等于, lt小于, lte小于等于
    curl -X GET "192.168.2.191:9200/bank/_search?pretty" -H 'Content-Type:application/json' -d'
    {
    "query": {
    "bool": {
    "must": { "match_all": {} },
    "filter": {
    "range": {
    "balance": {
    "gte": 20000,
    "lte": 30000
    }
    }
    }
    }
    }
    }'
    

    相关文章

      网友评论

          本文标题:常用Elasticsearch API接口

          本文链接:https://www.haomeiwen.com/subject/bqjyyltx.html