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");
        }

附上源码地址

你可能感兴趣的:(elasticsearch)