Java Code Examples for org.apache.lucene.search.CollectionTerminatedException

The following examples show how to use org.apache.lucene.search.CollectionTerminatedException. These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source Project: Elasticsearch   Source File: ChildrenQuery.java    License: Apache License 2.0 6 votes vote down vote up
@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;
}
 
Example 2
Source Project: lucene-solr   Source File: SuggestIndexSearcher.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * 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
      }
    }
  }
}
 
Example 3
@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);
  }
}
 
Example 4
Source Project: Elasticsearch   Source File: ScanContext.java    License: Apache License 2.0 5 votes vote down vote up
@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;
}
 
Example 5
Source Project: lucene-solr   Source File: TopSuggestDocsCollector.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * 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();
  }
}