From 3f2f1f088d07e7b16d5f667714fe96b53f7e769f Mon Sep 17 00:00:00 2001 From: Martijn van Groningen Date: Fri, 23 May 2014 19:05:16 +0200 Subject: [PATCH] Set the sortValues on SearchHit post aggregation instead of during the reduce. --- .../aggregations/bucket/tophits/InternalTopHits.java | 4 ---- .../aggregations/bucket/tophits/TopHitsAggregator.java | 7 ++++++- 2 files changed, 6 insertions(+), 5 deletions(-) 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 4529ec6392f..3f74391529b 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 @@ -109,10 +109,6 @@ public class InternalTopHits extends InternalAggregation implements TopHits, ToX for (int i = 0; i < reducedTopDocs.scoreDocs.length; i++) { ScoreDoc scoreDoc = reducedTopDocs.scoreDocs[i]; hits[i] = (InternalSearchHit) shardHits[scoreDoc.shardIndex].getAt(tracker[scoreDoc.shardIndex]++); - if (scoreDoc instanceof FieldDoc) { - FieldDoc fieldDoc = (FieldDoc) scoreDoc; - hits[i].sortValues(fieldDoc.fields); - } } return new InternalTopHits(name, new InternalSearchHits(hits, reducedTopDocs.totalHits, reducedTopDocs.getMaxScore())); } catch (IOException e) { diff --git a/src/main/java/org/elasticsearch/search/aggregations/bucket/tophits/TopHitsAggregator.java b/src/main/java/org/elasticsearch/search/aggregations/bucket/tophits/TopHitsAggregator.java index 99f48b4cef1..165f9e5198d 100644 --- a/src/main/java/org/elasticsearch/search/aggregations/bucket/tophits/TopHitsAggregator.java +++ b/src/main/java/org/elasticsearch/search/aggregations/bucket/tophits/TopHitsAggregator.java @@ -79,9 +79,14 @@ public class TopHitsAggregator extends BucketsAggregator implements ScorerAware FetchSearchResult fetchResult = topHitsContext.fetchResult(); InternalSearchHit[] internalHits = fetchResult.fetchResult().hits().internalHits(); for (int i = 0; i < internalHits.length; i++) { + ScoreDoc scoreDoc = topDocs.scoreDocs[i]; InternalSearchHit searchHitFields = internalHits[i]; searchHitFields.shard(topHitsContext.shardTarget()); - searchHitFields.score(topDocs.scoreDocs[i].score); + searchHitFields.score(scoreDoc.score); + if (scoreDoc instanceof FieldDoc) { + FieldDoc fieldDoc = (FieldDoc) scoreDoc; + searchHitFields.sortValues(fieldDoc.fields); + } } return new InternalTopHits(name, topHitsContext.size(), topHitsContext.sort(), topDocs, fetchResult.hits()); }