use of org.elasticsearch.common.ParsingException in project elasticsearch by elastic.
the class TermsQueryBuilderTests method testBothValuesAndLookupSet.
public void testBothValuesAndLookupSet() throws IOException {
String query = "{\n" + " \"terms\": {\n" + " \"field\": [\n" + " \"blue\",\n" + " \"pill\"\n" + " ],\n" + " \"field_lookup\": {\n" + " \"index\": \"pills\",\n" + " \"type\": \"red\",\n" + " \"id\": \"3\",\n" + " \"path\": \"white rabbit\"\n" + " }\n" + " }\n" + "}";
ParsingException e = expectThrows(ParsingException.class, () -> parseQuery(query));
assertThat(e.getMessage(), containsString("[" + TermsQueryBuilder.NAME + "] query does not support more than one field."));
}
use of org.elasticsearch.common.ParsingException in project elasticsearch by elastic.
the class AbstractQueryTestCase method testUnknownField.
/**
* Test that unknown field trigger ParsingException.
* To find the right position in the root query, we add a marker as `queryName` which
* all query builders support. The added bogus field after that should trigger the exception.
* Queries that allow arbitrary field names at this level need to override this test.
*/
public void testUnknownField() throws IOException {
String marker = "#marker#";
QB testQuery;
do {
testQuery = createTestQueryBuilder();
} while (testQuery.toString().contains(marker));
// to find root query to add additional bogus field there
testQuery.queryName(marker);
String queryAsString = testQuery.toString().replace("\"" + marker + "\"", "\"" + marker + "\", \"bogusField\" : \"someValue\"");
ParsingException e = expectThrows(ParsingException.class, () -> parseQuery(queryAsString));
// we'd like to see the offending field name here
assertThat(e.getMessage(), containsString("bogusField"));
}
use of org.elasticsearch.common.ParsingException in project elasticsearch by elastic.
the class ConstructingObjectParserTests method testBadParamBeforeObjectBuilt.
public void testBadParamBeforeObjectBuilt() throws IOException {
XContentParser parser = createParser(JsonXContent.jsonXContent, "{\n" + " \"a\": \"supercalifragilisticexpialidocious\",\n" + " \"animal\": \"cat\"\n," + " \"vegetable\": 2\n" + "}");
ParsingException e = expectThrows(ParsingException.class, () -> randomFrom(HasCtorArguments.ALL_PARSERS).apply(parser, null));
assertEquals("[has_required_arguments] failed to parse field [vegetable]", e.getMessage());
assertEquals(4, e.getLineNumber());
e = (ParsingException) e.getCause();
assertEquals("failed to build [has_required_arguments] after last required field arrived", e.getMessage());
assertEquals(2, e.getLineNumber());
e = (ParsingException) e.getCause();
assertEquals("[has_required_arguments] failed to parse field [a]", e.getMessage());
assertEquals(2, e.getLineNumber());
assertEquals("[a] must be less than 10 characters in length but was [supercalifragilisticexpialidocious]", e.getCause().getMessage());
}
use of org.elasticsearch.common.ParsingException in project elasticsearch by elastic.
the class ObjectParserTests method testParseNamedObjectInOrderNotSupported.
public void testParseNamedObjectInOrderNotSupported() throws IOException {
XContentParser parser = createParser(JsonXContent.jsonXContent, "{\"named\": [\n" + " {\"a\": {}}" + "]}");
// Create our own parser for this test so we can disable support for the "ordered" mode specified by the array above
ObjectParser<NamedObjectHolder, Void> objectParser = new ObjectParser<>("named_object_holder", NamedObjectHolder::new);
objectParser.declareNamedObjects(NamedObjectHolder::setNamed, NamedObject.PARSER, new ParseField("named"));
// Now firing the xml through it fails
ParsingException e = expectThrows(ParsingException.class, () -> objectParser.apply(parser, null));
assertEquals("[named_object_holder] failed to parse field [named]", e.getMessage());
assertEquals("[named] doesn't support arrays. Use a single object with multiple fields.", e.getCause().getMessage());
}
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"));
}
}
Aggregations