Elasticsearch commonly used aggregation (group by || sum || count) group query

1.elasticsearch groups the specified fields and finds the total amount of each parameter after grouping

   Example: Find the total amount for each person

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

  Note: group_by_name is a custom alias, and terms is the specified grouping field. order Sort order based on total amount

             The parameter value of name corresponds to the key of the following result

  search result:

"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
          }
        },

     Note: doc_count represents 8 pieces of data, amount is the total amount after sum, and key is the value of the grouping field.

Continuous updates will follow......

If you need a statistical method of aggregation, please leave a message below. The article will be updated when you see it.

QQ group number: 119170668 

Guess you like

Origin blog.csdn.net/Qensq/article/details/103081923