OpsTimestamp

更新时间:
复制 MD 格式

OpsTimestamp is the Java class equivalent of the TIMESTAMP data type in OpenSearch. It stores a timestamp as a long value in milliseconds. Use it in CAVA scoring scripts to read timestamp fields from documents.

Constructor

SignatureDescription
OpsTimestamp(long timestamp)Creates an OpsTimestamp object from the given timestamp value.

Constructor details

`OpsTimestamp(long timestamp)`

Creates an OpsTimestamp object from the specified timestamp.

ParameterTypeDescription
timestamplongThe timestamp value in milliseconds. Use millisecond precision to stay consistent with OpenSearch's TIMESTAMP data type.

Methods

SignatureDescription
long getValue()Returns the timestamp value in milliseconds.

Method details

`long getValue()`

Returns the current timestamp as a long in milliseconds. OpenSearch stores TIMESTAMP values at millisecond precision.

Example

The following example shows a CAVA scorer that reads a timestamp field named date and logs its value for debugging.

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.framework.OpsTimestamp;
class BasicSimilarityScorer {
    boolean init(OpsScorerInitParams params) {
        return params.getDoc().requireAttribute("date");
    }
    double score(OpsScoreParams params) {
        OpsDoc doc = params.getDoc();
        OpsTimestamp timestamp = doc.docFieldTimestamp("date");
        if (timestamp == null) {
            doc.trace("timestamp is null");
        } else {
            doc.trace("timestamp value: ", timestamp.getValue());
        }
        return 0.0;
    }
}

getValue() returns a long in milliseconds. To compute time-based scores — for example, ranking newer documents higher — calculate the difference between the document timestamp and the current time.