Search in sources :

Example 6 with ReverseNested

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

the class ReverseNestedIT method testSameParentDocHavingMultipleBuckets.

public void testSameParentDocHavingMultipleBuckets() throws Exception {
    XContentBuilder mapping = jsonBuilder().startObject().startObject("product").field("dynamic", "strict").startObject("properties").startObject("id").field("type", "long").endObject().startObject("category").field("type", "nested").startObject("properties").startObject("name").field("type", "keyword").endObject().endObject().endObject().startObject("sku").field("type", "nested").startObject("properties").startObject("sku_type").field("type", "keyword").endObject().startObject("colors").field("type", "nested").startObject("properties").startObject("name").field("type", "keyword").endObject().endObject().endObject().endObject().endObject().endObject().endObject().endObject();
    assertAcked(prepareCreate("idx3").setSettings(Settings.builder().put(SETTING_NUMBER_OF_SHARDS, 1).put(SETTING_NUMBER_OF_REPLICAS, 0)).addMapping("product", mapping));
    client().prepareIndex("idx3", "product", "1").setRefreshPolicy(IMMEDIATE).setSource(jsonBuilder().startObject().startArray("sku").startObject().field("sku_type", "bar1").startArray("colors").startObject().field("name", "red").endObject().startObject().field("name", "green").endObject().startObject().field("name", "yellow").endObject().endArray().endObject().startObject().field("sku_type", "bar1").startArray("colors").startObject().field("name", "red").endObject().startObject().field("name", "blue").endObject().startObject().field("name", "white").endObject().endArray().endObject().startObject().field("sku_type", "bar1").startArray("colors").startObject().field("name", "black").endObject().startObject().field("name", "blue").endObject().endArray().endObject().startObject().field("sku_type", "bar2").startArray("colors").startObject().field("name", "orange").endObject().endArray().endObject().startObject().field("sku_type", "bar2").startArray("colors").startObject().field("name", "pink").endObject().endArray().endObject().endArray().startArray("category").startObject().field("name", "abc").endObject().startObject().field("name", "klm").endObject().startObject().field("name", "xyz").endObject().endArray().endObject()).get();
    SearchResponse response = client().prepareSearch("idx3").addAggregation(nested("nested_0", "category").subAggregation(terms("group_by_category").field("category.name").subAggregation(reverseNested("to_root").subAggregation(nested("nested_1", "sku").subAggregation(filter("filter_by_sku", termQuery("sku.sku_type", "bar1")).subAggregation(count("sku_count").field("sku.sku_type"))))))).get();
    assertNoFailures(response);
    assertHitCount(response, 1);
    Nested nested0 = response.getAggregations().get("nested_0");
    assertThat(nested0.getDocCount(), equalTo(3L));
    Terms terms = nested0.getAggregations().get("group_by_category");
    assertThat(terms.getBuckets().size(), equalTo(3));
    for (String bucketName : new String[] { "abc", "klm", "xyz" }) {
        logger.info("Checking results for bucket {}", bucketName);
        Terms.Bucket bucket = terms.getBucketByKey(bucketName);
        assertThat(bucket.getDocCount(), equalTo(1L));
        ReverseNested toRoot = bucket.getAggregations().get("to_root");
        assertThat(toRoot.getDocCount(), equalTo(1L));
        Nested nested1 = toRoot.getAggregations().get("nested_1");
        assertThat(nested1.getDocCount(), equalTo(5L));
        Filter filterByBar = nested1.getAggregations().get("filter_by_sku");
        assertThat(filterByBar.getDocCount(), equalTo(3L));
        ValueCount barCount = filterByBar.getAggregations().get("sku_count");
        assertThat(barCount.getValue(), equalTo(3L));
    }
    response = client().prepareSearch("idx3").addAggregation(nested("nested_0", "category").subAggregation(terms("group_by_category").field("category.name").subAggregation(reverseNested("to_root").subAggregation(nested("nested_1", "sku").subAggregation(filter("filter_by_sku", termQuery("sku.sku_type", "bar1")).subAggregation(nested("nested_2", "sku.colors").subAggregation(filter("filter_sku_color", termQuery("sku.colors.name", "red")).subAggregation(reverseNested("reverse_to_sku").path("sku").subAggregation(count("sku_count").field("sku.sku_type")))))))))).get();
    assertNoFailures(response);
    assertHitCount(response, 1);
    nested0 = response.getAggregations().get("nested_0");
    assertThat(nested0.getDocCount(), equalTo(3L));
    terms = nested0.getAggregations().get("group_by_category");
    assertThat(terms.getBuckets().size(), equalTo(3));
    for (String bucketName : new String[] { "abc", "klm", "xyz" }) {
        logger.info("Checking results for bucket {}", bucketName);
        Terms.Bucket bucket = terms.getBucketByKey(bucketName);
        assertThat(bucket.getDocCount(), equalTo(1L));
        ReverseNested toRoot = bucket.getAggregations().get("to_root");
        assertThat(toRoot.getDocCount(), equalTo(1L));
        Nested nested1 = toRoot.getAggregations().get("nested_1");
        assertThat(nested1.getDocCount(), equalTo(5L));
        Filter filterByBar = nested1.getAggregations().get("filter_by_sku");
        assertThat(filterByBar.getDocCount(), equalTo(3L));
        Nested nested2 = filterByBar.getAggregations().get("nested_2");
        assertThat(nested2.getDocCount(), equalTo(8L));
        Filter filterBarColor = nested2.getAggregations().get("filter_sku_color");
        assertThat(filterBarColor.getDocCount(), equalTo(2L));
        ReverseNested reverseToBar = filterBarColor.getAggregations().get("reverse_to_sku");
        assertThat(reverseToBar.getDocCount(), equalTo(2L));
        ValueCount barCount = reverseToBar.getAggregations().get("sku_count");
        assertThat(barCount.getValue(), equalTo(2L));
    }
}
Also used : ValueCount(org.elasticsearch.search.aggregations.metrics.valuecount.ValueCount) Filter(org.elasticsearch.search.aggregations.bucket.filter.Filter) 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) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder) 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