Search in sources :

Example 6 with ParsingException

use of org.opensearch.common.ParsingException in project OpenSearch by opensearch-project.

the class MultiGetRequest method parseDocuments.

private static void parseDocuments(XContentParser parser, List<Item> items, @Nullable String defaultIndex, @Nullable String[] defaultFields, @Nullable FetchSourceContext defaultFetchSource, @Nullable String defaultRouting, boolean allowExplicitIndex) throws IOException {
    String currentFieldName = null;
    Token token;
    while ((token = parser.nextToken()) != Token.END_ARRAY) {
        if (token != Token.START_OBJECT) {
            throw new IllegalArgumentException("docs array element should include an object");
        }
        String index = defaultIndex;
        String id = null;
        String routing = defaultRouting;
        List<String> storedFields = null;
        long version = Versions.MATCH_ANY;
        VersionType versionType = VersionType.INTERNAL;
        FetchSourceContext fetchSourceContext = FetchSourceContext.FETCH_SOURCE;
        while ((token = parser.nextToken()) != Token.END_OBJECT) {
            if (token == Token.FIELD_NAME) {
                currentFieldName = parser.currentName();
            } else if (token.isValue()) {
                if (INDEX.match(currentFieldName, parser.getDeprecationHandler())) {
                    if (!allowExplicitIndex) {
                        throw new IllegalArgumentException("explicit index in multi get is not allowed");
                    }
                    index = parser.text();
                } else if (ID.match(currentFieldName, parser.getDeprecationHandler())) {
                    id = parser.text();
                } else if (ROUTING.match(currentFieldName, parser.getDeprecationHandler())) {
                    routing = parser.text();
                } else if (FIELDS.match(currentFieldName, parser.getDeprecationHandler())) {
                    throw new ParsingException(parser.getTokenLocation(), "Unsupported field [fields] used, expected [stored_fields] instead");
                } else if (STORED_FIELDS.match(currentFieldName, parser.getDeprecationHandler())) {
                    storedFields = new ArrayList<>();
                    storedFields.add(parser.text());
                } else if (VERSION.match(currentFieldName, parser.getDeprecationHandler())) {
                    version = parser.longValue();
                } else if (VERSION_TYPE.match(currentFieldName, parser.getDeprecationHandler())) {
                    versionType = VersionType.fromString(parser.text());
                } else if (SOURCE.match(currentFieldName, parser.getDeprecationHandler())) {
                    if (parser.isBooleanValue()) {
                        fetchSourceContext = new FetchSourceContext(parser.booleanValue(), fetchSourceContext.includes(), fetchSourceContext.excludes());
                    } else if (token == Token.VALUE_STRING) {
                        fetchSourceContext = new FetchSourceContext(fetchSourceContext.fetchSource(), new String[] { parser.text() }, fetchSourceContext.excludes());
                    } else {
                        throw new OpenSearchParseException("illegal type for _source: [{}]", token);
                    }
                } else {
                    throw new OpenSearchParseException("failed to parse multi get request. unknown field [{}]", currentFieldName);
                }
            } else if (token == Token.START_ARRAY) {
                if (FIELDS.match(currentFieldName, parser.getDeprecationHandler())) {
                    throw new ParsingException(parser.getTokenLocation(), "Unsupported field [fields] used, expected [stored_fields] instead");
                } else if (STORED_FIELDS.match(currentFieldName, parser.getDeprecationHandler())) {
                    storedFields = new ArrayList<>();
                    while ((token = parser.nextToken()) != Token.END_ARRAY) {
                        storedFields.add(parser.text());
                    }
                } else if (SOURCE.match(currentFieldName, parser.getDeprecationHandler())) {
                    ArrayList<String> includes = new ArrayList<>();
                    while ((token = parser.nextToken()) != Token.END_ARRAY) {
                        includes.add(parser.text());
                    }
                    fetchSourceContext = new FetchSourceContext(fetchSourceContext.fetchSource(), includes.toArray(Strings.EMPTY_ARRAY), fetchSourceContext.excludes());
                }
            } else if (token == Token.START_OBJECT) {
                if (SOURCE.match(currentFieldName, parser.getDeprecationHandler())) {
                    List<String> currentList = null, includes = null, excludes = null;
                    while ((token = parser.nextToken()) != Token.END_OBJECT) {
                        if (token == Token.FIELD_NAME) {
                            currentFieldName = parser.currentName();
                            if ("includes".equals(currentFieldName) || "include".equals(currentFieldName)) {
                                currentList = includes != null ? includes : (includes = new ArrayList<>(2));
                            } else if ("excludes".equals(currentFieldName) || "exclude".equals(currentFieldName)) {
                                currentList = excludes != null ? excludes : (excludes = new ArrayList<>(2));
                            } else {
                                throw new OpenSearchParseException("source definition may not contain [{}]", parser.text());
                            }
                        } else if (token == Token.START_ARRAY) {
                            while ((token = parser.nextToken()) != Token.END_ARRAY) {
                                currentList.add(parser.text());
                            }
                        } else if (token.isValue()) {
                            currentList.add(parser.text());
                        } else {
                            throw new OpenSearchParseException("unexpected token while parsing source settings");
                        }
                    }
                    fetchSourceContext = new FetchSourceContext(fetchSourceContext.fetchSource(), includes == null ? Strings.EMPTY_ARRAY : includes.toArray(new String[includes.size()]), excludes == null ? Strings.EMPTY_ARRAY : excludes.toArray(new String[excludes.size()]));
                }
            }
        }
        String[] aFields;
        if (storedFields != null) {
            aFields = storedFields.toArray(new String[storedFields.size()]);
        } else {
            aFields = defaultFields;
        }
        items.add(new Item(index, id).routing(routing).storedFields(aFields).version(version).versionType(versionType).fetchSourceContext(fetchSourceContext == FetchSourceContext.FETCH_SOURCE ? defaultFetchSource : fetchSourceContext));
    }
}
Also used : ArrayList(java.util.ArrayList) Token(org.opensearch.common.xcontent.XContentParser.Token) VersionType(org.opensearch.index.VersionType) FetchSourceContext(org.opensearch.search.fetch.subphase.FetchSourceContext) OpenSearchParseException(org.opensearch.OpenSearchParseException) ParsingException(org.opensearch.common.ParsingException) ArrayList(java.util.ArrayList) List(java.util.List)

