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

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

Introduction

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

Prototype

public abstract long cost();

Source Link

Document

Returns the estimated cost of this DocIdSetIterator .

Usage

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;
    }/*from   ww  w. j av  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.solr.search.DocSetBuilder.java

License:Apache License

public void add(DocIdSetIterator iter, int base) throws IOException {
    grow((int) Math.min(Integer.MAX_VALUE, iter.cost()));

    if (bitSet != null) {
        add(bitSet, iter, base);/* w w w .  j  a  v  a 2  s.co m*/
    } else {
        while (true) {
            for (int i = pos; i < buffer.length; ++i) {
                final int doc = iter.nextDoc();
                if (doc == DocIdSetIterator.NO_MORE_DOCS) {
                    pos = i; // update pos
                    return;
                }
                buffer[i] = doc + base; // using the loop counter may help with removal of bounds checking
            }

            pos = buffer.length; // update pos
            if (pos + 1 >= threshold) {
                break;
            }

            growBuffer(pos + 1);
        }

        upgradeToBitSet();
        add(bitSet, iter, base);
    }
}

From source file:org.codelibs.elasticsearch.search.profile.query.ProfileScorer.java

License:Apache License

@Override
public DocIdSetIterator iterator() {
    final DocIdSetIterator in = scorer.iterator();
    return new DocIdSetIterator() {

        @Override//  w w w  .  ja v a 2  s . co  m
        public int advance(int target) throws IOException {
            profile.startTime(QueryTimingType.ADVANCE);
            try {
                return in.advance(target);
            } finally {
                profile.stopAndRecordTime();
            }
        }

        @Override
        public int nextDoc() throws IOException {
            profile.startTime(QueryTimingType.NEXT_DOC);
            try {
                return in.nextDoc();
            } finally {
                profile.stopAndRecordTime();
            }
        }

        @Override
        public int docID() {
            return in.docID();
        }

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

From source file:org.codelibs.elasticsearch.search.profile.query.ProfileScorer.java

License:Apache License

@Override
public TwoPhaseIterator twoPhaseIterator() {
    final TwoPhaseIterator in = scorer.twoPhaseIterator();
    if (in == null) {
        return null;
    }//  w  w w .jav  a2 s .c om
    final DocIdSetIterator inApproximation = in.approximation();
    final DocIdSetIterator approximation = new DocIdSetIterator() {

        @Override
        public int advance(int target) throws IOException {
            profile.startTime(QueryTimingType.ADVANCE);
            try {
                return inApproximation.advance(target);
            } finally {
                profile.stopAndRecordTime();
            }
        }

        @Override
        public int nextDoc() throws IOException {
            profile.startTime(QueryTimingType.NEXT_DOC);
            try {
                return inApproximation.nextDoc();
            } finally {
                profile.stopAndRecordTime();
            }
        }

        @Override
        public int docID() {
            return inApproximation.docID();
        }

        @Override
        public long cost() {
            return inApproximation.cost();
        }
    };
    return new TwoPhaseIterator(approximation) {
        @Override
        public boolean matches() throws IOException {
            profile.startTime(QueryTimingType.MATCH);
            try {
                return in.matches();
            } finally {
                profile.stopAndRecordTime();
            }
        }

        @Override
        public float matchCost() {
            return in.matchCost();
        }
    };
}

From source file:org.elasticsearch.search.aggregations.bucket.composite.SortedDocsProducer.java

License:Apache License

/**
 * Visits all non-deleted documents in <code>iterator</code> and fills the provided <code>queue</code>
 * with the top composite buckets extracted from the collection.
 * Documents that contain a top composite bucket are added in the provided <code>builder</code> if it is not null.
 *
 * Returns true if the queue is full and the current <code>leadSourceBucket</code> did not produce any competitive
 * composite buckets./*from w  w  w  .j  a va2  s. c om*/
 */
protected boolean processBucket(CompositeValuesCollectorQueue queue, LeafReaderContext context,
        DocIdSetIterator iterator, Comparable<?> leadSourceBucket, @Nullable DocIdSetBuilder builder)
        throws IOException {
    final int[] topCompositeCollected = new int[1];
    final boolean[] hasCollected = new boolean[1];
    final LeafBucketCollector queueCollector = new LeafBucketCollector() {
        int lastDoc = -1;

        // we need to add the matching document in the builder
        // so we build a bulk adder from the approximate cost of the iterator
        // and rebuild the adder during the collection if needed
        int remainingBits = (int) Math.min(iterator.cost(), Integer.MAX_VALUE);
        DocIdSetBuilder.BulkAdder adder = builder == null ? null : builder.grow(remainingBits);

        @Override
        public void collect(int doc, long bucket) throws IOException {
            hasCollected[0] = true;
            int slot = queue.addIfCompetitive();
            if (slot != -1) {
                topCompositeCollected[0]++;
                if (adder != null && doc != lastDoc) {
                    if (remainingBits == 0) {
                        // the cost approximation was lower than the real size, we need to grow the adder
                        // by some numbers (128) to ensure that we can add the extra documents
                        adder = builder.grow(128);
                        remainingBits = 128;
                    }
                    adder.add(doc);
                    remainingBits--;
                    lastDoc = doc;
                }
            }
        }
    };
    final Bits liveDocs = context.reader().getLiveDocs();
    final LeafBucketCollector collector = queue.getLeafCollector(leadSourceBucket, context, queueCollector);
    while (iterator.nextDoc() != DocIdSetIterator.NO_MORE_DOCS) {
        if (liveDocs == null || liveDocs.get(iterator.docID())) {
            collector.collect(iterator.docID());
        }
    }
    if (queue.isFull() && hasCollected[0] && topCompositeCollected[0] == 0) {
        return true;
    }
    return false;
}

From source file:org.elasticsearch.search.profile.ProfileScorer.java

License:Apache License

@Override
public DocIdSetIterator iterator() {
    final DocIdSetIterator in = scorer.iterator();
    return new DocIdSetIterator() {

        @Override// w w w  .  ja  v  a 2  s .  c  o  m
        public int advance(int target) throws IOException {
            profile.startTime(ProfileBreakdown.TimingType.ADVANCE);
            try {
                return in.advance(target);
            } finally {
                profile.stopAndRecordTime();
            }
        }

        @Override
        public int nextDoc() throws IOException {
            profile.startTime(ProfileBreakdown.TimingType.NEXT_DOC);
            try {
                return in.nextDoc();
            } finally {
                profile.stopAndRecordTime();
            }
        }

        @Override
        public int docID() {
            return in.docID();
        }

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

From source file:org.elasticsearch.search.profile.ProfileScorer.java

License:Apache License

@Override
public TwoPhaseIterator twoPhaseIterator() {
    final TwoPhaseIterator in = scorer.twoPhaseIterator();
    if (in == null) {
        return null;
    }//w ww .j  av  a 2s  .  co m
    final DocIdSetIterator inApproximation = in.approximation();
    final DocIdSetIterator approximation = new DocIdSetIterator() {

        @Override
        public int advance(int target) throws IOException {
            profile.startTime(ProfileBreakdown.TimingType.ADVANCE);
            try {
                return inApproximation.advance(target);
            } finally {
                profile.stopAndRecordTime();
            }
        }

        @Override
        public int nextDoc() throws IOException {
            profile.startTime(ProfileBreakdown.TimingType.NEXT_DOC);
            try {
                return inApproximation.nextDoc();
            } finally {
                profile.stopAndRecordTime();
            }
        }

        @Override
        public int docID() {
            return inApproximation.docID();
        }

        @Override
        public long cost() {
            return inApproximation.cost();
        }
    };
    return new TwoPhaseIterator(approximation) {
        @Override
        public boolean matches() throws IOException {
            profile.startTime(ProfileBreakdown.TimingType.MATCH);
            try {
                return in.matches();
            } finally {
                profile.stopAndRecordTime();
            }
        }

        @Override
        public float matchCost() {
            return in.matchCost();
        }
    };
}

From source file:org.opengrok.indexer.search.context.OGKUnifiedHighlighter.java

License:Apache License

/**
 * Produces original text by reading from OpenGrok source content relative
 * to {@link RuntimeEnvironment#getSourceRootPath()} and returns the content
 * for each document if the timestamp matches -- or else just {@code null}
 * for a missing file or a timestamp mismatch (as "the returned Strings must
 * be identical to what was indexed.")/* w  ww.j a  v  a 2 s  . c o m*/
 * <p>
 * "This method must load fields for at least one document from the given
 * {@link DocIdSetIterator} but need not return all of them; by default the
 * character lengths are summed and this method will return early when
 * {@code cacheCharsThreshold} is exceeded. Specifically if that number is
 * 0, then only one document is fetched no matter what. Values in the array
 * of {@link CharSequence} will be {@code null} if no value was found."
 * @return a defined instance
 * @throws IOException if an I/O error occurs
 */
@Override
protected List<CharSequence[]> loadFieldValues(String[] fields, DocIdSetIterator docIter,
        int cacheCharsThreshold) throws IOException {

    List<CharSequence[]> docListOfFields = new ArrayList<>(
            cacheCharsThreshold == 0 ? 1 : (int) Math.min(64, docIter.cost()));

    int sumChars = 0;
    do {
        int docId = docIter.nextDoc();
        if (docId == DocIdSetIterator.NO_MORE_DOCS) {
            break;
        }
        Document doc = searcher.doc(docId);

        String path = doc.get(QueryBuilder.PATH);
        String storedU = doc.get(QueryBuilder.U);
        String content = getRepoFileContent(path, storedU);

        CharSequence[] seqs = new CharSequence[fields.length];
        Arrays.fill(seqs, content);
        docListOfFields.add(seqs);

        if (content != null) {
            sumChars += content.length();
        }
    } while (sumChars <= cacheCharsThreshold && cacheCharsThreshold != 0);

    return docListOfFields;
}