Example usage for org.apache.lucene.search DocIdSetIterator NO_MORE_DOCS

List of usage examples for org.apache.lucene.search DocIdSetIterator NO_MORE_DOCS

Introduction

In this page you can find the example usage for org.apache.lucene.search DocIdSetIterator NO_MORE_DOCS.

Prototype

int NO_MORE_DOCS

To view the source code for org.apache.lucene.search DocIdSetIterator NO_MORE_DOCS.

Click Source Link

Document

When returned by #nextDoc() , #advance(int) and #docID() it means there are no more docs in the iterator.

Usage

From source file:nl.inl.blacklab.MockSpansInBuckets.java

License:Apache License

@Override
public int advance(int target) throws IOException {
    alreadyAtFirstBucket = false;/*w ww.j ava  2  s  . co  m*/
    int doc;
    do {
        doc = nextDoc();
    } while (doc != DocIdSetIterator.NO_MORE_DOCS && doc < target);
    if (doc != DocIdSetIterator.NO_MORE_DOCS)
        alreadyAtFirstBucket = true;
    return doc;
}

From source file:nl.inl.blacklab.MyTermSpans.java

License:Apache License

@Override
public int nextDoc() throws IOException {
    doc = postings.nextDoc();//from  www .j a  va 2s  . c o  m
    if (doc != DocIdSetIterator.NO_MORE_DOCS) {
        freq = postings.freq();
        assert freq >= 1;
        count = 0;
    }
    position = -1;
    return doc;
}

From source file:nl.inl.blacklab.MyTermSpans.java

License:Apache License

@Override
public int advance(int target) throws IOException {
    assert target > doc;
    doc = postings.advance(target);//from w w w.  j  av  a 2  s.c o m
    if (doc != DocIdSetIterator.NO_MORE_DOCS) {
        freq = postings.freq();
        assert freq >= 1;
        count = 0;
    }
    position = -1;
    return doc;
}

From source file:nl.inl.blacklab.perdocument.DocResults.java

License:Apache License

/**
 * Construct DocResults from a Scorer (Lucene document results).
 *
 * @param searcher the searcher that generated the results
 * @param scorer the scorer to read document results from
 *//*from  ww  w . ja  v a 2 s.co  m*/
