Search in sources :

Example 1 with ReverseNested

use of org.elasticsearch.search.aggregations.bucket.nested.ReverseNested in project elasticsearch by elastic.

the class ReverseNestedIT method testSimpleNested1ToRootToNested2.

public void testSimpleNested1ToRootToNested2() throws Exception {
    SearchResponse response = client().prepareSearch("idx").setTypes("type2").addAggregation(nested("nested1", "nested1").subAggregation(reverseNested("nested1_to_root").subAggregation(nested("root_to_nested2", "nested1.nested2")))).get();
    assertSearchResponse(response);
    Nested nested = response.getAggregations().get("nested1");
    assertThat(nested.getName(), equalTo("nested1"));
    assertThat(nested.getDocCount(), equalTo(9L));
    ReverseNested reverseNested = nested.getAggregations().get("nested1_to_root");
    assertThat(reverseNested.getName(), equalTo("nested1_to_root"));
    assertThat(reverseNested.getDocCount(), equalTo(4L));
    nested = reverseNested.getAggregations().get("root_to_nested2");
    assertThat(nested.getName(), equalTo("root_to_nested2"));
    assertThat(nested.getDocCount(), equalTo(27L));
}
Also used : AggregationBuilders.reverseNested(org.elasticsearch.search.aggregations.AggregationBuilders.reverseNested) Nested(org.elasticsearch.search.aggregations.bucket.nested.Nested) ReverseNested(org.elasticsearch.search.aggregations.bucket.nested.ReverseNested) ReverseNested(org.elasticsearch.search.aggregations.bucket.nested.ReverseNested) SearchResponse(org.elasticsearch.action.search.SearchResponse) ElasticsearchAssertions.assertSearchResponse(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)

Example 2 with ReverseNested

use of org.elasticsearch.search.aggregations.bucket.nested.ReverseNested in project elasticsearch by elastic.

the class ReverseNestedIT method testSimpleReverseNestedToNested1.

