且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在ElasticSearch中计算两个日期时间之间的差异

更新时间:2023-02-07 18:04:41

***的是脚本字段。只要您启用了动态脚本功能,并且将这些日期字段定义为映射中的日期,上述搜索查询就可以正常工作。

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.