Search in sources :

Example 11 with QueryParseContext

use of org.elasticsearch.index.query.QueryParseContext in project elasticsearch by elastic.

the class AggregatorFactoriesTests method testTwoAggs.

public void testTwoAggs() throws Exception {
    assumeFalse("Test only makes sense if XContent parser doesn't have strict duplicate checks enabled", XContent.isStrictDuplicateDetectionEnabled());
    XContentBuilder source = JsonXContent.contentBuilder().startObject().startObject("by_date").startObject("date_histogram").field("field", "timestamp").field("interval", "month").endObject().startObject("aggs").startObject("tag_count").startObject("cardinality").field("field", "tag").endObject().endObject().endObject().startObject(// 2nd "aggs": illegal
    "aggs").startObject("tag_count2").startObject("cardinality").field("field", "tag").endObject().endObject().endObject().endObject().endObject();
    XContentParser parser = createParser(source);
    QueryParseContext parseContext = new QueryParseContext(parser);
    assertSame(XContentParser.Token.START_OBJECT, parser.nextToken());
    Exception e = expectThrows(ParsingException.class, () -> AggregatorFactories.parseAggregators(parseContext));
    assertThat(e.toString(), containsString("Found two sub aggregation definitions under [by_date]"));
}
Also used : QueryParseContext(org.elasticsearch.index.query.QueryParseContext) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder) XContentParser(org.elasticsearch.common.xcontent.XContentParser) ParsingException(org.elasticsearch.common.ParsingException)

Example 12 with QueryParseContext

use of org.elasticsearch.index.query.QueryParseContext in project elasticsearch by elastic.

the class AggregatorFactoriesTests method testInvalidAggregationName.

public void testInvalidAggregationName() throws Exception {
    Matcher matcher = Pattern.compile("[^\\[\\]>]+").matcher("");
    String name;
    Random rand = random();
    int len = randomIntBetween(1, 5);
    char[] word = new char[len];
    while (true) {
        for (int i = 0; i < word.length; i++) {
            word[i] = (char) rand.nextInt(127);
        }
        name = String.valueOf(word);
        if (!matcher.reset(name).matches()) {
            break;
        }
    }
    XContentBuilder source = JsonXContent.contentBuilder().startObject().startObject(name).startObject("filter").startObject("range").startObject("stock").field("gt", 0).endObject().endObject().endObject().endObject().endObject();
    XContentParser parser = createParser(source);
    QueryParseContext parseContext = new QueryParseContext(parser);
    assertSame(XContentParser.Token.START_OBJECT, parser.nextToken());
    Exception e = expectThrows(ParsingException.class, () -> AggregatorFactories.parseAggregators(parseContext));
    assertThat(e.toString(), containsString("Invalid aggregation name [" + name + "]"));
}
Also used : Random(java.util.Random) QueryParseContext(org.elasticsearch.index.query.QueryParseContext) Matcher(java.util.regex.Matcher) Matchers.containsString(org.hamcrest.Matchers.containsString) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder) XContentParser(org.elasticsearch.common.xcontent.XContentParser) ParsingException(org.elasticsearch.common.ParsingException)

Example 13 with QueryParseContext

use of org.elasticsearch.index.query.QueryParseContext 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 14 with QueryParseContext

use of org.elasticsearch.index.query.QueryParseContext in project elasticsearch by elastic.

the class GeoHashGridParserTests method testParseValidFromStrings.

public void testParseValidFromStrings() throws Exception {
    int precision = randomIntBetween(1, 12);
    XContentParser stParser = createParser(JsonXContent.jsonXContent, "{\"field\":\"my_loc\", \"precision\":\"" + precision + "\", \"size\": \"500\", \"shard_size\": \"550\"}");
    QueryParseContext parseContext = new QueryParseContext(stParser);
    XContentParser.Token token = stParser.nextToken();
    assertSame(XContentParser.Token.START_OBJECT, token);
    // can create a factory
    assertNotNull(GeoGridAggregationBuilder.parse("geohash_grid", parseContext));
}
Also used : QueryParseContext(org.elasticsearch.index.query.QueryParseContext) XContentParser(org.elasticsearch.common.xcontent.XContentParser)

Example 15 with QueryParseContext

use of org.elasticsearch.index.query.QueryParseContext in project elasticsearch by elastic.

the class GeoHashGridParserTests method testParseErrorOnBooleanPrecision.

public void testParseErrorOnBooleanPrecision() throws Exception {
    XContentParser stParser = createParser(JsonXContent.jsonXContent, "{\"field\":\"my_loc\", \"precision\":false}");
    QueryParseContext parseContext = new QueryParseContext(stParser);
    XContentParser.Token token = stParser.nextToken();
    assertSame(XContentParser.Token.START_OBJECT, token);
    try {
        GeoGridAggregationBuilder.parse("geohash_grid", parseContext);
        fail();
    } catch (IllegalArgumentException ex) {
        assertEquals("[geohash_grid] precision doesn't support values of type: VALUE_BOOLEAN", ex.getMessage());
    }
}
Also used : QueryParseContext(org.elasticsearch.index.query.QueryParseContext) XContentParser(org.elasticsearch.common.xcontent.XContentParser)

Aggregations

QueryParseContext (org.elasticsearch.index.query.QueryParseContext)55 XContentParser (org.elasticsearch.common.xcontent.XContentParser)45 XContentBuilder (org.elasticsearch.common.xcontent.XContentBuilder)14 ParsingException (org.elasticsearch.common.ParsingException)12 IOException (java.io.IOException)7 QueryBuilder (org.elasticsearch.index.query.QueryBuilder)5 ArrayList (java.util.ArrayList)2 Collections.unmodifiableMap (java.util.Collections.unmodifiableMap)2 List (java.util.List)2 Map (java.util.Map)2 Consumer (java.util.function.Consumer)2 SearchRequest (org.elasticsearch.action.search.SearchRequest)2 SearchResponse (org.elasticsearch.action.search.SearchResponse)2 BytesReference (org.elasticsearch.common.bytes.BytesReference)2 NamedXContentRegistry (org.elasticsearch.common.xcontent.NamedXContentRegistry)2 BoolQueryBuilder (org.elasticsearch.index.query.BoolQueryBuilder)2 SignificanceHeuristicParser (org.elasticsearch.search.aggregations.bucket.significant.heuristics.SignificanceHeuristicParser)2 IncludeExclude (org.elasticsearch.search.aggregations.bucket.terms.support.IncludeExclude)2 SearchSourceBuilder (org.elasticsearch.search.builder.SearchSourceBuilder)2 SearchContext (org.elasticsearch.search.internal.SearchContext)2