Search in sources :

Example 6 with TopHits

use of org.elasticsearch.search.aggregations.metrics.tophits.TopHits in project elasticsearch by elastic.

the class TopHitsIT method testTopHitsInNested.

public void testTopHitsInNested() throws Exception {
    SearchResponse searchResponse = client().prepareSearch("articles").addAggregation(histogram("dates").field("date").interval(5).order(Histogram.Order.aggregation("to-comments", true)).subAggregation(nested("to-comments", "comments").subAggregation(topHits("comments").highlighter(new HighlightBuilder().field(new HighlightBuilder.Field("comments.message").highlightQuery(matchQuery("comments.message", "text")))).sort("comments.id", SortOrder.ASC)))).get();
    Histogram histogram = searchResponse.getAggregations().get("dates");
    for (int i = 0; i < numArticles; i += 5) {
        Histogram.Bucket bucket = histogram.getBuckets().get(i / 5);
        assertThat(bucket.getDocCount(), equalTo(5L));
        long numNestedDocs = 10 + (5 * i);
        Nested nested = bucket.getAggregations().get("to-comments");
        assertThat(nested.getDocCount(), equalTo(numNestedDocs));
        TopHits hits = nested.getAggregations().get("comments");
        SearchHits searchHits = hits.getHits();
        assertThat(searchHits.getTotalHits(), equalTo(numNestedDocs));
        for (int j = 0; j < 3; j++) {
            assertThat(searchHits.getAt(j).getNestedIdentity().getField().string(), equalTo("comments"));
            assertThat(searchHits.getAt(j).getNestedIdentity().getOffset(), equalTo(0));
            assertThat((Integer) searchHits.getAt(j).getSourceAsMap().get("id"), equalTo(0));
            HighlightField highlightField = searchHits.getAt(j).getHighlightFields().get("comments.message");
            assertThat(highlightField.getFragments().length, equalTo(1));
            assertThat(highlightField.getFragments()[0].string(), equalTo("some <em>text</em>"));
        }
    }
}
Also used : SearchHitField(org.elasticsearch.search.SearchHitField) HighlightField(org.elasticsearch.search.fetch.subphase.highlight.HighlightField) Histogram(org.elasticsearch.search.aggregations.bucket.histogram.Histogram) TopHits(org.elasticsearch.search.aggregations.metrics.tophits.TopHits) Nested(org.elasticsearch.search.aggregations.bucket.nested.Nested) HighlightField(org.elasticsearch.search.fetch.subphase.highlight.HighlightField) SearchHits(org.elasticsearch.search.SearchHits) HighlightBuilder(org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder) SearchResponse(org.elasticsearch.action.search.SearchResponse) ElasticsearchAssertions.assertSearchResponse(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)

Example 7 with TopHits

use of org.elasticsearch.search.aggregations.metrics.tophits.TopHits in project elasticsearch by elastic.

the class TopHitsIT method testNoStoredFields.

public void testNoStoredFields() throws Exception {
    SearchResponse response = client().prepareSearch("idx").setTypes("type").addAggregation(terms("terms").executionHint(randomExecutionHint()).field(TERMS_AGGS_FIELD).subAggregation(topHits("hits").storedField("_none_"))).get();
    assertSearchResponse(response);
    Terms terms = response.getAggregations().get("terms");
    assertThat(terms, notNullValue());
    assertThat(terms.getName(), equalTo("terms"));
    assertThat(terms.getBuckets().size(), equalTo(5));
    for (int i = 0; i < 5; i++) {
        Terms.Bucket bucket = terms.getBucketByKey("val" + i);
        assertThat(bucket, notNullValue());
        assertThat(key(bucket), equalTo("val" + i));
        assertThat(bucket.getDocCount(), equalTo(10L));
        TopHits topHits = bucket.getAggregations().get("hits");
        SearchHits hits = topHits.getHits();
        assertThat(hits.getTotalHits(), equalTo(10L));
        assertThat(hits.getHits().length, equalTo(3));
        for (SearchHit hit : hits) {
            assertThat(hit.getSourceAsMap(), nullValue());
            assertThat(hit.getId(), nullValue());
            assertThat(hit.getType(), nullValue());
        }
    }
}
Also used : TopHits(org.elasticsearch.search.aggregations.metrics.tophits.TopHits) SearchHit(org.elasticsearch.search.SearchHit) 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 8 with TopHits

use of org.elasticsearch.search.aggregations.metrics.tophits.TopHits in project elasticsearch by elastic.

the class TopHitsIT method testBasicsGetProperty.

