簡體   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