Skip to content

Commit

Permalink
Children aggregation: The children aggs' post collection translates t…
Browse files Browse the repository at this point in the history
…he buckets on the parent level to the child level and because of that it needs to invoke the post collection of its nested aggs.

Closes elastic#9271
  • Loading branch information
martijnvg committed Jan 19, 2015
1 parent ebf739b commit 79785ae
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 3 deletions.
Expand Up @@ -180,6 +180,10 @@ protected void doPostCollection() throws IOException {
}
}
}
// Need to invoke post collection on all aggs that the children agg is wrapping,
// otherwise any post work that is required, because we started to collect buckets
// in the method will not be performed.
collectableSubAggregators.postCollection();
}

@Override
Expand Down
Expand Up @@ -32,11 +32,11 @@

import java.util.*;

import static org.elasticsearch.index.query.QueryBuilders.hasChildQuery;
import static org.elasticsearch.index.query.QueryBuilders.matchQuery;
import static org.elasticsearch.index.query.QueryBuilders.termQuery;
import static org.elasticsearch.search.aggregations.AggregationBuilders.*;
import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertAcked;
import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertNoFailures;
import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse;
import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.*;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.greaterThan;
import static org.hamcrest.Matchers.is;
Expand Down Expand Up @@ -258,6 +258,66 @@ public void testWithDeletes() throws Exception {
}
}

@Test
public void testPostCollection() throws Exception {
String indexName = "prodcatalog";
String masterType = "masterprod";
String childType = "variantsku";
assertAcked(
prepareCreate(indexName)
.addMapping(masterType, "brand", "type=string", "name", "type=string", "material", "type=string")
.addMapping(childType, "_parent", "type=masterprod", "color", "type=string", "size", "type=string")
);

List<IndexRequestBuilder> requests = new ArrayList<>();
requests.add(client().prepareIndex(indexName, masterType, "1").setSource("brand", "Levis", "name", "Style 501", "material", "Denim"));
requests.add(client().prepareIndex(indexName, childType, "0").setParent("1").setSource("color", "blue", "size", "32"));
requests.add(client().prepareIndex(indexName, childType, "1").setParent("1").setSource("color", "blue", "size", "34"));
requests.add(client().prepareIndex(indexName, childType, "2").setParent("1").setSource("color", "blue", "size", "36"));
requests.add(client().prepareIndex(indexName, childType, "3").setParent("1").setSource("color", "black", "size", "38"));
requests.add(client().prepareIndex(indexName, childType, "4").setParent("1").setSource("color", "black", "size", "40"));
requests.add(client().prepareIndex(indexName, childType, "5").setParent("1").setSource("color", "gray", "size", "36"));

requests.add(client().prepareIndex(indexName, masterType, "2").setSource("brand", "Wrangler", "name", "Regular Cut", "material", "Leather"));
requests.add(client().prepareIndex(indexName, childType, "6").setParent("2").setSource("color", "blue", "size", "32"));
requests.add(client().prepareIndex(indexName, childType, "7").setParent("2").setSource("color", "blue", "size", "34"));
requests.add(client().prepareIndex(indexName, childType, "8").setParent("2").setSource("color", "black", "size", "36"));
requests.add(client().prepareIndex(indexName, childType, "9").setParent("2").setSource("color", "black", "size", "38"));
requests.add(client().prepareIndex(indexName, childType, "10").setParent("2").setSource("color", "black", "size", "40"));
requests.add(client().prepareIndex(indexName, childType, "11").setParent("2").setSource("color", "orange", "size", "36"));
requests.add(client().prepareIndex(indexName, childType, "12").setParent("2").setSource("color", "green", "size", "44"));
indexRandom(true, requests);

SearchResponse response = client().prepareSearch(indexName).setTypes(masterType)
.setQuery(hasChildQuery(childType, termQuery("color", "orange")))
.addAggregation(children("my-refinements")
.childType(childType)
.subAggregation(terms("my-colors").field(childType + ".color"))
.subAggregation(terms("my-sizes").field(childType + ".size"))
).get();
assertNoFailures(response);
assertHitCount(response, 1);

Children childrenAgg = response.getAggregations().get("my-refinements");
assertThat(childrenAgg.getDocCount(), equalTo(7l));

Terms termsAgg = childrenAgg.getAggregations().get("my-colors");
assertThat(termsAgg.getBuckets().size(), equalTo(4));
assertThat(termsAgg.getBucketByKey("black").getDocCount(), equalTo(3l));
assertThat(termsAgg.getBucketByKey("blue").getDocCount(), equalTo(2l));
assertThat(termsAgg.getBucketByKey("green").getDocCount(), equalTo(1l));
assertThat(termsAgg.getBucketByKey("orange").getDocCount(), equalTo(1l));

termsAgg = childrenAgg.getAggregations().get("my-sizes");
assertThat(termsAgg.getBuckets().size(), equalTo(6));
assertThat(termsAgg.getBucketByKey("36").getDocCount(), equalTo(2l));
assertThat(termsAgg.getBucketByKey("32").getDocCount(), equalTo(1l));
assertThat(termsAgg.getBucketByKey("34").getDocCount(), equalTo(1l));
assertThat(termsAgg.getBucketByKey("38").getDocCount(), equalTo(1l));
assertThat(termsAgg.getBucketByKey("40").getDocCount(), equalTo(1l));
assertThat(termsAgg.getBucketByKey("44").getDocCount(), equalTo(1l));
}

private static final class Control {

final String category;
Expand Down

0 comments on commit 79785ae

Please sign in to comment.