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

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

Introduction

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

Prototype

public abstract int advance(int target) throws IOException;

Source Link

Document

Advances to the first beyond the current whose document number is greater than or equal to target, and returns the document number itself.

Usage

From source file:DocIdSetBenchmark.java

License:Apache License

private static int exhaustIterator(DocIdSet set, int increment) throws IOException {
    int dummy = 0;
    final DocIdSetIterator it = set.iterator();
    for (int doc = -1; doc != DocIdSetIterator.NO_MORE_DOCS; doc = it.advance(doc + increment)) {
        dummy += doc;/*from   www  .j av  a  2s .c  o  m*/
    }
    return dummy;
}

From source file:lucene.security.search.DocumentVisibilityFilter.java

License:Apache License

public static DocIdSet getLogicalOr(final List<DocIdSet> list) throws IOException {
    if (list.size() == 0) {
        return DocIdSet.EMPTY_DOCIDSET;
    }// w  w w.j  a v a2 s.  co m
    if (list.size() == 1) {
        DocIdSet docIdSet = list.get(0);
        Bits bits = docIdSet.bits();
        if (bits == null) {
            throw new IOException("Bits are not allowed to be null for DocIdSet [" + docIdSet + "].");
        }
        return docIdSet;
    }
    int index = 0;
    final Bits[] bitsArray = new Bits[list.size()];
    int length = -1;
    for (DocIdSet docIdSet : list) {
        Bits bits = docIdSet.bits();
        if (bits == null) {
            throw new IOException("Bits are not allowed to be null for DocIdSet [" + docIdSet + "].");
        }
        bitsArray[index] = bits;
        index++;
        if (length < 0) {
            length = bits.length();
        } else if (length != bits.length()) {
            throw new IOException(
                    "Bits length need to be the same [" + length + "] and [" + bits.length() + "]");
        }
    }
    final int len = length;
    return new DocIdSet() {

        @Override
        public Bits bits() throws IOException {
            return new Bits() {

                @Override
                public boolean get(int index) {
                    for (int i = 0; i < bitsArray.length; i++) {
                        if (bitsArray[i].get(index)) {
                            return true;
                        }
                    }
                    return false;
                }

                @Override
                public int length() {
                    return len;
                }

            };
        }

        @Override
        public boolean isCacheable() {
            return true;
        }

        @Override
        public DocIdSetIterator iterator() throws IOException {
            final DocIdSetIterator[] docIdSetIteratorArray = new DocIdSetIterator[list.size()];
            long c = 0;
            int index = 0;
            for (DocIdSet docIdSet : list) {
                DocIdSetIterator iterator = docIdSet.iterator();
                iterator.nextDoc();
                docIdSetIteratorArray[index] = iterator;
                c += iterator.cost();
                index++;
            }
            final long cost = c;
            return new DocIdSetIterator() {

                private int _docId = -1;

                @Override
                public int advance(int target) throws IOException {
                    callAdvanceOnAllThatAreBehind(target);
                    Arrays.sort(docIdSetIteratorArray, COMPARATOR);
                    DocIdSetIterator iterator = docIdSetIteratorArray[0];
                    return _docId = iterator.docID();
                }

                private void callAdvanceOnAllThatAreBehind(int target) throws IOException {
                    for (int i = 0; i < docIdSetIteratorArray.length; i++) {
                        DocIdSetIterator iterator = docIdSetIteratorArray[i];
                        if (iterator.docID() < target) {
                            iterator.advance(target);
                        }
                    }
                }

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

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

                @Override
                public long cost() {
                    return cost;
                }

            };
        }
    };
}

From source file:org.apache.blur.manager.IndexManager.java

License:Apache License

@SuppressWarnings("unchecked")
private static boolean isFiltered(int notAdjustedDocId, IndexReader reader, Filter filter) throws IOException {
    if (filter == null) {
        return false;
    }//  www .jav  a 2s.com
    if (reader instanceof BaseCompositeReader) {
        BaseCompositeReader<IndexReader> indexReader = (BaseCompositeReader<IndexReader>) reader;
        List<? extends IndexReader> sequentialSubReaders = BaseCompositeReaderUtil
                .getSequentialSubReaders(indexReader);
        int readerIndex = BaseCompositeReaderUtil.readerIndex(indexReader, notAdjustedDocId);
        int readerBase = BaseCompositeReaderUtil.readerBase(indexReader, readerIndex);
        int docId = notAdjustedDocId - readerBase;
        IndexReader orgReader = sequentialSubReaders.get(readerIndex);
        SegmentReader sReader = AtomicReaderUtil.getSegmentReader(orgReader);
        if (sReader != null) {
            SegmentReader segmentReader = (SegmentReader) sReader;
            DocIdSet docIdSet = filter.getDocIdSet(segmentReader.getContext(), segmentReader.getLiveDocs());
            DocIdSetIterator iterator = docIdSet.iterator();
            if (iterator == null) {
                return true;
            }
            if (iterator.advance(docId) == docId) {
                return false;
            }
            return true;
        }
        throw new RuntimeException("Reader has to be a SegmentReader [" + orgReader + "]");
    } else {
        throw new RuntimeException("Reader has to be a BaseCompositeReader [" + reader + "]");
    }
}

