zoukankan      html  css  js  c++  java
  • 执行聚合

    source:https://www.elastic.co/guide/en/elasticsearch/reference/6.4/getting-started-aggregations.html

    聚合操作提供了对数据进行分组和提取统计的能力。简单的理解就是跟SQL中group by的操作一样的。

    在es中,你可以在一个查询中同时进行查询命中和聚合操作返回两种结果。

    这种一次简单的查询同时获取到两种结果的操作非常的强大且高效,

    下面的历史是对status进行聚合,然后返回前10名聚合后的数据倒序(默认):

    curl -X GET "localhost:9200/bank/_search?pretty" -H 'Content-Type: application/json' -d'
    {
    "size": 0,
    "aggs": {
    "group_by_state": {
    "terms": {
    "field": "state.keyword"
    }
    }
    }
    }
    '

    上面的操作类似于sql的:

    SELECT state, COUNT(*) FROM bank GROUP BY state ORDER BY COUNT(*) DESC LIMIT 10;

     返回如下:

    {
      "took": 29,
      "timed_out": false,
      "_shards": {
        "total": 5,
        "successful": 5,
        "skipped" : 0,
        "failed": 0
      },
      "hits" : {
        "total" : 1000,
        "max_score" : 0.0,
        "hits" : [ ]
      },
      "aggregations" : {
        "group_by_state" : {
          "doc_count_error_upper_bound": 20,
          "sum_other_doc_count": 770,
          "buckets" : [ {
            "key" : "ID",
            "doc_count" : 27
          }, {
            "key" : "TX",
            "doc_count" : 27
          }, {
            "key" : "AL",
            "doc_count" : 25
          }, {
            "key" : "MD",
            "doc_count" : 25
          }, {
            "key" : "TN",
            "doc_count" : 23
          }, {
            "key" : "MA",
            "doc_count" : 21
          }, {
            "key" : "NC",
            "doc_count" : 21
          }, {
            "key" : "ND",
            "doc_count" : 21
          }, {
            "key" : "ME",
            "doc_count" : 20
          }, {
            "key" : "MO",
            "doc_count" : 20
          } ]
        }
      }
    }

    我们可以看到有ID这个key有27条数据,tx这个也有27条,AL这个有25条,以此类推

    注意我们设置了size=0,因为我们只想返回聚合操作的数据。

    基于前面的聚合查询,下面这个是计算每个state对应balance的平均值(依旧是返回倒序的前十条)

    curl -X GET "localhost:9200/bank/_search?pretty" -H 'Content-Type: application/json' -d'
    {
    "size": 0,
    "aggs": {
    "group_by_state": {
    "terms": {
    "field": "state.keyword"
    },
    "aggs": {
    "average_balance": {
    "avg": {
    "field": "balance"
    }
    }
    }
    }
    }
    }
    '

    注意我们在group_by_state这个聚合中嵌套了average_balace聚合。这种是所有聚合查询的通用模式:

    你可以在聚合中任意嵌套聚合,达到你的查询目的。

    基于前面的聚合查询,我们现在按照平均balance降序排序:

    curl -X GET "localhost:9200/bank/_search?pretty" -H 'Content-Type: application/json' -d'

    {
    "size": 0,
    "aggs": {
    "group_by_state": {
    "terms": {
    "field": "state.keyword",
    "order": {
    "average_balance": "desc"
    }
    },
    "aggs": {
    "average_balance": {
    "avg": {
    "field": "balance"
    }
    }
    }
    }
    }
    }
    '

    这个示例展示了如何通过年龄区间来进行聚合,然后得到年龄区间每个性别的平均balance值

    curl -X GET "localhost:9200/bank/_search?pretty" -H 'Content-Type: application/json' -d'
    {
    "size": 0,
    "aggs": {
    "group_by_age": {
    "range": {
    "field": "age",
    "ranges": [
    {
    "from": 20,
    "to": 30
    },
    {
    "from": 30,
    "to": 40
    },
    {
    "from": 40,
    "to": 50
    }
    ]
    },
    "aggs": {
    "group_by_gender": {
    "terms": {
    "field": "gender.keyword"
    },
    "aggs": {
    "average_balance": {
    "avg": {
    "field": "balance"
    }
    }
    }
    }
    }
    }
    }
    }
    '

    还有很多别的聚合查询我们不一一列举了,如果想继续学习可以参考:https://www.elastic.co/guide/en/elasticsearch/reference/6.4/search-aggregations.html

    总结:

    es是一个简单且复杂的产品,我们到目前为止只学习了一些基础,如何深入学习,如何用过restAPI进行操作,希望这些文件可以给你一个好的参考,学习es的真谛和探索出它更好的功能。

  • 相关阅读:
    vpp l3 bvi
    set interface ip address: failed to add 1 on loop1 which conflicts with
    Failed: no source address for egress interface
    vpp vrf
    vpp bvi
    creates 2 connected namespaces vpp1 & vpp2
    unknown input `arp'
    vpp cmd
    vxlan bum
    Go流程结构(if)
  • 原文地址:https://www.cnblogs.com/supermanwx/p/11929272.html
Copyright © 2011-2022 走看看