Search in sources :

Example 1 with NamedObjectNotFoundException

use of org.opensearch.common.xcontent.NamedObjectNotFoundException in project OpenSearch by opensearch-project.

the class AggregatorFactories method parseAggregators.

private static AggregatorFactories.Builder parseAggregators(XContentParser parser, int level) throws IOException {
    Matcher validAggMatcher = VALID_AGG_NAME.matcher("");
    AggregatorFactories.Builder factories = new AggregatorFactories.Builder();
    XContentParser.Token token = null;
    while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
        if (token != XContentParser.Token.FIELD_NAME) {
            throw new ParsingException(parser.getTokenLocation(), "Unexpected token " + token + " in [aggs]: aggregations definitions must start with the name of the aggregation.");
        }
        final String aggregationName = parser.currentName();
        if (!validAggMatcher.reset(aggregationName).matches()) {
            throw new ParsingException(parser.getTokenLocation(), "Invalid aggregation name [" + aggregationName + "]. Aggregation names can contain any character except '[', ']', and '>'");
        }
        token = parser.nextToken();
        if (token != XContentParser.Token.START_OBJECT) {
            throw new ParsingException(parser.getTokenLocation(), "Aggregation definition for [" + aggregationName + " starts with a [" + token + "], expected a [" + XContentParser.Token.START_OBJECT + "].");
        }
        BaseAggregationBuilder aggBuilder = null;
        AggregatorFactories.Builder subFactories = null;
        Map<String, Object> metadata = null;
        while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
            if (token != XContentParser.Token.FIELD_NAME) {
                throw new ParsingException(parser.getTokenLocation(), "Expected [" + XContentParser.Token.FIELD_NAME + "] under a [" + XContentParser.Token.START_OBJECT + "], but got a [" + token + "] in [" + aggregationName + "]", parser.getTokenLocation());
            }
            final String fieldName = parser.currentName();
            token = parser.nextToken();
            if (token == XContentParser.Token.START_OBJECT) {
                switch(fieldName) {
                    case "meta":
                        metadata = parser.map();
                        break;
                    case "aggregations":
                    case "aggs":
                        if (subFactories != null) {
                            throw new ParsingException(parser.getTokenLocation(), "Found two sub aggregation definitions under [" + aggregationName + "]");
                        }
                        subFactories = parseAggregators(parser, level + 1);
                        break;
                    default:
                        if (aggBuilder != null) {
                            throw new ParsingException(parser.getTokenLocation(), "Found two aggregation type definitions in [" + aggregationName + "]: [" + aggBuilder.getType() + "] and [" + fieldName + "]");
                        }
                        try {
                            aggBuilder = parser.namedObject(BaseAggregationBuilder.class, fieldName, aggregationName);
                        } catch (NamedObjectNotFoundException ex) {
                            String message = String.format(Locale.ROOT, "Unknown aggregation type [%s]%s", fieldName, SuggestingErrorOnUnknown.suggest(fieldName, ex.getCandidates()));
                            throw new ParsingException(new XContentLocation(ex.getLineNumber(), ex.getColumnNumber()), message, ex);
                        }
                }
            } else {
                throw new ParsingException(parser.getTokenLocation(), "Expected [" + XContentParser.Token.START_OBJECT + "] under [" + fieldName + "], but got a [" + token + "] in [" + aggregationName + "]");
            }
        }
        if (aggBuilder == null) {
            throw new ParsingException(parser.getTokenLocation(), "Missing definition for aggregation [" + aggregationName + "]", parser.getTokenLocation());
        } else {
            if (metadata != null) {
                aggBuilder.setMetadata(metadata);
            }
            if (subFactories != null) {
                aggBuilder.subAggregations(subFactories);
            }
            if (aggBuilder instanceof AggregationBuilder) {
                factories.addAggregator((AggregationBuilder) aggBuilder);
            } else {
                factories.addPipelineAggregator((PipelineAggregationBuilder) aggBuilder);
            }
        }
    }
    return factories;
}
Also used : GlobalAggregationBuilder(org.opensearch.search.aggregations.bucket.global.GlobalAggregationBuilder) TermsAggregationBuilder(org.opensearch.search.aggregations.bucket.terms.TermsAggregationBuilder) Matcher(java.util.regex.Matcher) GlobalAggregationBuilder(org.opensearch.search.aggregations.bucket.global.GlobalAggregationBuilder) XContentBuilder(org.opensearch.common.xcontent.XContentBuilder) TermsAggregationBuilder(org.opensearch.search.aggregations.bucket.terms.TermsAggregationBuilder) XContentLocation(org.opensearch.common.xcontent.XContentLocation) ParsingException(org.opensearch.common.ParsingException) NamedObjectNotFoundException(org.opensearch.common.xcontent.NamedObjectNotFoundException) ToXContentObject(org.opensearch.common.xcontent.ToXContentObject) XContentParser(org.opensearch.common.xcontent.XContentParser)