From source file:org.apache.blur.utils.BlurUtil.java

License:Apache License

private static OpenBitSet getMask(DocIdSet docIdSet, int primeDocRowId, int numberOfDocsInRow)
        throws IOException {
    OpenBitSet mask = new OpenBitSet(numberOfDocsInRow);
    DocIdSetIterator iterator = docIdSet.iterator();
    if (iterator == null) {
        return mask;
    }//w w  w. j  ava2s.  co m
    int docId = iterator.advance(primeDocRowId);
    int end = numberOfDocsInRow + primeDocRowId;
    while (docId < end) {
        mask.set(docId - primeDocRowId);
        docId = iterator.nextDoc();
    }
    return mask;
}

From source file:org.apache.solr.search.SolrIndexSearcher.java

License:Apache License

/**
 * Returns the set of document ids matching all queries.
 * This method is cache-aware and attempts to retrieve the answer from the cache if possible.
 * If the answer was not cached, it may have been inserted into the cache as a result of this call.
 * This method can handle negative queries.
 * <p>/*from  ww w . j ava2s .com*/
 * The DocSet returned should <b>not</b> be modified.
 */
public DocSet getDocSet(List<Query> queries) throws IOException {
    ProcessedFilter pf = getProcessedFilter(null, queries);
    if (pf.answer != null)
        return pf.answer;

    DocSetCollector setCollector = new DocSetCollector(maxDoc() >> 6, maxDoc());
    Collector collector = setCollector;
    if (pf.postFilter != null) {
        pf.postFilter.setLastDelegate(collector);
        collector = pf.postFilter;
    }

    for (final AtomicReaderContext leaf : leafContexts) {
        final AtomicReader reader = leaf.reader();
        final Bits liveDocs = reader.getLiveDocs(); // TODO: the filter may already only have liveDocs...
        DocIdSet idSet = null;
        if (pf.filter != null) {
            idSet = pf.filter.getDocIdSet(leaf, liveDocs);
            if (idSet == null)
                continue;
        }
        DocIdSetIterator idIter = null;
        if (idSet != null) {
            idIter = idSet.iterator();
            if (idIter == null)
                continue;
        }

        collector.setNextReader(leaf);
        int max = reader.maxDoc();

        if (idIter == null) {
            for (int docid = 0; docid < max; docid++) {
                if (liveDocs != null && !liveDocs.get(docid))
                    continue;
                collector.collect(docid);
            }
        } else {
            for (int docid = -1; (docid = idIter.advance(docid + 1)) < max;) {
                collector.collect(docid);
            }
        }
    }

    if (collector instanceof DelegatingCollector) {
        ((DelegatingCollector) collector).finish();
    }

    return setCollector.getDocSet();
}

From source file:org.apache.solr.search.TestDocSet.java

License:Apache License

public void doTestIteratorEqual(DocIdSet a, DocIdSet b) throws IOException {
    DocIdSetIterator ia = a.iterator();
    DocIdSetIterator ib = b.iterator();/*  ww  w  . j  a  v a 2s .  co m*/

    // 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 {
                int target = doc + rand.nextInt(10) + 1; // keep in mind future edge cases like probing (increase if necessary)
                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;
        }
    }
}

From source file:org.apache.solr.search.TestFilteredDocIdSet.java

License:Apache License

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

        @Override/* ww  w.j a va2s.c om*/
        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) {
            return docid % 2 == 0; //validate only even docids
        }
    };

    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();
    }
}

From source file:org.codelibs.elasticsearch.common.lucene.Lucene.java

License:Apache License

/**
 * Given a {Scorer}, return a {Bits} instance that will match
 * all documents contained in the set. Note that the returned {Bits}
 * instance MUST be consumed in order.// w  w  w  . java 2 s .  co m
 */