Example 7 with ParsingException

use of org.opensearch.common.ParsingException in project OpenSearch by opensearch-project.

the class CompletionFieldMapper method parse.

/**
 * Acceptable inputs:
 *  "STRING" - interpreted as the field value (input)
 *  "OBJECT" - { "input": STRING|ARRAY, "weight": STRING|INT, "contexts": ARRAY|OBJECT }
 */
private void parse(ParseContext parseContext, Token token, XContentParser parser, Map<String, CompletionInputMetadata> inputMap) throws IOException {
    String currentFieldName = null;
    if (token == Token.VALUE_STRING) {
        inputMap.put(parser.text(), new CompletionInputMetadata(parser.text(), Collections.<String, Set<String>>emptyMap(), 1));
    } else if (token == Token.START_OBJECT) {
        Set<String> inputs = new HashSet<>();
        int weight = 1;
        Map<String, Set<String>> contextsMap = new HashMap<>();
        while ((token = parser.nextToken()) != Token.END_OBJECT) {
            if (token == Token.FIELD_NAME) {
                currentFieldName = parser.currentName();
                if (!ALLOWED_CONTENT_FIELD_NAMES.contains(currentFieldName)) {
                    throw new IllegalArgumentException("unknown field name [" + currentFieldName + "], must be one of " + ALLOWED_CONTENT_FIELD_NAMES);
                }
            } else if (currentFieldName != null) {
                if (Fields.CONTENT_FIELD_NAME_INPUT.equals(currentFieldName)) {
                    if (token == Token.VALUE_STRING) {
                        inputs.add(parser.text());
                    } else if (token == Token.START_ARRAY) {
                        while ((token = parser.nextToken()) != Token.END_ARRAY) {
                            if (token == Token.VALUE_STRING) {
                                inputs.add(parser.text());
                            } else {
                                throw new IllegalArgumentException("input array must have string values, but was [" + token.name() + "]");
                            }
                        }
                    } else {
                        throw new IllegalArgumentException("input must be a string or array, but was [" + token.name() + "]");
                    }
                } else if (Fields.CONTENT_FIELD_NAME_WEIGHT.equals(currentFieldName)) {
                    final Number weightValue;
                    if (token == Token.VALUE_STRING) {
                        try {
                            weightValue = Long.parseLong(parser.text());
                        } catch (NumberFormatException e) {
                            throw new IllegalArgumentException("weight must be an integer, but was [" + parser.text() + "]");
                        }
                    } else if (token == Token.VALUE_NUMBER) {
                        NumberType numberType = parser.numberType();
                        if (NumberType.LONG != numberType && NumberType.INT != numberType) {
                            throw new IllegalArgumentException("weight must be an integer, but was [" + parser.numberValue() + "]");
                        }
                        weightValue = parser.numberValue();
                    } else {
                        throw new IllegalArgumentException("weight must be a number or string, but was [" + token.name() + "]");
                    }
                    // always parse a long to make sure we don't get overflow
                    if (weightValue.longValue() < 0 || weightValue.longValue() > Integer.MAX_VALUE) {
                        throw new IllegalArgumentException("weight must be in the interval [0..2147483647], but was [" + weightValue.longValue() + "]");
                    }
                    weight = weightValue.intValue();
                } else if (Fields.CONTENT_FIELD_NAME_CONTEXTS.equals(currentFieldName)) {
                    if (fieldType().hasContextMappings() == false) {
                        throw new IllegalArgumentException("contexts field is not supported for field: [" + fieldType().name() + "]");
                    }
                    ContextMappings contextMappings = fieldType().getContextMappings();
                    XContentParser.Token currentToken = parser.currentToken();
                    if (currentToken == XContentParser.Token.START_OBJECT) {
                        ContextMapping contextMapping = null;
                        String fieldName = null;
                        while ((currentToken = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
                            if (currentToken == XContentParser.Token.FIELD_NAME) {
                                fieldName = parser.currentName();
                                contextMapping = contextMappings.get(fieldName);
                            } else {
                                assert fieldName != null;
                                assert !contextsMap.containsKey(fieldName);
                                contextsMap.put(fieldName, contextMapping.parseContext(parseContext, parser));
                            }
                        }
                    } else {
                        throw new IllegalArgumentException("contexts must be an object or an array , but was [" + currentToken + "]");
                    }
                }
            }
        }
        for (String input : inputs) {
            if (inputMap.containsKey(input) == false || inputMap.get(input).weight < weight) {
                inputMap.put(input, new CompletionInputMetadata(input, contextsMap, weight));
            }
        }
    } else {
        throw new ParsingException(parser.getTokenLocation(), "failed to parse [" + parser.currentName() + "]: expected text or object, but got " + token.name());
    }
}
Also used : HashSet(java.util.HashSet) Set(java.util.Set) ContextMappings(org.opensearch.search.suggest.completion.context.ContextMappings) Token(org.opensearch.common.xcontent.XContentParser.Token) NumberType(org.opensearch.common.xcontent.XContentParser.NumberType) ContextMapping(org.opensearch.search.suggest.completion.context.ContextMapping) ParsingException(org.opensearch.common.ParsingException) HashMap(java.util.HashMap) Map(java.util.Map) XContentParser(org.opensearch.common.xcontent.XContentParser)

Example 8 with ParsingException

use of org.opensearch.common.ParsingException in project OpenSearch by opensearch-project.

the class MatchQueryBuilderTests method testParseFailsWithMultipleFields.

public void testParseFailsWithMultipleFields() throws IOException {
    String json = "{\n" + "  \"match\" : {\n" + "    \"message1\" : {\n" + "      \"query\" : \"this is a test\"\n" + "    },\n" + "    \"message2\" : {\n" + "      \"query\" : \"this is a test\"\n" + "    }\n" + "  }\n" + "}";
    ParsingException e = expectThrows(ParsingException.class, () -> parseQuery(json));
    assertEquals("[match] query doesn't support multiple fields, found [message1] and [message2]", e.getMessage());
    String shortJson = "{\n" + "  \"match\" : {\n" + "    \"message1\" : \"this is a test\",\n" + "    \"message2\" : \"this is a test\"\n" + "  }\n" + "}";
    e = expectThrows(ParsingException.class, () -> parseQuery(shortJson));
    assertEquals("[match] query doesn't support multiple fields, found [message1] and [message2]", e.getMessage());
}
Also used : ParsingException(org.opensearch.common.ParsingException) Matchers.containsString(org.hamcrest.Matchers.containsString)

Example 9 with ParsingException

use of org.opensearch.common.ParsingException in project OpenSearch by opensearch-project.

the class AbstractQueryBuilderTests method testParseInnerQueryBuilderExceptions.

public void testParseInnerQueryBuilderExceptions() throws IOException {
    String source = "{ \"foo\": \"bar\" }";
    try (XContentParser parser = createParser(JsonXContent.jsonXContent, source)) {
        parser.nextToken();
        // don't start with START_OBJECT to provoke exception
        parser.nextToken();
        ParsingException exception = expectThrows(ParsingException.class, () -> parseInnerQueryBuilder(parser));
        assertEquals("[_na] query malformed, must start with start_object", exception.getMessage());
    }
    source = "{}";
    try (XContentParser parser = createParser(JsonXContent.jsonXContent, source)) {
        IllegalArgumentException exception = expectThrows(IllegalArgumentException.class, () -> parseInnerQueryBuilder(parser));
        assertEquals("query malformed, empty clause found at [1:2]", exception.getMessage());
    }
    source = "{ \"foo\" : \"bar\" }";
    try (XContentParser parser = createParser(JsonXContent.jsonXContent, source)) {
        ParsingException exception = expectThrows(ParsingException.class, () -> parseInnerQueryBuilder(parser));
        assertEquals("[foo] query malformed, no start_object after query name", exception.getMessage());
    }
    source = "{ \"boool\" : {} }";
    try (XContentParser parser = createParser(JsonXContent.jsonXContent, source)) {
        ParsingException exception = expectThrows(ParsingException.class, () -> parseInnerQueryBuilder(parser));
        assertEquals("unknown query [boool] did you mean [bool]?", exception.getMessage());
    }
    source = "{ \"match_\" : {} }";
    try (XContentParser parser = createParser(JsonXContent.jsonXContent, source)) {
        ParsingException exception = expectThrows(ParsingException.class, () -> parseInnerQueryBuilder(parser));
        assertEquals("unknown query [match_] did you mean any of [match, match_all, match_none]?", exception.getMessage());
    }
}
Also used : ParsingException(org.opensearch.common.ParsingException) XContentParser(org.opensearch.common.xcontent.XContentParser)

Example 10 with ParsingException

use of org.opensearch.common.ParsingException in project OpenSearch by opensearch-project.

the class IdsQueryBuilderTests method testIdsQueryWithInvalidValues.

// see #7686.
public void testIdsQueryWithInvalidValues() throws Exception {
    String query = "{ \"ids\": { \"values\": [[1]] } }";
    ParsingException e = expectThrows(ParsingException.class, () -> parseQuery(query));
    assertThat(e.getMessage(), containsString("[ids] failed to parse field [values]"));
}
Also used : ParsingException(org.opensearch.common.ParsingException) Matchers.containsString(org.hamcrest.Matchers.containsString)

Aggregations

ParsingException (org.opensearch.common.ParsingException)157 XContentParser (org.opensearch.common.xcontent.XContentParser)92 ArrayList (java.util.ArrayList)27 Matchers.containsString (org.hamcrest.Matchers.containsString)21 IOException (java.io.IOException)14 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 HashMap (java.util.HashMap)7 QueryBuilder (org.opensearch.index.query.QueryBuilder)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