Example usage for org.apache.lucene.util BitSet prevSetBit

List of usage examples for org.apache.lucene.util BitSet prevSetBit

Introduction

In this page you can find the example usage for org.apache.lucene.util BitSet prevSetBit.

Prototype

public abstract int prevSetBit(int index);

Source Link

Document

Returns the index of the last set bit before or on the index specified.

Usage

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/*from   ww w  .ja v a  2 s.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: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   ww w. j  a  v  a  2  s .c o 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//from   ww w.  ja v  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);
            }
        }
    };
}

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

License:Apache License

/**
 * Return a {NumericDoubleValues} 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 a v  a2  s  .  co  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 NumericDoubleValues select(final SortedNumericDoubleValues values, final double missingValue,
        final BitSet rootDocs, final DocIdSetIterator innerDocs, int maxDoc) throws IOException {
    if (rootDocs == null || innerDocs == null) {
        return select(FieldData.emptySortedNumericDoubles(maxDoc), missingValue);
    }

    return new NumericDoubleValues() {

        int lastSeenRootDoc = 0;
        double lastEmittedValue = missingValue;

        @Override
        public double 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);
            }
        }
    };
}

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

License:Apache License

/**
 * Return a {BinaryDocValues} 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: MIN, MAX//from   w w w  .  j  a  v  a2 s  . co 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 BinaryDocValues select(final SortedBinaryDocValues values, final BytesRef missingValue,
        final BitSet rootDocs, final DocIdSetIterator innerDocs, int maxDoc) throws IOException {
    if (rootDocs == null || innerDocs == null) {
        return select(FieldData.emptySortedBinary(maxDoc), missingValue);
    }
    final BinaryDocValues selectedValues = select(values, null);

    return new BinaryDocValues() {

        final BytesRefBuilder builder = new BytesRefBuilder();

        int lastSeenRootDoc = 0;
        BytesRef lastEmittedValue = missingValue;

        @Override
        public BytesRef 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(selectedValues, builder, innerDocs, firstNestedDoc, rootDoc);
                if (lastEmittedValue == null) {
                    lastEmittedValue = missingValue;
                }
                return lastEmittedValue;
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
    };
}

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

License:Apache License

/**
 * Return a {SortedDocValues} 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.
 *
 * Allowed Modes: MIN, MAX/*from www  .  j  a  v  a 2  s . co 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 SortedDocValues select(final RandomAccessOrds values, final BitSet rootDocs,
        final DocIdSetIterator innerDocs) throws IOException {
    if (rootDocs == null || innerDocs == null) {
        return select(DocValues.emptySortedSet());
    }
    final SortedDocValues selectedValues = select(values);

    return new SortedDocValues() {

        int lastSeenRootDoc = 0;
        int lastEmittedOrd = -1;

        @Override
        public BytesRef lookupOrd(int ord) {
            return selectedValues.lookupOrd(ord);
        }

        @Override
        public int getValueCount() {
            return selectedValues.getValueCount();
        }

        @Override
        public int getOrd(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 lastEmittedOrd;
            }

            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;
                return lastEmittedOrd = pick(selectedValues, innerDocs, firstNestedDoc, rootDoc);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
    };
}

From source file:org.elasticsearch.index.shard.ShardSplittingQuery.java

License:Apache License

private void markChildDocs(BitSet parentDocs, BitSet matchingDocs) {
    int currentDeleted = 0;
    while (currentDeleted < matchingDocs.length()
            && (currentDeleted = matchingDocs.nextSetBit(currentDeleted)) != DocIdSetIterator.NO_MORE_DOCS) {
        int previousParent = parentDocs.prevSetBit(Math.max(0, currentDeleted - 1));
        for (int i = previousParent + 1; i < currentDeleted; i++) {
            matchingDocs.set(i);// w w w.  j  av a 2  s  .  c  o m
        }
        currentDeleted++;
    }
}