gpt4 book ai didi

elasticsearch - 将持续时间转换为总毫秒数

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

我的 flex 搜索中有一个字符串字段,其持续时间格式为00:00:00.000000。我想将其转换为数字字段,即毫秒总数。
编辑:
我考虑过使用脚本字段,但是它不起作用。

try{
String duration = doc['app.duration'].value;

String[] durationParts = new String[3];

int firstIdx = duration.indexOf(":");
int secondIdx = duration.indexOf(":", firstIdx+1);

durationParts[1] = duration.substring(firstIdx+1, secondIdx);
durationParts[2] = duration.substring(secondIdx+1);

long minutes = Long.parseLong(durationParts[1]) * 60000;
float seconds = Float.parseFloat(durationParts[2]) * 1000;

long mAndS = Math.round(minutes + seconds);
return mAndS;
}
catch(Exception e) {
return -1;
}
以上在第一行 String duration = doc['app.duration'].value;上失败,我得到-1。但是,如果我将第一行更改为像 String duration = "00:00:01.5250000";这样的硬编码值,那么我会得到期望的预期 1525
我不确定自己在做什么错。通过使用 doc对象阅读的内容是如何访问字段的。

最佳答案

您可能想尝试keyword子字段,因为它包含您添加到源中的确切原始字符串值(即00:00:00.000000),而app.duration字段包含所分析的文本值,根据分析仪的不同,该值可能不是您期望的你已经设置好了。

 doc['app.duration.keyword'].value
否则,下面是一个更简单的脚本来获取您想要的内容:0和1525:
    def parts = /:/.split(doc['app.duration'].value);
def total = 0;
total += Long.parseLong(parts[0]) * 24 * 60 * 1000;
total += Long.parseLong(parts[1]) * 60 * 1000;
total += Float.parseFloat(parts[2]) * 1000;
return total;
请注意,您需要将以下行添加到 elasticsearch.yml配置文件中,以启用正则表达式引擎。
script.painless.regex.enabled: true

关于elasticsearch - 将持续时间转换为总毫秒数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63677432/

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