OpsTimestamp is the object representation of the TIMESTAMP data type supported by OpenSearch. An OpsTimestamp object represents a timestamp.
Constructor
| Signature | Description |
|---|---|
OpsTimestamp(long timestamp) | Creates an OpsTimestamp object from a timestamp value. |
Parameter:
| Name | Type | Description |
|---|---|---|
timestamp | long | The timestamp value. Set this to millisecond precision to maintain consistency with the OpenSearch TIMESTAMP data type. |
Methods
| Signature | Return type | Description |
|---|---|---|
getValue() | long | Returns 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;
}
}该文章对您有帮助吗?