Example usage for org.apache.lucene.search DocIdSet iterator

List of usage examples for org.apache.lucene.search DocIdSet iterator

Introduction

In this page you can find the example usage for org.apache.lucene.search DocIdSet iterator.

Prototype

public abstract DocIdSetIterator iterator() throws IOException;

Source Link

Document

Provides a DocIdSetIterator to access the set.

Usage

From source file:DocIdSetBenchmark.java

License:Apache License

private static int exhaustIterator(DocIdSet set) throws IOException {
    int dummy = 0;
    final DocIdSetIterator it = set.iterator();
    for (int doc = it.nextDoc(); doc != DocIdSetIterator.NO_MORE_DOCS; doc = it.nextDoc()) {
        dummy += doc;/*ww  w.j a v a  2 s. com*/
    }
    return dummy;
}

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;//w w w .j a  va  2  s .  com
    }
    return dummy;
}

From source file:DocIdSetBenchmark.java

License:Apache License

public static long scoreBuildFixedBitSet(DocIdSet set, int maxDoc) throws IOException {
    final long start = System.nanoTime();
    int dummy = 0;
    long score = 0;
    while (System.nanoTime() - start < SECOND) {
        final FixedBitSet copy = new FixedBitSet(maxDoc);
        DocIdSetIterator iterator = set.iterator();
        if (iterator != null) {
            copy.or(iterator);//from  w  w  w  . ja  v a  2s  .co m
        }
        dummy += copy.hashCode();
        ++score;
    }
    DUMMY += dummy;
    return score;
}

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 ww  w  . j a  va  2 s.c  o  m
                    break;
                }

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

From source file:de.ingrid.search.utils.facet.FacetUtils.java

License:EUPL

public static OpenBitSet[] getBitSetsFromQuery(Query query, LuceneIndexReaderWrapper indexReaderWrapper) {
    long start = 0;
    if (LOG.isDebugEnabled()) {
        start = System.currentTimeMillis();
    }/*from   w ww.  ja va 2  s. c  om*/
    CachingWrapperFilter filter = new CachingWrapperFilter(new QueryWrapperFilter(query));
    try {
        IndexReader[] indexReaders = indexReaderWrapper.getIndexReader();
        OpenBitSet[] result = new OpenBitSet[indexReaders.length];
        for (int i = 0; i < indexReaders.length; i++) {
            DocIdSet queryBitset = filter.getDocIdSet(indexReaders[i]);
            OpenBitSet queryOpenBitset;
            // check for an required OpenBitSet, create one if the docIdSet
            // is not already a OpenBitSet instance
            // not 100% sure when an openBitSet is returned and when not
            // was observed if the query is no Boolean query or if the query
            // is a single/multiple MUST_NOT query
            if (queryBitset instanceof OpenBitSet) {
                queryOpenBitset = (OpenBitSet) queryBitset;
            } else {
                queryOpenBitset = new OpenBitSetDISI(queryBitset.iterator(), indexReaders[i].maxDoc());
            }

            result[i] = queryOpenBitset;
            if (LOG.isDebugEnabled()) {
                LOG.debug("Create bit set for indexreader[" + i + "] for lucene query '" + query
                        + "' with cardinallity=" + queryOpenBitset.cardinality() + " in "
                        + (System.currentTimeMillis() - start) + " ms.");
            }
        }
        return result;
    } catch (IOException e) {
        LOG.error("Error producing bitset from query '" + query + "'.", e);
    }
    return null;
}

From source file:de.unihildesheim.iw.lucene.document.FeedbackQueryTest.java

License:Open Source License

private static int getNumDocsFromSet(final DocIdSet set) throws IOException {
    final DocIdSetIterator disi = set.iterator();
    int count = 0;
    if (disi != null) {
        while (disi.nextDoc() != DocIdSetIterator.NO_MORE_DOCS) {
            count++;// w  w  w  . j  a  v a 2 s  .  co  m
        }
    }
    return count;
}

