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:com.meltwater.elasticsearch.index.BatchPercolatorService.java

License:Apache License

private boolean hasDocumentMatchingFilter(IndexReader reader, Optional<Filter> optionalFilter)
        throws IOException {
    if (optionalFilter.isPresent()) {
        Filter filter = optionalFilter.get();
        boolean found = false;
        // If you are not familiar with Lucene, this basically means that we try to
        // create an iterator for valid id:s for the filter for the given reader.
        // The filter and DocIdSet can both return null, to enable optimisations,
        // thus the null-checks. Null means that there were no matching docs, and
        // the same is true if the iterator refers to NO_MORE_DOCS immediately.
        for (AtomicReaderContext leaf : reader.leaves()) {
            DocIdSet idSet = filter.getDocIdSet(leaf, leaf.reader().getLiveDocs());
            if (idSet != null) {
                DocIdSetIterator iter = idSet.iterator();
                if (iter != null && iter.nextDoc() != DocIdSetIterator.NO_MORE_DOCS) {
                    found = true;//from  w w w.java2s.c  o  m
                    break;
                }

            }
        }
        return found;
    } else {
        return true;
    }
}

From source file:com.rocana.lucene.codec.v1.TestBlockPostingsFormat3.java

License:Apache License

/**
 * checks docs + freqs + positions + payloads, sequentially
 *//*from   w  w  w .j a  v a2  s  .  c o  m*/
public void assertDocsAndPositionsEnum(PostingsEnum leftDocs, PostingsEnum rightDocs) throws Exception {
    assertNotNull(leftDocs);
    assertNotNull(rightDocs);
    assertEquals(-1, leftDocs.docID());
    assertEquals(-1, rightDocs.docID());
    int docid;
    while ((docid = leftDocs.nextDoc()) != DocIdSetIterator.NO_MORE_DOCS) {
        assertEquals(docid, rightDocs.nextDoc());
        int freq = leftDocs.freq();
        assertEquals(freq, rightDocs.freq());
        for (int i = 0; i < freq; i++) {
            assertEquals(leftDocs.nextPosition(), rightDocs.nextPosition());
            // we don't assert offsets/payloads, they are allowed to be different
        }
    }
    assertEquals(DocIdSetIterator.NO_MORE_DOCS, rightDocs.nextDoc());
}

From source file:com.rocana.lucene.codec.v1.TestBlockPostingsFormat3.java

License:Apache License

/**
 * checks docs + freqs, sequentially//from  ww  w. ja  v a  2s . co m
 */
public void assertDocsEnum(PostingsEnum leftDocs, PostingsEnum rightDocs) throws Exception {
    if (leftDocs == null) {
        assertNull(rightDocs);
        return;
    }
    assertEquals(-1, leftDocs.docID());
    assertEquals(-1, rightDocs.docID());
    int docid;
    while ((docid = leftDocs.nextDoc()) != DocIdSetIterator.NO_MORE_DOCS) {
        assertEquals(docid, rightDocs.nextDoc());
        // we don't assert freqs, they are allowed to be different
    }
    assertEquals(DocIdSetIterator.NO_MORE_DOCS, rightDocs.nextDoc());
}

From source file:com.rocana.lucene.codec.v1.TestBlockPostingsFormat3.java

License:Apache License

/**
 * checks advancing docs//from  w w  w .  j  a v a2s .  co  m
 */
public void assertDocsSkipping(int docFreq, PostingsEnum leftDocs, PostingsEnum rightDocs) throws Exception {
    if (leftDocs == null) {
        assertNull(rightDocs);
        return;
    }
    int docid = -1;
    int averageGap = MAXDOC / (1 + docFreq);
    int skipInterval = 16;

    while (true) {
        if (random().nextBoolean()) {
            // nextDoc()
            docid = leftDocs.nextDoc();
            assertEquals(docid, rightDocs.nextDoc());
        } else {
            // advance()
            int skip = docid + (int) Math.ceil(Math.abs(skipInterval + random().nextGaussian() * averageGap));
            docid = leftDocs.advance(skip);
            assertEquals(docid, rightDocs.advance(skip));
        }

        if (docid == DocIdSetIterator.NO_MORE_DOCS) {
            return;
        }
        // we don't assert freqs, they are allowed to be different
    }
}

