Search in sources :

Example 1 with ParsingException

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

the class StoredScriptTests method testSourceParsingErrors.

public void testSourceParsingErrors() throws Exception {
    // check for missing lang parameter when parsing a template
    try (XContentBuilder builder = XContentFactory.contentBuilder(XContentType.JSON)) {
        builder.startObject().field("template", "code").endObject();
        IllegalArgumentException iae = expectThrows(IllegalArgumentException.class, () -> StoredScriptSource.parse(null, builder.bytes(), XContentType.JSON));
        assertThat(iae.getMessage(), equalTo("unexpected stored script format"));
    }
    // check for missing lang parameter when parsing a script
    try (XContentBuilder builder = XContentFactory.contentBuilder(XContentType.JSON)) {
        builder.startObject().field("script").startObject().field("code", "code").endObject().endObject();
        IllegalArgumentException iae = expectThrows(IllegalArgumentException.class, () -> StoredScriptSource.parse(null, builder.bytes(), XContentType.JSON));
        assertThat(iae.getMessage(), equalTo("must specify lang for stored script"));
    }
    // check for missing code parameter when parsing a script
    try (XContentBuilder builder = XContentFactory.contentBuilder(XContentType.JSON)) {
        builder.startObject().field("script").startObject().field("lang", "lang").endObject().endObject();
        IllegalArgumentException iae = expectThrows(IllegalArgumentException.class, () -> StoredScriptSource.parse(null, builder.bytes(), XContentType.JSON));
        assertThat(iae.getMessage(), equalTo("must specify code for stored script"));
    }
    // check for illegal options parameter when parsing a script
    try (XContentBuilder builder = XContentFactory.contentBuilder(XContentType.JSON)) {
        builder.startObject().field("script").startObject().field("lang", "lang").field("code", "code").startObject("options").field("option", "option").endObject().endObject().endObject();
        IllegalArgumentException iae = expectThrows(IllegalArgumentException.class, () -> StoredScriptSource.parse(null, builder.bytes(), XContentType.JSON));
        assertThat(iae.getMessage(), equalTo("illegal compiler options [{option=option}] specified"));
    }
    // check for illegal use of content type option
    try (XContentBuilder builder = XContentFactory.contentBuilder(XContentType.JSON)) {
        builder.startObject().field("script").startObject().field("lang", "lang").field("code", "code").startObject("options").field("content_type", "option").endObject().endObject().endObject();
        ParsingException pe = expectThrows(ParsingException.class, () -> StoredScriptSource.parse(null, builder.bytes(), XContentType.JSON));
        assertThat(pe.getRootCause().getMessage(), equalTo("content_type cannot be user-specified"));
    }
}
Also used : ParsingException(org.elasticsearch.common.ParsingException) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder)

Example 2 with ParsingException

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

the class SignificanceHeuristicTests method checkParseException.

protected void checkParseException(ParseFieldRegistry<SignificanceHeuristicParser> significanceHeuristicParserRegistry, String faultyHeuristicDefinition, String expectedError) throws IOException {
    try {
        XContentParser stParser = createParser(JsonXContent.jsonXContent, "{\"field\":\"text\", " + faultyHeuristicDefinition + ",\"min_doc_count\":200}");
        QueryParseContext parseContext = new QueryParseContext(stParser);
        stParser.nextToken();
        SignificantTermsAggregationBuilder.getParser(significanceHeuristicParserRegistry).parse("testagg", parseContext);
        fail();
    } catch (ParsingException e) {
        assertThat(e.getCause().getMessage(), containsString(expectedError));
    }
}
Also used : QueryParseContext(org.elasticsearch.index.query.QueryParseContext) ParsingException(org.elasticsearch.common.ParsingException) XContentParser(org.elasticsearch.common.xcontent.XContentParser)

Example 3 with ParsingException

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

the class SearchSourceBuilderTests method assertIndicesBoostParseErrorMessage.

private void assertIndicesBoostParseErrorMessage(String restContent, String expectedErrorMessage) throws IOException {
    try (XContentParser parser = createParser(JsonXContent.jsonXContent, restContent)) {
        ParsingException e = expectThrows(ParsingException.class, () -> SearchSourceBuilder.fromXContent(createParseContext(parser)));
        assertEquals(expectedErrorMessage, e.getMessage());
    }
}
Also used : ParsingException(org.elasticsearch.common.ParsingException) XContentParser(org.elasticsearch.common.xcontent.XContentParser)

Example 4 with ParsingException

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

the class SearchSourceBuilderTests method testMultipleQueryObjectsAreRejected.

public void testMultipleQueryObjectsAreRejected() throws Exception {
    String restContent = " { \"query\": {\n" + "    \"multi_match\": {\n" + "      \"query\": \"workd\",\n" + "      \"fields\": [\"title^5\", \"plain_body\"]\n" + "    },\n" + "    \"filters\": {\n" + "      \"terms\": {\n" + "        \"status\": [ 3 ]\n" + "      }\n" + "    }\n" + "  } }";
    try (XContentParser parser = createParser(JsonXContent.jsonXContent, restContent)) {
        ParsingException e = expectThrows(ParsingException.class, () -> SearchSourceBuilder.fromXContent(createParseContext(parser)));
        assertEquals("[multi_match] malformed query, expected [END_OBJECT] but found [FIELD_NAME]", e.getMessage());
    }
}
Also used : ParsingException(org.elasticsearch.common.ParsingException) Matchers.hasToString(org.hamcrest.Matchers.hasToString) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) XContentParser(org.elasticsearch.common.xcontent.XContentParser)

Example 5 with ParsingException

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

the class HighlightBuilderTests method testTwoFieldsInObjectInFieldsArray.

public void testTwoFieldsInObjectInFieldsArray() throws IOException {
    ParsingException e = expectParseThrows(ParsingException.class, "{\n" + "  \"fields\" : [ {\n" + "     \"body\" : {},\n" + "     \"nope\" : {}\n" + "   }] \n" + "}\n");
    assertEquals("[highlight] failed to parse field [fields]", e.getMessage());
    assertEquals("[fields] can be a single object with any number of fields or an array where each entry is an object with a single field", e.getCause().getMessage());
}
Also used : ParsingException(org.elasticsearch.common.ParsingException)

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