Time

简介

Time类提供了一系列与时间操作相关的函数,用户可以通过这些函数获取当前时间,判断一个文档的新旧程度。

函数列表

函数原型

函数简介

static long now()

获取当前时间,返回从1970-01-01 00:00:00到现在的秒数

static float timeliness(long pubTime)

计算时效性分数

static float timelinessMs(long pubTime)

计算时效性分数

函数详情

static long now()

获取当前时间。返回值:返回从1970-01-01 00:00:00到现在的秒数。代码示例:

package users.scorer;
import com.aliyun.opensearch.cava.framework.OpsScoreParams;
import com.aliyun.opensearch.cava.framework.OpsScorerInitParams;
import com.aliyun.opensearch.cava.framework.OpsRequest;
import com.aliyun.opensearch.cava.framework.OpsDoc;
import com.aliyun.opensearch.cava.features.Time;

class BasicSimilarityScorer {
    boolean init(OpsScorerInitParams params) {
        return true;
    }

    double score(OpsScoreParams params) {
        long current = Time.now();
        params.getDoc().trace(current);
        return current;
    }
}

static float timeliness(long pubTime)

计算文档的时效性分数,用于衡量文档的新旧程度。pubTime越靠近当前时间,时效性分数越大,表示文档越新。参数列表:pubTime — 文档更新时间,时间单位为秒。返回值:返回文档的失效分,取值范围为[0, 1],如果pubTime小于0或者大于(或者等于)当前时间返回0。代码示例:

package users.scorer;
import com.aliyun.opensearch.cava.framework.OpsScoreParams;
import com.aliyun.opensearch.cava.framework.OpsScorerInitParams;
import com.aliyun.opensearch.cava.framework.OpsRequest;
import com.aliyun.opensearch.cava.framework.OpsDoc;
import com.aliyun.opensearch.cava.features.Time;

class BasicSimilarityScorer {
    boolean init(OpsScorerInitParams params) {
        return true;
    }

    double score(OpsScoreParams params) {
        long current = Time.now() - 1;
        double score = Time.timeliness(current);
        return score;
    }
}

static float timelinessMs(long pubTime)

计算文档的时效性分数,用于衡量文档的新旧程度。pubTime越靠近当前时间,时效性分数越大,表示文档越新。参数列表:pubTime — 文档更新时间,时间单位为毫秒。返回值:返回文档的失效分,取值范围为[0, 1],如果pubTime小于0或者大于(或者等于)当前时间返回0。代码示例:

package users.scorer;
import com.aliyun.opensearch.cava.framework.OpsScoreParams;
import com.aliyun.opensearch.cava.framework.OpsScorerInitParams;
import com.aliyun.opensearch.cava.framework.OpsRequest;
import com.aliyun.opensearch.cava.framework.OpsDoc;
import com.aliyun.opensearch.cava.features.Time;

class BasicSimilarityScorer {
    boolean init(OpsScorerInitParams params) {
        return true;
    }

    double score(OpsScoreParams params) {
        long current = (Time.now() - 1) * 1000;
        double score = Time.timelinessMs(current);
        return score;
    }
}