use of org.opensearch.common.ParsingException in project OpenSearch by opensearch-project.
the class AbstractQueryBuilder method parseInnerQueryBuilder.
/**
* Parses a query excluding the query element that wraps it
*/
public static QueryBuilder parseInnerQueryBuilder(XContentParser parser) throws IOException {
if (parser.currentToken() != XContentParser.Token.START_OBJECT) {
if (parser.nextToken() != XContentParser.Token.START_OBJECT) {
throw new ParsingException(parser.getTokenLocation(), "[_na] query malformed, must start with start_object");
}
}
if (parser.nextToken() == XContentParser.Token.END_OBJECT) {
// we encountered '{}' for a query clause, it used to be supported, deprecated in 5.0 and removed in 6.0
throw new IllegalArgumentException("query malformed, empty clause found at [" + parser.getTokenLocation() + "]");
}
if (parser.currentToken() != XContentParser.Token.FIELD_NAME) {
throw new ParsingException(parser.getTokenLocation(), "[_na] query malformed, no field after start_object");
}
String queryName = parser.currentName();
// move to the next START_OBJECT
if (parser.nextToken() != XContentParser.Token.START_OBJECT) {
throw new ParsingException(parser.getTokenLocation(), "[" + queryName + "] query malformed, no start_object after query name");
}
QueryBuilder result;
try {
result = parser.namedObject(QueryBuilder.class, queryName, null);
} catch (NamedObjectNotFoundException e) {
String message = String.format(Locale.ROOT, "unknown query [%s]%s", queryName, SuggestingErrorOnUnknown.suggest(queryName, e.getCandidates()));
throw new ParsingException(new XContentLocation(e.getLineNumber(), e.getColumnNumber()), message, e);
}
// end_object of the specific query (e.g. match, multi_match etc.) element
if (parser.currentToken() != XContentParser.Token.END_OBJECT) {
throw new ParsingException(parser.getTokenLocation(), "[" + queryName + "] malformed query, expected [END_OBJECT] but found [" + parser.currentToken() + "]");
}
// end_object of the query object
if (parser.nextToken() != XContentParser.Token.END_OBJECT) {
throw new ParsingException(parser.getTokenLocation(), "[" + queryName + "] malformed query, expected [END_OBJECT] but found [" + parser.currentToken() + "]");
}
return result;
}
use of org.opensearch.common.ParsingException in project OpenSearch by opensearch-project.
the class ConstantScoreQueryBuilder method fromXContent.
public static ConstantScoreQueryBuilder fromXContent(XContentParser parser) throws IOException {
QueryBuilder query = null;
boolean queryFound = false;
String queryName = null;
float boost = AbstractQueryBuilder.DEFAULT_BOOST;
String currentFieldName = null;
XContentParser.Token token;
while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
if (token == XContentParser.Token.FIELD_NAME) {
currentFieldName = parser.currentName();
} else if (token == XContentParser.Token.START_OBJECT) {
if (INNER_QUERY_FIELD.match(currentFieldName, parser.getDeprecationHandler())) {
query = parseInnerQueryBuilder(parser);
queryFound = true;
} else {
throw new ParsingException(parser.getTokenLocation(), "[constant_score] query does not support [" + currentFieldName + "]");
}
} else if (token.isValue()) {
if (AbstractQueryBuilder.NAME_FIELD.match(currentFieldName, parser.getDeprecationHandler())) {
queryName = parser.text();
} else if (AbstractQueryBuilder.BOOST_FIELD.match(currentFieldName, parser.getDeprecationHandler())) {
boost = parser.floatValue();
} else {
throw new ParsingException(parser.getTokenLocation(), "[constant_score] query does not support [" + currentFieldName + "]");
}
} else {
throw new ParsingException(parser.getTokenLocation(), "unexpected token [" + token + "]");
}
}
if (!queryFound) {
throw new ParsingException(parser.getTokenLocation(), "[constant_score] requires a 'filter' element");
}
ConstantScoreQueryBuilder constantScoreBuilder = new ConstantScoreQueryBuilder(query);
constantScoreBuilder.boost(boost);
constantScoreBuilder.queryName(queryName);
return constantScoreBuilder;
}
use of org.opensearch.common.ParsingException in project OpenSearch by opensearch-project.
the class GeoBoundingBoxQueryBuilder method fromXContent.
public static GeoBoundingBoxQueryBuilder fromXContent(XContentParser parser) throws IOException {
String fieldName = null;
float boost = AbstractQueryBuilder.DEFAULT_BOOST;
String queryName = null;
String currentFieldName = null;
XContentParser.Token token;
GeoValidationMethod validationMethod = null;
boolean ignoreUnmapped = DEFAULT_IGNORE_UNMAPPED;
// bottom (minLat), top (maxLat), left (minLon), right (maxLon)
GeoBoundingBox bbox = null;
String type = "memory";
while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
if (token == XContentParser.Token.FIELD_NAME) {
currentFieldName = parser.currentName();
} else if (token == XContentParser.Token.START_OBJECT) {
try {
bbox = GeoBoundingBox.parseBoundingBox(parser);
fieldName = currentFieldName;
} catch (Exception e) {
throw new OpenSearchParseException("failed to parse [{}] query. [{}]", NAME, e.getMessage());
}
} else if (token.isValue()) {
if (AbstractQueryBuilder.NAME_FIELD.match(currentFieldName, parser.getDeprecationHandler())) {
queryName = parser.text();
} else if (AbstractQueryBuilder.BOOST_FIELD.match(currentFieldName, parser.getDeprecationHandler())) {
boost = parser.floatValue();
} else if (VALIDATION_METHOD_FIELD.match(currentFieldName, parser.getDeprecationHandler())) {
validationMethod = GeoValidationMethod.fromString(parser.text());
} else if (IGNORE_UNMAPPED_FIELD.match(currentFieldName, parser.getDeprecationHandler())) {
ignoreUnmapped = parser.booleanValue();
} else if (TYPE_FIELD.match(currentFieldName, parser.getDeprecationHandler())) {
type = parser.text();
} else {
throw new ParsingException(parser.getTokenLocation(), "failed to parse [{}] query. unexpected field [{}]", NAME, currentFieldName);
}
}
}
if (bbox == null) {
throw new OpenSearchParseException("failed to parse [{}] query. bounding box not provided", NAME);
}
GeoBoundingBoxQueryBuilder builder = new GeoBoundingBoxQueryBuilder(fieldName);
builder.setCorners(bbox.topLeft(), bbox.bottomRight());
builder.queryName(queryName);
builder.boost(boost);
builder.type(GeoExecType.fromString(type));
builder.ignoreUnmapped(ignoreUnmapped);
if (validationMethod != null) {
// ignore deprecated coerce/ignoreMalformed settings if validationMethod is set
builder.setValidationMethod(validationMethod);
}
return builder;
}
use of org.opensearch.common.ParsingException in project OpenSearch by opensearch-project.
the class MoreLikeThisQueryBuilder method fromXContent.
public static MoreLikeThisQueryBuilder fromXContent(XContentParser parser) throws IOException {
// document inputs
List<String> fields = null;
List<String> likeTexts = new ArrayList<>();
List<String> unlikeTexts = new ArrayList<>();
List<Item> likeItems = new ArrayList<>();
List<Item> unlikeItems = new ArrayList<>();
// term selection parameters
int maxQueryTerms = MoreLikeThisQueryBuilder.DEFAULT_MAX_QUERY_TERMS;
int minTermFreq = MoreLikeThisQueryBuilder.DEFAULT_MIN_TERM_FREQ;
int minDocFreq = MoreLikeThisQueryBuilder.DEFAULT_MIN_DOC_FREQ;
int maxDocFreq = MoreLikeThisQueryBuilder.DEFAULT_MAX_DOC_FREQ;
int minWordLength = MoreLikeThisQueryBuilder.DEFAULT_MIN_WORD_LENGTH;
int maxWordLength = MoreLikeThisQueryBuilder.DEFAULT_MAX_WORD_LENGTH;
List<String> stopWords = null;
String analyzer = null;
// query formation parameters
String minimumShouldMatch = MoreLikeThisQueryBuilder.DEFAULT_MINIMUM_SHOULD_MATCH;
float boostTerms = MoreLikeThisQueryBuilder.DEFAULT_BOOST_TERMS;
boolean include = MoreLikeThisQueryBuilder.DEFAULT_INCLUDE;
// other parameters
boolean failOnUnsupportedField = MoreLikeThisQueryBuilder.DEFAULT_FAIL_ON_UNSUPPORTED_FIELDS;
float boost = AbstractQueryBuilder.DEFAULT_BOOST;
String queryName = null;
XContentParser.Token token;
String currentFieldName = null;
while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
if (token == XContentParser.Token.FIELD_NAME) {
currentFieldName = parser.currentName();
} else if (token.isValue()) {
if (LIKE.match(currentFieldName, parser.getDeprecationHandler())) {
parseLikeField(parser, likeTexts, likeItems);
} else if (UNLIKE.match(currentFieldName, parser.getDeprecationHandler())) {
parseLikeField(parser, unlikeTexts, unlikeItems);
} else if (MAX_QUERY_TERMS.match(currentFieldName, parser.getDeprecationHandler())) {
maxQueryTerms = parser.intValue();
} else if (MIN_TERM_FREQ.match(currentFieldName, parser.getDeprecationHandler())) {
minTermFreq = parser.intValue();
} else if (MIN_DOC_FREQ.match(currentFieldName, parser.getDeprecationHandler())) {
minDocFreq = parser.intValue();
} else if (MAX_DOC_FREQ.match(currentFieldName, parser.getDeprecationHandler())) {
maxDocFreq = parser.intValue();
} else if (MIN_WORD_LENGTH.match(currentFieldName, parser.getDeprecationHandler())) {
minWordLength = parser.intValue();
} else if (MAX_WORD_LENGTH.match(currentFieldName, parser.getDeprecationHandler())) {
maxWordLength = parser.intValue();
} else if (ANALYZER.match(currentFieldName, parser.getDeprecationHandler())) {
analyzer = parser.text();
} else if (MINIMUM_SHOULD_MATCH.match(currentFieldName, parser.getDeprecationHandler())) {
minimumShouldMatch = parser.text();
} else if (BOOST_TERMS.match(currentFieldName, parser.getDeprecationHandler())) {
boostTerms = parser.floatValue();
} else if (INCLUDE.match(currentFieldName, parser.getDeprecationHandler())) {
include = parser.booleanValue();
} else if (FAIL_ON_UNSUPPORTED_FIELD.match(currentFieldName, parser.getDeprecationHandler())) {
failOnUnsupportedField = parser.booleanValue();
} else if ("boost".equals(currentFieldName)) {
boost = parser.floatValue();
} else if ("_name".equals(currentFieldName)) {
queryName = parser.text();
} else {
throw new ParsingException(parser.getTokenLocation(), "[mlt] query does not support [" + currentFieldName + "]");
}
} else if (token == XContentParser.Token.START_ARRAY) {
if (FIELDS.match(currentFieldName, parser.getDeprecationHandler())) {
fields = new ArrayList<>();
while ((token = parser.nextToken()) != XContentParser.Token.END_ARRAY) {
fields.add(parser.text());
}
} else if (LIKE.match(currentFieldName, parser.getDeprecationHandler())) {
while ((token = parser.nextToken()) != XContentParser.Token.END_ARRAY) {
parseLikeField(parser, likeTexts, likeItems);
}
} else if (UNLIKE.match(currentFieldName, parser.getDeprecationHandler())) {
while ((token = parser.nextToken()) != XContentParser.Token.END_ARRAY) {
parseLikeField(parser, unlikeTexts, unlikeItems);
}
} else if (STOP_WORDS.match(currentFieldName, parser.getDeprecationHandler())) {
stopWords = new ArrayList<>();
while ((token = parser.nextToken()) != XContentParser.Token.END_ARRAY) {
stopWords.add(parser.text());
}
} else {
throw new ParsingException(parser.getTokenLocation(), "[mlt] query does not support [" + currentFieldName + "]");
}
} else if (token == XContentParser.Token.START_OBJECT) {
if (LIKE.match(currentFieldName, parser.getDeprecationHandler())) {
parseLikeField(parser, likeTexts, likeItems);
} else if (UNLIKE.match(currentFieldName, parser.getDeprecationHandler())) {
parseLikeField(parser, unlikeTexts, unlikeItems);
} else {
throw new ParsingException(parser.getTokenLocation(), "[mlt] query does not support [" + currentFieldName + "]");
}
}
}
if (likeTexts.isEmpty() && likeItems.isEmpty()) {
throw new ParsingException(parser.getTokenLocation(), "more_like_this requires 'like' to be specified");
}
if (fields != null && fields.isEmpty()) {
throw new ParsingException(parser.getTokenLocation(), "more_like_this requires 'fields' to be non-empty");
}
String[] fieldsArray = fields == null ? null : fields.toArray(new String[fields.size()]);
String[] likeTextsArray = likeTexts.isEmpty() ? null : likeTexts.toArray(new String[likeTexts.size()]);
String[] unlikeTextsArray = unlikeTexts.isEmpty() ? null : unlikeTexts.toArray(new String[unlikeTexts.size()]);
Item[] likeItemsArray = likeItems.isEmpty() ? null : likeItems.toArray(new Item[likeItems.size()]);
Item[] unlikeItemsArray = unlikeItems.isEmpty() ? null : unlikeItems.toArray(new Item[unlikeItems.size()]);
MoreLikeThisQueryBuilder moreLikeThisQueryBuilder = new MoreLikeThisQueryBuilder(fieldsArray, likeTextsArray, likeItemsArray).unlike(unlikeTextsArray).unlike(unlikeItemsArray).maxQueryTerms(maxQueryTerms).minTermFreq(minTermFreq).minDocFreq(minDocFreq).maxDocFreq(maxDocFreq).minWordLength(minWordLength).maxWordLength(maxWordLength).analyzer(analyzer).minimumShouldMatch(minimumShouldMatch).boostTerms(boostTerms).include(include).failOnUnsupportedField(failOnUnsupportedField).boost(boost).queryName(queryName);
if (stopWords != null) {
moreLikeThisQueryBuilder.stopWords(stopWords);
}
return moreLikeThisQueryBuilder;
}
use of org.opensearch.common.ParsingException in project OpenSearch by opensearch-project.
the class RestActions method parseTopLevelQueryBuilder.
/**
* Parses a top level query including the query element that wraps it
*/
private static QueryBuilder parseTopLevelQueryBuilder(String fieldName, XContentParser parser) {
try {
QueryBuilder queryBuilder = null;
XContentParser.Token first = parser.nextToken();
if (first == null) {
return null;
} else if (first != XContentParser.Token.START_OBJECT) {
throw new ParsingException(parser.getTokenLocation(), "Expected [" + XContentParser.Token.START_OBJECT + "] but found [" + first + "]", parser.getTokenLocation());
}
for (XContentParser.Token token = parser.nextToken(); token != XContentParser.Token.END_OBJECT; token = parser.nextToken()) {
if (token == XContentParser.Token.FIELD_NAME) {
String currentName = parser.currentName();
if (fieldName.equals(currentName)) {
queryBuilder = parseInnerQueryBuilder(parser);
} else {
throw new ParsingException(parser.getTokenLocation(), "request does not support [" + parser.currentName() + "]");
}
}
}
return queryBuilder;
} catch (ParsingException e) {
throw e;
} catch (Exception e) {
throw new ParsingException(parser == null ? null : parser.getTokenLocation(), "Failed to parse", e);
}
}
Aggregations