Time

更新时间:
复制 MD 格式

The Time class provides time-related functions for use in CAVA scoring scripts. Use these functions to get the current timestamp or to score documents based on how recently they were updated.

Functions

FunctionReturn typeDescription
static long now()longReturns the current time as a Unix epoch timestamp in seconds.
static float timeliness(long pubTime)floatReturns a timeliness score in [0, 1] based on a publication time in seconds.
static float timelinessMs(long pubTime)floatReturns a timeliness score in [0, 1] based on a publication time in milliseconds.

Function details

now()

Returns the current time as a Unix epoch timestamp — the number of seconds elapsed since 00:00:00 Thursday, January 1, 1970.

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

timeliness() and timelinessMs()

Both functions calculate a timeliness score for a document based on its publication time. A score closer to 1 indicates a more recently updated document; a score of 0 means the document is treated as not timely.

The only difference between the two functions is the unit of pubTime:

FunctionpubTime unit
timeliness(long pubTime)Seconds since Unix epoch
timelinessMs(long pubTime)Milliseconds since Unix epoch

Parameters

ParameterTypeDescription
pubTimelongThe time when the document was last updated, expressed as seconds (for timeliness) or milliseconds (for timelinessMs) since 00:00:00 Thursday, January 1, 1970.

Return value

A float in the range [0, 1]. The more recent the pubTime, the higher the score. Returns 0 if pubTime is less than 0 or greater than or equal to the current time.

Example: timeliness()

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

Example: timelinessMs()

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