public void testSimpleReverseNestedToNested1() throws Exception {
    SearchResponse response = client().prepareSearch("idx").setTypes("type2").addAggregation(nested("nested1", "nested1.nested2").subAggregation(terms("field2").field("nested1.nested2.field2").order(Terms.Order.term(true)).collectMode(randomFrom(SubAggCollectionMode.values())).size(10000).subAggregation(reverseNested("nested1_to_field1").path("nested1").subAggregation(terms("field1").field("nested1.field1").order(Terms.Order.term(true)).collectMode(randomFrom(SubAggCollectionMode.values())))))).get();
    assertSearchResponse(response);
    Nested nested = response.getAggregations().get("nested1");
    assertThat(nested, notNullValue());
    assertThat(nested.getName(), equalTo("nested1"));
    assertThat(nested.getDocCount(), equalTo(27L));
    assertThat(nested.getAggregations().asList().isEmpty(), is(false));
    Terms usernames = nested.getAggregations().get("field2");
    assertThat(usernames, notNullValue());
    assertThat(usernames.getBuckets().size(), equalTo(5));
    List<Terms.Bucket> usernameBuckets = new ArrayList<>(usernames.getBuckets());
    Terms.Bucket bucket = usernameBuckets.get(0);
    assertThat(bucket.getKeyAsString(), equalTo("0"));
    assertThat(bucket.getDocCount(), equalTo(12L));
    ReverseNested reverseNested = bucket.getAggregations().get("nested1_to_field1");
    assertThat(reverseNested.getDocCount(), equalTo(5L));
    Terms tags = reverseNested.getAggregations().get("field1");
    List<Terms.Bucket> tagsBuckets = new ArrayList<>(tags.getBuckets());
    assertThat(tagsBuckets.size(), equalTo(2));
    assertThat(tagsBuckets.get(0).getKeyAsString(), equalTo("a"));
    assertThat(tagsBuckets.get(0).getDocCount(), equalTo(3L));
    assertThat(tagsBuckets.get(1).getKeyAsString(), equalTo("b"));
    assertThat(tagsBuckets.get(1).getDocCount(), equalTo(2L));
    bucket = usernameBuckets.get(1);
    assertThat(bucket.getKeyAsString(), equalTo("1"));
    assertThat(bucket.getDocCount(), equalTo(6L));
    reverseNested = bucket.getAggregations().get("nested1_to_field1");
    assertThat(reverseNested.getDocCount(), equalTo(4L));
    tags = reverseNested.getAggregations().get("field1");
    tagsBuckets = new ArrayList<>(tags.getBuckets());
    assertThat(tagsBuckets.size(), equalTo(4));
    assertThat(tagsBuckets.get(0).getKeyAsString(), equalTo("a"));
    assertThat(tagsBuckets.get(0).getDocCount(), equalTo(1L));
    assertThat(tagsBuckets.get(1).getKeyAsString(), equalTo("b"));
    assertThat(tagsBuckets.get(1).getDocCount(), equalTo(1L));
    assertThat(tagsBuckets.get(2).getKeyAsString(), equalTo("c"));
    assertThat(tagsBuckets.get(2).getDocCount(), equalTo(1L));
    assertThat(tagsBuckets.get(3).getKeyAsString(), equalTo("e"));
    assertThat(tagsBuckets.get(3).getDocCount(), equalTo(1L));
    bucket = usernameBuckets.get(2);
    assertThat(bucket.getKeyAsString(), equalTo("2"));
    assertThat(bucket.getDocCount(), equalTo(5L));
    reverseNested = bucket.getAggregations().get("nested1_to_field1");
    assertThat(reverseNested.getDocCount(), equalTo(4L));
    tags = reverseNested.getAggregations().get("field1");
    tagsBuckets = new ArrayList<>(tags.getBuckets());
    assertThat(tagsBuckets.size(), equalTo(4));
    assertThat(tagsBuckets.get(0).getKeyAsString(), equalTo("a"));
    assertThat(tagsBuckets.get(0).getDocCount(), equalTo(1L));
    assertThat(tagsBuckets.get(1).getKeyAsString(), equalTo("b"));
    assertThat(tagsBuckets.get(1).getDocCount(), equalTo(1L));
    assertThat(tagsBuckets.get(2).getKeyAsString(), equalTo("c"));
    assertThat(tagsBuckets.get(2).getDocCount(), equalTo(1L));
    assertThat(tagsBuckets.get(3).getKeyAsString(), equalTo("e"));
    assertThat(tagsBuckets.get(3).getDocCount(), equalTo(1L));
    bucket = usernameBuckets.get(3);
    assertThat(bucket.getKeyAsString(), equalTo("3"));
    assertThat(bucket.getDocCount(), equalTo(2L));
    reverseNested = bucket.getAggregations().get("nested1_to_field1");
    assertThat(reverseNested.getDocCount(), equalTo(2L));
    tags = reverseNested.getAggregations().get("field1");
    tagsBuckets = new ArrayList<>(tags.getBuckets());
    assertThat(tagsBuckets.size(), equalTo(2));
    assertThat(tagsBuckets.get(0).getKeyAsString(), equalTo("d"));
    assertThat(tagsBuckets.get(0).getDocCount(), equalTo(1L));
    assertThat(tagsBuckets.get(1).getKeyAsString(), equalTo("f"));
    bucket = usernameBuckets.get(4);
    assertThat(bucket.getKeyAsString(), equalTo("4"));
    assertThat(bucket.getDocCount(), equalTo(2L));
    reverseNested = bucket.getAggregations().get("nested1_to_field1");
    assertThat(reverseNested.getDocCount(), equalTo(2L));
    tags = reverseNested.getAggregations().get("field1");
    tagsBuckets = new ArrayList<>(tags.getBuckets());
    assertThat(tagsBuckets.size(), equalTo(2));
    assertThat(tagsBuckets.get(0).getKeyAsString(), equalTo("d"));
    assertThat(tagsBuckets.get(0).getDocCount(), equalTo(1L));
    assertThat(tagsBuckets.get(1).getKeyAsString(), equalTo("f"));
}
Also used : AggregationBuilders.reverseNested(org.elasticsearch.search.aggregations.AggregationBuilders.reverseNested) Nested(org.elasticsearch.search.aggregations.bucket.nested.Nested) ReverseNested(org.elasticsearch.search.aggregations.bucket.nested.ReverseNested) Terms(org.elasticsearch.search.aggregations.bucket.terms.Terms) ArrayList(java.util.ArrayList) ReverseNested(org.elasticsearch.search.aggregations.bucket.nested.ReverseNested) SearchResponse(org.elasticsearch.action.search.SearchResponse) ElasticsearchAssertions.assertSearchResponse(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)

