美文网首页
elasticsearch-4. 常用命令请求

elasticsearch-4. 常用命令请求

作者: donglq | 来源:发表于2018-04-30 01:02 被阅读0次

    常用命令请求

    1. 获取健康状况
    GET /_cat/health?v
    
    1. 获取集群中节点列表
    GET /_cat/nodes?v
    
    1. 获取索引列表
    GET /_cat/indices?v
    
    1. 创建名字为customer的索引
    PUT /customer?pretty
    
    1. 给customer索引存入数据
    PUT /customer/doc/1?pretty
    {
      "name": "John Doe"
    }
    

    Elasticsearch并不要求你在索引文档之前先显性的创建索引。在4、5步骤中,如果在执行步骤5时,customer索引还没有创建,Elasticsearch将会自动的创建这个索引

    1. 删除索引
    DELETE /customer?pretty
    

    访问数据命令模板为:<REST Verb> /<Index>/<Type>/<ID>

    1. 修改数据
    PUT /customer/doc/1?pretty
    {
      "name": "Jane Doe"
    }
    
    1. 非显性的指定id,elasticsearch会自动创建
    POST /customer/doc?pretty
    {
      "name": "Jane Doe"
    }
    
    1. 更新文档
    POST /customer/doc/1/_update?pretty
    {
      "doc": { "name": "Jane Doe" }
    }
    
    1. 更新并添加数据
    POST /customer/doc/1/_update?pretty
    {
      "doc": { "name": "Jane Doe", "age": 20 }
    }
    
    1. 删除文档
    DELETE /customer/doc/2?pretty
    
    1. 批量操作,使用_bulk
    • 索引两个文档
    POST /customer/doc/_bulk?pretty
    {"index":{"_id":"1"}}
    {"name": "John Doe" }
    {"index":{"_id":"2"}}
    {"name": "Jane Doe" }
    
    • 更新id=1的文档,删除id=2的文档
    POST /customer/doc/_bulk?pretty
    {"update":{"_id":"1"}}
    {"doc": { "name": "John Doe becomes Jane Doe" } }
    {"delete":{"_id":"2"}}
    
    
    1. 搜索,通过_search
    • 通过请求URL
    GET /bank/_search?q=*&sort=account_number:asc&pretty //返回bank索引的所有文档
    
    - q=* 匹配bank索引中的所有文档
    - sort=account_number:asc 使用每个文档中的account_number字段进行升序排序
    - pretty 告诉Elasticsearch返回 pretty-printed JSON 结果
    
    • 通过请求体
    GET /bank/_search
    {
      "query": { "match_all": {} },
      "sort": [
        { "account_number": "asc" }
      ]
    }
    
    • 结果解析
    {
      "took" : 63,
      "timed_out" : false,
      "_shards" : {
        "total" : 5,
        "successful" : 5,
        "skipped" : 0,
        "failed" : 0
      },
      "hits" : {
        "total" : 1000,
        "max_score" : null,
        "hits" : [ {
          "_index" : "bank",
          "_type" : "account",
          "_id" : "0",
          "sort": [0],
          "_score" : null,
          "_source" : {"account_number":0,"balance":16623,"firstname":"Bradshaw","lastname":"Mckenzie","age":29,"gender":"F","address":"244 Columbus Place","employer":"Euron","email":"bradshawmckenzie@euron.com","city":"Hobucken","state":"CO"}
        }, {
          "_index" : "bank",
          "_type" : "account",
          "_id" : "1",
          "sort": [1],
          "_score" : null,
          "_source" : {"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"}
        }, ...
        ]
      }
    }
    
    - took Elasticsearch查询耗费时间,单位毫秒
    - timed_out 查询是否超时
    - _shards 查询了多少个分片,和查询成功/跳过/失败的分片的数量
    - hits 查询结果
    - hits.total 匹配搜索条件的所有文档数
    - hits.hits 搜索结果的实际数组(默认前10个文档)
    - hits.sort 结果排序的key
    - hits._score 和 max_score 现在可以忽略的字段
    

    相关文章

      网友评论

          本文标题:elasticsearch-4. 常用命令请求

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