如何创建可以计算时差的查询? [英] how to create query which can calculate time difference?
本文介绍了如何创建可以计算时差的查询?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我拥有以下文件并为其编制索引:
I have and index with following documents:
{name: 'Device1', type: 'start', 'eventTime': '2013-02-19 12:00:00'}
{name: 'Device2', type: 'start', 'eventTime': '2013-02-19 12:02:00'}
{name: 'Device1', type: 'stop', 'eventTime': '2013-02-19 12:45:00'}
{name: 'Device2', type: 'stop', 'eventTime': '2013-02-19 12:50:00'}
我想创建一个查询,该查询将为我返回一个新字段,该字段的eventTime之间存在时差,以设备名称和字段类型表示.对于该示例,应为:
I would like to create a query which will return for me a new field with time differences between eventTime's, faceted by device name and regards with field type. For the example it should be:
{name: 'Device1', 'type': 'it really doesnt matter', eventTime: 'also doesnt matter', duration: '00:45:00'}
{name 'Device2', 'type': 'it really doesnt matter', eventTime: 'also doesnt matter', duration: '00:48:00'}
弹性搜索查询语言是否可能?
Is it possible with elastic search query language?
推荐答案
我认为您当前使用每个文档的方式都不可行.如果您将文档存储为:
I don't believe this is possible with the way you have each document currently. If you store the documents as:
{name: 'Device1', startTime: '2013-02-19 12:00:00', endTime: '2013-02-19 12:45:00'}
{name: 'Device2', startTime: '2013-02-19 12:02:00', endTime: '2013-02-19 12:50:00'}
然后,您可以返回脚本字段,即时差.
You could then return a script field which is the time difference.
{
"query" : {
...
},
"script_fields" : {
"timedifference" : {
"script" : "doc['endTime'].value - doc['startTime'].value"
}
}
}
这篇关于如何创建可以计算时差的查询?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文