elasticsearch - 在ElasticSearch中过滤嵌套的聚合?

标签 elasticsearch aggregation

我在ElasticSearch中有以下文档列表(分数为nested):

{
    'type': 'typeA',
    'scores': [
       {'type': 'A', 'val': 45},
       {'type': 'A', 'val': 55},
       {'type': 'B', 'val': 65},
    ]
},
{
    'type': 'typeA',
    'scores': [
       {'type': 'A', 'val': 55},
       {'type': 'A', 'val': 50},
       {'type': 'A', 'val': 57},
    ]
},
{
    'type': 'typeB',
    'scores': [
       {'type': 'B', 'val': 40},
       {'type': 'A', 'val': 50},
       {'type': 'A', 'val': 60},
    ]
}

是否有可能返回每个type平均得分的查询,但前提是scores.type为“A”?

说明(如果我手动完成):

1)仅过滤“A”分数(简化):
{'type': 'typeA', 'scores': [45, 55]},
{'type': 'typeA', 'scores': [55, 50, 57]},
{'type': 'typeB', 'scores': [50, 60]},

2)查找每个文档的AVG:
{'type': 'typeA', 'avg': 50}, // (45+55) / 2
{'type': 'typeA', 'avg': 54}, // (55+50+57) / 3
{'type': 'typeB', 'avg': 55}, // (50 + 60) / 2

3)每种类型的最终汇总:
'typeA' : 52, // (50+54) / 2
'typeB': 55, // (55) / 1

有可能还是我应该为此坚持到底?

最佳答案

是的,绝对可以通过termsnestedavg聚合的组合来做到这一点,如下所示:

{
  "size": 0,
  "aggs": {
    "top_level_type": {                    <---- group by top-level type
      "terms": {
        "field": "type"
      },
      "aggs": {
        "nest": {
          "nested": {                      <---- "dive" your nested scores
            "path": "scores"
          },
          "aggs": {
            "type_filter": {
              "filter": {                  <---- filter only score type A
                "term": {
                  "scores.type": "A"
                }
              },
              "aggs": {
                "average": {
                  "avg": {                 <---- compute the average of the score values
                    "field": "scores.val"
                  }
                }
              }
            }
          }
        }
      }
    }
  }
}

结果值如下所示:
{
  ...
  "aggregations" : {
    "top_level_type" : {
      "doc_count_error_upper_bound" : 0,
      "sum_other_doc_count" : 0,
      "buckets" : [ {
        "key" : "typea",
        "doc_count" : 2,
        "nest" : {
          "doc_count" : 6,
          "type_filter" : {
            "doc_count" : 5,
            "average" : {
              "value" : 52.4
            }
          }
        }
      }, {
        "key" : "typeb",
        "doc_count" : 1,
        "nest" : {
          "doc_count" : 3,
          "type_filter" : {
            "doc_count" : 2,
            "average" : {
              "value" : 55.0
            }
          }
        }
      } ]
    }
  }
}

关于elasticsearch - 在ElasticSearch中过滤嵌套的聚合?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32132061/

相关文章:

python - ElasticSearch term suggest on analyzed field 不返回任何建议

elasticsearch - 将ElasticSearch从2升级到7.6

java - 如何从 Spring data MongoDB 中的 AggregationOperation 列表创建聚合?

SQL查询进行聚合

elasticsearch - ElasticSearch has_child查询什么都找不到

elasticsearch - elasticsearch通过子聚合doc_count排除聚合桶

elasticsearch - 密集向量数据类型的值为null的Elasticsearch错误

Python条件聚合

mysql - 查找重复值

ElasticSearch 聚合 - (术语?)查询中的 sum_other_doc_count?