use of org.opensearch.common.ParsingException 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;
}
use of org.opensearch.common.ParsingException in project OpenSearch by opensearch-project.
the class RescorerBuilder method parseFromXContent.
public static RescorerBuilder<?> parseFromXContent(XContentParser parser) throws IOException {
String fieldName = null;
RescorerBuilder<?> rescorer = null;
Integer windowSize = null;
XContentParser.Token token;
while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
if (token == XContentParser.Token.FIELD_NAME) {
fieldName = parser.currentName();
} else if (token.isValue()) {
if (WINDOW_SIZE_FIELD.match(fieldName, parser.getDeprecationHandler())) {
windowSize = parser.intValue();
} else {
throw new ParsingException(parser.getTokenLocation(), "rescore doesn't support [" + fieldName + "]");
}
} else if (token == XContentParser.Token.START_OBJECT) {
rescorer = parser.namedObject(RescorerBuilder.class, fieldName, null);
} else {
throw new ParsingException(parser.getTokenLocation(), "unexpected token [" + token + "] after [" + fieldName + "]");
}
}
if (rescorer == null) {
throw new ParsingException(parser.getTokenLocation(), "missing rescore type");
}
if (windowSize != null) {
rescorer.windowSize(windowSize.intValue());
}
return rescorer;
}
use of org.opensearch.common.ParsingException in project OpenSearch by opensearch-project.
the class SuggestionBuilder method fromXContent.
static SuggestionBuilder<?> fromXContent(XContentParser parser) throws IOException {
XContentParser.Token token;
String currentFieldName = null;
String suggestText = null;
String prefix = null;
String regex = null;
SuggestionBuilder<?> suggestionBuilder = null;
while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
if (token == XContentParser.Token.FIELD_NAME) {
currentFieldName = parser.currentName();
} else if (token.isValue()) {
if (TEXT_FIELD.match(currentFieldName, parser.getDeprecationHandler())) {
suggestText = parser.text();
} else if (PREFIX_FIELD.match(currentFieldName, parser.getDeprecationHandler())) {
prefix = parser.text();
} else if (REGEX_FIELD.match(currentFieldName, parser.getDeprecationHandler())) {
regex = parser.text();
} else {
throw new ParsingException(parser.getTokenLocation(), "suggestion does not support [" + currentFieldName + "]");
}
} else if (token == XContentParser.Token.START_OBJECT) {
suggestionBuilder = parser.namedObject(SuggestionBuilder.class, currentFieldName, null);
}
}
if (suggestionBuilder == null) {
throw new OpenSearchParseException("missing suggestion object");
}
if (suggestText != null) {
suggestionBuilder.text(suggestText);
}
if (prefix != null) {
suggestionBuilder.prefix(prefix);
}
if (regex != null) {
suggestionBuilder.regex(regex);
}
return suggestionBuilder;
}
use of org.opensearch.common.ParsingException in project OpenSearch by opensearch-project.
the class LinearInterpolation method fromXContent.
public static LinearInterpolation fromXContent(XContentParser parser) throws IOException {
XContentParser.Token token;
String fieldName = null;
double trigramLambda = 0.0;
double bigramLambda = 0.0;
double unigramLambda = 0.0;
while ((token = parser.nextToken()) != Token.END_OBJECT) {
if (token == XContentParser.Token.FIELD_NAME) {
fieldName = parser.currentName();
} else if (token.isValue()) {
if (TRIGRAM_FIELD.match(fieldName, parser.getDeprecationHandler())) {
trigramLambda = parser.doubleValue();
if (trigramLambda < 0) {
throw new IllegalArgumentException("trigram_lambda must be positive");
}
} else if (BIGRAM_FIELD.match(fieldName, parser.getDeprecationHandler())) {
bigramLambda = parser.doubleValue();
if (bigramLambda < 0) {
throw new IllegalArgumentException("bigram_lambda must be positive");
}
} else if (UNIGRAM_FIELD.match(fieldName, parser.getDeprecationHandler())) {
unigramLambda = parser.doubleValue();
if (unigramLambda < 0) {
throw new IllegalArgumentException("unigram_lambda must be positive");
}
} else {
throw new IllegalArgumentException("suggester[phrase][smoothing][linear] doesn't support field [" + fieldName + "]");
}
} else {
throw new ParsingException(parser.getTokenLocation(), "[" + NAME + "] unknown token [" + token + "] after [" + fieldName + "]");
}
}
return new LinearInterpolation(trigramLambda, bigramLambda, unigramLambda);
}
use of org.opensearch.common.ParsingException in project OpenSearch by opensearch-project.
the class ExceptionSerializationTests method testUnknownException.
public void testUnknownException() throws IOException {
ParsingException parsingException = new ParsingException(1, 2, "foobar", null);
final Exception ex = new UnknownException("eggplant", parsingException);
Exception exception = serialize(ex);
assertEquals("unknown_exception: eggplant", exception.getMessage());
assertTrue(exception instanceof OpenSearchException);
ParsingException e = (ParsingException) exception.getCause();
assertEquals(parsingException.getIndex(), e.getIndex());
assertEquals(parsingException.getMessage(), e.getMessage());
assertEquals(parsingException.getLineNumber(), e.getLineNumber());
assertEquals(parsingException.getColumnNumber(), e.getColumnNumber());
}
Aggregations