Search in sources :

Example 16 with QueryParseContext

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

the class GeoHashGridParserTests method testParseValidFromInts.

public void testParseValidFromInts() 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 17 with QueryParseContext

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

the class FiltersTests method testOtherBucket.

public void testOtherBucket() throws IOException {
    XContentBuilder builder = XContentFactory.contentBuilder(randomFrom(XContentType.values()));
    builder.startObject();
    builder.startArray("filters").endArray();
    builder.endObject();
    XContentParser parser = createParser(shuffleXContent(builder));
    parser.nextToken();
    QueryParseContext context = new QueryParseContext(parser);
    FiltersAggregationBuilder filters = FiltersAggregationBuilder.parse("agg_name", context);
    // The other bucket is disabled by default
    assertFalse(filters.otherBucket());
    builder = XContentFactory.contentBuilder(randomFrom(XContentType.values()));
    builder.startObject();
    builder.startArray("filters").endArray();
    builder.field("other_bucket_key", "some_key");
    builder.endObject();
    parser = createParser(shuffleXContent(builder));
    parser.nextToken();
    context = new QueryParseContext(parser);
    filters = FiltersAggregationBuilder.parse("agg_name", context);
    // but setting a key enables it automatically
    assertTrue(filters.otherBucket());
    builder = XContentFactory.contentBuilder(randomFrom(XContentType.values()));
    builder.startObject();
    builder.startArray("filters").endArray();
    builder.field("other_bucket", false);
    builder.field("other_bucket_key", "some_key");
    builder.endObject();
    parser = createParser(shuffleXContent(builder));
    parser.nextToken();
    context = new QueryParseContext(parser);
    filters = FiltersAggregationBuilder.parse("agg_name", context);
    // unless the other bucket is explicitly disabled
    assertFalse(filters.otherBucket());
}
Also used : QueryParseContext(org.elasticsearch.index.query.QueryParseContext) FiltersAggregationBuilder(org.elasticsearch.search.aggregations.bucket.filters.FiltersAggregationBuilder) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder) XContentParser(org.elasticsearch.common.xcontent.XContentParser)

Example 18 with QueryParseContext

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

the class TopHitsTests method testFailWithSubAgg.

public void testFailWithSubAgg() throws Exception {
    String source = "{\n" + "    \"top-tags\": {\n" + "      \"terms\": {\n" + "        \"field\": \"tags\"\n" + "      },\n" + "      \"aggs\": {\n" + "        \"top_tags_hits\": {\n" + "          \"top_hits\": {},\n" + "          \"aggs\": {\n" + "            \"max\": {\n" + "              \"max\": {\n" + "                \"field\": \"age\"\n" + "              }\n" + "            }\n" + "          }\n" + "        }\n" + "      }\n" + "    }\n" + "}";
    XContentParser parser = createParser(JsonXContent.jsonXContent, source);
    QueryParseContext parseContext = new QueryParseContext(parser);
    assertSame(XContentParser.Token.START_OBJECT, parser.nextToken());
    Exception e = expectThrows(AggregationInitializationException.class, () -> AggregatorFactories.parseAggregators(parseContext));
    assertThat(e.toString(), containsString("Aggregator [top_tags_hits] of type [top_hits] cannot accept sub-aggregations"));
}
Also used : QueryParseContext(org.elasticsearch.index.query.QueryParseContext) Matchers.containsString(org.hamcrest.Matchers.containsString) XContentParser(org.elasticsearch.common.xcontent.XContentParser) AggregationInitializationException(org.elasticsearch.search.aggregations.AggregationInitializationException)

Example 19 with QueryParseContext

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

the class IncludeExcludeTests method serialize.

// Serializes/deserializes an IncludeExclude statement with a single clause
private IncludeExclude serialize(IncludeExclude incExc, ParseField field) throws IOException {
    XContentBuilder builder = XContentFactory.contentBuilder(randomFrom(XContentType.values()));
    if (randomBoolean()) {
        builder.prettyPrint();
    }
    builder.startObject();
    incExc.toXContent(builder, ToXContent.EMPTY_PARAMS);
    builder.endObject();
    XContentParser parser = createParser(builder);
    XContentParser.Token token = parser.nextToken();
    assertEquals(token, XContentParser.Token.START_OBJECT);
    token = parser.nextToken();
    assertEquals(token, XContentParser.Token.FIELD_NAME);
    assertEquals(field.getPreferredName(), parser.currentName());
    token = parser.nextToken();
    QueryParseContext parseContext = new QueryParseContext(parser);
    if (field.getPreferredName().equalsIgnoreCase("include")) {
        return IncludeExclude.parseInclude(parser, parseContext);
    } else if (field.getPreferredName().equalsIgnoreCase("exclude")) {
        return IncludeExclude.parseExclude(parser, parseContext);
    } else {
        throw new IllegalArgumentException("Unexpected field name serialized in test: " + field.getPreferredName());
    }
}
Also used : QueryParseContext(org.elasticsearch.index.query.QueryParseContext) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder) XContentParser(org.elasticsearch.common.xcontent.XContentParser)

Example 20 with QueryParseContext

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

the class HighlightBuilderTests method expectParseThrows.

private <T extends Throwable> T expectParseThrows(Class<T> exceptionClass, String highlightElement) throws IOException {
    XContentParser parser = createParser(JsonXContent.jsonXContent, highlightElement);
    QueryParseContext context = new QueryParseContext(parser);
    return expectThrows(exceptionClass, () -> HighlightBuilder.fromXContent(context));
}
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