DocResults(Searcher searcher, Scorer scorer) {
    this.searcher = searcher;
    if (scorer == null)
        return; // no matches, empty result set
    try {
        IndexReader indexReader = searcher.getIndexReader();
        while (true) {
            int docId;
            try {
                docId = scorer.nextDoc();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
            if (docId == DocIdSetIterator.NO_MORE_DOCS)
                break;

            Document d = indexReader.document(docId);
            DocResult dr = new DocResult(searcher, null, docId, d, scorer.score());
            results.add(dr);
        }
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

From source file:nl.inl.blacklab.search.Hit.java

License:Apache License

/**
 * Retrieve a list of Hit objects from a Spans.
 *
 * @param spans/*from  www .  j  a  v a2 s  .c  o m*/
 *            where to retrieve the hits
 * @return the list of hits
 * @deprecated use Hits class
 */
@Deprecated
public static List<Hit> hitList(BLSpans spans) {
    List<Hit> result = new ArrayList<>();
    try {
        while (spans.nextDoc() != DocIdSetIterator.NO_MORE_DOCS) {
            while (spans.nextStartPosition() != Spans.NO_MORE_POSITIONS) {
                result.add(spans.getHit());
            }
        }
        return result;
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}

From source file:nl.inl.blacklab.search.HitsImpl.java

License:Apache License

/**
 * Construct a Hits object from a Spans.
 *
 * If possible, don't use this constructor, use the one that takes
 * a SpanQuery, as it's more efficient.//from   www .j a va2s .  c  om
 *
 * @param searcher
 *            the searcher object
 * @param source
 *            where to retrieve the Hit objects from
 */
HitsImpl(Searcher searcher, Spans source) {
    this(searcher, (List<Hit>) null);

    currentSourceSpans = BLSpansWrapper.optWrapSortUniq(source);
    try {
        sourceSpansFullyRead = currentSourceSpans.nextDoc() != DocIdSetIterator.NO_MORE_DOCS;
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}

From source file:nl.inl.blacklab.search.HitsImpl.java

License:Apache License

/**
 * Ensure that we have read at least as many hits as specified in the parameter.
 *
 * @param number the minimum number of hits that will have been read when this method
 *   returns (unless there are fewer hits than this); if negative, reads all hits
 * @throws InterruptedException if the thread was interrupted during this operation
 *///ww w.ja  va  2  s .  c  o m
void ensureHitsRead(int number) throws InterruptedException {
    if (sourceSpansFullyRead || (number >= 0 && hits.size() >= number))
        return;

    synchronized (this) {
        boolean readAllHits = number < 0;
        try {
            int maxHitsToCount = settings.maxHitsToCount();
            int maxHitsToRetrieve = settings.maxHitsToRetrieve();
            while (readAllHits || hits.size() < number) {

                // Don't hog the CPU, don't take too long
                etiquette.behave();

                // Stop if we're at the maximum number of hits we want to count
                if (maxHitsToCount >= 0 && hitsCounted >= maxHitsToCount) {
                    maxHitsCounted = true;
                    break;
                }

                // Get the next hit from the spans, moving to the next
                // segment when necessary.
                while (true) {
                    while (currentSourceSpans == null) {
                        // Exhausted (or not started yet); get next segment spans.

                        if (spanQuery == null) {
                            // We started from a Spans, not a SpanQuery. We're done now.
                            // (only used in deprecated methods or while testing)
                            return;
                        }

                        atomicReaderContextIndex++;
                        if (atomicReaderContexts != null
                                && atomicReaderContextIndex >= atomicReaderContexts.size()) {
                            sourceSpansFullyRead = true;
                            return;
                        }
                        if (atomicReaderContexts != null) {
                            // Get the atomic reader context and get the next Spans from it.
                            LeafReaderContext context = atomicReaderContexts.get(atomicReaderContextIndex);
                            currentDocBase = context.docBase;
                            Bits liveDocs = context.reader().getLiveDocs();
                            currentSourceSpans = BLSpansWrapper
                                    .optWrapSortUniq(spanQuery.getSpans(context, liveDocs, termContexts));
                        } else {
                            // TESTING
                            currentDocBase = 0;
                            if (atomicReaderContextIndex > 0) {
                                sourceSpansFullyRead = true;
                                return;
                            }
                            currentSourceSpans = BLSpansWrapper
                                    .optWrapSortUniq(spanQuery.getSpans(null, null, termContexts));
                        }

                        if (currentSourceSpans != null) {
                            // Update the hit query context with our new spans,
                            // and notify the spans of the hit query context
                            // (TODO: figure out if we need to call setHitQueryContext()
                            //    for each segment or not; if it's just about capture groups
                            //    registering themselves, we only need that for the first Spans.
                            //    But it's probably required for backreferences, etc. anyway,
                            //    and there won't be that many segments, so it's probably ok)
                            hitQueryContext.setSpans(currentSourceSpans);
                            currentSourceSpans.setHitQueryContext(hitQueryContext); // let captured groups register themselves
                            if (capturedGroups == null && hitQueryContext.numberOfCapturedGroups() > 0) {
                                capturedGroups = new HashMap<>();
                            }

                            int doc = currentSourceSpans.nextDoc();
                            if (doc == DocIdSetIterator.NO_MORE_DOCS)
                                currentSourceSpans = null; // no matching docs in this segment, try next
                        }
                    }

                    // Advance to next hit
                    int start = currentSourceSpans.nextStartPosition();
                    if (start == Spans.NO_MORE_POSITIONS) {
                        int doc = currentSourceSpans.nextDoc();
                        if (doc != DocIdSetIterator.NO_MORE_DOCS) {
                            // Go to first hit in doc
                            start = currentSourceSpans.nextStartPosition();
                        } else {
                            // This one is exhausted; go to the next one.
                            currentSourceSpans = null;
                        }
                    }
                    if (currentSourceSpans != null) {
                        // We're at the next hit.
                        break;
                    }
                }

                // Count the hit and add it (unless we've reached the maximum number of hits we
                // want)
                hitsCounted++;
                int hitDoc = currentSourceSpans.docID() + currentDocBase;
                if (hitDoc != previousHitDoc) {
                    docsCounted++;
                    if (!maxHitsRetrieved)
                        docsRetrieved++;
                    previousHitDoc = hitDoc;
                }
                maxHitsRetrieved = maxHitsToRetrieve >= 0 && hits.size() >= maxHitsToRetrieve;
                if (!maxHitsRetrieved) {
                    Hit hit = currentSourceSpans.getHit();
                    Hit offsetHit = new Hit(hit.doc + currentDocBase, hit.start, hit.end);
                    if (capturedGroups != null) {
                        Span[] groups = new Span[hitQueryContext.numberOfCapturedGroups()];
                        hitQueryContext.getCapturedGroups(groups);
                        capturedGroups.put(offsetHit, groups);
                    }
                    hits.add(offsetHit);
                }
            }
        } catch (InterruptedException e) {
            maxHitsRetrieved = maxHitsCounted = true; // we've stopped retrieving/counting
            throw e;
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}

From source file:nl.inl.blacklab.search.lucene.SpansFiltered.java

License:Apache License

public SpansFiltered(Spans spans, DocIdSet filterDocs) throws IOException {
    this.spans = BLSpansWrapper.optWrap(spans);
    docIdSetIter = filterDocs.iterator();
    more = false;/*w  ww.  ja v  a  2 s  . co m*/
    if (docIdSetIter != null) {
        more = (docIdSetIter.nextDoc() != DocIdSetIterator.NO_MORE_DOCS);
    }
}

From source file:nl.inl.blacklab.search.lucene.SpansFiltered.java

License:Apache License

private boolean synchronize() throws IOException {
    while (more && spans.doc() != docIdSetIter.docID()) {
        if (spans.doc() < docIdSetIter.docID()) {
            more = spans.skipTo(docIdSetIter.docID());
        } else if (docIdSetIter.advance(spans.doc()) == DocIdSetIterator.NO_MORE_DOCS) {
            more = false;/*w ww . j  a  v  a2  s. c o  m*/
        }
    }
    return more;
}

From source file:nl.inl.blacklab.search.lucene.SpansInBucketsAbstract.java

License:Apache License

@Override
public int nextDoc() throws IOException {
    bucketSize = -1; // not at a valid bucket anymore
    if (currentDoc != DocIdSetIterator.NO_MORE_DOCS) {
        currentDoc = source.nextDoc();/*from www.  ja va 2  s  . c om*/
        if (currentDoc != DocIdSetIterator.NO_MORE_DOCS) {
            source.nextStartPosition(); // start gathering at the first hit
            //gatherHitsInternal();
        }
    }
    return currentDoc;
}