Search in sources :

Example 91 with ParsingException

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;
}
Also used : ParsingException(org.opensearch.common.ParsingException) NamedObjectNotFoundException(org.opensearch.common.xcontent.NamedObjectNotFoundException) XContentLocation(org.opensearch.common.xcontent.XContentLocation)

Example 92 with ParsingException

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;
}
Also used : ParsingException(org.opensearch.common.ParsingException) XContentParser(org.opensearch.common.xcontent.XContentParser)

Example 93 with ParsingException

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;
}
Also used : OpenSearchParseException(org.opensearch.OpenSearchParseException) ParsingException(org.opensearch.common.ParsingException) GeoBoundingBox(org.opensearch.common.geo.GeoBoundingBox) XContentParser(org.opensearch.common.xcontent.XContentParser) OpenSearchParseException(org.opensearch.OpenSearchParseException) IOException(java.io.IOException) ParsingException(org.opensearch.common.ParsingException)

Example 94 with ParsingException

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;
}
Also used : ArrayList(java.util.ArrayList) ParsingException(org.opensearch.common.ParsingException) XContentParser(org.opensearch.common.xcontent.XContentParser)

Example 95 with ParsingException

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);
    }
}
Also used : ParsingException(org.opensearch.common.ParsingException) AbstractQueryBuilder.parseInnerQueryBuilder(org.opensearch.index.query.AbstractQueryBuilder.parseInnerQueryBuilder) QueryStringQueryBuilder(org.opensearch.index.query.QueryStringQueryBuilder) QueryBuilder(org.opensearch.index.query.QueryBuilder) XContentParser(org.opensearch.common.xcontent.XContentParser) ShardOperationFailedException(org.opensearch.action.ShardOperationFailedException) ParsingException(org.opensearch.common.ParsingException) FailedNodeException(org.opensearch.action.FailedNodeException) IOException(java.io.IOException)

Aggregations

ParsingException (org.opensearch.common.ParsingException)160 XContentParser (org.opensearch.common.xcontent.XContentParser)94 ArrayList (java.util.ArrayList)28 Matchers.containsString (org.hamcrest.Matchers.containsString)21 IOException (java.io.IOException)15 Token (org.opensearch.common.xcontent.XContentParser.Token)11 List (java.util.List)10 ShardId (org.opensearch.index.shard.ShardId)10 SearchShardTarget (org.opensearch.search.SearchShardTarget)10 BytesReference (org.opensearch.common.bytes.BytesReference)9 XContentBuilder (org.opensearch.common.xcontent.XContentBuilder)9 QueryBuilder (org.opensearch.index.query.QueryBuilder)9 HashMap (java.util.HashMap)7 ShardSearchFailure (org.opensearch.action.search.ShardSearchFailure)6 CoreMatchers.containsString (org.hamcrest.CoreMatchers.containsString)5 OpenSearchParseException (org.opensearch.OpenSearchParseException)5 TimestampParsingException (org.opensearch.action.TimestampParsingException)5 XContentLocation (org.opensearch.common.xcontent.XContentLocation)5 Script (org.opensearch.script.Script)5 GapPolicy (org.opensearch.search.aggregations.pipeline.BucketHelpers.GapPolicy)5