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:org.eu.bitzone.Leia.java

License:Apache License

public void showNextTermDoc(final Object fText) {
    final Term t = (Term) getProperty(fText, "term");
    if (t == null) {
        return;/*from  ww w. j a  va 2s  .c om*/
    }
    if (ir == null) {
        showStatus(MSG_NOINDEX);
        return;
    }
    final SlowThread st = new SlowThread(this) {

        @Override
        public void execute() {
            try {
                final DocsEnum td = (DocsEnum) getProperty(fText, "td");
                if (td == null) {
                    showFirstTermDoc(fText);
                    return;
                }
                if (td.nextDoc() == DocIdSetIterator.NO_MORE_DOCS) {
                    showStatus("No more docs for this term");
                    return;
                }
                final Object tdNum = find("tdNum");
                final String sCnt = getString(tdNum, "text");
                int cnt = 1;
                try {
                    cnt = Integer.parseInt(sCnt);
                } catch (final Exception e) {
                }
                ;
                setString(tdNum, "text", String.valueOf(cnt + 1));
                _showTermDoc(fText, td);
            } catch (final Exception e) {
                e.printStackTrace();
                showStatus(e.getMessage());
            }
        }
    };
    if (slowAccess) {
        st.start();
    } else {
        st.execute();
    }
}

From source file:org.exoplatform.services.jcr.impl.core.query.lucene.LuceneQueryHits.java

License:Apache License

/**
 * {@inheritDoc}/*ww w  .j  av a 2s .co m*/
 */
public ScoreNode nextScoreNode() throws IOException {
    if (scorer == null) {
        return null;
    }
    int doc = scorer.nextDoc();
    if (doc == DocIdSetIterator.NO_MORE_DOCS) {
        return null;
    }
    String uuid = reader.document(doc).get(FieldNames.UUID);
    return new ScoreNode(uuid, scorer.score(), doc);
}

From source file:org.hibernate.search.filter.impl.AndDocIdSet.java

License:Open Source License

private DocIdSet makeDocIdSetOnAgreedBits(final DocIdSetIterator[] iterators) throws IOException {
    final OpenBitSet result = new OpenBitSet(maxDocNumber);
    final int numberOfIterators = iterators.length;

    int targetPosition = findFirstTargetPosition(iterators, result);

    if (targetPosition == DocIdSetIterator.NO_MORE_DOCS) {
        return DocIdSet.EMPTY_DOCIDSET;
    }/*  w w  w .  j  av a 2 s.co  m*/

    // Each iterator can vote "ok" for the current target to
    // be reached; when all agree the bit is set.
    // if an iterator disagrees (it jumped longer), it's current position becomes the new targetPosition
    // for the others and he is considered "first" in the voting round (every iterator votes for himself ;-)

    int i = 0;
    int votes = 0; //could be smarter but would make the code even more complex for a minor optimization out of cycle.
    // enter main loop:
    while (true) {
        final DocIdSetIterator iterator = iterators[i];
        int position = targetPosition;
        if (!iteratorAlreadyOnTargetPosition(targetPosition, iterator)) {
            position = iterator.advance(targetPosition);
        }
        if (position == DocIdSetIterator.NO_MORE_DOCS) {
            return result;
        } //exit condition
        if (position == targetPosition) {
            if (++votes == numberOfIterators) {
                result.fastSet(position);
                votes = 0;
                targetPosition++;
            }
        } else {
            votes = 1;
            targetPosition = position;
        }
        i = ++i % numberOfIterators;
    }
}

From source file:org.hibernate.search.filter.impl.AndDocIdSet.java

License:Open Source License

private int findFirstTargetPosition(final DocIdSetIterator[] iterators, OpenBitSet result) throws IOException {
    int targetPosition = iterators[0].nextDoc();
    if (targetPosition == DocIdSetIterator.NO_MORE_DOCS) {
        // first iterator has no values, so skip all
        return DocIdSetIterator.NO_MORE_DOCS;
    }/*from  ww w  . j a va  2  s .c o m*/

    boolean allIteratorsShareSameFirstTarget = true;

    //iterator initialize, just one "next" for each DocIdSetIterator
    for (int i = 1; i < iterators.length; i++) {
        final DocIdSetIterator iterator = iterators[i];
        final int position = iterator.nextDoc();
        if (position == DocIdSetIterator.NO_MORE_DOCS) {
            //current iterator has no values, so skip all
            return DocIdSetIterator.NO_MORE_DOCS;
        }
        if (targetPosition != position) {
            targetPosition = max(targetPosition, position);
            allIteratorsShareSameFirstTarget = false;
        }
    }
    // end iterator initialize

    if (allIteratorsShareSameFirstTarget) {
        result.fastSet(targetPosition);
        targetPosition++;
    }

    return targetPosition;
}

From source file:org.hibernate.search.spatial.impl.SpatialHashFilter.java

License:LGPL

/**
 * Search the index for document having the correct spatial hash cell id at given grid level.
 *
 * @param context the {@link LeafReaderContext} for which to return the {@link DocIdSet}.
 * @param acceptDocs Bits that represent the allowable docs to match (typically deleted docs but possibly filtering
 * other documents)/*from  w  w  w.  j a v  a  2s. co m*/
 * @return a {@link DocIdSet} with the document ids matching
 */
