在ElasticSearch中过滤嵌套的聚合? [英] Filtered nested aggregation in ElasticSearch?

查看:444
本文介绍了在ElasticSearch中过滤嵌套的聚合?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have following document list in ElasticSearch (where scores are 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},
    ]
}

是否有可能返回每种类型的平均分数的查询,但仅当 scores.type 是 A吗?

Is it possible to have a query that returns average scores per type, but only if scores.type is "A"?

说明(如果我手动进行的话):

Explanation (if I did it manually):

1)仅过滤 A分数(简化):

1) filter only "A" scores (simplified):

{'type': 'typeA', 'scores': [45, 55]},
{'type': 'typeA', 'scores': [55, 50, 57]},
{'type': 'typeB', 'scores': [50, 60]},

2)查找每个文档的AVG:

2) find AVG per document:

{'type': 'typeA', 'avg': 50}, // (45+55) / 2
{'type': 'typeA', 'avg': 54}, // (55+50+57) / 3
{'type': 'typeB', 'avg': 55}, // (50 + 60) / 2

3)每种类型的最终汇总:

3) Final aggregation per type:

'typeA' : 52, // (50+54) / 2
'typeB': 55, // (55) / 1

是否可以这样做?

推荐答案

是的,使用条款嵌套 avg 聚合,例如:

Yes, it is definitely possible to do it with a combination of terms, nested and avg aggregations, like this:

{
  "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中过滤嵌套的聚合?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