Elasticsearch 常用聚合(group by || sum || count)分组查询

1.elasticsearch 对指定字段分组,求出分组后每个参数的总金额

   例如:求出每个人的总金额

GET index_name/_search
{
  "query": {
    "match_all": {}
  },
  "aggs": {
    "group_by_name": {
      "terms": {
        "field": "name",
        "size": 10000,
        "order": {
          "amount": "desc"
        }
      },
      "aggs": {
        "amount": {
          "sum": {
            "field": "amount"
          }
        }
      }
    }
  }
}

  说明:group_by_name 是一个自定义的别名,terms 为指定分组字段。order根据总金额排序顺序

             name的参数值对应下面结果的key

  查询结果:

"aggregations": {
    "group_by_name": {
      "doc_count_error_upper_bound": 0,
      "sum_other_doc_count": 0,
      "buckets": [
        {
          "key": "张三",
          "doc_count": 22,
          "amount": {
            "value": 222978
          }
        },
        {
          "key": "李四",
          "doc_count": 10,
          "amount": {
            "value": 169578.5
          }
        },
        {
          "key": "王五",
          "doc_count": 8,
          "amount": {
            "value": 159871
          }
        },
        {
          "key": "王二小",
          "doc_count": 5,
          "amount": {
            "value": 99871
          }
        },

     说明:doc_count表示8条数据,amount 为sum后的总金额 ,key 为分组字段的值

后续持续更新.........

需要聚合的统计方式,请在下方留言,看到会更新文章内

QQ群号:119170668 

你可能感兴趣的:(java,搜索引擎,elasticsearch)