@Override
public DocIdSet getDocIdSet(LeafReaderContext context, Bits acceptDocs) throws IOException {
    if (spatialHashCellsIds.size() == 0) {
        return null;
    }

    final LeafReader atomicReader = context.reader();

    BitDocIdSet matchedDocumentsIds = new BitDocIdSet(new FixedBitSet(atomicReader.maxDoc()));
    Boolean found = false;
    for (int i = 0; i < spatialHashCellsIds.size(); i++) {
        Term spatialHashCellTerm = new Term(fieldName, spatialHashCellsIds.get(i));
        DocsEnum spatialHashCellsDocs = atomicReader.termDocsEnum(spatialHashCellTerm);
        if (spatialHashCellsDocs != null) {
            while (true) {
                final int docId = spatialHashCellsDocs.nextDoc();
                if (docId == DocIdSetIterator.NO_MORE_DOCS) {
                    break;
                } else {
                    if (acceptDocs == null || acceptDocs.get(docId)) {
                        matchedDocumentsIds.bits().set(docId);
                        found = true;
                    }
                }
            }
        }
    }

    if (found) {
        return matchedDocumentsIds;
    } else {
        return null;
    }
}

From source file:org.hibernate.search.spatial.impl.SpatialHashQuery.java

License:LGPL

/**
 * Search the index for document having the correct spatial hash cell id at given grid level.
 *
 * @param context the {@link LeafReaderContext} for which to return the {@link DocIdSet}.
 * @return a {@link DocIdSetIterator} with the matching document ids
 *//* w  w w .ja v a  2s  .  c o m*/
private DocIdSetIterator createDocIdSetIterator(LeafReaderContext context) throws IOException {
    if (spatialHashCellsIds.size() == 0) {
        return null;
    }

    final LeafReader atomicReader = context.reader();

    BitDocIdSet matchedDocumentsIds = new BitDocIdSet(new FixedBitSet(atomicReader.maxDoc()));
    boolean found = false;
    for (int i = 0; i < spatialHashCellsIds.size(); i++) {
        Term spatialHashCellTerm = new Term(fieldName, spatialHashCellsIds.get(i));
        PostingsEnum spatialHashCellsDocs = atomicReader.postings(spatialHashCellTerm);
        if (spatialHashCellsDocs != null) {
            while (true) {
                final int docId = spatialHashCellsDocs.nextDoc();
                if (docId == DocIdSetIterator.NO_MORE_DOCS) {
                    break;
                } else {
                    matchedDocumentsIds.bits().set(docId);
                    found = true;
                }
            }
        }
    }

    if (found) {
        return matchedDocumentsIds.iterator();
    } else {
        return DocIdSetIterator.empty();
    }
}

From source file:org.hibernate.search.test.filter.AndDocIdSetsTest.java

License:Open Source License

@Test
public void testIteratorMatchesTestArray() throws IOException {
    DocIdSet docIdSet0_9 = arrayToDocIdSet(testDataFrom0to9);
    DocIdSetIterator docIdSetIterator = docIdSet0_9.iterator();
    assertTrue(docIdSetIterator.nextDoc() != DocIdSetIterator.NO_MORE_DOCS);
    assertEquals(0, docIdSetIterator.docID());
    assertEquals(9, docIdSetIterator.advance(9));
    assertEquals(DocIdSetIterator.NO_MORE_DOCS, docIdSetIterator.advance(10));
}

From source file:org.hibernate.search.test.filter.AndDocIdSetsTest.java

License:Open Source License

private static void iterateOnResults(DocIdSet docIdBitSet) throws IOException {
    DocIdSetIterator iterator = docIdBitSet.iterator();
    int currentDoc;
    do {/*  w  w  w.  j ava 2s.co m*/
        currentDoc = iterator.nextDoc();
    } while (currentDoc != DocIdSetIterator.NO_MORE_DOCS);
}

From source file:org.hibernate.search.test.filter.AndDocIdSetsTest.java

License:Open Source License

/**
 * @param expected the doc id set as expected
 * @param actual the doc id test as returned by the test
 *
 * @return true if the two DocIdSet are equal: contain the same number of ids, same order and all are equal
 *///from   ww w  .  j  av  a  2  s . c o  m
public static boolean docIdSetsEqual(DocIdSet expected, DocIdSet actual) {
    try {
        DocIdSetIterator iterA = expected.iterator();
        DocIdSetIterator iterB = actual.iterator();
        int nextA;
        int nextB;
        do {
            nextA = iterA.nextDoc();
            nextB = iterB.nextDoc();
            if (nextA != nextB) {
                return false;
            }
            assertEquals(iterA.docID(), iterB.docID());
        } while (nextA != DocIdSetIterator.NO_MORE_DOCS);
    } catch (IOException ioe) {
        fail("these DocIdSetIterator instances should not throw any exceptions");
    }
    return true;
}

From source file:org.hibernate.search.test.filter.FiltersOptimizationTest.java

License:Open Source License

/**
 * Verifies if the docIdSet is representing a specific
 * sequence of docIds./*from   ww w. j a  v a  2s.  c  om*/
 * @param docIdSet the docIdSet to test
 * @param expectedIds an array of document ids
 * @return true if iterating on docIdSet returns the expectedIds
 * @throws IOException should not happen
 */
private boolean isIdSetSequenceSameTo(DocIdSet docIdSet, int... expectedIds) throws IOException {
    DocIdSetIterator idSetIterator = docIdSet.iterator();
    for (int setBit : expectedIds) {
        int currentId = idSetIterator.nextDoc();
        if (currentId == DocIdSetIterator.NO_MORE_DOCS) {
            return false;
        }
        if (currentId != setBit) {
            return false;
        }
    }
    // and now test both sequences are at the end:
    return idSetIterator.nextDoc() == DocIdSetIterator.NO_MORE_DOCS;
}