如何在ElasticSearch中计算两个日期时间之间的差异 [英] How to calculate difference between two datetime in ElasticSearch
问题描述
我正在使用ES,我需要一个查询,该查询返回两个日期时间(mysql timediff)之间的时差,但是没有找到ES的任何功能来做到这一点。有人可以帮助我吗?
I'm working with ES and I need a query that returns the difference between two datetime (mysql timediff), but have not found any function of ES to do that. Someone who can help me?
MySQL查询
SELECT SEC_TO_TIME(
AVG(
TIME_TO_SEC(
TIMEDIFF(r.acctstoptime,r.acctstarttime)
)
)
) as average_access
FROM radacct
谢谢!
推荐答案
最好的是脚本字段。只要您启用了动态脚本功能,并且将这些日期字段定义为映射中的日期,上述搜索查询就可以正常工作。
Your best best is scripted fields. The above search query should work , provided you have enabled dynamic scripting and these date fields are defined as date in the mapping.
{
"script_fields": {
"test1": {
"script": "doc['acctstoptime'].value - doc['acctstarttime'].value"
}
}
}
请注意,您将在epoch中获得结果您需要转换为面额。
Note that you would be getting result in epoch , which you need to convert to your denomination.
You can read about scripted field here and some of its examples here.
这篇关于如何在ElasticSearch中计算两个日期时间之间的差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!