Search in sources :

Example 11 with TopHits

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

the class TopHitsIT method testTopHitsInSecondLayerNested.

public void testTopHitsInSecondLayerNested() throws Exception {
    SearchResponse searchResponse = client().prepareSearch("articles").setQuery(matchQuery("title", "title")).addAggregation(nested("to-comments", "comments").subAggregation(nested("to-reviewers", "comments.reviewers").subAggregation(// Also need to sort on _doc because there are two reviewers with the same name
    topHits("top-reviewers").sort("comments.reviewers.name", SortOrder.ASC).sort("_doc", SortOrder.DESC).size(7))).subAggregation(topHits("top-comments").sort("comments.date", SortOrder.DESC).size(4))).get();
    assertNoFailures(searchResponse);
    Nested toComments = searchResponse.getAggregations().get("to-comments");
    assertThat(toComments.getDocCount(), equalTo(4L));
    TopHits topComments = toComments.getAggregations().get("top-comments");
    assertThat(topComments.getHits().getTotalHits(), equalTo(4L));
    assertThat(topComments.getHits().getHits().length, equalTo(4));
    assertThat(topComments.getHits().getAt(0).getId(), equalTo("2"));
    assertThat(topComments.getHits().getAt(0).getNestedIdentity().getField().string(), equalTo("comments"));
    assertThat(topComments.getHits().getAt(0).getNestedIdentity().getOffset(), equalTo(1));
    assertThat(topComments.getHits().getAt(0).getNestedIdentity().getChild(), nullValue());
    assertThat(topComments.getHits().getAt(1).getId(), equalTo("2"));
    assertThat(topComments.getHits().getAt(1).getNestedIdentity().getField().string(), equalTo("comments"));
    assertThat(topComments.getHits().getAt(1).getNestedIdentity().getOffset(), equalTo(0));
    assertThat(topComments.getHits().getAt(1).getNestedIdentity().getChild(), nullValue());
    assertThat(topComments.getHits().getAt(2).getId(), equalTo("1"));
    assertThat(topComments.getHits().getAt(2).getNestedIdentity().getField().string(), equalTo("comments"));
    assertThat(topComments.getHits().getAt(2).getNestedIdentity().getOffset(), equalTo(1));
    assertThat(topComments.getHits().getAt(2).getNestedIdentity().getChild(), nullValue());
    assertThat(topComments.getHits().getAt(3).getId(), equalTo("1"));
    assertThat(topComments.getHits().getAt(3).getNestedIdentity().getField().string(), equalTo("comments"));
    assertThat(topComments.getHits().getAt(3).getNestedIdentity().getOffset(), equalTo(0));
    assertThat(topComments.getHits().getAt(3).getNestedIdentity().getChild(), nullValue());
    Nested toReviewers = toComments.getAggregations().get("to-reviewers");
    assertThat(toReviewers.getDocCount(), equalTo(7L));
    TopHits topReviewers = toReviewers.getAggregations().get("top-reviewers");
    assertThat(topReviewers.getHits().getTotalHits(), equalTo(7L));
    assertThat(topReviewers.getHits().getHits().length, equalTo(7));
    assertThat(topReviewers.getHits().getAt(0).getId(), equalTo("1"));
    assertThat((String) topReviewers.getHits().getAt(0).getSourceAsMap().get("name"), equalTo("user a"));
    assertThat(topReviewers.getHits().getAt(0).getNestedIdentity().getField().string(), equalTo("comments"));
    assertThat(topReviewers.getHits().getAt(0).getNestedIdentity().getOffset(), equalTo(0));
    assertThat(topReviewers.getHits().getAt(0).getNestedIdentity().getChild().getField().string(), equalTo("reviewers"));
    assertThat(topReviewers.getHits().getAt(0).getNestedIdentity().getChild().getOffset(), equalTo(0));
    assertThat(topReviewers.getHits().getAt(1).getId(), equalTo("1"));
    assertThat((String) topReviewers.getHits().getAt(1).getSourceAsMap().get("name"), equalTo("user b"));
    assertThat(topReviewers.getHits().getAt(1).getNestedIdentity().getField().string(), equalTo("comments"));
    assertThat(topReviewers.getHits().getAt(1).getNestedIdentity().getOffset(), equalTo(0));
    assertThat(topReviewers.getHits().getAt(1).getNestedIdentity().getChild().getField().string(), equalTo("reviewers"));
    assertThat(topReviewers.getHits().getAt(1).getNestedIdentity().getChild().getOffset(), equalTo(1));
    assertThat(topReviewers.getHits().getAt(2).getId(), equalTo("1"));
    assertThat((String) topReviewers.getHits().getAt(2).getSourceAsMap().get("name"), equalTo("user c"));
    assertThat(topReviewers.getHits().getAt(2).getNestedIdentity().getField().string(), equalTo("comments"));
    assertThat(topReviewers.getHits().getAt(2).getNestedIdentity().getOffset(), equalTo(0));
    assertThat(topReviewers.getHits().getAt(2).getNestedIdentity().getChild().getField().string(), equalTo("reviewers"));
    assertThat(topReviewers.getHits().getAt(2).getNestedIdentity().getChild().getOffset(), equalTo(2));
    assertThat(topReviewers.getHits().getAt(3).getId(), equalTo("1"));
    assertThat((String) topReviewers.getHits().getAt(3).getSourceAsMap().get("name"), equalTo("user c"));
    assertThat(topReviewers.getHits().getAt(3).getNestedIdentity().getField().string(), equalTo("comments"));
    assertThat(topReviewers.getHits().getAt(3).getNestedIdentity().getOffset(), equalTo(1));
    assertThat(topReviewers.getHits().getAt(3).getNestedIdentity().getChild().getField().string(), equalTo("reviewers"));
    assertThat(topReviewers.getHits().getAt(3).getNestedIdentity().getChild().getOffset(), equalTo(0));
    assertThat(topReviewers.getHits().getAt(4).getId(), equalTo("1"));
    assertThat((String) topReviewers.getHits().getAt(4).getSourceAsMap().get("name"), equalTo("user d"));
    assertThat(topReviewers.getHits().getAt(4).getNestedIdentity().getField().string(), equalTo("comments"));
    assertThat(topReviewers.getHits().getAt(4).getNestedIdentity().getOffset(), equalTo(1));
    assertThat(topReviewers.getHits().getAt(4).getNestedIdentity().getChild().getField().string(), equalTo("reviewers"));
    assertThat(topReviewers.getHits().getAt(4).getNestedIdentity().getChild().getOffset(), equalTo(1));
    assertThat(topReviewers.getHits().getAt(5).getId(), equalTo("1"));
    assertThat((String) topReviewers.getHits().getAt(5).getSourceAsMap().get("name"), equalTo("user e"));
    assertThat(topReviewers.getHits().getAt(5).getNestedIdentity().getField().string(), equalTo("comments"));
    assertThat(topReviewers.getHits().getAt(5).getNestedIdentity().getOffset(), equalTo(1));
    assertThat(topReviewers.getHits().getAt(5).getNestedIdentity().getChild().getField().string(), equalTo("reviewers"));
    assertThat(topReviewers.getHits().getAt(5).getNestedIdentity().getChild().getOffset(), equalTo(2));
    assertThat(topReviewers.getHits().getAt(6).getId(), equalTo("2"));
    assertThat((String) topReviewers.getHits().getAt(6).getSourceAsMap().get("name"), equalTo("user f"));
    assertThat(topReviewers.getHits().getAt(0).getNestedIdentity().getField().string(), equalTo("comments"));
    assertThat(topReviewers.getHits().getAt(0).getNestedIdentity().getOffset(), equalTo(0));
    assertThat(topReviewers.getHits().getAt(0).getNestedIdentity().getChild().getField().string(), equalTo("reviewers"));
    assertThat(topReviewers.getHits().getAt(0).getNestedIdentity().getChild().getOffset(), equalTo(0));
}
Also used : TopHits(org.elasticsearch.search.aggregations.metrics.tophits.TopHits) Nested(org.elasticsearch.search.aggregations.bucket.nested.Nested) SearchResponse(org.elasticsearch.action.search.SearchResponse) ElasticsearchAssertions.assertSearchResponse(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)

