OpsTimestamp

更新时间:
复制 MD 格式

OpsTimestamp is the object representation of the TIMESTAMP data type supported by OpenSearch. An OpsTimestamp object represents a timestamp.

Constructor

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

Parameter:

NameTypeDescription
timestamplongThe timestamp value. Set this to millisecond precision to maintain consistency with the OpenSearch TIMESTAMP data type.

Methods

SignatureReturn typeDescription
getValue()longReturns the value of the current timestamp. In OpenSearch, values of the TIMESTAMP type are accurate to milliseconds.

Example

The following example shows how to read a TIMESTAMP field from a document in a custom scorer. docFieldTimestamp() returns null if the field does not exist — always check for null before calling getValue().

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