0

我需要在 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
   }
]

我需要如下输出:

[
   {
      "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"
                    }
                }
            }
        }
    }
}
4

1 回答 1

0

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

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

{
   "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"
                     }
                  }
               }
            }
         }
      }
   }
}
于 2021-02-23T21:13:04.740 回答