OpsGeoPoint

更新时间:
复制 MD 格式

OpsGeoPoint represents the GEO_POINT data type in OpenSearch. Use it in a CAVA scorer to read the longitude and latitude of a geo_point field from a document.

With OpsGeoPoint, you can:

  • Read the longitude of a geo_point field.

  • Read the latitude of a geo_point field.

  • Incorporate geographic coordinates into custom relevance scoring logic.

Constructor

SignatureDescription
OpsGeoPoint(double longitude, double latitude)Creates an OpsGeoPoint object with the given longitude and latitude.

Parameters

ParameterTypeDescription
longitudedoubleThe longitude of the point.
latitudedoubleThe latitude of the point.

Functions

SignatureReturn typeDescription
getLongitude()doubleReturns the longitude of the point.
getLatitude()doubleReturns the latitude of the point.

Example

The following example retrieves the location field as an OpsGeoPoint object and logs both the longitude and latitude.

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.OpsGeoPoint;
class BasicSimilarityScorer {
    boolean init(OpsScorerInitParams params) {
        return params.getDoc().requireAttribute("location");
    }
    double score(OpsScoreParams params) {
        OpsDoc doc = params.getDoc();
        OpsGeoPoint geopointValue = doc.docFieldGeoPoint("location");
        if (geopointValue == null) {
            doc.trace("geopoint is null");
        } else {
            doc.trace("geopoint longitude: ", geopointValue.getLongitude());
            doc.trace("geopoint latitude: ", geopointValue.getLatitude());
        }
        return 0.0;
    }
}

docFieldGeoPoint("location") returns null when the field is absent or its value is null. Always check for null before calling getLongitude() or getLatitude().