美文网首页
ElasticSearch6.6.0强大的REST API详解

ElasticSearch6.6.0强大的REST API详解

作者: 往后余生9375 | 来源:发表于2019-02-16 22:48 被阅读0次

    Elasticsearch提供了一个非常全面和强大的REST API,您可以使用它与集群进行交互。使用API​​可以完成的一些事项如下:

    • 检查群集,节点和索引运行状况,状态和统计信息
    • 管理您的群集,节点和索引数据和元数据
    • 对索引执行CRUD(创建,读取,更新和删除)和搜索操作
    • 执行高级搜索操作,例如分页,排序,过滤,脚本编写,聚合等等

    集群健康

    GET http://192.168.199.213:9200/_cat/health?v

    epoch      timestamp cluster       status node.total node.data shards pri relo init unassign pending_tasks max_task_wait_time active_shards_percent
    1550324800 13:46:40  elasticsearch green           1         1      0   0    0    0        0             0                  -                100.0%
    

    每当我们要求群集健康时,我们要么获得绿色,黄色或红色。
    绿色 - 一切都很好(集群功能齐全)
    黄色 - 所有数据都可用,但尚未分配一些副本(群集功能齐全)
    红色 - 某些数据由于某种原因不可用(群集部分功能)

    节点列表

    GET http://192.168.199.213:9200/_cat/nodes?v

    ip              heap.percent ram.percent cpu load_1m load_5m load_15m node.role master name
    192.168.199.213           15          93   2    0.03    0.04     0.05 mdi       *      swbeA8h
    

    列出所有索引

    GET http://192.168.199.213:9200/_cat/indices?v

     health status index uuid pri rep docs.count docs.deleted store.size pri.store.size
    

    创建索引

    PUT http://192.168.199.213:9200/goods?pretty

    {
        "acknowledged": true,
        "shards_acknowledged": true,
        "index": "goods"
    }
    

    此时查看所有索引

    health status index uuid                   pri rep docs.count docs.deleted store.size pri.store.size
    yellow open   goods 3fA1F6HfQ9aZKHqs9EFt3g   5   1          0            0      1.1kb          1.1kb
    
    

    添加一条数据

    PUT http://192.168.199.213:9200/goods/goods/1?pretty
    {
    "title":"防螨虫床上用品四件套纯棉床笠枕套被套床垫罩儿童防尘螨过敏床品"
    }

    {
        "_index": "goods",
        "_type": "goods",
        "_id": "1",
        "_version": 1,
        "result": "created",
        "_shards": {
            "total": 2,
            "successful": 1,
            "failed": 0
        },
        "_seq_no": 0,
        "_primary_term": 1
    }
    

    查询ID为1的数据

    GET http://192.168.199.213:9200/goods/goods/1?pretty

    {
        "_index": "goods",
        "_type": "goods",
        "_id": "1",
        "_version": 1,
        "_seq_no": 0,
        "_primary_term": 1,
        "found": true,
        "_source": {
            "title": "防螨虫床上用品四件套纯棉床笠枕套被套床垫罩儿童防尘螨过敏床品"
        }
    }
    

    删除索引

    DELETE http://192.168.199.213:9200/goods?pretty

    {
        "acknowledged": true
    }
    

    更新文档

    POST http://192.168.199.213:9200/goods/goods/1?pretty
    {
    "title":"防螨虫床上用品四件套纯棉床笠枕套被套床垫罩儿童防尘螨过敏床品",
    "price":"65.00"
    }

    {
        "_index": "goods",
        "_type": "goods",
        "_id": "1",
        "_version": 1,
        "result": "created",
        "_shards": {
            "total": 2,
            "successful": 1,
            "failed": 0
        },
        "_seq_no": 0,
        "_primary_term": 1
    }
    

    删除文档

    DELETE http://192.168.199.213:9200/goods/goods/1?pretty

    搜索

    GET http://192.168.199.213:9200/goods/_search?q=四件套&sort=字段名:asc&pretty

    {
        "took": 9,
        "timed_out": false,
        "_shards": {
            "total": 5,
            "successful": 5,
            "skipped": 0,
            "failed": 0
        },
        "hits": {
            "total": 1,
            "max_score": 1.027436,
            "hits": [
                {
                    "_index": "goods",
                    "_type": "goods",
                    "_id": "1",
                    "_score": 1.027436,
                    "_source": {
                        "title": "防螨虫床上用品四件套纯棉床笠枕套被套床垫罩儿童防尘螨过敏床品",
                        "price": "65.00"
                    }
                }
            ]
        }
    }
    

    官方推荐的搜搜方式

    GET http://192.168.199.213:9200/goods/_search
    {
    "query": { "match_all": {} },#查询方式
    "sort": [#排序方式
    { "account_number": "asc" }
    ],
    "from": 10,#此写法表示从第10条查询到底19条数据
    "size": 10,
    "_source":["title","price"]#设置只返回某几个字段
    }

    指定字段条件查询
    {
      "query": { "match": { "price": 65.00 } }
    }
    
    bool must查询

    必须所欲条件都满足

    {
      "query": {
        "bool": {
          "must": [
            { "match": { "address": "mill" } },
            { "match": { "address": "lane" } }
          ]
        }
      }
    }
    
    bool should查询

    任意满足一个

    {
      "query": {
        "bool": {
          "should": [
            { "match": { "address": "mill" } },
            { "match": { "address": "lane" } }
          ]
        }
      }
    }
    
    bool not must查询

    必须都不满足

    {
      "query": {
        "bool": {
          "must_not": [
            { "match": { "address": "mill" } },
            { "match": { "address": "lane" } }
          ]
        }
      }
    }
    

    bool 组合查询

    {
      "query": {
        "bool": {
          "must": [
            { "match": { "age": "40" } }
          ],
          "must_not": [
            { "match": { "state": "ID" } }
          ]
        }
      }
    }
    

    filter过滤

    此示例使用bool查询返回所有余额介于20000和30000之间的帐户。换句话说,我们希望找到余额大于或等于20000且小于或等于30000的帐户。

    {
      "query": {
        "bool": {
          "must": { "match_all": {} },
          "filter": {
            "range": {
              "balance": {
                "gte": 20000,
                "lte": 30000
              }
            }
          }
        }
      }
    }
    

    客户端使用

    https://www.elastic.co/guide/en/elasticsearch/client/java-api/6.6/index.html

    相关文章

      网友评论

          本文标题:ElasticSearch6.6.0强大的REST API详解

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