Example 3 with ReverseNested

use of org.elasticsearch.search.aggregations.bucket.nested.ReverseNested in project snow-owl by b2ihealthcare.

the class EsDocumentSearcher method aggregate.

@Override
public <T> Aggregation<T> aggregate(AggregationBuilder<T> aggregation) throws IOException {
    final String aggregationName = aggregation.getName();
    final EsClient client = admin.client();
    final DocumentMapping mapping = admin.mappings().getMapping(aggregation.getFrom());
    final EsQueryBuilder esQueryBuilder = new EsQueryBuilder(mapping, admin.settings(), admin.log());
    final QueryBuilder esQuery = esQueryBuilder.build(aggregation.getQuery());
    final SearchRequest req = new SearchRequest(admin.getTypeIndex(mapping));
    final SearchSourceBuilder reqSource = req.source().query(esQuery).size(0).trackScores(false).trackTotalHitsUpTo(Integer.MAX_VALUE);
    // field selection
    final boolean fetchSource = applySourceFiltering(aggregation.getFields(), mapping, reqSource);
    reqSource.aggregation(toEsAggregation(mapping, aggregation, fetchSource));
    SearchResponse response = null;
    try {
        response = client.search(req);
    } catch (Exception e) {
        admin.log().error("Couldn't execute aggregation", e);
        throw new IndexException("Couldn't execute aggregation: " + e.getMessage(), null);
    }
    ImmutableMap.Builder<Object, Bucket<T>> buckets = ImmutableMap.builder();
    Aggregations topLevelAggregations = response.getAggregations();
    Nested nested = topLevelAggregations.get(nestedAggName(aggregation));
    Terms aggregationResult;
    if (nested != null) {
        aggregationResult = nested.getAggregations().get(aggregationName);
    } else {
        aggregationResult = topLevelAggregations.get(aggregationName);
    }
    for (org.elasticsearch.search.aggregations.bucket.terms.Terms.Bucket bucket : aggregationResult.getBuckets()) {
        final TopHits topHits;
        if (nested != null) {
            final ReverseNested reverseNested = bucket.getAggregations().get(reverseNestedAggName(aggregation));
            topHits = reverseNested.getAggregations().get(topHitsAggName(aggregation));
        } else {
            topHits = bucket.getAggregations().get(topHitsAggName(aggregation));
        }
        Hits<T> hits;
        if (topHits != null) {
            hits = toHits(aggregation.getSelect(), List.of(aggregation.getFrom()), aggregation.getFields(), fetchSource, aggregation.getBucketHitsLimit(), (int) bucket.getDocCount(), null, topHits.getHits());
        } else {
            hits = new Hits<>(Collections.emptyList(), null, aggregation.getBucketHitsLimit(), (int) bucket.getDocCount());
        }
        buckets.put(bucket.getKey(), new Bucket<>(bucket.getKey(), hits));
    }
    return new Aggregation<>(aggregationName, buckets.build());
}
Also used : SearchRequest(org.elasticsearch.action.search.SearchRequest) Aggregations(org.elasticsearch.search.aggregations.Aggregations) ReverseNested(org.elasticsearch.search.aggregations.bucket.nested.ReverseNested) Nested(org.elasticsearch.search.aggregations.bucket.nested.Nested) EsQueryBuilder(com.b2international.index.es.query.EsQueryBuilder) QueryBuilder(org.elasticsearch.index.query.QueryBuilder) SearchSourceBuilder(org.elasticsearch.search.builder.SearchSourceBuilder) Aggregation(com.b2international.index.aggregations.Aggregation) TopHits(org.elasticsearch.search.aggregations.metrics.TopHits) EsQueryBuilder(com.b2international.index.es.query.EsQueryBuilder) ReverseNested(org.elasticsearch.search.aggregations.bucket.nested.ReverseNested) Terms(org.elasticsearch.search.aggregations.bucket.terms.Terms) EsClient(com.b2international.index.es.client.EsClient) DocumentMapping(com.b2international.index.mapping.DocumentMapping) FormattedRuntimeException(com.b2international.commons.exceptions.FormattedRuntimeException) ElasticsearchStatusException(org.elasticsearch.ElasticsearchStatusException) BadRequestException(com.b2international.commons.exceptions.BadRequestException) IOException(java.io.IOException) SearchResponse(org.elasticsearch.action.search.SearchResponse) Bucket(com.b2international.index.aggregations.Bucket)

