下面列出了org.apache.lucene.search.BulkScorer#org.apache.lucene.search.CollectionTerminatedException 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Override
protected boolean match(int doc) {
if (parentWeight.remaining == 0) {
throw new CollectionTerminatedException();
}
final long parentOrd = ordinals.getOrd(doc);
if (parentOrd >= 0) {
final long parentIdx = parentIds.find(parentOrd);
if (parentIdx != -1) {
parentWeight.remaining--;
int count = occurrences.get(parentIdx);
if (count >= minChildren && count <= maxChildren) {
return true;
}
}
}
return false;
}
/**
* Lower-level suggest API.
* Collects completion hits through <code>collector</code> for <code>query</code>.
*
* <p>{@link TopSuggestDocsCollector#collect(int, CharSequence, CharSequence, float)}
* is called for every matching completion hit.
*/
public void suggest(CompletionQuery query, TopSuggestDocsCollector collector) throws IOException {
// TODO use IndexSearcher.rewrite instead
// have to implement equals() and hashCode() in CompletionQuerys and co
query = (CompletionQuery) query.rewrite(getIndexReader());
Weight weight = query.createWeight(this, collector.scoreMode(), 1f);
for (LeafReaderContext context : getIndexReader().leaves()) {
BulkScorer scorer = weight.bulkScorer(context);
if (scorer != null) {
try {
scorer.score(collector.getLeafCollector(context), context.reader().getLiveDocs());
} catch (CollectionTerminatedException e) {
// collection was terminated prematurely
// continue with the following leaf
}
}
}
}
@Override
public LeafCollector getLeafCollector(LeafReaderContext context) throws IOException {
Sort segmentSort = context.reader().getMetaData().getSort();
if (segmentSort != null && canEarlyTerminate(sort, segmentSort) == false) {
throw new IllegalStateException("Cannot early terminate with sort order " + sort + " if segments are sorted with " + segmentSort);
}
if (segmentSort != null) {
// segment is sorted, can early-terminate
return new FilterLeafCollector(super.getLeafCollector(context)) {
private int numCollected;
@Override
public void collect(int doc) throws IOException {
super.collect(doc);
if (++numCollected >= numDocsToCollect) {
terminatedEarly.set(true);
throw new CollectionTerminatedException();
}
}
};
} else {
return super.getLeafCollector(context);
}
}
@Override
public void doSetNextReader(LeafReaderContext context) throws IOException {
if (docs.size() >= size || context.docBase + context.reader().maxDoc() <= docUpTo) {
// no need to collect a new segment, we either already collected enough
// or the segment is not competitive
throw new CollectionTerminatedException();
}
docBase = context.docBase;
}
/**
* Called for every matched completion,
* similar to {@link org.apache.lucene.search.LeafCollector#collect(int)}
* but for completions.
*
* NOTE: collection at the leaf level is guaranteed to be in
* descending order of score
*/
public void collect(int docID, CharSequence key, CharSequence context, float score) throws IOException {
SuggestScoreDoc current = new SuggestScoreDoc(docBase + docID, key, context, score);
if (current == priorityQueue.insertWithOverflow(current)) {
// if the current SuggestScoreDoc has overflown from pq,
// we can assume all of the successive collections from
// this leaf will be overflown as well
// TODO: reuse the overflow instance?
throw new CollectionTerminatedException();
}
}