private TopDocs topDocs(IndexSearcher searcher, Query query, Sort sort, ScoreDoc after, int count, boolean usesRelevance) throws IOException { if (sort == null) { if (!usesRelevance) { FieldDoc start = after == null ? null : (FieldDoc) after; TopFieldCollector tfc = TopFieldCollector.create(this.sort, count, start, true, false, false, false); Collector collector = new EarlyTerminatingSortingCollector(tfc, this.sort, count); searcher.search(query, collector); return tfc.topDocs(); } else { return searcher.searchAfter(after, query, count); } } else { return searcher.searchAfter(after, query, count, sort); } }