Aggregation top_hits: Move sort resolution to the reduce method, so it is always guaranteed to be invoked.

This commit is contained in:
Martijn van Groningen 2014-09-10 17:50:56 +02:00
parent bb1dfbfa42
commit dbfac659f9
2 changed files with 10 additions and 8 deletions

View File

@ -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);
}

View File

@ -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