public static Bits asSequentialAccessBits(final int maxDoc, @Nullable Scorer scorer) throws IOException {
    if (scorer == null) {
        return new Bits.MatchNoBits(maxDoc);
    }
    final TwoPhaseIterator twoPhase = scorer.twoPhaseIterator();
    final DocIdSetIterator iterator;
    if (twoPhase == null) {
        iterator = scorer.iterator();
    } else {
        iterator = twoPhase.approximation();
    }

    return new Bits() {

        int previous = -1;
        boolean previousMatched = false;

        @Override
        public boolean get(int index) {
            if (index < 0 || index >= maxDoc) {
                throw new IndexOutOfBoundsException(index + " is out of bounds: [" + 0 + "-" + maxDoc + "[");
            }
            if (index < previous) {
                throw new IllegalArgumentException("This Bits instance can only be consumed in order. "
                        + "Got called on [" + index + "] while previously called on [" + previous + "]");
            }
            if (index == previous) {
                // we cache whether it matched because it is illegal to call
                // twoPhase.matches() twice
                return previousMatched;
            }
            previous = index;

            int doc = iterator.docID();
            if (doc < index) {
                try {
                    doc = iterator.advance(index);
                } catch (IOException e) {
                    throw new IllegalStateException("Cannot advance iterator", e);
                }
            }
            if (index == doc) {
                try {
                    return previousMatched = twoPhase == null || twoPhase.matches();
                } catch (IOException e) {
                    throw new IllegalStateException("Cannot validate match", e);
                }
            }
            return previousMatched = false;
        }

        @Override
        public int length() {
            return maxDoc;
        }
    };
}

From source file:org.codelibs.elasticsearch.search.aggregations.bucket.nested.NestedAggregator.java

License:Apache License

@Override
public LeafBucketCollector getLeafCollector(final LeafReaderContext ctx, final LeafBucketCollector sub)
        throws IOException {
    IndexReaderContext topLevelContext = ReaderUtil.getTopLevelContext(ctx);
    IndexSearcher searcher = new IndexSearcher(topLevelContext);
    searcher.setQueryCache(null);//from w  ww . j a va2  s  . co  m
    Weight weight = searcher.createNormalizedWeight(childFilter, false);
    Scorer childDocsScorer = weight.scorer(ctx);

    final BitSet parentDocs = parentFilter.getBitSet(ctx);
    final DocIdSetIterator childDocs = childDocsScorer != null ? childDocsScorer.iterator() : null;
    return new LeafBucketCollectorBase(sub, null) {
        @Override
        public void collect(int parentDoc, long bucket) throws IOException {
            // if parentDoc is 0 then this means that this parent doesn't have child docs (b/c these appear always before the parent
            // doc), so we can skip:
            if (parentDoc == 0 || parentDocs == null || childDocs == null) {
                return;
            }

            final int prevParentDoc = parentDocs.prevSetBit(parentDoc - 1);
            int childDocId = childDocs.docID();
            if (childDocId <= prevParentDoc) {
                childDocId = childDocs.advance(prevParentDoc + 1);
            }

            for (; childDocId < parentDoc; childDocId = childDocs.nextDoc()) {
                collectBucket(sub, childDocId, bucket);
            }
        }
    };
}

From source file:org.codelibs.elasticsearch.search.MultiValueMode.java

License:Apache License

/**
 * Return a {NumericDocValues} instance that can be used to sort root documents
 * with this mode, the provided values and filters for root/inner documents.
 *
 * For every root document, the values of its inner documents will be aggregated.
 * If none of the inner documents has a value, then <code>missingValue</code> is returned.
 *
 * Allowed Modes: SUM, AVG, MIN, MAX//  ww  w.  j av  a 2  s  .c  o  m
 *
 * NOTE: Calling the returned instance on docs that are not root docs is illegal
 *       The returned instance can only be evaluate the current and upcoming docs
 */
public NumericDocValues select(final SortedNumericDocValues values, final long missingValue,
        final BitSet rootDocs, final DocIdSetIterator innerDocs, int maxDoc) throws IOException {
    if (rootDocs == null || innerDocs == null) {
        return select(DocValues.emptySortedNumeric(maxDoc), missingValue);
    }

    return new NumericDocValues() {

        int lastSeenRootDoc = 0;
        long lastEmittedValue = missingValue;

        @Override
        public long get(int rootDoc) {
            assert rootDocs.get(rootDoc) : "can only sort root documents";
            assert rootDoc >= lastSeenRootDoc : "can only evaluate current and upcoming root docs";
            if (rootDoc == lastSeenRootDoc) {
                return lastEmittedValue;
            }
            try {
                final int prevRootDoc = rootDocs.prevSetBit(rootDoc - 1);
                final int firstNestedDoc;
                if (innerDocs.docID() > prevRootDoc) {
                    firstNestedDoc = innerDocs.docID();
                } else {
                    firstNestedDoc = innerDocs.advance(prevRootDoc + 1);
                }

                lastSeenRootDoc = rootDoc;
                lastEmittedValue = pick(values, missingValue, innerDocs, firstNestedDoc, rootDoc);
                return lastEmittedValue;
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
    };
}