use of org.opensearch.index.query.GeoValidationMethod in project OpenSearch by opensearch-project.
the class GeoDistanceSortBuilder method fromXContent.
/**
* Creates a new {@link GeoDistanceSortBuilder} from the query held by the {@link XContentParser} in
* {@link org.opensearch.common.xcontent.XContent} format.
*
* @param parser the input parser. The state on the parser contained in this context will be changed as a
* side effect of this method call
* @param elementName in some sort syntax variations the field name precedes the xContent object that specifies
* further parameters, e.g. in '{ "foo": { "order" : "asc"} }'. When parsing the inner object,
* the field name can be passed in via this argument
*/
public static GeoDistanceSortBuilder fromXContent(XContentParser parser, String elementName) throws IOException {
String fieldName = null;
List<GeoPoint> geoPoints = new ArrayList<>();
DistanceUnit unit = DistanceUnit.DEFAULT;
GeoDistance geoDistance = GeoDistance.ARC;
SortOrder order = SortOrder.ASC;
SortMode sortMode = null;
QueryBuilder nestedFilter = null;
String nestedPath = null;
NestedSortBuilder nestedSort = null;
GeoValidationMethod validation = null;
boolean ignoreUnmapped = false;
XContentParser.Token token;
String currentName = parser.currentName();
while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
if (token == XContentParser.Token.FIELD_NAME) {
currentName = parser.currentName();
} else if (token == XContentParser.Token.START_ARRAY) {
parseGeoPoints(parser, geoPoints);
fieldName = currentName;
} else if (token == XContentParser.Token.START_OBJECT) {
if (NESTED_FILTER_FIELD.match(currentName, parser.getDeprecationHandler())) {
deprecationLogger.deprecate("geo_distance_nested_filter", "[nested_filter] has been deprecated in favour of the [nested] parameter");
nestedFilter = parseInnerQueryBuilder(parser);
} else if (NESTED_FIELD.match(currentName, parser.getDeprecationHandler())) {
nestedSort = NestedSortBuilder.fromXContent(parser);
} else {
// the json in the format of -> field : { lat : 30, lon : 12 }
if (fieldName != null && fieldName.equals(currentName) == false) {
throw new ParsingException(parser.getTokenLocation(), "Trying to reset fieldName to [{}], already set to [{}].", currentName, fieldName);
}
fieldName = currentName;
GeoPoint point = new GeoPoint();
GeoUtils.parseGeoPoint(parser, point);
geoPoints.add(point);
}
} else if (token.isValue()) {
if (ORDER_FIELD.match(currentName, parser.getDeprecationHandler())) {
order = SortOrder.fromString(parser.text());
} else if (UNIT_FIELD.match(currentName, parser.getDeprecationHandler())) {
unit = DistanceUnit.fromString(parser.text());
} else if (DISTANCE_TYPE_FIELD.match(currentName, parser.getDeprecationHandler())) {
geoDistance = GeoDistance.fromString(parser.text());
} else if (VALIDATION_METHOD_FIELD.match(currentName, parser.getDeprecationHandler())) {
validation = GeoValidationMethod.fromString(parser.text());
} else if (SORTMODE_FIELD.match(currentName, parser.getDeprecationHandler())) {
sortMode = SortMode.fromString(parser.text());
} else if (NESTED_PATH_FIELD.match(currentName, parser.getDeprecationHandler())) {
deprecationLogger.deprecate("geo_distance_nested_path", "[nested_path] has been deprecated in favour of the [nested] parameter");
nestedPath = parser.text();
} else if (IGNORE_UNMAPPED.match(currentName, parser.getDeprecationHandler())) {
ignoreUnmapped = parser.booleanValue();
} else if (token == Token.VALUE_STRING) {
if (fieldName != null && fieldName.equals(currentName) == false) {
throw new ParsingException(parser.getTokenLocation(), "Trying to reset fieldName to [{}], already set to [{}].", currentName, fieldName);
}
GeoPoint point = new GeoPoint();
point.resetFromString(parser.text());
geoPoints.add(point);
fieldName = currentName;
} else if (fieldName.equals(currentName)) {
throw new ParsingException(parser.getTokenLocation(), "Only geohashes of type string supported for field [{}]", currentName);
} else {
throw new ParsingException(parser.getTokenLocation(), "[{}] does not support [{}]", NAME, currentName);
}
}
}
GeoDistanceSortBuilder result = new GeoDistanceSortBuilder(fieldName, geoPoints.toArray(new GeoPoint[geoPoints.size()]));
result.geoDistance(geoDistance);
result.unit(unit);
result.order(order);
if (sortMode != null) {
result.sortMode(sortMode);
}
if (nestedFilter != null) {
result.setNestedFilter(nestedFilter);
}
result.setNestedPath(nestedPath);
if (nestedSort != null) {
result.setNestedSort(nestedSort);
}
if (validation != null) {
result.validation(validation);
}
result.ignoreUnmapped(ignoreUnmapped);
return result;
}
Aggregations