繁体   English   中英

在 date_histogram + 嵌套 ES 查询中需要帮助

[英]Need help in date_histogram + nested ES query

我需要在 Timestamp 上创建 15m 的桶,然后在每个时间戳中,我需要对每种类型的书籍进行求和,当然还有书籍的总数。

例如,我的数据如下

[
   {
      "books":[
         {
            "id":0,
            "count":10
         },
         {
            "id":1,
            "count":11
         },
         {
            "id":2,
            "count":7
         },
         {
            "id":3,
            "count":9
         },
         {
            "id":4,
            "count":16
         }
      ],
      "timestamp":1613693700000,
      "total":53
   },
   {
      "books":[
         {
            "id":0,
            "count":0
         },
         {
            "id":1,
            "count":4
         },
         {
            "id":2,
            "count":9
         },
         {
            "id":3,
            "count":10
         },
         {
            "id":4,
            "count":1
         }
      ],
      "timestamp":1613694600000,
      "total":24
   }
]

我需要 output 如下所示:

[
   {
      "timestamp":1613693700000,
      "total_count":77,
      "data":[
         {
            "id":0,
            "count":10
         },
         {
            "id":1,
            "count":15
         },
         {
            "id":2,
            "count":16
         },
         {
            "id":3,
            "count":19
         },
         {
            "id":4,
            "count":17
         }
      ]
   }
]

我已经尝试过下面的查询,现在我被嵌套查询困住了,以获取每个时间戳存储桶中每种书籍类型的总和。 在这方面需要帮助。

{
    "aggs": {
        "count": {

            "date_histogram": {
                "field": "timestamp",
                "interval": "15m"
            },
            "aggs": {
                "total_count": {
                    "sum": {
                        "field": "total"
                    }
                }
            }
        }
    }
}

工作。 output 中的命名结构不完全相同,但它解决了我在问题中遇到的实际问题

如果有人在同一条船上,请发布它。

{
   "aggs":{
      "bucket_by_time":{
         "date_histogram":{
            "field":"timestamp",
            "interval":"15m"
         },
         "aggs":{
            "bucket_by_type":{
               "nested":{
                  "path":"data"
               },
               "aggs":{
                  "books":{
                     "terms":{
                        "field":"data.id"
                     },
                     "aggs":{
                        "count":{
                           "sum":{
                              "field":"data.count"
                           }
                        }
                     }
                  },
                  "total_count":{
                     "sum_bucket":{
                        "buckets_path":"books>count"
                     }
                  }
               }
            }
         }
      }
   }
}

暂无
暂无

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

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