Example 12 with TopHits

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

the class TopHitsIT method testSortByBucket.

public void testSortByBucket() throws Exception {
    SearchResponse response = client().prepareSearch("idx").setTypes("type").addAggregation(terms("terms").executionHint(randomExecutionHint()).field(TERMS_AGGS_FIELD).order(Terms.Order.aggregation("max_sort", false)).subAggregation(topHits("hits").sort(SortBuilders.fieldSort(SORT_FIELD).order(SortOrder.DESC)).trackScores(true)).subAggregation(max("max_sort").field(SORT_FIELD))).get();
    assertSearchResponse(response);
    Terms terms = response.getAggregations().get("terms");
    assertThat(terms, notNullValue());
    assertThat(terms.getName(), equalTo("terms"));
    assertThat(terms.getBuckets().size(), equalTo(5));
    long higestSortValue = 50;
    int currentBucket = 4;
    for (Terms.Bucket bucket : terms.getBuckets()) {
        assertThat(key(bucket), equalTo("val" + currentBucket--));
        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((Long) hits.getAt(0).getSortValues()[0], equalTo(higestSortValue));
        assertThat((Long) hits.getAt(1).getSortValues()[0], equalTo(higestSortValue - 1));
        assertThat((Long) hits.getAt(2).getSortValues()[0], equalTo(higestSortValue - 2));
        Max max = bucket.getAggregations().get("max_sort");
        assertThat(max.getValue(), equalTo(((Long) higestSortValue).doubleValue()));
        higestSortValue -= 10;
    }
}
Also used : TopHits(org.elasticsearch.search.aggregations.metrics.tophits.TopHits) Max(org.elasticsearch.search.aggregations.metrics.max.Max) 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 13 with TopHits

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

