【问题标题】:Need help in date_histogram + nested ES query在 date_histogram + 嵌套 ES 查询中需要帮助
【发布时间】:2021-02-22 20:33:46
【问题描述】:

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

【问题讨论】:

  • 您能检查一下您的样本索引数据吗?两个文档是否都只包含"id":0"?并且要计算两个文档中每个id 对应的计数总和?
  • 抱歉错字,已在问题中修复。是的,我想计算每个id对应的count总和,可能有n个这样的文档

标签: elasticsearch nested date-histogram


【解决方案1】:

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

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

{
   "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-08-02
    • 1970-01-01
    • 1970-01-01
    • 2011-08-06
    • 1970-01-01
    • 2016-04-01
    • 2011-04-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多