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());
}
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]"));
}
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());
}
}
use of org.elasticsearch.common.ParsingException in project elasticsearch by elastic.
the class MatchPhraseQueryBuilderTests method testParseFailsWithMultipleFields.
public void testParseFailsWithMultipleFields() throws IOException {
String json = "{\n" + " \"match_phrase\" : {\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_phrase] query doesn't support multiple fields, found [message1] and [message2]", e.getMessage());
String shortJson = "{\n" + " \"match_phrase\" : {\n" + " \"message1\" : \"this is a test\",\n" + " \"message2\" : \"this is a test\"\n" + " }\n" + "}";
e = expectThrows(ParsingException.class, () -> parseQuery(shortJson));
assertEquals("[match_phrase] query doesn't support multiple fields, found [message1] and [message2]", e.getMessage());
}
use of org.elasticsearch.common.ParsingException in project elasticsearch by elastic.
the class RangeQueryBuilderTests method testParseFailsWithMultipleFields.
public void testParseFailsWithMultipleFields() throws IOException {
String json = "{\n" + " \"range\": {\n" + " \"age\": {\n" + " \"gte\": 30,\n" + " \"lte\": 40\n" + " },\n" + " \"price\": {\n" + " \"gte\": 10,\n" + " \"lte\": 30\n" + " }\n" + " }\n" + " }";
ParsingException e = expectThrows(ParsingException.class, () -> parseQuery(json));
assertEquals("[range] query doesn't support multiple fields, found [age] and [price]", e.getMessage());
}
Aggregations