use of org.elasticsearch.search.aggregations.bucket.children.Children in project elasticsearch by elastic.
the class ChildrenIT method testPostCollection.
public void testPostCollection() throws Exception {
String indexName = "prodcatalog";
String masterType = "masterprod";
String childType = "variantsku";
assertAcked(prepareCreate(indexName).addMapping(masterType, "brand", "type=text", "name", "type=keyword", "material", "type=text").addMapping(childType, "_parent", "type=masterprod", "color", "type=keyword", "size", "type=keyword"));
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"), ScoreMode.None)).addAggregation(children("my-refinements", childType).subAggregation(terms("my-colors").field("color")).subAggregation(terms("my-sizes").field("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));
}
use of org.elasticsearch.search.aggregations.bucket.children.Children in project elasticsearch by elastic.
the class ChildrenIT method testWithDeletes.
public void testWithDeletes() throws Exception {
String indexName = "xyz";
assertAcked(prepareCreate(indexName).addMapping("parent").addMapping("child", "_parent", "type=parent", "count", "type=long"));
List<IndexRequestBuilder> requests = new ArrayList<>();
requests.add(client().prepareIndex(indexName, "parent", "1").setSource("{}", XContentType.JSON));
requests.add(client().prepareIndex(indexName, "child", "0").setParent("1").setSource("count", 1));
requests.add(client().prepareIndex(indexName, "child", "1").setParent("1").setSource("count", 1));
requests.add(client().prepareIndex(indexName, "child", "2").setParent("1").setSource("count", 1));
requests.add(client().prepareIndex(indexName, "child", "3").setParent("1").setSource("count", 1));
indexRandom(true, requests);
for (int i = 0; i < 10; i++) {
SearchResponse searchResponse = client().prepareSearch(indexName).addAggregation(children("children", "child").subAggregation(sum("counts").field("count"))).get();
assertNoFailures(searchResponse);
Children children = searchResponse.getAggregations().get("children");
assertThat(children.getDocCount(), equalTo(4L));
Sum count = children.getAggregations().get("counts");
assertThat(count.getValue(), equalTo(4.));
String idToUpdate = Integer.toString(randomInt(3));
/*
* The whole point of this test is to test these things with deleted
* docs in the index so we turn off detect_noop to make sure that
* the updates cause that.
*/
UpdateResponse updateResponse = client().prepareUpdate(indexName, "child", idToUpdate).setParent("1").setDoc(Requests.INDEX_CONTENT_TYPE, "count", 1).setDetectNoop(false).get();
assertThat(updateResponse.getVersion(), greaterThan(1L));
refresh();
}
}
Aggregations