Aggregations: Track scores should be applied properly for `top_hits` aggregation.

Closes #6934
This commit is contained in:
Martijn van Groningen 2014-07-20 21:57:27 +02:00
parent 2d2088b259
commit ba1042e7d1
2 changed files with 49 additions and 4 deletions

View File

@ -107,7 +107,7 @@ public class TopHitsAggregator extends MetricsAggregator implements ScorerAware
int topN = topHitsContext.from() + topHitsContext.size(); int topN = topHitsContext.from() + topHitsContext.size();
topDocsCollectors.put( topDocsCollectors.put(
bucketOrdinal, bucketOrdinal,
topDocsCollector = sort != null ? TopFieldCollector.create(sort, topN, true, topHitsContext.trackScores(), true, false) : TopScoreDocCollector.create(topN, false) topDocsCollector = sort != null ? TopFieldCollector.create(sort, topN, true, topHitsContext.trackScores(), topHitsContext.trackScores(), false) : TopScoreDocCollector.create(topN, false)
); );
topDocsCollector.setNextReader(currentContext); topDocsCollector.setNextReader(currentContext);
topDocsCollector.setScorer(currentScorer); topDocsCollector.setScorer(currentScorer);

View File

@ -48,6 +48,7 @@ import static org.elasticsearch.search.aggregations.AggregationBuilders.*;
import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse; import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse;
import static org.hamcrest.Matchers.containsString; import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.not;
import static org.hamcrest.core.IsNull.notNullValue; import static org.hamcrest.core.IsNull.notNullValue;
/** /**
@ -432,9 +433,6 @@ public class TopHitsTests extends ElasticsearchIntegrationTest {
} }
} }
@Test @Test
public void testEmptyIndex() throws Exception { public void testEmptyIndex() throws Exception {
SearchResponse response = client().prepareSearch("empty").setTypes("type") SearchResponse response = client().prepareSearch("empty").setTypes("type")
@ -448,4 +446,51 @@ public class TopHitsTests extends ElasticsearchIntegrationTest {
assertThat(hits.getHits().totalHits(), equalTo(0l)); assertThat(hits.getHits().totalHits(), equalTo(0l));
} }
@Test
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")
.setTrackScores(trackScore)
.setSize(1)
.addSort("_id", 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).score(), 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).score(), 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).score(), trackScore ? not(equalTo(Float.NaN)) : equalTo(Float.NaN));
}
}
} }