List of usage examples for org.apache.lucene.search DocIdSetIterator docID
public abstract int docID();
-1
if #nextDoc() or #advance(int) were not called yet. From source file:com.kamikaze.docidset.utils.DisiDocQueue.java
License:Apache License
/** * Adds a DocIdSetIterator to the DisiDocQueue in log(size) time if either * the DisiDocQueue is not full, or not lessThan(disi, top()). * @param disi/* w ww . j a v a2 s.c o m*/ * @return true if DocIdSetIterator is added, false otherwise. */ public final boolean insert(DocIdSetIterator disi) { if (size < maxSize) { put(disi); return true; } else { int docNr = disi.docID(); if ((size > 0) && (!(docNr < topHDD.doc))) { // heap[1] is top() heap[1] = new HeapedDisiDoc(disi, docNr); downHeap(); return true; } else { return false; } } }
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 w w w . j a v a 2 s. c o 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.Filter.java
License:Apache License
@Override public Weight createWeight(IndexSearcher searcher, boolean needsScores, float boost) throws IOException { return new Weight(this) { @Override//from www. j av a 2 s .c o m public void extractTerms(Set<Term> terms) { } @Override public Explanation explain(LeafReaderContext context, int doc) throws IOException { final Scorer scorer = scorer(context); final boolean match = (scorer != null && scorer.iterator().advance(doc) == doc); if (match) { assert scorer.score() == 0f; return Explanation.match(0f, "Match on id " + doc); } else { return Explanation.match(0f, "No match on id " + doc); } } @Override public Scorer scorer(LeafReaderContext context) throws IOException { final DocIdSet set = getDocIdSet(context, null); if (set == null) { return null; } if (applyLazily && set.bits() != null) { final Bits bits = set.bits(); final DocIdSetIterator approximation = DocIdSetIterator.all(context.reader().maxDoc()); final TwoPhaseIterator twoPhase = new TwoPhaseIterator(approximation) { @Override public boolean matches() throws IOException { return bits.get(approximation.docID()); } @Override public float matchCost() { return 10; // TODO use cost of bits.get() } }; return new ConstantScoreScorer(this, 0f, twoPhase); } final DocIdSetIterator iterator = set.iterator(); if (iterator == null) { return null; } return new ConstantScoreScorer(this, 0f, iterator); } }; }
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();/*from w w w . java 2 s. c o 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.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./*from w w w. j a va 2 s .c o 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);/* ww w . ja v a 2 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/* www . j a va2s . c om*/ * * 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/* w w w.ja 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 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/* w w w.ja va 2 s. c om*/ * * 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 w w w .j av a 2s . c om * * 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); } } }; }