【问题标题】:How to retrieve timestamp along with minimum value within an elastic aggregation如何在弹性聚合中检索时间戳和最小值
【发布时间】:2017-12-04 17:40:51
【问题描述】:

我正在对弹性数据集执行聚合查询 我想在时间跨度内检索最大值和最小值 我用

实现了这一点
"aggs": {
    "DateRangeFilter": {
      "filter": {
        "range": {
          "@timestamp": {
            "gte": "2015-10-16T11:13:17.000",
            "lte": "2015-10-16T12:29:47.000"
          }
        }
      },
      "aggs": {
        "min_chan4": {
          "min": {
            "field": "ch_004"
          }
        },
        "max_chan4": {
          "max": {
            "field": "ch_004"
          }
}

这给了我:

"DateRangeFilter": {
    "doc_count": 153,
    "min_chan4": {
      "value": 0.7463656663894653
    },
    "max_chan4": {
      "value": 5.170884132385254
    }

太棒了。

我还需要检索每一个发生的时间 我的文档如下所示:

    "_source": {
      "GroupId": "blahblah",
      "@timestamp": "2015-10-14T12:41:30Z",
      "ch_004": 1.5608633995056154
    }

所以我希望不仅能够检索给定日期范围内的最小值和最大值,还能够检索记录最小值或最大值的时间(@timestamp)

例如最小值minX出现在时间t1,最大值maxX出现在时间t2

【问题讨论】:

    标签: elasticsearch bucket elasticsearch-aggregation


    【解决方案1】:

    您可以使用Top Hits Aggregation 来实现它。例子可以找到here:

    {
      "aggs": {
        "DateRangeFilter": {
          "filter": {
            "range": {
              "@timestamp": {
                "gte": "2015-10-16T11:13:17.000",
                "lte": "2015-10-16T12:29:47.000"
              }
            }
          },
          "aggs": {
            "ch_004_min": {
              "top_hits": {
                "size": 1,
                "sort": [
                  {
                    "timestamp": {
                      "order": "asc"
                    }
                  }
                ]
              }
            },
            "ch_004_max": {
              "top_hits": {
                "size": 1,
                "sort": [
                  {
                    "timestamp": {
                      "order": "desc"
                    }
                  }
                ]
              }
            }
          }
        }
      }
    }
    

    【讨论】:

    • 非常感谢您将我指向 top_hits。通过一个小的改变,我能够想出这个给出了我正在寻找的确切答案: "top_hits": { "size": 1, "sort": [ { "ch_004": { "order": "desc " } } ], "_source": { "includes": [ "ch_004", "@timestamp"] } }
    猜你喜欢
    • 2015-05-24
    • 1970-01-01
    • 2017-12-24
    • 2018-01-24
    • 2015-06-06
    • 1970-01-01
    • 1970-01-01
    • 2016-05-27
    • 2016-09-24
    相关资源
    最近更新 更多