diff --git a/src/main/java/org/elasticsearch/search/aggregations/bucket/tophits/InternalTopHits.java b/src/main/java/org/elasticsearch/search/aggregations/bucket/tophits/InternalTopHits.java index 411b7b30cf2..6ec8aed2dbf 100644 --- a/src/main/java/org/elasticsearch/search/aggregations/bucket/tophits/InternalTopHits.java +++ b/src/main/java/org/elasticsearch/search/aggregations/bucket/tophits/InternalTopHits.java @@ -18,7 +18,10 @@ */ package org.elasticsearch.search.aggregations.bucket.tophits; -import org.apache.lucene.search.*; +import org.apache.lucene.search.ScoreDoc; +import org.apache.lucene.search.Sort; +import org.apache.lucene.search.TopDocs; +import org.apache.lucene.search.TopFieldDocs; import org.elasticsearch.ExceptionsHelper; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; @@ -110,7 +113,11 @@ public class InternalTopHits extends InternalAggregation implements TopHits, ToX InternalSearchHit[] hits = new InternalSearchHit[reducedTopDocs.scoreDocs.length]; for (int i = 0; i < reducedTopDocs.scoreDocs.length; i++) { ScoreDoc scoreDoc = reducedTopDocs.scoreDocs[i]; - hits[i] = (InternalSearchHit) shardHits[scoreDoc.shardIndex].getAt(tracker[scoreDoc.shardIndex]++); + int position; + do { + position = tracker[scoreDoc.shardIndex]++; + } while (shardDocs[scoreDoc.shardIndex].scoreDocs[position] != scoreDoc); + hits[i] = (InternalSearchHit) shardHits[scoreDoc.shardIndex].getAt(position); } return new InternalTopHits(name, new InternalSearchHits(hits, reducedTopDocs.totalHits, reducedTopDocs.getMaxScore())); } catch (IOException e) { diff --git a/src/test/java/org/elasticsearch/search/aggregations/bucket/TopHitsTests.java b/src/test/java/org/elasticsearch/search/aggregations/bucket/TopHitsTests.java index 925d44ae70c..ea5dda563cb 100644 --- a/src/test/java/org/elasticsearch/search/aggregations/bucket/TopHitsTests.java +++ b/src/test/java/org/elasticsearch/search/aggregations/bucket/TopHitsTests.java @@ -209,6 +209,7 @@ public class TopHitsTests extends ElasticsearchIntegrationTest { assertThat(hits.totalHits(), equalTo(controlHits.totalHits())); assertThat(hits.getHits().length, equalTo(controlHits.getHits().length)); for (int i = 0; i < hits.getHits().length; i++) { + logger.info(i + ": top_hits: [" + hits.getAt(i).id() + "][" + hits.getAt(i).sortValues()[0] + "] control: [" + controlHits.getAt(i).id() + "][" + controlHits.getAt(i).sortValues()[0] + "]"); assertThat(hits.getAt(i).id(), equalTo(controlHits.getAt(i).id())); assertThat(hits.getAt(i).sortValues()[0], equalTo(controlHits.getAt(i).sortValues()[0])); }