package org.apache.lucene.facet;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.apache.lucene.index.LeafReaderContext;
import org.apache.lucene.search.Collector;
import org.apache.lucene.search.DocIdSet;
import org.apache.lucene.search.FieldDoc;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.MultiCollector;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.Scorable;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.search.ScoreMode;
import org.apache.lucene.search.SimpleCollector;
import org.apache.lucene.search.Sort;
import org.apache.lucene.search.TopDocs;
import org.apache.lucene.search.TopDocsCollector;
import org.apache.lucene.search.TopFieldCollector;
import org.apache.lucene.search.TopFieldDocs;
import org.apache.lucene.search.TopScoreDocCollector;
import org.apache.lucene.search.TotalHitCountCollector;
import org.apache.lucene.search.TotalHits;
import org.apache.lucene.util.ArrayUtil;
import org.apache.lucene.util.DocIdSetBuilder;
public class FacetsCollector extends SimpleCollector implements Collector {
private LeafReaderContext context;
private Scorable scorer;
private int totalHits;
private float[] scores;
private final boolean keepScores;
private final List<MatchingDocs> matchingDocs = new ArrayList<>();
private DocIdSetBuilder docsBuilder;
public final static class MatchingDocs {
public final LeafReaderContext context;
public final DocIdSet bits;
public final float[] scores;
public final int totalHits;
public MatchingDocs(LeafReaderContext context, DocIdSet bits, int totalHits, float[] scores) {
this.context = context;
this.bits = bits;
this.scores = scores;
this.totalHits = totalHits;
}
}
public FacetsCollector() {
this(false);
}
public FacetsCollector(boolean keepScores) {
this.keepScores = keepScores;
}
public final boolean getKeepScores() {
return keepScores;
}
public List<MatchingDocs> getMatchingDocs() {
if (docsBuilder != null) {
matchingDocs.add(new MatchingDocs(this.context, docsBuilder.build(), totalHits, scores));
docsBuilder = null;
scores = null;
context = null;
}
return matchingDocs;
}
@Override
public final void collect(int doc) throws IOException {
docsBuilder.grow(1).add(doc);
if (keepScores) {
if (totalHits >= scores.length) {
float[] newScores = new float[ArrayUtil.oversize(totalHits + 1, 4)];
System.arraycopy(scores, 0, newScores, 0, totalHits);
scores = newScores;
}
scores[totalHits] = scorer.score();
}
totalHits++;
}
@Override
public ScoreMode scoreMode() {
return ScoreMode.COMPLETE;
}
@Override
public final void setScorer(Scorable scorer) throws IOException {
this.scorer = scorer;
}
@Override
protected void doSetNextReader(LeafReaderContext context) throws IOException {
if (docsBuilder != null) {
matchingDocs.add(new MatchingDocs(this.context, docsBuilder.build(), totalHits, scores));
}
docsBuilder = new DocIdSetBuilder(context.reader().maxDoc());
totalHits = 0;
if (keepScores) {
scores = new float[64];
}
this.context = context;
}
public static TopDocs search(IndexSearcher searcher, Query q, int n, Collector fc) throws IOException {
return doSearch(searcher, null, q, n, null, false, fc);
}
public static TopFieldDocs search(IndexSearcher searcher, Query q, int n, Sort sort, Collector fc) throws IOException {
if (sort == null) {
throw new IllegalArgumentException("sort must not be null");
}
return (TopFieldDocs) doSearch(searcher, null, q, n, sort, false, fc);
}
public static TopFieldDocs search(IndexSearcher searcher, Query q, int n, Sort sort, boolean doDocScores, Collector fc) throws IOException {
if (sort == null) {
throw new IllegalArgumentException("sort must not be null");
}
return (TopFieldDocs) doSearch(searcher, null, q, n, sort, doDocScores, fc);
}
public static TopDocs searchAfter(IndexSearcher searcher, ScoreDoc after, Query q, int n, Collector fc) throws IOException {
return doSearch(searcher, after, q, n, null, false, fc);
}
public static TopDocs searchAfter(IndexSearcher searcher, ScoreDoc after, Query q, int n, Sort sort, Collector fc) throws IOException {
if (sort == null) {
throw new IllegalArgumentException("sort must not be null");
}
return doSearch(searcher, after, q, n, sort, false, fc);
}
public static TopDocs searchAfter(IndexSearcher searcher, ScoreDoc after, Query q, int n, Sort sort, boolean doDocScores, Collector fc) throws IOException {
if (sort == null) {
throw new IllegalArgumentException("sort must not be null");
}
return doSearch(searcher, after, q, n, sort, doDocScores, fc);
}
private static TopDocs doSearch(IndexSearcher searcher, ScoreDoc after, Query q, int n, Sort sort,
boolean doDocScores, Collector fc) throws IOException {
int limit = searcher.getIndexReader().maxDoc();
if (limit == 0) {
limit = 1;
}
n = Math.min(n, limit);
if (after != null && after.doc >= limit) {
throw new IllegalArgumentException("after.doc exceeds the number of documents in the reader: after.doc="
+ after.doc + " limit=" + limit);
}
TopDocs topDocs = null;
if (n==0) {
TotalHitCountCollector totalHitCountCollector = new TotalHitCountCollector();
searcher.search(q, MultiCollector.wrap(totalHitCountCollector, fc));
topDocs = new TopDocs(new TotalHits(totalHitCountCollector.getTotalHits(), TotalHits.Relation.EQUAL_TO), new ScoreDoc[0]);
} else {
TopDocsCollector<?> hitsCollector;
if (sort != null) {
if (after != null && !(after instanceof FieldDoc)) {
throw new IllegalArgumentException("after must be a FieldDoc; got " + after);
}
hitsCollector = TopFieldCollector.create(sort, n,
(FieldDoc) after,
Integer.MAX_VALUE);
} else {
hitsCollector = TopScoreDocCollector.create(n, after, Integer.MAX_VALUE);
}
searcher.search(q, MultiCollector.wrap(hitsCollector, fc));
topDocs = hitsCollector.topDocs();
if (doDocScores) {
TopFieldCollector.populateScores(topDocs.scoreDocs, searcher, q);
}
}
return topDocs;
}
}