Skip to content

Commit

Permalink
Aggregations: Track scores should be applied properly for top_hits
Browse files Browse the repository at this point in the history
…aggregation.

Closes #6934
  • Loading branch information
martijnvg committed Jul 24, 2014
1 parent 370f5cb commit c033f41
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ public void collect(int docId, long bucketOrdinal) throws IOException {
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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
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;

/**
Expand Down Expand Up @@ -431,9 +432,6 @@ public void testFailDeferred() throws Exception {
assertThat(e.getMessage(), containsString("ElasticsearchParseException"));
}
}




@Test
public void testEmptyIndex() throws Exception {
Expand All @@ -448,4 +446,51 @@ public void testEmptyIndex() throws Exception {
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));
}
}

}

0 comments on commit c033f41

Please sign in to comment.