Search in sources :

Example 16 with TopHits

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));
}
Also used : TopHits(org.elasticsearch.search.aggregations.metrics.tophits.TopHits) Nested(org.elasticsearch.search.aggregations.bucket.nested.Nested) Terms(org.elasticsearch.search.aggregations.bucket.terms.Terms) SearchHits(org.elasticsearch.search.SearchHits) SearchResponse(org.elasticsearch.action.search.SearchResponse) ElasticsearchAssertions.assertSearchResponse(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)

Example 17 with TopHits

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));
}
Also used : TopHits(org.elasticsearch.search.aggregations.metrics.tophits.TopHits) SearchResponse(org.elasticsearch.action.search.SearchResponse) ElasticsearchAssertions.assertSearchResponse(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)

Example 18 with TopHits

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));
            }
        }
    }
}
Also used : HashSet(java.util.HashSet) Set(java.util.Set) TopHits(org.elasticsearch.search.aggregations.metrics.tophits.TopHits) SearchHit(org.elasticsearch.search.SearchHit) Terms(org.elasticsearch.search.aggregations.bucket.terms.Terms) Children(org.elasticsearch.search.aggregations.bucket.children.Children) HashMap(java.util.HashMap) Map(java.util.Map) SearchResponse(org.elasticsearch.action.search.SearchResponse) ElasticsearchAssertions.assertSearchResponse(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)

Aggregations

SearchResponse (org.elasticsearch.action.search.SearchResponse)18 TopHits (org.elasticsearch.search.aggregations.metrics.tophits.TopHits)18 ElasticsearchAssertions.assertSearchResponse (org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)18 SearchHits (org.elasticsearch.search.SearchHits)13 Terms (org.elasticsearch.search.aggregations.bucket.terms.Terms)13 SearchHit (org.elasticsearch.search.SearchHit)5 Nested (org.elasticsearch.search.aggregations.bucket.nested.Nested)4 SearchHitField (org.elasticsearch.search.SearchHitField)3 HighlightBuilder (org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder)3 HighlightField (org.elasticsearch.search.fetch.subphase.highlight.HighlightField)3 Explanation (org.apache.lucene.search.Explanation)2 Script (org.elasticsearch.script.Script)2 Children (org.elasticsearch.search.aggregations.bucket.children.Children)2 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 Map (java.util.Map)1 Set (java.util.Set)1 Global (org.elasticsearch.search.aggregations.bucket.global.Global)1 Histogram (org.elasticsearch.search.aggregations.bucket.histogram.Histogram)1 Max (org.elasticsearch.search.aggregations.metrics.max.Max)1