the class TopHitsIT method testFetchFeatures.

public void testFetchFeatures() {
    SearchResponse response = client().prepareSearch("idx").setTypes("type").setQuery(matchQuery("text", "text").queryName("test")).addAggregation(terms("terms").executionHint(randomExecutionHint()).field(TERMS_AGGS_FIELD).subAggregation(topHits("hits").size(1).highlighter(new HighlightBuilder().field("text")).explain(true).storedField("text").fieldDataField("field1").scriptField("script", new Script(ScriptType.INLINE, MockScriptEngine.NAME, "5", Collections.emptyMap())).fetchSource("text", null).version(true))).get();
    assertSearchResponse(response);
    Terms terms = response.getAggregations().get("terms");
    assertThat(terms, notNullValue());
    assertThat(terms.getName(), equalTo("terms"));
    assertThat(terms.getBuckets().size(), equalTo(5));
    for (Terms.Bucket bucket : terms.getBuckets()) {
        TopHits topHits = bucket.getAggregations().get("hits");
        SearchHits hits = topHits.getHits();
        assertThat(hits.getTotalHits(), equalTo(10L));
        assertThat(hits.getHits().length, equalTo(1));
        SearchHit hit = hits.getAt(0);
        HighlightField highlightField = hit.getHighlightFields().get("text");
        assertThat(highlightField.getFragments().length, equalTo(1));
        assertThat(highlightField.getFragments()[0].string(), equalTo("some <em>text</em> to entertain"));
        Explanation explanation = hit.getExplanation();
        assertThat(explanation.toString(), containsString("text:text"));
        long version = hit.getVersion();
        assertThat(version, equalTo(1L));
        assertThat(hit.getMatchedQueries()[0], equalTo("test"));
        SearchHitField field = hit.field("field1");
        assertThat(field.getValue().toString(), equalTo("5"));
        assertThat(hit.getSourceAsMap().get("text").toString(), equalTo("some text to entertain"));
        field = hit.field("script");
        assertThat(field.getValue().toString(), equalTo("5"));
        assertThat(hit.getSourceAsMap().size(), equalTo(1));
        assertThat(hit.getSourceAsMap().get("text").toString(), equalTo("some text to entertain"));
    }
}
Also used : Script(org.elasticsearch.script.Script) TopHits(org.elasticsearch.search.aggregations.metrics.tophits.TopHits) SearchHit(org.elasticsearch.search.SearchHit) Explanation(org.apache.lucene.search.Explanation) Terms(org.elasticsearch.search.aggregations.bucket.terms.Terms) HighlightField(org.elasticsearch.search.fetch.subphase.highlight.HighlightField) SearchHitField(org.elasticsearch.search.SearchHitField) 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 14 with TopHits

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

