Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Track scores should be applied properly for top_hits aggregation. #6934

Merged
merged 1 commit into from Jul 21, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
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));
}
}

}