gpt4 book ai didi

elasticsearch - elasticsearch用先前的值填补空白

转载 作者:行者123 更新时间:2023-12-03 01:20:14 25 4
gpt4 key购买 nike

我在Elasticsearch中有时间序列数据,我想将其汇总以创建直方图。我要实现的是用上一个数据点的值填充空存储桶。我知道我可以使用min_doc_count: 0,但是它将值设置为0,我找不到通过Elastic做到这一点的现成方法。可能有一些我不知道的把戏?
感谢您的反馈。

最佳答案

我认为Date Histogram Aggregation没有提供执行所需内容的本地方法。

我能想到的最接近的东西是使用missing value。但是,这将为所有未找到值的日期设置一个静态值,这并不是您想要的。

我还想到了按以下逻辑使用Painless:

  • 获取直方图中的第一个值并将其存储在变量current中。
  • 如果下一个值不同于0,则将此值存储到current
  • 如果值为0,则将current值设置为直方图日期。不要更改current
  • 重复步骤2,直到完成直方图。

  • 根据我的经验,使用无痛确实很痛苦,但是您可以将其视为替代方法。

    另外,我建议您限制ES来执行搜索和聚合。如果您需要输出的其他逻辑,请考虑在ES之外执行。您可以使用 Python ES Client为例。

    我可以想到以下与无痛脚本具有相似逻辑的脚本:
    current = 0
    results = es.search(...)
    for i in res["aggregations"]["my_histogram_name"]["buckets"]:
    if not i["doc_count"]: #this is the same as "if i["doc_count"]==0"
    i["doc_count"] = current
    current = i["doc_count"] #changed or not, we always use the last value to current

    之后,直方图应看起来像您想要的并准备显示。

    希望这会有所帮助! :)

    关于elasticsearch - elasticsearch用先前的值填补空白,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60529674/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com