elasticsearch的衰减函数decay的日期类型参数

使用elasticsearch进行检索的时候有一个需求,距离现在越近的结果评分越高。决定采用function_score中的衰减函数gauss。
检索条件如下:

{
      "window_size": 20,
      "query": {
        "rescore_query": {
          "function_score": {
            "boost": 4,
            "functions": [
                {
                "gauss": {
                  "date": {
                    "origin": "2020-05-22",
                    "offset": "2y",
                    "scale": "6y"
                  }
                },
                "weight": 4
              }
            ]
          }
        }
      }
    }

offset和scale的时间单位想要使用月或者年,结果报错如下:

"caused_by": {
      "type": "illegal_argument_exception",
      "reason": "failed to parse setting [DecayFunctionParser.scale] with value [6y] as a time value: unit is missing or unrecognized",
      "caused_by": {
        "type": "illegal_argument_exception",
        "reason": "failed to parse setting [DecayFunctionParser.scale] with value [6y] as a time value: unit is missing or unrecognized"
      }
    }

官网手册和其他网站搜了半天也没有资料介绍如何设置单位为年或者月,最后在源码找到原因了,它就是不支持年或者月,最长的单位是天。
源码此处的枚举如下:

final String normalized = sValue.toLowerCase(Locale.ROOT).trim();
        if (normalized.endsWith("nanos")) {
            return new TimeValue(parse(sValue, normalized, "nanos", settingName), TimeUnit.NANOSECONDS);
        } else if (normalized.endsWith("micros")) {
            return new TimeValue(parse(sValue, normalized, "micros", settingName), TimeUnit.MICROSECONDS);
        } else if (normalized.endsWith("ms")) {
            return new TimeValue(parse(sValue, normalized, "ms", settingName), TimeUnit.MILLISECONDS);
        } else if (normalized.endsWith("s")) {
            return new TimeValue(parse(sValue, normalized, "s", settingName), TimeUnit.SECONDS);
        } else if (sValue.endsWith("m")) {
            // parsing minutes should be case-sensitive as 'M' means "months", not "minutes"; this is the only special case.
            return new TimeValue(parse(sValue, normalized, "m", settingName), TimeUnit.MINUTES);
        } else if (normalized.endsWith("h")) {
            return new TimeValue(parse(sValue, normalized, "h", settingName), TimeUnit.HOURS);
        } else if (normalized.endsWith("d")) {
            return new TimeValue(parse(sValue, normalized, "d", settingName), TimeUnit.DAYS);
        } else if (normalized.matches("-0*1")) {
            return TimeValue.MINUS_ONE;
        } else if (normalized.matches("0+")) {
            return TimeValue.ZERO;
        } else {
            // Missing units:
            throw new IllegalArgumentException("failed to parse setting [" + settingName + "] with value [" + sValue +
                    "] as a time value: unit is missing or unrecognized");
        }

附上源码地址

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值