From source file:com.rocana.lucene.codec.v1.TestBlockPostingsFormat3.java

License:Apache License

/**
 * checks advancing docs + positions//  w  w w.  j a v  a  2s .  c o m
 */
public void assertPositionsSkipping(int docFreq, PostingsEnum leftDocs, PostingsEnum rightDocs)
        throws Exception {
    if (leftDocs == null || rightDocs == null) {
        assertNull(leftDocs);
        assertNull(rightDocs);
        return;
    }

    int docid = -1;
    int averageGap = MAXDOC / (1 + docFreq);
    int skipInterval = 16;

    while (true) {
        if (random().nextBoolean()) {
            // nextDoc()
            docid = leftDocs.nextDoc();
            assertEquals(docid, rightDocs.nextDoc());
        } else {
            // advance()
            int skip = docid + (int) Math.ceil(Math.abs(skipInterval + random().nextGaussian() * averageGap));
            docid = leftDocs.advance(skip);
            assertEquals(docid, rightDocs.advance(skip));
        }

        if (docid == DocIdSetIterator.NO_MORE_DOCS) {
            return;
        }
        int freq = leftDocs.freq();
        assertEquals(freq, rightDocs.freq());
        for (int i = 0; i < freq; i++) {
            assertEquals(leftDocs.nextPosition(), rightDocs.nextPosition());
            // we don't compare the payloads, it's allowed that one is empty etc
        }
    }
}

From source file:com.senseidb.abacus.api.codec.CodecTest.java

License:Apache License

