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
| Signature | Description |
|---|---|
OpsGeoPoint(double longitude, double latitude) | Creates an OpsGeoPoint object with the given longitude and latitude. |
Parameters
| Parameter | Type | Description |
|---|---|---|
longitude | double | The longitude of the point. |
latitude | double | The latitude of the point. |
Functions
| Signature | Return type | Description |
|---|---|---|
getLongitude() | double | Returns the longitude of the point. |
getLatitude() | double | Returns 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().
该文章对您有帮助吗?