use of org.elasticsearch.search.aggregations.metrics.tophits.TopHits in project elasticsearch by elastic.
the class TopHitsIT method testTopHitsInNestedSimple.
public void testTopHitsInNestedSimple() throws Exception {
SearchResponse searchResponse = client().prepareSearch("articles").setQuery(matchQuery("title", "title")).addAggregation(nested("to-comments", "comments").subAggregation(terms("users").field("comments.user").subAggregation(topHits("top-comments").sort("comments.date", SortOrder.ASC)))).get();
Nested nested = searchResponse.getAggregations().get("to-comments");
assertThat(nested.getDocCount(), equalTo(4L));
Terms terms = nested.getAggregations().get("users");
Terms.Bucket bucket = terms.getBucketByKey("a");
assertThat(bucket.getDocCount(), equalTo(1L));
TopHits topHits = bucket.getAggregations().get("top-comments");
SearchHits searchHits = topHits.getHits();
assertThat(searchHits.getTotalHits(), equalTo(1L));
assertThat(searchHits.getAt(0).getNestedIdentity().getField().string(), equalTo("comments"));
assertThat(searchHits.getAt(0).getNestedIdentity().getOffset(), equalTo(0));
assertThat((Integer) searchHits.getAt(0).getSourceAsMap().get("date"), equalTo(1));
bucket = terms.getBucketByKey("b");
assertThat(bucket.getDocCount(), equalTo(2L));
topHits = bucket.getAggregations().get("top-comments");
searchHits = topHits.getHits();
assertThat(searchHits.getTotalHits(), equalTo(2L));
assertThat(searchHits.getAt(0).getNestedIdentity().getField().string(), equalTo("comments"));
assertThat(searchHits.getAt(0).getNestedIdentity().getOffset(), equalTo(1));
assertThat((Integer) searchHits.getAt(0).getSourceAsMap().get("date"), equalTo(2));
assertThat(searchHits.getAt(1).getNestedIdentity().getField().string(), equalTo("comments"));
assertThat(searchHits.getAt(1).getNestedIdentity().getOffset(), equalTo(0));
assertThat((Integer) searchHits.getAt(1).getSourceAsMap().get("date"), equalTo(3));
bucket = terms.getBucketByKey("c");
assertThat(bucket.getDocCount(), equalTo(1L));
topHits = bucket.getAggregations().get("top-comments");
searchHits = topHits.getHits();
assertThat(searchHits.getTotalHits(), equalTo(1L));
assertThat(searchHits.getAt(0).getNestedIdentity().getField().string(), equalTo("comments"));
assertThat(searchHits.getAt(0).getNestedIdentity().getOffset(), equalTo(1));
assertThat((Integer) searchHits.getAt(0).getSourceAsMap().get("date"), equalTo(4));
}
use of org.elasticsearch.search.aggregations.metrics.tophits.TopHits in project elasticsearch by elastic.
the class TopHitsIT method testEmptyIndex.
public void testEmptyIndex() throws Exception {
SearchResponse response = client().prepareSearch("empty").setTypes("type").addAggregation(topHits("hits")).get();
assertSearchResponse(response);
TopHits hits = response.getAggregations().get("hits");
assertThat(hits, notNullValue());
assertThat(hits.getName(), equalTo("hits"));
assertThat(hits.getHits().getTotalHits(), equalTo(0L));
}
use of org.elasticsearch.search.aggregations.metrics.tophits.TopHits in project elasticsearch by elastic.
the class ChildrenIT method testChildrenAggs.
public void testChildrenAggs() throws Exception {
SearchResponse searchResponse = client().prepareSearch("test").setQuery(matchQuery("randomized", true)).addAggregation(terms("category").field("category").size(10000).subAggregation(children("to_comment", "comment").subAggregation(terms("commenters").field("commenter").size(10000).subAggregation(topHits("top_comments"))))).get();
assertSearchResponse(searchResponse);
Terms categoryTerms = searchResponse.getAggregations().get("category");
assertThat(categoryTerms.getBuckets().size(), equalTo(categoryToControl.size()));
for (Map.Entry<String, Control> entry1 : categoryToControl.entrySet()) {
Terms.Bucket categoryBucket = categoryTerms.getBucketByKey(entry1.getKey());
assertThat(categoryBucket.getKeyAsString(), equalTo(entry1.getKey()));
assertThat(categoryBucket.getDocCount(), equalTo((long) entry1.getValue().articleIds.size()));
Children childrenBucket = categoryBucket.getAggregations().get("to_comment");
assertThat(childrenBucket.getName(), equalTo("to_comment"));
assertThat(childrenBucket.getDocCount(), equalTo((long) entry1.getValue().commentIds.size()));
assertThat((long) childrenBucket.getProperty("_count"), equalTo((long) entry1.getValue().commentIds.size()));
Terms commentersTerms = childrenBucket.getAggregations().get("commenters");
assertThat((Terms) childrenBucket.getProperty("commenters"), sameInstance(commentersTerms));
assertThat(commentersTerms.getBuckets().size(), equalTo(entry1.getValue().commenterToCommentId.size()));
for (Map.Entry<String, Set<String>> entry2 : entry1.getValue().commenterToCommentId.entrySet()) {
Terms.Bucket commentBucket = commentersTerms.getBucketByKey(entry2.getKey());
assertThat(commentBucket.getKeyAsString(), equalTo(entry2.getKey()));
assertThat(commentBucket.getDocCount(), equalTo((long) entry2.getValue().size()));
TopHits topHits = commentBucket.getAggregations().get("top_comments");
for (SearchHit searchHit : topHits.getHits().getHits()) {
assertThat(entry2.getValue().contains(searchHit.getId()), is(true));
}
}
}
}
Aggregations