繁体   English   中英

在 Elasticsearch 中的桶聚合中查找“关键”值的总和

[英]Finding sum of the "key" values in bucket aggregations in Elasticsearch

我有以下 ES 查询:

GET database/_search
{
  "from": 0,
  "size": 0,
  "query": {
    "bool": {
      "must": [
        {
          "nested": {
            "query": {
              "term": {
                "colleges.institution_full_name": {
                  "value": "Academy of Sciences",
                  "boost": 1.0
                }
              }
            },
            "path": "colleges"
          }
        }
      ]
    }
  },
  "_source": false,
  "aggs": {
    "publication_years": {
      "terms": {
        "field": "publication_year"
      }
    }
  }
}

我得到了以下回应:

{
  "took" : 3,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 232,
      "relation" : "eq"
    },
    "max_score" : null,
    "hits" : [ ]
  },
  "aggregations" : {
    "publication_years" : {
      "doc_count_error_upper_bound" : 0,
      "sum_other_doc_count" : 0,
      "buckets" : [
        {
          "key" : 2016,
          "doc_count" : 119
        },
        {
          "key" : 2017,
          "doc_count" : 90
        },
        {
          "key" : 2018,
          "doc_count" : 22
        },
        {
          "key" : 2019,
          "doc_count" : 1
        }
      ]
    }
  }
}

现在我想计算出版年份关键值的平均值,即 2016 年、2017 年、2018 年和 2019 年的平均值。那么我如何修改我的 ES 查询以获取出版年份的平均值而不是单独获取每年。 我尝试使用“avg”聚合,但它在计算平均值时也考虑了“doc_count”。

试试吧

POST database/_search
{
  "size": 0, 
  "aggs": {
    "groupByYear": {
      "terms": {
        "field": "publication_year"
      },
      "aggs": {
        "avgYear": {
          "avg": {
            "field": "publication_year"
          }
        }
      }
    },
    "avg_year": {
      "avg_bucket": {
        "buckets_path": "groupByYear>avgYear" 
      }
    }
  }
}

不清楚你想要什么,你想要2016、2017、2018、2019年的平均值吗? 这意味着你想要2017.5

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM