简体   繁体   English

如何在Elasticsearch中进行多级子聚合?

[英]How to do multi-level child aggregations in elasticsearch?

I have a USER type and it has children of EVENT type. 我具有USER类型,并且具有EVENT类型的子代。

The events can have different fields. 事件可以具有不同的字段。 So one event might be have a visitDate field, and another event might be a demographics event and have an age field. 因此,一个事件可能有一个visitDate字段,而另一个事件可能是一个人口统计事件并具有一个age字段。

I want to do multi-level aggregations, where I can see, for example, the breakdown of ages each day. 我想进行多层次的汇总,例如可以看到每天的年龄细分。 Ie: 即:

1st July
  Age: 24 - 10 docs
  Age: 25 - 15 docs 
2nd July
  Age: 24 - 5 docs
  Age: 25 - 6 docs
etc

The problem I am facing is that if I do a children aggregation like: 我面临的问题是,如果我进行子聚合,例如:

{
  "size": 2,
  "aggs": {
    "events": {
      "children": {
        "type" : "event"
      },
      "aggs": {
        "visitDate": {
          "terms":{
            "field":"visitDate",
            "size":0
          },
          "aggs":{
            "byAge":{
              "terms":{
                "field":"age",
                "size":0
              } 
            } 
          } 
        }
      }
    }
  }
}

etc 等等

the second-level aggregation (on age) only has access to the child documents that matched the aggregation on visitDate, NOT any of the other child documents that are children of the same parents. 第二级汇总(按年龄分类)只能访问与visitDate上的汇总相匹配的子级文档,而不能访问属于同一父级的其他子级文档。

How can I aggregate arbitrarily deeply, but with each successive aggregation looking at the PARENTS of the children in that bucket? 我如何才能进行任意深度的汇总,但是每次连续的汇总都着眼于该存储桶中孩子的父母?

You can achieve that using the nested aggregation, I had the same case before and I used the following query to get such result (aggregating audio book time usage by language which is a nested child doc in the audio_book): 您可以使用嵌套聚合来实现,我之前也有相同的情况,并且使用以下查询来获得这样的结果(按语言聚合音频书的时间使用情况,这是audio_book中的嵌套子文档):

Elasticsearch doc: Elasticsearch文档:

  {
    "_index": "dev_analytics",
    "_type": "UsageAnalytics",
    "_id": "AWLc_3OL-IIrUiI24XbD",
    "_score": 2.1704133,
    "_source": {
      "timestamp": 1523983693559,
      "audio_book": {
        "id": "0032423404234234234",
        "name": "Jag ger dig solen",
        "languages": [
          {
            "code": "sv",
            "type": "TEXT"
          }
        ],
      "usage": {
        "unitOfMeasure": "SEC",
        "totalUsage": 1200
      }
    }
  }

Required aggregation: 所需的汇总:

"buckets": [
          {
            "key": "sv",
            "doc_count": 247,
            "usage": {
              "doc_count": 247,
              "total_usage": {
                "count": 247,
                "min": 1200,
                "max": 1200,
                "avg": 1200,
                "sum": 296400
              }
            }
          },
          {
            "key": "en",
            "doc_count": 47,
            "usage": {
              "doc_count": 47,
              "total_usage": {
                "count": 47,
                "min": 1200,
                "max": 1200,
                "avg": 1200,
                "sum": 56400
              }
            }
          },
          {
            "key": "de",
            "doc_count": 24,
            "usage": {
              "doc_count": 24,
              "total_usage": {
                "count": 24,
                "min": 1200,
                "max": 1200,
                "avg": 1200,
                "sum": 28800
              }
            }
          },
          {
            "key": "ar",
            "doc_count": 20,
            "usage": {
              "doc_count": 20,
              "total_usage": {
                "count": 20,
                "min": 1200,
                "max": 1200,
                "avg": 1200,
                "sum": 24000
              }
            }
          }
        ]

Nested aggregation query: 嵌套聚合查询:

GET dev_analytics/UsageAnalytics/_search
{
  "size": 0,
  "aggs": {
     "lang": {
       "nested": {
         "path": "title.languages"
       },
       "aggs": {
         "terms": {
           "terms": {
             "field": "title.languages.code"
           },
           "aggs": {
             "usage": {
               "reverse_nested": {},
               "aggs": {
                 "total_usage": {
                   "stats": {
                     "field": "usage.totalUsage"
                   }
                 }
               }
             }
           }
         }
       }
     }
   }
}

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

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