Example 2 with NamedObjectNotFoundException

use of org.opensearch.common.xcontent.NamedObjectNotFoundException 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 3 with NamedObjectNotFoundException

use of org.opensearch.common.xcontent.NamedObjectNotFoundException in project OpenSearch by opensearch-project.

the class SuggestionTests method testUnknownSuggestionTypeThrows.

public void testUnknownSuggestionTypeThrows() throws IOException {
    XContent xContent = JsonXContent.jsonXContent;
    String suggestionString = ("{" + "  \"unknownType#suggestionName\": [" + "    {" + "      \"text\": \"entryText\"," + "      \"offset\": 42," + "      \"length\": 313," + "      \"options\": [" + "        {" + "          \"text\": \"someText\"," + "          \"highlighted\": \"somethingHighlighted\"," + "          \"score\": 1.3," + "          \"collate_match\": true" + "        }" + "      ]" + "    }" + "  ]" + "}").replaceAll("\\s+", "");
    try (XContentParser parser = xContent.createParser(xContentRegistry(), DeprecationHandler.THROW_UNSUPPORTED_OPERATION, suggestionString)) {
        ensureExpectedToken(XContentParser.Token.START_OBJECT, parser.nextToken(), parser);
        ensureExpectedToken(XContentParser.Token.FIELD_NAME, parser.nextToken(), parser);
        ensureExpectedToken(XContentParser.Token.START_ARRAY, parser.nextToken(), parser);
        NamedObjectNotFoundException e = expectThrows(NamedObjectNotFoundException.class, () -> Suggestion.fromXContent(parser));
        assertEquals("[1:31] unknown field [unknownType]", e.getMessage());
    }
}
Also used : ToXContent(org.opensearch.common.xcontent.ToXContent) XContentHelper.toXContent(org.opensearch.common.xcontent.XContentHelper.toXContent) XContent(org.opensearch.common.xcontent.XContent) JsonXContent(org.opensearch.common.xcontent.json.JsonXContent) NamedObjectNotFoundException(org.opensearch.common.xcontent.NamedObjectNotFoundException) XContentParser(org.opensearch.common.xcontent.XContentParser)

Aggregations

NamedObjectNotFoundException (org.opensearch.common.xcontent.NamedObjectNotFoundException)3 ParsingException (org.opensearch.common.ParsingException)2 XContentLocation (org.opensearch.common.xcontent.XContentLocation)2 XContentParser (org.opensearch.common.xcontent.XContentParser)2 Matcher (java.util.regex.Matcher)1 ToXContent (org.opensearch.common.xcontent.ToXContent)1 ToXContentObject (org.opensearch.common.xcontent.ToXContentObject)1 XContent (org.opensearch.common.xcontent.XContent)1 XContentBuilder (org.opensearch.common.xcontent.XContentBuilder)1 XContentHelper.toXContent (org.opensearch.common.xcontent.XContentHelper.toXContent)1 JsonXContent (org.opensearch.common.xcontent.json.JsonXContent)1 GlobalAggregationBuilder (org.opensearch.search.aggregations.bucket.global.GlobalAggregationBuilder)1 TermsAggregationBuilder (org.opensearch.search.aggregations.bucket.terms.TermsAggregationBuilder)1