the class TopHitsIT method testBasics.

public void testBasics() throws Exception {
    SearchResponse response = client().prepareSearch("idx").setTypes("type").addAggregation(terms("terms").executionHint(randomExecutionHint()).field(TERMS_AGGS_FIELD).subAggregation(topHits("hits").sort(SortBuilders.fieldSort(SORT_FIELD).order(SortOrder.DESC)))).get();
    assertSearchResponse(response);
    Terms terms = response.getAggregations().get("terms");
    assertThat(terms, notNullValue());
    assertThat(terms.getName(), equalTo("terms"));
    assertThat(terms.getBuckets().size(), equalTo(5));
    long higestSortValue = 0;
    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));
        higestSortValue += 10;
        assertThat((Long) hits.getAt(0).getSortValues()[0], equalTo(higestSortValue));
        assertThat((Long) hits.getAt(1).getSortValues()[0], equalTo(higestSortValue - 1));
        assertThat((Long) hits.getAt(2).getSortValues()[0], equalTo(higestSortValue - 2));
        assertThat(hits.getAt(0).getSourceAsMap().size(), equalTo(4));
    }
}
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 15 with TopHits

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

the class TopHitsIT method testTrackScores.

public void testTrackScores() throws Exception {
    boolean[] trackScores = new boolean[] { true, false };
    for (boolean trackScore : trackScores) {
        logger.info("Track score={}", trackScore);
        SearchResponse response = client().prepareSearch("idx").setTypes("field-collapsing").setQuery(matchQuery("text", "term rare")).addAggregation(terms("terms").field("group").subAggregation(topHits("hits").trackScores(trackScore).size(1).sort("_uid", SortOrder.DESC))).get();
        assertSearchResponse(response);
        Terms terms = response.getAggregations().get("terms");
        assertThat(terms, notNullValue());
        assertThat(terms.getName(), equalTo("terms"));
        assertThat(terms.getBuckets().size(), equalTo(3));
        Terms.Bucket bucket = terms.getBucketByKey("a");
        assertThat(key(bucket), equalTo("a"));
        TopHits topHits = bucket.getAggregations().get("hits");
        SearchHits hits = topHits.getHits();
        assertThat(hits.getMaxScore(), trackScore ? not(equalTo(Float.NaN)) : equalTo(Float.NaN));
        assertThat(hits.getAt(0).getScore(), trackScore ? not(equalTo(Float.NaN)) : equalTo(Float.NaN));
        bucket = terms.getBucketByKey("b");
        assertThat(key(bucket), equalTo("b"));
        topHits = bucket.getAggregations().get("hits");
        hits = topHits.getHits();
        assertThat(hits.getMaxScore(), trackScore ? not(equalTo(Float.NaN)) : equalTo(Float.NaN));
        assertThat(hits.getAt(0).getScore(), trackScore ? not(equalTo(Float.NaN)) : equalTo(Float.NaN));
        bucket = terms.getBucketByKey("c");
        assertThat(key(bucket), equalTo("c"));
        topHits = bucket.getAggregations().get("hits");
        hits = topHits.getHits();
        assertThat(hits.getMaxScore(), trackScore ? not(equalTo(Float.NaN)) : equalTo(Float.NaN));
        assertThat(hits.getAt(0).getScore(), trackScore ? not(equalTo(Float.NaN)) : equalTo(Float.NaN));
    }
}
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