Aggregations: Track scores should be applied properly for `top_hits` aggregation.
Closes #6934
This commit is contained in:
parent
2d2088b259
commit
ba1042e7d1
|
@ -107,7 +107,7 @@ public class TopHitsAggregator extends MetricsAggregator implements ScorerAware
|
|||
int topN = topHitsContext.from() + topHitsContext.size();
|
||||
topDocsCollectors.put(
|
||||
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.setScorer(currentScorer);
|
||||
|
|
|
@ -48,6 +48,7 @@ import static org.elasticsearch.search.aggregations.AggregationBuilders.*;
|
|||
import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse;
|
||||
import static org.hamcrest.Matchers.containsString;
|
||||
import static org.hamcrest.Matchers.equalTo;
|
||||
import static org.hamcrest.Matchers.not;
|
||||
import static org.hamcrest.core.IsNull.notNullValue;
|
||||
|
||||
/**
|
||||
|
@ -431,9 +432,6 @@ public class TopHitsTests extends ElasticsearchIntegrationTest {
|
|||
assertThat(e.getMessage(), containsString("ElasticsearchParseException"));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
@Test
|
||||
public void testEmptyIndex() throws Exception {
|
||||
|
@ -448,4 +446,51 @@ public class TopHitsTests extends ElasticsearchIntegrationTest {
|
|||
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));
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue