ElasticSearch

时间:2015-07-10 11:38:20

标签: elasticsearch

我有许多代表starts_atends_at字段的事件的文档。在某个特定时间点,如果相关点位于starts_at之后和ends_at之前,则该事件被视为有效。

我正在寻找一个聚合,这应该会产生一个日期直方图,其中每个桶包含该时间间隔内活动事件的数量。

到目前为止,我发现的最好的近似是创建一组计算每个间隔中启动次数的桶,以及计算结束次数的相应一组桶,然后通过减去数字对它们进行后处理从每个区间的结束数开始:

{
  "size": "0",
  "query": {
    "filtered": {
      "query": {
        "match_all": {}
      },
      "filter": {
        "and": [
          {
            "term": {
              "_type": "event"
            }
          },
          {
            "range": {
              "starts_at": {
                "gte": "2015-06-14T05:25:03Z",
                "lte": "2015-06-21T05:25:03Z"
              }
            }
          }
        ]
      }
    }
  },
  "aggs": {
    "starts": {
      "date_histogram": {
        "field": "starts_at",
        "interval": "15m",
        "extended_bounds": {
          "max": "2015-06-21T05:25:04Z",
          "min": "2015-06-14T05:25:04Z"
        },
        "min_doc_count": 0
      }
    },
    "ends": {
      "date_histogram": {
        "field": "ends_at",
        "interval": "15m",
        "extended_bounds": {
          "max": "2015-06-21T05:25:04Z",
          "min": "2015-06-14T05:25:04Z"
        },
        "min_doc_count": 0
      }
    }
  }
}

我正在寻找像this solution这样的东西。

有没有办法通过单个查询实现这一目标?

1 个答案:

答案 0 :(得分:0)

我不是百分百肯定,但即将到来的pipeline aggregations可能会以更优雅的方式在不久的将来解决这个问题。

与此同时,除了starts_atends_at字段之外,您还可以选择所需的时间分辨率,并在索引时生成active_at字段。它将是一个时间戳数组,您可以使用任一术语(如果它被映射为not_analyzed字符串)或date_histogram聚合,以获得每个时间段的正确“活动事件计数”。

由于需要聚合更多字段值,因此存储需求可能会增加并且性能可能会下降。无论如何,如果你不选择像1分钟那么高的时间分辨率,那应该不会太糟糕。