Example 4 with ReverseNested

use of org.elasticsearch.search.aggregations.bucket.nested.ReverseNested in project elasticsearch by elastic.

the class ReverseNestedIT method testSimpleReverseNestedToRoot.

public void testSimpleReverseNestedToRoot() throws Exception {
    SearchResponse response = client().prepareSearch("idx").setTypes("type1").addAggregation(nested("nested1", "nested1").subAggregation(terms("field2").field("nested1.field2").subAggregation(reverseNested("nested1_to_field1").subAggregation(terms("field1").field("field1").collectMode(randomFrom(SubAggCollectionMode.values())))))).get();
    assertSearchResponse(response);
    Nested nested = response.getAggregations().get("nested1");
    assertThat(nested, notNullValue());
    assertThat(nested.getName(), equalTo("nested1"));
    assertThat(nested.getDocCount(), equalTo(25L));
    assertThat(nested.getAggregations().asList().isEmpty(), is(false));
    Terms usernames = nested.getAggregations().get("field2");
    assertThat(usernames, notNullValue());
    assertThat(usernames.getBuckets().size(), equalTo(9));
    List<Terms.Bucket> usernameBuckets = new ArrayList<>(usernames.getBuckets());
    // nested.field2: 1
    Terms.Bucket bucket = usernameBuckets.get(0);
    assertThat(bucket.getKeyAsString(), equalTo("1"));
    assertThat(bucket.getDocCount(), equalTo(6L));
    ReverseNested reverseNested = bucket.getAggregations().get("nested1_to_field1");
    assertThat(reverseNested.getProperty("_count"), equalTo(5L));
    Terms tags = reverseNested.getAggregations().get("field1");
    assertThat(reverseNested.getProperty("field1"), sameInstance(tags));
    List<Terms.Bucket> tagsBuckets = new ArrayList<>(tags.getBuckets());
    assertThat(tagsBuckets.size(), equalTo(6));
    assertThat(tagsBuckets.get(0).getKeyAsString(), equalTo("c"));
    assertThat(tagsBuckets.get(0).getDocCount(), equalTo(4L));
    assertThat(tagsBuckets.get(1).getKeyAsString(), equalTo("a"));
    assertThat(tagsBuckets.get(1).getDocCount(), equalTo(3L));
    assertThat(tagsBuckets.get(2).getKeyAsString(), equalTo("e"));
    assertThat(tagsBuckets.get(2).getDocCount(), equalTo(2L));
    assertThat(tagsBuckets.get(3).getKeyAsString(), equalTo("b"));
    assertThat(tagsBuckets.get(3).getDocCount(), equalTo(1L));
    assertThat(tagsBuckets.get(4).getKeyAsString(), equalTo("d"));
    assertThat(tagsBuckets.get(4).getDocCount(), equalTo(1L));
    assertThat(tagsBuckets.get(5).getKeyAsString(), equalTo("x"));
    assertThat(tagsBuckets.get(5).getDocCount(), equalTo(1L));
    // nested.field2: 4
    bucket = usernameBuckets.get(1);
    assertThat(bucket.getKeyAsString(), equalTo("4"));
    assertThat(bucket.getDocCount(), equalTo(4L));
    reverseNested = bucket.getAggregations().get("nested1_to_field1");
    tags = reverseNested.getAggregations().get("field1");
    tagsBuckets = new ArrayList<>(tags.getBuckets());
    assertThat(tagsBuckets.size(), equalTo(5));
    assertThat(tagsBuckets.get(0).getKeyAsString(), equalTo("a"));
    assertThat(tagsBuckets.get(0).getDocCount(), equalTo(3L));
    assertThat(tagsBuckets.get(1).getKeyAsString(), equalTo("b"));
    assertThat(tagsBuckets.get(1).getDocCount(), equalTo(2L));
    assertThat(tagsBuckets.get(2).getKeyAsString(), equalTo("c"));
    assertThat(tagsBuckets.get(2).getDocCount(), equalTo(2L));
    assertThat(tagsBuckets.get(3).getKeyAsString(), equalTo("d"));
    assertThat(tagsBuckets.get(3).getDocCount(), equalTo(1L));
    assertThat(tagsBuckets.get(4).getKeyAsString(), equalTo("e"));
    assertThat(tagsBuckets.get(4).getDocCount(), equalTo(1L));
    // nested.field2: 7
    bucket = usernameBuckets.get(2);
    assertThat(bucket.getKeyAsString(), equalTo("7"));
    assertThat(bucket.getDocCount(), equalTo(3L));
    reverseNested = bucket.getAggregations().get("nested1_to_field1");
    tags = reverseNested.getAggregations().get("field1");
    tagsBuckets = new ArrayList<>(tags.getBuckets());
    assertThat(tagsBuckets.size(), equalTo(5));
    assertThat(tagsBuckets.get(0).getKeyAsString(), equalTo("c"));
    assertThat(tagsBuckets.get(0).getDocCount(), equalTo(2L));
    assertThat(tagsBuckets.get(1).getKeyAsString(), equalTo("d"));
    assertThat(tagsBuckets.get(1).getDocCount(), equalTo(2L));
    assertThat(tagsBuckets.get(2).getKeyAsString(), equalTo("e"));
    assertThat(tagsBuckets.get(2).getDocCount(), equalTo(2L));
    assertThat(tagsBuckets.get(3).getKeyAsString(), equalTo("a"));
    assertThat(tagsBuckets.get(3).getDocCount(), equalTo(1L));
    assertThat(tagsBuckets.get(4).getKeyAsString(), equalTo("b"));
    assertThat(tagsBuckets.get(4).getDocCount(), equalTo(1L));
    // nested.field2: 2
    bucket = usernameBuckets.get(3);
    assertThat(bucket.getKeyAsString(), equalTo("2"));
    assertThat(bucket.getDocCount(), equalTo(2L));
    reverseNested = bucket.getAggregations().get("nested1_to_field1");
    tags = reverseNested.getAggregations().get("field1");
    tagsBuckets = new ArrayList<>(tags.getBuckets());
    assertThat(tagsBuckets.size(), equalTo(3));
    assertThat(tagsBuckets.get(0).getKeyAsString(), equalTo("a"));
    assertThat(tagsBuckets.get(0).getDocCount(), equalTo(2L));
    assertThat(tagsBuckets.get(1).getKeyAsString(), equalTo("c"));
    assertThat(tagsBuckets.get(1).getDocCount(), equalTo(2L));
    assertThat(tagsBuckets.get(2).getKeyAsString(), equalTo("b"));
    assertThat(tagsBuckets.get(2).getDocCount(), equalTo(1L));
    // nested.field2: 3
    bucket = usernameBuckets.get(4);
    assertThat(bucket.getKeyAsString(), equalTo("3"));
    assertThat(bucket.getDocCount(), equalTo(2L));
    reverseNested = bucket.getAggregations().get("nested1_to_field1");
    tags = reverseNested.getAggregations().get("field1");
    tagsBuckets = new ArrayList<>(tags.getBuckets());
    assertThat(tagsBuckets.size(), equalTo(3));
    assertThat(tagsBuckets.get(0).getKeyAsString(), equalTo("a"));
    assertThat(tagsBuckets.get(0).getDocCount(), equalTo(2L));
    assertThat(tagsBuckets.get(1).getKeyAsString(), equalTo("b"));
    assertThat(tagsBuckets.get(1).getDocCount(), equalTo(1L));
    assertThat(tagsBuckets.get(2).getKeyAsString(), equalTo("c"));
    assertThat(tagsBuckets.get(2).getDocCount(), equalTo(1L));
    // nested.field2: 5
    bucket = usernameBuckets.get(5);
    assertThat(bucket.getKeyAsString(), equalTo("5"));
    assertThat(bucket.getDocCount(), equalTo(2L));
    reverseNested = bucket.getAggregations().get("nested1_to_field1");
    tags = reverseNested.getAggregations().get("field1");
    tagsBuckets = new ArrayList<>(tags.getBuckets());
    assertThat(tagsBuckets.size(), equalTo(4));
    assertThat(tagsBuckets.get(0).getKeyAsString(), equalTo("b"));
    assertThat(tagsBuckets.get(0).getDocCount(), equalTo(1L));
    assertThat(tagsBuckets.get(1).getKeyAsString(), equalTo("c"));
    assertThat(tagsBuckets.get(1).getDocCount(), equalTo(1L));
    assertThat(tagsBuckets.get(2).getKeyAsString(), equalTo("d"));
    assertThat(tagsBuckets.get(2).getDocCount(), equalTo(1L));
    assertThat(tagsBuckets.get(3).getKeyAsString(), equalTo("z"));
    assertThat(tagsBuckets.get(3).getDocCount(), equalTo(1L));
    // nested.field2: 6
    bucket = usernameBuckets.get(6);
    assertThat(bucket.getKeyAsString(), equalTo("6"));
    assertThat(bucket.getDocCount(), equalTo(2L));
    reverseNested = bucket.getAggregations().get("nested1_to_field1");
    tags = reverseNested.getAggregations().get("field1");
    tagsBuckets = new ArrayList<>(tags.getBuckets());
    assertThat(tagsBuckets.size(), equalTo(4));
    assertThat(tagsBuckets.get(0).getKeyAsString(), equalTo("c"));
    assertThat(tagsBuckets.get(0).getDocCount(), equalTo(2L));
    assertThat(tagsBuckets.get(1).getKeyAsString(), equalTo("b"));
    assertThat(tagsBuckets.get(1).getDocCount(), equalTo(1L));
    assertThat(tagsBuckets.get(2).getKeyAsString(), equalTo("d"));
    assertThat(tagsBuckets.get(2).getDocCount(), equalTo(1L));
    assertThat(tagsBuckets.get(3).getKeyAsString(), equalTo("y"));
    assertThat(tagsBuckets.get(3).getDocCount(), equalTo(1L));
    // nested.field2: 8
    bucket = usernameBuckets.get(7);
    assertThat(bucket.getKeyAsString(), equalTo("8"));
    assertThat(bucket.getDocCount(), equalTo(2L));
    reverseNested = bucket.getAggregations().get("nested1_to_field1");
    tags = reverseNested.getAggregations().get("field1");
    tagsBuckets = new ArrayList<>(tags.getBuckets());
    assertThat(tagsBuckets.size(), equalTo(4));
    assertThat(tagsBuckets.get(0).getKeyAsString(), equalTo("c"));
    assertThat(tagsBuckets.get(0).getDocCount(), equalTo(2L));
    assertThat(tagsBuckets.get(1).getKeyAsString(), equalTo("d"));
    assertThat(tagsBuckets.get(1).getDocCount(), equalTo(1L));
    assertThat(tagsBuckets.get(2).getKeyAsString(), equalTo("e"));
    assertThat(tagsBuckets.get(2).getDocCount(), equalTo(1L));
    assertThat(tagsBuckets.get(3).getKeyAsString(), equalTo("x"));
    assertThat(tagsBuckets.get(3).getDocCount(), equalTo(1L));
    // nested.field2: 9
    bucket = usernameBuckets.get(8);
    assertThat(bucket.getKeyAsString(), equalTo("9"));
    assertThat(bucket.getDocCount(), equalTo(2L));
    reverseNested = bucket.getAggregations().get("nested1_to_field1");
    tags = reverseNested.getAggregations().get("field1");
    tagsBuckets = new ArrayList<>(tags.getBuckets());
    assertThat(tagsBuckets.size(), equalTo(4));
    assertThat(tagsBuckets.get(0).getKeyAsString(), equalTo("c"));
    assertThat(tagsBuckets.get(0).getDocCount(), equalTo(1L));
    assertThat(tagsBuckets.get(1).getKeyAsString(), equalTo("d"));
    assertThat(tagsBuckets.get(1).getDocCount(), equalTo(1L));
    assertThat(tagsBuckets.get(2).getKeyAsString(), equalTo("e"));
    assertThat(tagsBuckets.get(2).getDocCount(), equalTo(1L));
    assertThat(tagsBuckets.get(3).getKeyAsString(), equalTo("z"));
    assertThat(tagsBuckets.get(3).getDocCount(), equalTo(1L));
}
Also used : AggregationBuilders.reverseNested(org.elasticsearch.search.aggregations.AggregationBuilders.reverseNested) Nested(org.elasticsearch.search.aggregations.bucket.nested.Nested) ReverseNested(org.elasticsearch.search.aggregations.bucket.nested.ReverseNested) Terms(org.elasticsearch.search.aggregations.bucket.terms.Terms) ArrayList(java.util.ArrayList) ReverseNested(org.elasticsearch.search.aggregations.bucket.nested.ReverseNested) SearchResponse(org.elasticsearch.action.search.SearchResponse) ElasticsearchAssertions.assertSearchResponse(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)

