Aggregations top_hits: Fixed inconsistent sorting of the hits

In the reduce logic of the top_hits aggregation if the first shard result to process contained has no results then the merging of all the shard results can go wrong resulting in an incorrect sorted hits.
This bug can only manifest with a sort other than score.

Closes #7697
This commit is contained in:
Martijn van Groningen 2014-09-11 17:26:05 +02:00
parent 3ef6860679
commit d0300b3f59
1 changed files with 10 additions and 7 deletions

View File

@ -94,20 +94,23 @@ public class InternalTopHits extends InternalMetricsAggregation implements TopHi
List<InternalAggregation> aggregations = reduceContext.aggregations();
TopDocs[] shardDocs = new TopDocs[aggregations.size()];
InternalSearchHits[] shardHits = new InternalSearchHits[aggregations.size()];
TopDocs topDocs = this.topDocs;
for (int i = 0; i < shardDocs.length; i++) {
InternalTopHits topHitsAgg = (InternalTopHits) aggregations.get(i);
shardDocs[i] = topHitsAgg.topDocs;
shardHits[i] = topHitsAgg.searchHits;
if (topDocs.scoreDocs.length == 0) {
topDocs = topHitsAgg.topDocs;
}
}
final Sort sort;
if (topDocs instanceof TopFieldDocs) {
sort = new Sort(((TopFieldDocs) topDocs).fields);
} else {
sort = null;
}
try {
final Sort sort;
if (topDocs instanceof TopFieldDocs) {
sort = new Sort(((TopFieldDocs) topDocs).fields);
} else {
sort = null;
}
int[] tracker = new int[shardHits.length];
TopDocs reducedTopDocs = TopDocs.merge(sort, from, size, shardDocs);
InternalSearchHit[] hits = new InternalSearchHit[reducedTopDocs.scoreDocs.length];