Elasticsearch 中的时间戳差异

Timestamp difference in Elasticsearch

我知道有人问过这个问题,但 ELK 的变化似乎非常迅速,也许此时有些事情是可能的。 因此,我正在使用 Kibana 4,并且我正在尝试可视化(或至少计算)查询中两个文档之间的时间差。 我的文档是来自具有很多字段的批处理日志,时间戳就是其中之一("date" 类型)。 是否可以使用脚本字段计算查询中连续文档之间的时间差? (返回什么类型并不重要)。

我希望我说得有道理,我是 ELK 的新手。 提前致谢。

道塔

_据我了解,您不能在 Kibana 中执行此操作,因为脚本字段会逐一应用于文档。但是,如果对您来说重要的是获得计算结果,您可以在 ES 查询中使用 scripted_metric 聚合来实现。

我觉得可能像

{
    "sort" : [
        { "mydatefield" : {"order" : "asc"}}
    ],
    "query" : {
        (something of match, range, match_all...)
    },
    "aggregations": {
        "scripted_metric": {
            "init_script": "(declarations, eg :) _agg['myarray']=[]",
            "map_script": (store relevant timestamps in datastructure, eg :) "_ạgg.myarray.add(doc['mydatefield'])",
            "reduce_script": "(aggregate results, eg :) otherarray = [] ; for (x in ạggs){otherarray.putAll(x.myarray)} ;
            (and sort otherarray) ; result = [] ;
           for (i = 0 ; i < otherarray.length-1 ; i++){result.add(otherarray[i+1]-otherarray[i])} ;
           return result ;"
        }
    }
}