Example 5 with ReverseNested

use of org.elasticsearch.search.aggregations.bucket.nested.ReverseNested in project elasticsearch by elastic.

the class ReverseNestedIT method testNonExistingNestedField.

public void testNonExistingNestedField() throws Exception {
    SearchResponse searchResponse = client().prepareSearch("idx").setQuery(matchAllQuery()).addAggregation(nested("nested2", "nested1.nested2").subAggregation(reverseNested("incorrect").path("nested3"))).get();
    Nested nested = searchResponse.getAggregations().get("nested2");
    assertThat(nested, notNullValue());
    assertThat(nested.getName(), equalTo("nested2"));
    ReverseNested reverseNested = nested.getAggregations().get("incorrect");
    assertThat(reverseNested.getDocCount(), is(0L));
    // Test that parsing the reverse_nested agg doesn't fail, because the parent nested agg is unmapped:
    searchResponse = client().prepareSearch("idx").setQuery(matchAllQuery()).addAggregation(nested("incorrect1", "incorrect1").subAggregation(reverseNested("incorrect2").path("incorrect2"))).get();
    nested = searchResponse.getAggregations().get("incorrect1");
    assertThat(nested, notNullValue());
    assertThat(nested.getName(), equalTo("incorrect1"));
    assertThat(nested.getDocCount(), is(0L));
}
Also used : AggregationBuilders.reverseNested(org.elasticsearch.search.aggregations.AggregationBuilders.reverseNested) Nested(org.elasticsearch.search.aggregations.bucket.nested.Nested) ReverseNested(org.elasticsearch.search.aggregations.bucket.nested.ReverseNested) ReverseNested(org.elasticsearch.search.aggregations.bucket.nested.ReverseNested) SearchResponse(org.elasticsearch.action.search.SearchResponse) ElasticsearchAssertions.assertSearchResponse(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)

