【问题标题】:summing a bunch of values given a condition in elasticsearch在elasticsearch中对给定条件的一堆值求和
【发布时间】:2015-11-30 16:44:04
【问题描述】:

鉴于以下 elasticsearch 文档,我将如何构建一个搜索来对给定日期时间范围的秒列的值求和?

请参阅下面的我当前的查询。

{
  "took": 1,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "failed": 0
  },
  "hits": {
    "total": 2,
    "max_score": 1,
    "hits": [
      {
        "_index": "searchdb",
        "_type": "profile",
        "_id": "1825",
        "_score": 1,
        "_source": {
          "id": 1825,
          "market": "Chicago",
          "geo_location": {
            "lat": 41.1234,
            "lon": -87.5678
          },
          "hourly_values": [
            {
              "datetime": "1997-07-16T19:00:00.00+00:00",
              "seconds": 1200
            },
            {
              "datetime": "1997-07-16T19:20:00.00+00:00",
              "seconds": 1200
            },
            {
              "datetime": "1997-07-16T19:20:00.00+00:00",
              "seconds": 1200
            }
          ]
        }
      },
      {
        "_index": "searchdb",
        "_type": "profile",
        "_id": "1808",
        "_score": 1,
        "_source": {
          "id": 1808,
          "market": "Chicago",
          "geo_location": {
            "lat": 41.1234,
            "lon": -87.5678
          },
          "hourly_values": [
            {
              "datetime": "1997-07-16T19:00:00.00+00:00",
              "seconds": 900
            },
            {
              "datetime": "1997-07-16T19:20:00.00+00:00",
              "seconds": 1200
            },
            {
              "datetime": "1997-07-16T19:20:00.00+00:00",
              "seconds": 800
            }
          ]
        }
      }
    ]
  }

以下是我当前的查询。它的问题是它没有考虑到日期时间字段。我只需要对查询中给定日期时间范围内的秒值求和。

{
    "aggs": {
        "Ids": {
            "terms": {
                "field": "id",
                "size": 0
            },
            "aggs": {
                "Nesting": {
                    "nested": {
                        "path": "hourly_values"
                    },
                    "aggs": {
                        "availability": {
                            "sum": {
                                "field": "hourly_values.seconds"
                            }
                        }
                    }
                }
            }
        }
    }
} 

我知道你可以使用一个范围,像这样:

"filter" : {
                "range" : { "timestamp" : { "from" : "now/1d+9.5h", "to" : "now/1d+16h" }}
            }

但我不知道如何将其集成到我的查询中以获得所需的输出。

为了清楚起见,我想要的输出是返回从查询返回的每个对象,以及秒字段的总和值,但我只想对给定时间范围内的值求和。

【问题讨论】:

    标签: elasticsearch


    【解决方案1】:

    我认为这可以通过filter aggregation 完成

    试试这个

    {
      "aggs": {
        "Ids": {
          "terms": {
            "field": "id",
            "size": 0
          },
          "aggs": {
            "Nesting": {
              "nested": {
                "path": "hourly_values"
              },
              "aggs": {
                "filtered_result": {
                  "filter": {
                    "query": {
                      "range": {
                        "hourly_values.datetime": {
                          "gt": "1997-07-16T19:10:00.00+00:00",
                          "lt": "1997-07-16T19:22:00.00+00:00"
                        }
                      }
                    }
                  },
                  "aggs": {
                    "availability": {
                      "sum": {
                        "field": "hourly_values.seconds"
                      }
                    }
                  }
                }
              }
            }
          }
        }
      },
      "size": 0
    } 
    

    我得到的结果

    "aggregations": {
          "Ids": {
             "doc_count_error_upper_bound": 0,
             "sum_other_doc_count": 0,
             "buckets": [
                {
                   "key": "1808",
                   "doc_count": 1,
                   "Nesting": {
                      "doc_count": 3,
                      "filtered_result": {
                         "doc_count": 2,
                         "availability": {
                            "value": 2000
                         }
                      }
                   }
                },
                {
                   "key": "1825",
                   "doc_count": 1,
                   "Nesting": {
                      "doc_count": 3,
                      "filtered_result": {
                         "doc_count": 2,
                         "availability": {
                            "value": 2400
                         }
                      }
                   }
                }
             ]
          }
       }
    

    这有帮助吗?

    【讨论】:

    • 不幸的是返回"Found two aggregation type definitions in [Nesting]: [nested] and [filter]"。在尝试添加范围的不同方法时,我经常遇到此错误。
    • 对不起,我没有在我的机器上尝试过。
    • 我想我成功了。请看我更新的答案
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-12
    • 2021-07-21
    • 1970-01-01
    • 2015-03-29
    • 2018-05-04
    • 1970-01-01
    相关资源
    最近更新 更多