如何在ElasticSearch中聚合其他聚合的结果?

qyswt5oh  于 2022-12-11  发布在  ElasticSearch
关注(0)|答案(1)|浏览(164)

在这个问题中,我遇到了一个困惑,那就是如何聚合其他聚合的结果?下面是我的第一个桶结果:

{
  "took" : 2,
  "timed_out" : false,
  "_shards" : {
    "total" : 35,
    "successful" : 35,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : 864,
    "max_score" : 0.0,
    "hits" : [ ]
  },
  "aggregations" : {
    "group_by_uid" : {
      "doc_count_error_upper_bound" : 0,
      "sum_other_doc_count" : 240,
      "buckets" : [
        {
          "key" : "18133455169",
          "doc_count" : 110
        },
        {
          "key" : "15736767693",
          "doc_count" : 100
        },
        {
          "key" : "17354023823",
          "doc_count" : 76
        },
        {
          "key" : "18119669395",
          "doc_count" : 76
        },
        {
          "key" : "18119772327",
          "doc_count" : 56
        },
        {
          "key" : "18156993756",
          "doc_count" : 52
        },
        {
          "key" : "18727508556",
          "doc_count" : 46
        },
        {
          "key" : "18895657556",
          "doc_count" : 45
        },
        {
          "key" : "18605910769",
          "doc_count" : 42
        },
        {
          "key" : "13182871522",
          "doc_count" : 21
        }
      ]
    }
  }
}

下面是我第一个查询DSL:

GET /dailyactiveindex-*/_search
{
  "query": {
    "match_all": {}
  }, 
  "size": 0,
  "aggs": {
    "group_by_uid": {
      "terms": {"field": "userId.keyword"}
    }
  }
}

现在,我想对第一个bucket的结果进行聚合,并计算相同的doc_count
我想达到的目标是:

{'buckets': [{'key': '110', 'doc_count': 1},
    {'key': '100', 'doc_count': 1},
    {'key': '76', 'doc_count': 2},
    {'key': '56', 'doc_count': 1},
    {'key': '52', 'doc_count': 1},
    {'key': '46', 'doc_count': 1},
    {'key': '45', 'doc_count': 1},
    {'key': '42', 'doc_count': 1},
    {'key': '21', 'doc_count': 1}]}

谢谢你,谢谢你

wwwo4jvm

wwwo4jvm1#

{
  "query": {
    "match_all": {}
  }, 
  "size": 1000,
  "aggs": {
    "intersect": {
      "scripted_metric": {
        "init_script": "state.map=[:]",
        "map_script": "state.map[doc.userId.value]=1",
        "combine_script": "return state.map",
        "reduce_script": "return states"
      }
    }
  }
}

相关问题