From dbfac659f9235e9860b5ff392c5033165051b37d Mon Sep 17 00:00:00 2001 From: Martijn van Groningen Date: Wed, 10 Sep 2014 17:50:56 +0200 Subject: [PATCH] Aggregation top_hits: Move sort resolution to the reduce method, so it is always guaranteed to be invoked. --- .../metrics/tophits/InternalTopHits.java | 14 ++++++++------ .../metrics/tophits/TopHitsAggregator.java | 4 ++-- 2 files changed, 10 insertions(+), 8 deletions(-) diff --git a/src/main/java/org/elasticsearch/search/aggregations/metrics/tophits/InternalTopHits.java b/src/main/java/org/elasticsearch/search/aggregations/metrics/tophits/InternalTopHits.java index fb8d3b2f8b3..81a4868fd75 100644 --- a/src/main/java/org/elasticsearch/search/aggregations/metrics/tophits/InternalTopHits.java +++ b/src/main/java/org/elasticsearch/search/aggregations/metrics/tophits/InternalTopHits.java @@ -58,18 +58,16 @@ public class InternalTopHits extends InternalMetricsAggregation implements TopHi private int from; private int size; - private Sort sort; private TopDocs topDocs; private InternalSearchHits searchHits; InternalTopHits() { } - public InternalTopHits(String name, int from, int size, Sort sort, TopDocs topDocs, InternalSearchHits searchHits) { + public InternalTopHits(String name, int from, int size, TopDocs topDocs, InternalSearchHits searchHits) { this.name = name; this.from = from; this.size = size; - this.sort = sort; this.topDocs = topDocs; this.searchHits = searchHits; } @@ -103,6 +101,13 @@ public class InternalTopHits extends InternalMetricsAggregation implements TopHi } 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]; @@ -126,9 +131,6 @@ public class InternalTopHits extends InternalMetricsAggregation implements TopHi from = in.readVInt(); size = in.readVInt(); topDocs = Lucene.readTopDocs(in); - if (topDocs instanceof TopFieldDocs) { - sort = new Sort(((TopFieldDocs) topDocs).fields); - } searchHits = InternalSearchHits.readSearchHits(in); } diff --git a/src/main/java/org/elasticsearch/search/aggregations/metrics/tophits/TopHitsAggregator.java b/src/main/java/org/elasticsearch/search/aggregations/metrics/tophits/TopHitsAggregator.java index dd0b232b3c0..ac82263cb4c 100644 --- a/src/main/java/org/elasticsearch/search/aggregations/metrics/tophits/TopHitsAggregator.java +++ b/src/main/java/org/elasticsearch/search/aggregations/metrics/tophits/TopHitsAggregator.java @@ -90,13 +90,13 @@ public class TopHitsAggregator extends MetricsAggregator implements ScorerAware searchHitFields.sortValues(fieldDoc.fields); } } - return new InternalTopHits(name, topHitsContext.from(), topHitsContext.size(), topHitsContext.sort(), topDocs, fetchResult.hits()); + return new InternalTopHits(name, topHitsContext.from(), topHitsContext.size(), topDocs, fetchResult.hits()); } } @Override public InternalAggregation buildEmptyAggregation() { - return new InternalTopHits(name, topHitsContext.from(), topHitsContext.size(), topHitsContext.sort(), Lucene.EMPTY_TOP_DOCS, InternalSearchHits.empty()); + return new InternalTopHits(name, topHitsContext.from(), topHitsContext.size(), Lucene.EMPTY_TOP_DOCS, InternalSearchHits.empty()); } @Override