Aggregations

SearchResponse (org.elasticsearch.action.search.SearchResponse)6 Nested (org.elasticsearch.search.aggregations.bucket.nested.Nested)6 ReverseNested (org.elasticsearch.search.aggregations.bucket.nested.ReverseNested)6 AggregationBuilders.reverseNested (org.elasticsearch.search.aggregations.AggregationBuilders.reverseNested)5 ElasticsearchAssertions.assertSearchResponse (org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)5 Terms (org.elasticsearch.search.aggregations.bucket.terms.Terms)4 ArrayList (java.util.ArrayList)2 BadRequestException (com.b2international.commons.exceptions.BadRequestException)1 FormattedRuntimeException (com.b2international.commons.exceptions.FormattedRuntimeException)1 Aggregation (com.b2international.index.aggregations.Aggregation)1 Bucket (com.b2international.index.aggregations.Bucket)1 EsClient (com.b2international.index.es.client.EsClient)1 EsQueryBuilder (com.b2international.index.es.query.EsQueryBuilder)1 DocumentMapping (com.b2international.index.mapping.DocumentMapping)1 IOException (java.io.IOException)1 ElasticsearchStatusException (org.elasticsearch.ElasticsearchStatusException)1 SearchRequest (org.elasticsearch.action.search.SearchRequest)1 XContentBuilder (org.elasticsearch.common.xcontent.XContentBuilder)1 QueryBuilder (org.elasticsearch.index.query.QueryBuilder)1 Aggregations (org.elasticsearch.search.aggregations.Aggregations)1