OpsGeoPoint

更新时间:
复制 MD 格式

OpsGeoPoint represents a geographic coordinate point, corresponding to the GEO_POINT field type in OpenSearch. Use it in a custom CAVA scorer to read a document's geo_point field and incorporate location data into your scoring logic — for example, to apply distance-based scoring decay or to down-rank documents with missing location data.

Constructor

SignatureDescription
OpsGeoPoint(double longitude, double latitude)Creates an OpsGeoPoint object from the given longitude and latitude values

Parameters:

ParameterTypeDescription
longitudedoubleLongitude of the point
latitudedoubleLatitude of the point

Methods

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

Read a geo_point field in a scorer

Call doc.docFieldGeoPoint(fieldName) to retrieve a document's GEO_POINT field as an OpsGeoPoint object. The method returns null if the field is missing or has no value — check for null before accessing the coordinates.

The following example reads a location field 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) {
        // Register the location field as a required attribute
        return params.getDoc().requireAttribute("location");
    }

    double score(OpsScoreParams params) {
        OpsDoc doc = params.getDoc();
        OpsGeoPoint geopointValue = doc.docFieldGeoPoint("location");

        if (geopointValue == null) {
            // Field is missing or has no value — handle accordingly
            doc.trace("geopoint is null");
        } else {
            doc.trace("longitude: ", geopointValue.getLongitude());
            doc.trace("latitude: ", geopointValue.getLatitude());
        }

        return 0.0;
    }
}