Search in sources :

Example 11 with DocIdSetIterator

use of org.apache.lucene.search.DocIdSetIterator in project lucene-solr by apache.

the class TestDocsWithFieldSet method testSparse.

public void testSparse() throws IOException {
    DocsWithFieldSet set = new DocsWithFieldSet();
    int doc = random().nextInt(10000);
    set.add(doc);
    DocIdSetIterator it = set.iterator();
    assertEquals(doc, it.nextDoc());
    assertEquals(DocIdSetIterator.NO_MORE_DOCS, it.nextDoc());
    int doc2 = doc + TestUtil.nextInt(random(), 1, 100);
    set.add(doc2);
    it = set.iterator();
    assertEquals(doc, it.nextDoc());
    assertEquals(doc2, it.nextDoc());
    assertEquals(DocIdSetIterator.NO_MORE_DOCS, it.nextDoc());
}
Also used : DocIdSetIterator(org.apache.lucene.search.DocIdSetIterator)

Example 12 with DocIdSetIterator

use of org.apache.lucene.search.DocIdSetIterator in project lucene-solr by apache.

the class TestDocsWithFieldSet method testDenseThenSparse.

public void testDenseThenSparse() throws IOException {
    int denseCount = random().nextInt(10000);
    int nextDoc = denseCount + random().nextInt(10000);
    DocsWithFieldSet set = new DocsWithFieldSet();
    for (int i = 0; i < denseCount; ++i) {
        set.add(i);
    }
    set.add(nextDoc);
    DocIdSetIterator it = set.iterator();
    for (int i = 0; i < denseCount; ++i) {
        assertEquals(i, it.nextDoc());
    }
    assertEquals(nextDoc, it.nextDoc());
    assertEquals(DocIdSetIterator.NO_MORE_DOCS, it.nextDoc());
}
Also used : DocIdSetIterator(org.apache.lucene.search.DocIdSetIterator)

Example 13 with DocIdSetIterator

use of org.apache.lucene.search.DocIdSetIterator in project lucene-solr by apache.

the class NotDocIdSet method iterator.

@Override
public DocIdSetIterator iterator() throws IOException {
    final DocIdSetIterator inIterator = in.iterator();
    return new DocIdSetIterator() {

        int doc = -1;

        int nextSkippedDoc = -1;

        @Override
        public int nextDoc() throws IOException {
            return advance(doc + 1);
        }

        @Override
        public int advance(int target) throws IOException {
            doc = target;
            if (doc > nextSkippedDoc) {
                nextSkippedDoc = inIterator.advance(doc);
            }
            while (true) {
                if (doc >= maxDoc) {
                    return doc = NO_MORE_DOCS;
                }
                assert doc <= nextSkippedDoc;
                if (doc != nextSkippedDoc) {
                    return doc;
                }
                doc += 1;
                nextSkippedDoc = inIterator.nextDoc();
            }
        }

        @Override
        public int docID() {
            return doc;
        }

        @Override
        public long cost() {
            // costs O(maxDoc) in all cases
            return maxDoc;
        }
    };
}
Also used : DocIdSetIterator(org.apache.lucene.search.DocIdSetIterator)

Example 14 with DocIdSetIterator

use of org.apache.lucene.search.DocIdSetIterator in project lucene-solr by apache.

the class TestFilteredDocIdSet method testFilteredDocIdSet.

