Search in sources :

Example 16 with ParsingException

use of org.elasticsearch.common.ParsingException in project elasticsearch by elastic.

the class ConstantScoreQueryBuilderTests method testNoArrayAsFilterElements.

/**
     * test that "filter" does not accept an array of queries, throws {@link ParsingException}
     */
public void testNoArrayAsFilterElements() throws IOException {
    String queryString = "{ \"" + ConstantScoreQueryBuilder.NAME + "\" : {\n" + "\"filter\" : [ { \"term\": { \"foo\": \"a\" } },\n" + "{ \"term\": { \"foo\": \"x\" } } ]\n" + "} }";
    ParsingException e = expectThrows(ParsingException.class, () -> parseQuery(queryString));
    assertThat(e.getMessage(), containsString("unexpected token [START_ARRAY]"));
}
Also used : ParsingException(org.elasticsearch.common.ParsingException) Matchers.containsString(org.hamcrest.Matchers.containsString)

Example 17 with ParsingException

use of org.elasticsearch.common.ParsingException in project elasticsearch by elastic.

the class FuzzyQueryBuilderTests method testParseFailsWithMultipleFields.

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

Example 18 with ParsingException

use of org.elasticsearch.common.ParsingException in project elasticsearch by elastic.

the class BoolQueryBuilderTests method testTooManyQueriesInObject.

/**
     * test that two queries in object throws error
     */
public void testTooManyQueriesInObject() throws IOException {
    assumeFalse("Test only makes sense if XContent parser doesn't have strict duplicate checks enabled", XContent.isStrictDuplicateDetectionEnabled());
    String clauseType = randomFrom("must", "should", "must_not", "filter");
    // should also throw error if invalid query is preceded by a valid one
    String query = "{\n" + "  \"bool\": {\n" + "    \"" + clauseType + "\": {\n" + "      \"match\": {\n" + "        \"foo\": \"bar\"\n" + "      },\n" + "      \"match\": {\n" + "        \"baz\": \"buzz\"\n" + "      }\n" + "    }\n" + "  }\n" + "}";
    ParsingException ex = expectThrows(ParsingException.class, () -> parseQuery(query));
    assertEquals("[match] malformed query, expected [END_OBJECT] but found [FIELD_NAME]", ex.getMessage());
}
Also used : ParsingException(org.elasticsearch.common.ParsingException)

Example 19 with ParsingException

use of org.elasticsearch.common.ParsingException in project elasticsearch by elastic.

the class QueryStringQueryBuilderTests method testAllFieldsWithFields.

public void testAllFieldsWithFields() throws IOException {
    String json = "{\n" + "  \"query_string\" : {\n" + "    \"query\" : \"this AND that OR thus\",\n" + "    \"fields\" : [\"foo\"],\n" + "    \"all_fields\" : true\n" + "  }\n" + "}";
    ParsingException e = expectThrows(ParsingException.class, () -> parseQuery(json));
    assertThat(e.getMessage(), containsString("cannot use [all_fields] parameter in conjunction with [default_field] or [fields]"));
    String json2 = "{\n" + "  \"query_string\" : {\n" + "    \"query\" : \"this AND that OR thus\",\n" + "    \"default_field\" : \"foo\",\n" + "    \"all_fields\" : true\n" + "  }\n" + "}";
    e = expectThrows(ParsingException.class, () -> parseQuery(json2));
    assertThat(e.getMessage(), containsString("cannot use [all_fields] parameter in conjunction with [default_field] or [fields]"));
}
Also used : ParsingException(org.elasticsearch.common.ParsingException) Matchers.containsString(org.hamcrest.Matchers.containsString)

Example 20 with ParsingException

use of org.elasticsearch.common.ParsingException in project elasticsearch by elastic.

the class QueryParseContextTests 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();
        QueryParseContext context = new QueryParseContext(parser);
        ParsingException exception = expectThrows(ParsingException.class, () -> context.parseInnerQueryBuilder());
        assertEquals("[_na] query malformed, must start with start_object", exception.getMessage());
    }
    source = "{}";
    try (XContentParser parser = createParser(JsonXContent.jsonXContent, source)) {
        QueryParseContext context = new QueryParseContext(parser);
        IllegalArgumentException exception = expectThrows(IllegalArgumentException.class, () -> context.parseInnerQueryBuilder());
        assertEquals("query malformed, empty clause found at [1:2]", exception.getMessage());
    }
    source = "{ \"foo\" : \"bar\" }";
    try (XContentParser parser = createParser(JsonXContent.jsonXContent, source)) {
        QueryParseContext context = new QueryParseContext(parser);
        ParsingException exception = expectThrows(ParsingException.class, () -> context.parseInnerQueryBuilder());
        assertEquals("[foo] query malformed, no start_object after query name", exception.getMessage());
    }
    source = "{ \"foo\" : {} }";
    try (XContentParser parser = createParser(JsonXContent.jsonXContent, source)) {
        QueryParseContext context = new QueryParseContext(parser);
        ParsingException exception = expectThrows(ParsingException.class, () -> context.parseInnerQueryBuilder());
        assertEquals("no [query] registered for [foo]", exception.getMessage());
    }
}
Also used : ParsingException(org.elasticsearch.common.ParsingException) XContentParser(org.elasticsearch.common.xcontent.XContentParser)

Aggregations

ParsingException (org.elasticsearch.common.ParsingException)165 XContentParser (org.elasticsearch.common.xcontent.XContentParser)96 ArrayList (java.util.ArrayList)26 Matchers.containsString (org.hamcrest.Matchers.containsString)22 IOException (java.io.IOException)12 HashMap (java.util.HashMap)10 Token (org.elasticsearch.common.xcontent.XContentParser.Token)9 XContentBuilder (org.elasticsearch.common.xcontent.XContentBuilder)7 Index (org.elasticsearch.index.Index)7 Script (org.elasticsearch.script.Script)7 SearchShardTarget (org.elasticsearch.search.SearchShardTarget)7 List (java.util.List)6 BytesReference (org.elasticsearch.common.bytes.BytesReference)6 QueryParseContext (org.elasticsearch.index.query.QueryParseContext)6 ElasticsearchParseException (org.elasticsearch.ElasticsearchParseException)5 GeoPoint (org.elasticsearch.common.geo.GeoPoint)5 GapPolicy (org.elasticsearch.search.aggregations.pipeline.BucketHelpers.GapPolicy)5 SearchPhaseExecutionException (org.elasticsearch.action.search.SearchPhaseExecutionException)4 ToXContent (org.elasticsearch.common.xcontent.ToXContent)4 QueryBuilder (org.elasticsearch.index.query.QueryBuilder)4