【问题标题】:Date_histogram and top_hits from unique values only仅来自唯一值的 Date_histogram 和 top_hits
【发布时间】:2019-08-30 03:39:06
【问题描述】:

我正在尝试进行 date_histogram 聚合以显示每个小时的 Duration 总和。

我有以下文件:

{
  "EntryTimestamp": 1567029600000,
  "Username": "johndoe",
  "UpdateTimestamp": 1567029600000,
  "Duration": 10,
  "EntryID": "ASDF1234"
}

以下工作非常好,但我的问题是有时会出现多个文档具有相同的 EntryID。因此,理想情况下,我需要以某种方式添加 top_hits,并按 UpdateTimestamp 排序,因为我需要每个唯一 EntryID 的最后更新文档。但不确定如何将其添加到我的查询中。

{
    "size": 0,
    "query": {
        "bool": {
            "filter": [{
                    "range": {
                        "EntryTimestamp": {
                            "gte": "1567029600000",
                            "lte": "1567065599999",
                            "format": "epoch_millis"
                        }
                    }
                }, {
                    "query_string": {
                        "analyze_wildcard": true,
                        "query": "Username.keyword=johndoe"
                    }
                }
            ]
        }
    },
    "aggs": {
        "2": {
            "date_histogram": {
                "interval": "1h",
                "field": "EntryTimestamp",
                "min_doc_count": 0,
                "extended_bounds": {
                    "min": "1567029600000",
                    "max": "1567065599999"
                },
                "format": "epoch_millis"
            },
            "aggs": {
                "1": {
                    "sum": {
                        "field": "Duration"
                    }
                }
            }
        }
    }
}

【问题讨论】:

    标签: elasticsearch


    【解决方案1】:

    我认为您需要在 terms 聚合中使用 top_hits 聚合。

    terms 聚合将获得不同的 EntryID,其中的 top hit 聚合将只获得 terms 聚合的每个存储桶(每个不同的值)的最新文档(基于 UpdateTimestamp)。

    我没有适合您的上下文的明确语法,我相信您可能会遇到一些关于子聚合数量的问题(过去我遇到过高级聚合的一些限制)

    您可以查看this 帖子以获取更多信息;我希望它会证明对你有帮助。

    【讨论】:

      猜你喜欢
      • 2016-11-26
      • 1970-01-01
      • 2018-04-11
      • 2022-11-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多