public void testFilteredDocIdSet() throws Exception {
    final int maxdoc = 10;
    final DocIdSet innerSet = new DocIdSet() {

        @Override
        public long ramBytesUsed() {
            return 0L;
        }

        @Override
        public DocIdSetIterator iterator() {
            return new DocIdSetIterator() {

                int docid = -1;

                @Override
                public int docID() {
                    return docid;
                }

                @Override
                public int nextDoc() {
                    docid++;
                    return docid < maxdoc ? docid : (docid = NO_MORE_DOCS);
                }

                @Override
                public int advance(int target) throws IOException {
                    return slowAdvance(target);
                }

                @Override
                public long cost() {
                    return 1;
                }
            };
        }
    };
    DocIdSet filteredSet = new FilteredDocIdSet(innerSet) {

        @Override
        protected boolean match(int docid) {
            //validate only even docids
            return docid % 2 == 0;
        }
    };
    DocIdSetIterator iter = filteredSet.iterator();
    ArrayList<Integer> list = new ArrayList<>();
    int doc = iter.advance(3);
    if (doc != DocIdSetIterator.NO_MORE_DOCS) {
        list.add(Integer.valueOf(doc));
        while ((doc = iter.nextDoc()) != DocIdSetIterator.NO_MORE_DOCS) {
            list.add(Integer.valueOf(doc));
        }
    }
    int[] docs = new int[list.size()];
    int c = 0;
    Iterator<Integer> intIter = list.iterator();
    while (intIter.hasNext()) {
        docs[c++] = intIter.next().intValue();
    }
    int[] answer = new int[] { 4, 6, 8 };
    boolean same = Arrays.equals(answer, docs);
    if (!same) {
        System.out.println("answer: " + Arrays.toString(answer));
        System.out.println("gotten: " + Arrays.toString(docs));
        fail();
    }
}
Also used : ArrayList(java.util.ArrayList) DocIdSet(org.apache.lucene.search.DocIdSet) DocIdSetIterator(org.apache.lucene.search.DocIdSetIterator)

Example 15 with DocIdSetIterator

use of org.apache.lucene.search.DocIdSetIterator in project lucene-solr by apache.

the class TestDocSet method doTestIteratorEqual.

public void doTestIteratorEqual(DocIdSet a, DocIdSet b) throws IOException {
    DocIdSetIterator ia = a.iterator();
    DocIdSetIterator ib = b.iterator();
    // test for next() equivalence
    for (; ; ) {
        int da = ia.nextDoc();
        int db = ib.nextDoc();
        assertEquals(da, db);
        assertEquals(ia.docID(), ib.docID());
        if (da == DocIdSetIterator.NO_MORE_DOCS)
            break;
    }
    for (int i = 0; i < 10; i++) {
        // test random skipTo() and next()
        ia = a.iterator();
        ib = b.iterator();
        int doc = -1;
        for (; ; ) {
            int da, db;
            if (rand.nextBoolean()) {
                da = ia.nextDoc();
                db = ib.nextDoc();
            } else {
                // keep in mind future edge cases like probing (increase if necessary)
                int target = doc + rand.nextInt(10) + 1;
                da = ia.advance(target);
                db = ib.advance(target);
            }
            assertEquals(da, db);
            assertEquals(ia.docID(), ib.docID());
            if (da == DocIdSetIterator.NO_MORE_DOCS)
                break;
            doc = da;
        }
    }
}
Also used : DocIdSetIterator(org.apache.lucene.search.DocIdSetIterator)

Aggregations

DocIdSetIterator (org.apache.lucene.search.DocIdSetIterator)68 LeafReaderContext (org.apache.lucene.index.LeafReaderContext)28 Scorer (org.apache.lucene.search.Scorer)15 DocIdSet (org.apache.lucene.search.DocIdSet)14 Weight (org.apache.lucene.search.Weight)12 ConstantScoreScorer (org.apache.lucene.search.ConstantScoreScorer)10 BitSet (org.apache.lucene.util.BitSet)10 Bits (org.apache.lucene.util.Bits)10 BytesRef (org.apache.lucene.util.BytesRef)10 IOException (java.io.IOException)9 MatchingDocs (org.apache.lucene.facet.FacetsCollector.MatchingDocs)8 IndexSearcher (org.apache.lucene.search.IndexSearcher)8 SortedDocValues (org.apache.lucene.index.SortedDocValues)7 ConstantScoreWeight (org.apache.lucene.search.ConstantScoreWeight)7 TwoPhaseIterator (org.apache.lucene.search.TwoPhaseIterator)7 SortedSetDocValues (org.apache.lucene.index.SortedSetDocValues)6 Query (org.apache.lucene.search.Query)6 Document (org.apache.lucene.document.Document)5 IndexReader (org.apache.lucene.index.IndexReader)5 LeafReader (org.apache.lucene.index.LeafReader)5