All Products
Search
Document Center

Time

Last Updated: Sep 09, 2021

Overview

The Time class provides a set of time-related functions that allow you to obtain the current time and determine how updated a document is.

Functions

Function

Description

static long now()

Obtains the current time in the UNIX time format. It is the number of seconds that have elapsed since 00:00:00 Thursday, 1 January 1970.

static float timeliness(long pubTime)

Calculates the timeliness score.

static float timelinessMs(long pubTime)

Calculates the timeliness score.

Function details

static long now()

Obtains the current time. Return value: the current time in the UNIX time format. It is the number of seconds that have elapsed since 00:00:00 Thursday, 1 January 1970. Sample code:

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)

Calculates the timeliness score of the document, which indicates how updated the document is. The closer the value of the pubTime parameter is to the current time, the greater the timeliness score is and the more updated the document is. Parameter: pubTime: the time when the document was last updated. It is the number of seconds that have elapsed since 00:00:00 Thursday, 1 January 1970. Return value: the timeliness score of the document. Valid values: [0,1]. If the value of the pubTime parameter is less than 0 or greater than or equal to the current time, the return value is 0. Sample code:

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)

Calculates the timeliness score of the document, which indicates how updated the document is. The closer the value of the pubTime parameter is to the current time, the greater the timeliness score is and the more updated the document is. Parameter: pubTime: the time when the document was last updated. It is the number of milliseconds that have elapsed since 00:00:00 Thursday, 1 January 1970. Return value: the timeliness score of the document. Valid values: [0,1]. If the value of the pubTime parameter is less than 0 or greater than or equal to the current time, the return value is 0. Sample code:

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