public void testBasicsGetProperty() throws Exception {
    SearchResponse searchResponse = client().prepareSearch("idx").setQuery(matchAllQuery()).addAggregation(global("global").subAggregation(topHits("hits"))).execute().actionGet();
    assertSearchResponse(searchResponse);
    Global global = searchResponse.getAggregations().get("global");
    assertThat(global, notNullValue());
    assertThat(global.getName(), equalTo("global"));
    assertThat(global.getAggregations(), notNullValue());
    assertThat(global.getAggregations().asMap().size(), equalTo(1));
    TopHits topHits = global.getAggregations().get("hits");
    assertThat(topHits, notNullValue());
    assertThat(topHits.getName(), equalTo("hits"));
    assertThat((TopHits) global.getProperty("hits"), sameInstance(topHits));
}
Also used : TopHits(org.elasticsearch.search.aggregations.metrics.tophits.TopHits) Global(org.elasticsearch.search.aggregations.bucket.global.Global) SearchResponse(org.elasticsearch.action.search.SearchResponse) ElasticsearchAssertions.assertSearchResponse(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)

Example 9 with TopHits

use of org.elasticsearch.search.aggregations.metrics.tophits.TopHits in project elasticsearch by elastic.

the class TopHitsIT method testIssue11119.

public void testIssue11119() throws Exception {
    // Test that top_hits aggregation is fed scores if query results size=0
    SearchResponse response = client().prepareSearch("idx").setTypes("field-collapsing").setSize(0).setQuery(matchQuery("text", "x y z")).addAggregation(terms("terms").executionHint(randomExecutionHint()).field("group").subAggregation(topHits("hits"))).get();
    assertSearchResponse(response);
    assertThat(response.getHits().getTotalHits(), equalTo(8L));
    assertThat(response.getHits().getHits().length, equalTo(0));
    assertThat(response.getHits().getMaxScore(), equalTo(0f));
    Terms terms = response.getAggregations().get("terms");
    assertThat(terms, notNullValue());
    assertThat(terms.getName(), equalTo("terms"));
    assertThat(terms.getBuckets().size(), equalTo(3));
    for (Terms.Bucket bucket : terms.getBuckets()) {
        assertThat(bucket, notNullValue());
        TopHits topHits = bucket.getAggregations().get("hits");
        SearchHits hits = topHits.getHits();
        float bestScore = Float.MAX_VALUE;
        for (int h = 0; h < hits.getHits().length; h++) {
            float score = hits.getAt(h).getScore();
            assertThat(score, lessThanOrEqualTo(bestScore));
            assertThat(score, greaterThan(0f));
            bestScore = hits.getAt(h).getScore();
        }
    }
    // Also check that min_score setting works when size=0
    // (technically not a test of top_hits but implementation details are
    // tied up with the need to feed scores into the agg tree even when
    // users don't want ranked set of query results.)
    response = client().prepareSearch("idx").setTypes("field-collapsing").setSize(0).setMinScore(0.0001f).setQuery(matchQuery("text", "x y z")).addAggregation(terms("terms").executionHint(randomExecutionHint()).field("group")).get();
    assertSearchResponse(response);
    assertThat(response.getHits().getTotalHits(), equalTo(8L));
    assertThat(response.getHits().getHits().length, equalTo(0));
    assertThat(response.getHits().getMaxScore(), equalTo(0f));
    terms = response.getAggregations().get("terms");
    assertThat(terms, notNullValue());
    assertThat(terms.getName(), equalTo("terms"));
    assertThat(terms.getBuckets().size(), equalTo(3));
}
Also used : TopHits(org.elasticsearch.search.aggregations.metrics.tophits.TopHits) 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 10 with TopHits

use of org.elasticsearch.search.aggregations.metrics.tophits.TopHits in project elasticsearch by elastic.

the class TopHitsIT method testBreadthFirstWithAggOrderAndScoreNeeded.

public void testBreadthFirstWithAggOrderAndScoreNeeded() throws Exception {
    SearchResponse response = client().prepareSearch("idx").setTypes("type").addAggregation(terms("terms").executionHint(randomExecutionHint()).collectMode(SubAggCollectionMode.BREADTH_FIRST).field(TERMS_AGGS_FIELD).order(Terms.Order.aggregation("max", false)).subAggregation(max("max").field(SORT_FIELD)).subAggregation(topHits("hits").size(3))).get();
    assertSearchResponse(response);
    Terms terms = response.getAggregations().get("terms");
    assertThat(terms, notNullValue());
    assertThat(terms.getName(), equalTo("terms"));
    assertThat(terms.getBuckets().size(), equalTo(5));
    int id = 4;
    for (Terms.Bucket bucket : terms.getBuckets()) {
        assertThat(bucket, notNullValue());
        assertThat(key(bucket), equalTo("val" + id));
        assertThat(bucket.getDocCount(), equalTo(10L));
        TopHits topHits = bucket.getAggregations().get("hits");
        SearchHits hits = topHits.getHits();
        assertThat(hits.getTotalHits(), equalTo(10L));
        assertThat(hits.getHits().length, equalTo(3));
        assertThat(hits.getAt(0).getSourceAsMap().size(), equalTo(4));
        id--;
    }
}
Also used : TopHits(org.elasticsearch.search.aggregations.metrics.tophits.TopHits) 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)

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