其他分享
首页 > 其他分享> > 【ElasticSearch(十)进阶】Aggregations执行聚合

【ElasticSearch(十)进阶】Aggregations执行聚合

作者:互联网

【ElasticSearch(十)进阶】Aggregations执行聚合


【案例1】

搜索 address 中包含 mill 的所有人的年龄分布以及平均年龄。

aggs:聚合操作

ageAggageAvg:单个聚合操作的名称

terms:获取结果的不同数据个数。在这里是,统计不同年龄的分布人数。

field:要统计的属性

size:显示几个。在这里是,只获取前10种年龄分布。

avg:求平均值

GET /bank/_search
{
  "query":{
    "match":{
      "address":"mill"
    }
  },
  "aggs":{
    "ageAgg":{
      "terms":{
        "field": "age",
        "size": 10
      }
    },
    "ageAvg":{
      "avg":{
        "field":"age"
      }
    }
  }
}

(补充)如果 不想看query的详细结果,只看聚合结果,加上size=0

GET /bank/_search
{
  "query":{
    "match":{
      "address":"mill"
    }
  },
  "aggs":{
    "ageAgg":{
      "terms":{
        "field": "age",
        "size": 10
      }
    },
    "ageAvg":{
      "avg":{
        "field":"age"
      }
    }
  },
  "size": 0  //新增
}

返回的结果:可以看到hits这里是空的

{
  "took" : 4,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 4,
      "relation" : "eq"
    },
    "max_score" : null,
    "hits" : [ ]
  },
  "aggregations" : {
    "ageAgg" : {
      "doc_count_error_upper_bound" : 0,
      "sum_other_doc_count" : 0,
      "buckets" : [
        {
          "key" : 38,
          "doc_count" : 2
        },
        {
          "key" : 28,
          "doc_count" : 1
        },
        {
          "key" : 32,
          "doc_count" : 1
        }
      ]
    },
    "ageAvg" : {
      "value" : 34.0
    }
  }
}

【案例2】

按照年龄聚合,选出前3个,并且请求这些年龄段的这些人的平均薪资

这里先聚合了年龄,再在这个基础上计算这些人的平均薪资(用子聚合)。

GET /bank/_search
{
  "query":{
    "match_all": {}
  },
  "aggs":{
    "ageAgg":{
      "terms":{
        "field": "age",
        "size": 3
      },
      "aggs":{  //子聚合
        "balanceAvg":{
          "avg":{
            "field": "balance"
          }
        }
      }
    }
  },
  "size": 0
}

返回结果:

{
  "took" : 4,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 1000,
      "relation" : "eq"
    },
    "max_score" : null,
    "hits" : [ ]
  },
  "aggregations" : {
    "ageAgg" : {
      "doc_count_error_upper_bound" : 0,
      "sum_other_doc_count" : 820,
      "buckets" : [
        {
          "key" : 31,
          "doc_count" : 61,
          "balanceAvg" : {
            "value" : 28312.918032786885
          }
        },
        {
          "key" : 39,
          "doc_count" : 60,
          "balanceAvg" : {
            "value" : 25269.583333333332
          }
        },
        {
          "key" : 26,
          "doc_count" : 59,
          "balanceAvg" : {
            "value" : 23194.813559322032
          }
        }
      ]
    }
  }
}

【案例3】

查询出年龄分布,并且这些 年龄段中 性别为 M 的平均薪资 和 性别为 F 的平均薪资 以及 这个年龄段的总体平均薪资。

分解思路:

先查询出年龄分布,在它的子聚合里统计性别为M和F的分别人数,在M/F分别人数的结果里的用子聚合统计各自的平均薪资。

在年龄分布的子聚合里统计该年龄段的平均薪资。

GET bank/_search
{
  "query":{
    "match_all": {}
  },
  "aggs":{
    "ageAgg":{
      "terms": {
        "field": "age",
        "size": 3
      },
      "aggs": {
        "genderAgg": {
          "terms": {
            "field": "gender.keyword"
          },
          "aggs":{
            "balanceAvg":{
              "avg":{
                "field": "balance"
              }
            }
          }
        },
        "ageBalanceAvg":{
          "avg":{
            "field": "balance"
          }
        }
      }
    }
  },
  "size": 0
}

返回结果:

{
  "took" : 2,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 1000,
      "relation" : "eq"
    },
    "max_score" : null,
    "hits" : [ ]
  },
  "aggregations" : {
    "ageAgg" : {
      "doc_count_error_upper_bound" : 0,
      "sum_other_doc_count" : 820,
      "buckets" : [
        {
          "key" : 31,
          "doc_count" : 61,
          "genderAgg" : {
            "doc_count_error_upper_bound" : 0,
            "sum_other_doc_count" : 0,
            "buckets" : [
              {
                "key" : "M",
                "doc_count" : 35,
                "balanceAvg" : {
                  "value" : 29565.628571428573
                }
              },
              {
                "key" : "F",
                "doc_count" : 26,
                "balanceAvg" : {
                  "value" : 26626.576923076922
                }
              }
            ]
          },
          "ageBalanceAvg" : {
            "value" : 28312.918032786885
          }
        },
        {
          "key" : 39,
          "doc_count" : 60,
          "genderAgg" : {
            "doc_count_error_upper_bound" : 0,
            "sum_other_doc_count" : 0,
            "buckets" : [
              {
                "key" : "F",
                "doc_count" : 38,
                "balanceAvg" : {
                  "value" : 26348.684210526317
                }
              },
              {
                "key" : "M",
                "doc_count" : 22,
                "balanceAvg" : {
                  "value" : 23405.68181818182
                }
              }
            ]
          },
          "ageBalanceAvg" : {
            "value" : 25269.583333333332
          }
        },
        {
          "key" : 26,
          "doc_count" : 59,
          "genderAgg" : {
            "doc_count_error_upper_bound" : 0,
            "sum_other_doc_count" : 0,
            "buckets" : [
              {
                "key" : "M",
                "doc_count" : 32,
                "balanceAvg" : {
                  "value" : 25094.78125
                }
              },
              {
                "key" : "F",
                "doc_count" : 27,
                "balanceAvg" : {
                  "value" : 20943.0
                }
              }
            ]
          },
          "ageBalanceAvg" : {
            "value" : 23194.813559322032
          }
        }
      ]
    }
  }
}

标签:count,聚合,进阶,doc,balanceAvg,Aggregations,value,ElasticSearch,key
来源: https://www.cnblogs.com/musecho/p/15179999.html