From source file:de.unihildesheim.iw.lucene.util.DocIdSetUtils.java

License:Open Source License

/**
 * Get the count of documents available in the set.
 *
 * @param dis Documents id set/*from www . jav  a  2  s .  com*/
 * @return Cardinality
 * @throws IOException Thrown on low-level I/O-errors
 */
public static int cardinality(@NotNull final DocIdSet dis) throws IOException {
    final int cardinality;

    if (RoaringDocIdSet.class.isInstance(dis)) {
        cardinality = ((RoaringDocIdSet) dis).cardinality();
    } else {
        @Nullable
        final BitSet bits = bits(dis);
        if (bits == null) {
            @Nullable
            final DocIdSetIterator disi = dis.iterator();
            cardinality = disi == null ? 0 : (int) StreamUtils.stream(disi).count();
        } else {
            cardinality = bits.cardinality();
        }
    }
    return cardinality < 0 ? 0 : cardinality;
}

From source file:de.unihildesheim.iw.lucene.util.DocIdSetUtils.java

License:Open Source License

/**
 * Get the highest document id stored in the {@link DocIdSet}.
 *
 * @param dis DocIdSet/* w  w  w  . j  a v  a 2s  .c  o  m*/
 * @return Highest document number or {@code -1}, if there's no document
 * @throws IOException Thrown on low-level i/o-errors
 */
public static int maxDoc(@NotNull final DocIdSet dis) throws IOException {
    final int maxDoc;

    @Nullable
    final DocIdSetIterator disi = dis.iterator();
    if (disi == null) {
        maxDoc = 0;
    } else {
        @Nullable
        BitSet bitSet;
        bitSet = BitSetIterator.getFixedBitSetOrNull(disi);
        if (bitSet == null) {
            bitSet = BitSetIterator.getSparseFixedBitSetOrNull(disi);
        }
        if (bitSet == null) {
            bitSet = BitsUtils.bits2BitSet(dis.bits());
        }

        if (bitSet == null) {
            maxDoc = StreamUtils.stream(dis).sorted().max().getAsInt();
        } else {
            if (bitSet.length() == 0) {
                maxDoc = -1;
            } else if (bitSet.length() == 1) {
                maxDoc = bitSet.get(0) ? 0 : -1;
            } else {
                maxDoc = bitSet.prevSetBit(bitSet.length() - 1);
            }
        }
    }
    return maxDoc;
}

From source file:de.unihildesheim.iw.lucene.util.DocIdSetUtils.java

License:Open Source License

/**
 * Get a bits instance from a DocIdSet.//  w ww. j  a v a2 s  .  c  om
 *
 * @param dis Set whose bits to get
 * @return Bits or null, if no bits are set
 * @throws IOException Thrown on low-level I/O-errors
 */
@Nullable
public static BitSet bits(@NotNull final DocIdSet dis) throws IOException {
    @Nullable
    final DocIdSetIterator disi = dis.iterator();

    if (disi == null) {
        return null;
    } else {
        @Nullable
        BitSet bitSet;

        bitSet = BitSetIterator.getFixedBitSetOrNull(disi);
        if (bitSet == null) {
            bitSet = BitSetIterator.getSparseFixedBitSetOrNull(disi);
        }
        if (bitSet == null) {
            bitSet = BitsUtils.bits2BitSet(dis.bits());
        }

        if (bitSet == null) {
            bitSet = new SparseFixedBitSet(maxDoc(dis) + 1);
            StreamUtils.stream(disi).forEach(bitSet::set);
        }
        return bitSet;
    }
}

From source file:de.unihildesheim.iw.lucene.util.StreamUtils.java

License:Open Source License

/**
 * Stream contents of a {@link DocIdSet}.
 *
 * @param dis DocIdSet/* www  .  j a  va 2  s . co  m*/
 * @return Stream of sets content
 * @throws IOException Thrown on low-level i/o-errors
 */
public static IntStream stream(@NotNull final DocIdSet dis) throws IOException {
    return stream(dis.iterator());
}