Skip to content

Commit

Permalink
Aggregations top_hits: Fixed inconsistent sorting of the hits
Browse files Browse the repository at this point in the history
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
  • Loading branch information
martijnvg committed Sep 11, 2014
1 parent fa7b634 commit 4c30fb3
Showing 1 changed file with 10 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -94,20 +94,23 @@ public InternalAggregation reduce(ReduceContext reduceContext) {
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];
Expand Down

0 comments on commit 4c30fb3

Please sign in to comment.