static void testThreaded(int numThreads, final int numIter, final AtomicReader reader, final String field) {
    Runnable runnable = new Runnable() {
        public void run() {
            try {
                Fields f = reader.fields();
                Terms t = f.terms(field);

                TermsEnum te = t.iterator(null);

                ArrayList<BytesRef> termList = new ArrayList<BytesRef>();

                BytesRef termText;//from   w w  w.  j  av  a  2s .c o m
                while ((termText = te.next()) != null) {
                    termList.add(termText);
                }

                Random rand = new Random();

                for (int i = 0; i < numIter; ++i) {
                    int idx = rand.nextInt(termList.size());
                    termText = termList.get(idx);
                    te = t.iterator(null);
                    te.seekCeil(termText);
                    DocsEnum de = te.docs(null, null);
                    int doc;
                    while ((doc = de.nextDoc()) != DocIdSetIterator.NO_MORE_DOCS) {
                    }

                    de = te.docs(null, null);
                    doc = -1;
                    while ((doc = de.advance(doc + 2)) != DocIdSetIterator.NO_MORE_DOCS) {
                    }
                }

            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    };

    Thread[] threads = new Thread[numThreads];
    for (int i = 0; i < numThreads; ++i) {
        threads[i] = new Thread(runnable);
    }
    for (int i = 0; i < numThreads; ++i) {
        threads[i].start();
    }
    for (int i = 0; i < numThreads; ++i) {
        try {
            threads[i].join();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}

From source file:com.shaie.annots.AnnotationsUtils.java

License:Apache License

public static void printAnnotations(LeafReader reader, Term term) throws IOException {
    System.out.println("Annotations for " + term);
    final ByteArrayDataInput in = new ByteArrayDataInput();
    final PostingsEnum postings = reader.postings(term, PostingsEnum.PAYLOADS);
    for (int docID = postings.nextDoc(); docID != DocIdSetIterator.NO_MORE_DOCS; docID = postings.nextDoc()) {
        final int freq = postings.freq();
        System.out.println("  doc=" + docID + ", freq=" + freq);
        for (int i = 0; i < freq; i++) {
            postings.nextPosition();//from  w  w  w  .j a v  a 2 s.  com
            final BytesRef payload = postings.getPayload();
            in.reset(payload.bytes, payload.offset, payload.length);
            System.out.println("    start=" + in.readVInt() + ", length=" + in.readVInt());
        }
    }
}

From source file:com.shaie.utils.IndexUtils.java

License:Apache License

/** Prints the terms indexed under the given fields with full postings information. */
public static void printFieldTermsWithInfo(LeafReader reader, String... fields) throws IOException {
    for (final String field : fields) {
        System.out.println(format("Terms for field [%s], with positional info:", field));
        final TermsEnum te = reader.terms(field).iterator();
        BytesRef scratch;/* w  w w .  j  a  v  a 2  s  . c o m*/
        PostingsEnum postings = null;
        while ((scratch = te.next()) != null) {
            System.out.println(format("  %s", scratch.utf8ToString()));
            postings = te.postings(postings, PostingsEnum.ALL);
            for (postings.nextDoc(); postings.docID() != DocIdSetIterator.NO_MORE_DOCS; postings.nextDoc()) {
                final Map<Integer, BytesRef> positions = Maps.newTreeMap();
                boolean addedPayload = false;
                for (int i = 0; i < postings.freq(); i++) {
                    final int pos = postings.nextPosition();
                    final BytesRef payload = postings.getPayload();
                    if (payload != null) {
                        positions.put(pos, BytesRef.deepCopyOf(payload));
                        addedPayload = true;
                    } else {
                        positions.put(pos, null);
                    }
                }
                if (addedPayload) {
                    System.out.println(
                            format("    doc=%d, freq=%d", postings.docID(), postings.freq(), positions));
                    for (final Entry<Integer, BytesRef> e : positions.entrySet()) {
                        System.out.println(format("      pos=%d, payload=%s", e.getKey(), e.getValue()));
                    }
                } else {
                    System.out.println(format("    doc=%d, freq=%d, pos=%s", postings.docID(), postings.freq(),
                            positions.keySet()));
                }
            }
        }
    }
}

From source file:com.sindicetech.siren.search.node.TestLuceneProxyNodeScorer.java

License:Open Source License

@Test
public void testNextDoc() throws Exception {
    this.addDocuments("{ \"aaa bbb\" : \"aaa ccc\" , \"ccc\" : \"bbb ccc\" }", "{ \"aaa\" : \"aaa bbb ddd\" }");

    final Scorer scorer1 = this.getScorer(ntq("aaa").getLuceneProxyQuery());

    assertTrue(scorer1.nextDoc() != DocIdSetIterator.NO_MORE_DOCS);
    assertEquals(0, scorer1.docID());/*  w  ww . j a va 2 s  .c  o  m*/
    assertEquals(2, scorer1.freq(), 0);
    assertTrue(scorer1.nextDoc() != DocIdSetIterator.NO_MORE_DOCS);
    assertEquals(1, scorer1.docID());
    assertEquals(2, scorer1.freq(), 0);
    assertTrue(scorer1.nextDoc() == DocIdSetIterator.NO_MORE_DOCS);

    final Scorer scorer2 = this.getScorer(ntq("ccc").getLuceneProxyQuery());

    assertTrue(scorer2.nextDoc() != DocIdSetIterator.NO_MORE_DOCS);
    assertEquals(0, scorer2.docID());
    assertEquals(3, scorer2.freq(), 0);
    assertTrue(scorer2.nextDoc() == DocIdSetIterator.NO_MORE_DOCS);

    final Scorer scorer3 = this.getScorer(ntq("ddd").getLuceneProxyQuery());

    assertTrue(scorer3.nextDoc() != DocIdSetIterator.NO_MORE_DOCS);
    assertEquals(1, scorer3.docID());
    assertEquals(1, scorer3.freq(), 0);
    assertTrue(scorer3.nextDoc() == DocIdSetIterator.NO_MORE_DOCS);
}

From source file:com.sindicetech.siren.search.node.TestLuceneProxyNodeScorer.java

License:Open Source License

@Test
public void testAdvance() throws Exception {
    this.addDocuments("{ \"baba\" : \"aaa ccc\" , \"ccc\" : \"bbb ccc\" }", "{ \"aaa\" : \"aaa bbb ddd\" }",
            "{ \"ddd\" : [ \"bobo\", \"bibi\" ] }");

    final Scorer scorer1 = this.getScorer(ntq("bobo").getLuceneProxyQuery());

    assertTrue(scorer1.advance(2) != DocIdSetIterator.NO_MORE_DOCS);
    assertEquals(2, scorer1.docID());//from   w  w  w. j  av  a 2 s. c  om
    assertEquals(1, scorer1.freq(), 0);
    assertTrue(scorer1.nextDoc() == DocIdSetIterator.NO_MORE_DOCS);

    final Scorer scorer2 = this.getScorer(ntq("baba").getLuceneProxyQuery());
    assertTrue(scorer2.advance(2) == DocIdSetIterator.NO_MORE_DOCS);
}