Java Code Examples for org.apache.lucene.search.DocIdSetIterator

The following examples show how to use org.apache.lucene.search.DocIdSetIterator. These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source Project: lucene-solr   Source File: CrossCollectionJoinQuery.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public Scorer scorer(LeafReaderContext context) throws IOException {
  if (filter == null) {
    filter = getDocSet().getTopFilter();
  }

  DocIdSet readerSet = filter.getDocIdSet(context, null);
  if (readerSet == null) {
    return null;
  }
  DocIdSetIterator readerSetIterator = readerSet.iterator();
  if (readerSetIterator == null) {
    return null;
  }
  return new ConstantScoreScorer(this, score(), scoreMode, readerSetIterator);
}
 
Example 2
Source Project: lucene-solr   Source File: ValueSourceRangeFilter.java    License: Apache License 2.0 6 votes vote down vote up
@Override
@SuppressWarnings({"rawtypes"})
public DocIdSet getDocIdSet(final Map context, final LeafReaderContext readerContext, Bits acceptDocs) throws IOException {
  // NB the IndexSearcher parameter here can be null because Filter Weights don't
  // actually use it.
  Weight weight = createWeight(null, ScoreMode.COMPLETE, 1);
  return BitsFilteredDocIdSet.wrap(new DocIdSet() {
     @Override
     public DocIdSetIterator iterator() throws IOException {
       @SuppressWarnings({"unchecked"})
       Scorer scorer = valueSource.getValues(context, readerContext).getRangeScorer(weight, readerContext, lowerVal, upperVal, includeLower, includeUpper);
       return scorer == null ? null : scorer.iterator();
     }
     @Override
     public Bits bits() {
       return null;  // don't use random access
     }

     @Override
     public long ramBytesUsed() {
       return 0L;
     }
   }, acceptDocs);
}
 
Example 3
Source Project: lucene-solr   Source File: ExportWriter.java    License: Apache License 2.0 6 votes vote down vote up
protected void identifyLowestSortingUnexportedDocs(List<LeafReaderContext> leaves, SortDoc sortDoc, SortQueue queue) throws IOException {
  queue.reset();
  SortDoc top = queue.top();
  for (int i = 0; i < leaves.size(); i++) {
    sortDoc.setNextReader(leaves.get(i));
    DocIdSetIterator it = new BitSetIterator(sets[i], 0); // cost is not useful here
    int docId;
    while ((docId = it.nextDoc()) != DocIdSetIterator.NO_MORE_DOCS) {
      sortDoc.setValues(docId);
      if (top.lessThan(sortDoc)) {
        top.setValues(sortDoc);
        top = queue.updateTop();
      }
    }
  }
}
 
Example 4
Source Project: lucene-solr   Source File: TestDocCount.java    License: Apache License 2.0 6 votes vote down vote up
private void verifyCount(IndexReader ir) throws Exception {
  final Collection<String> fields = FieldInfos.getIndexedFields(ir);
  for (String field : fields) {
    Terms terms = MultiTerms.getTerms(ir, field);
    if (terms == null) {
      continue;
    }
    int docCount = terms.getDocCount();
    FixedBitSet visited = new FixedBitSet(ir.maxDoc());
    TermsEnum te = terms.iterator();
    while (te.next() != null) {
      PostingsEnum de = TestUtil.docs(random(), te, null, PostingsEnum.NONE);
      while (de.nextDoc() != DocIdSetIterator.NO_MORE_DOCS) {
        visited.set(de.docID());
      }
    }
    assertEquals(visited.cardinality(), docCount);
  }
}
 
Example 5
Source Project: crate   Source File: LuceneBatchIterator.java    License: Apache License 2.0 6 votes vote down vote up
private boolean innerMoveNext() throws IOException {
    while (tryAdvanceDocIdSetIterator()) {
        LeafReader reader = currentLeaf.reader();
        Bits liveDocs = reader.getLiveDocs();
        int doc;
        while ((doc = currentDocIdSetIt.nextDoc()) != DocIdSetIterator.NO_MORE_DOCS) {
            if (docDeleted(liveDocs, doc) || belowMinScore(currentScorer)) {
                continue;
            }
            onDoc(doc);
            return true;
        }
        currentDocIdSetIt = null;
    }
    clearState();
    return false;
}
 
Example 6
Source Project: lucene-solr   Source File: DocValuesFacets.java    License: Apache License 2.0 6 votes vote down vote up
/** accumulates per-segment single-valued facet counts, mapping to global ordinal space on-the-fly */
static void accumSingleGeneric(int counts[], int startTermIndex, SortedDocValues si, DocIdSetIterator disi, int subIndex, OrdinalMap map) throws IOException {
  final LongValues ordmap = map == null ? null : map.getGlobalOrds(subIndex);
  int doc;
  while ((doc = disi.nextDoc()) != DocIdSetIterator.NO_MORE_DOCS) {
    int term;
    if (si.advanceExact(doc)) {
      term = si.ordValue();
    } else {
      term = -1;
    }
    if (map != null && term >= 0) {
      term = (int) ordmap.get(term);
    }
    int arrIdx = term-startTermIndex;
    if (arrIdx>=0 && arrIdx<counts.length) counts[arrIdx]++;
  }
}
 
Example 7
Source Project: lucene-solr   Source File: LukeRequestHandler.java    License: Apache License 2.0 6 votes vote down vote up
private static Document getFirstLiveDoc(Terms terms, LeafReader reader) throws IOException {
  PostingsEnum postingsEnum = null;
  TermsEnum termsEnum = terms.iterator();
  BytesRef text;
  // Deal with the chance that the first bunch of terms are in deleted documents. Is there a better way?
  for (int idx = 0; idx < 1000 && postingsEnum == null; ++idx) {
    text = termsEnum.next();
    if (text == null) { // Ran off the end of the terms enum without finding any live docs with that field in them.
      return null;
    }
    postingsEnum = termsEnum.postings(postingsEnum, PostingsEnum.NONE);
    final Bits liveDocs = reader.getLiveDocs();
    if (postingsEnum.nextDoc() != DocIdSetIterator.NO_MORE_DOCS) {
      if (liveDocs != null && liveDocs.get(postingsEnum.docID())) {
        continue;
      }
      return reader.document(postingsEnum.docID());
    }
  }
  return null;
}
 
Example 8
Source Project: lucene-solr   Source File: TestRTGBase.java    License: Apache License 2.0 6 votes vote down vote up
protected int getFirstMatch(IndexReader r, Term t) throws IOException {
  Terms terms = MultiTerms.getTerms(r, t.field());
  if (terms == null) return -1;
  BytesRef termBytes = t.bytes();
  final TermsEnum termsEnum = terms.iterator();
  if (!termsEnum.seekExact(termBytes)) {
    return -1;
  }
  PostingsEnum docs = termsEnum.postings(null, PostingsEnum.NONE);
  docs = BitsFilteredPostingsEnum.wrap(docs, MultiBits.getLiveDocs(r));
  int id = docs.nextDoc();
  if (id != DocIdSetIterator.NO_MORE_DOCS) {
    int next = docs.nextDoc();
    assertEquals(DocIdSetIterator.NO_MORE_DOCS, next);
  }
  return id == DocIdSetIterator.NO_MORE_DOCS ? -1 : id;
}
 
Example 9
Source Project: lucene-solr   Source File: TestIndexedDISI.java    License: Apache License 2.0 6 votes vote down vote up
private void assertAdvanceEquality(IndexedDISI disi, BitSetIterator disi2, int step) throws IOException {
  int index = -1;
  while (true) {
    int target = disi2.docID() + step;
    int doc;
    do {
      doc = disi2.nextDoc();
      index++;
    } while (doc < target);
    assertEquals(doc, disi.advance(target));
    if (doc == DocIdSetIterator.NO_MORE_DOCS) {
      break;
    }
    assertEquals("Expected equality using step " + step + " at docID " + doc, index, disi.index());
  }
}
 
Example 10
Source Project: incubator-retired-blur   Source File: FilterCache.java    License: Apache License 2.0 6 votes vote down vote up
private DocIdSet docIdSetToCache(DocIdSet docIdSet, AtomicReader reader, String segmentName, Directory directory)
    throws IOException {
  if (docIdSet == null) {
    // this is better than returning null, as the nonnull result can be cached
    return DocIdSet.EMPTY_DOCIDSET;
  } else if (docIdSet.isCacheable()) {
    return docIdSet;
  } else {
    final DocIdSetIterator it = docIdSet.iterator();
    // null is allowed to be returned by iterator(),
    // in this case we wrap with the empty set,
    // which is cacheable.
    if (it == null) {
      return DocIdSet.EMPTY_DOCIDSET;
    } else {
      final IndexFileBitSet bits = new IndexFileBitSet(reader.maxDoc(), _id, segmentName, directory);
      if (!bits.exists()) {
        bits.create(it);
      }
      bits.load();
      return bits;
    }
  }
}
 
Example 11
Source Project: lucene-solr   Source File: SerializedDVStrategy.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public Weight createWeight(IndexSearcher searcher, ScoreMode scoreMode, float boost) throws IOException {
  return new ConstantScoreWeight(this, boost) {
    @Override
    public Scorer scorer(LeafReaderContext context) throws IOException {
      DocIdSetIterator approximation = DocIdSetIterator.all(context.reader().maxDoc());
      TwoPhaseIterator it = predicateValueSource.iterator(context, approximation);
      return new ConstantScoreScorer(this, score(), scoreMode, it);
    }

    @Override
    public boolean isCacheable(LeafReaderContext ctx) {
      return predicateValueSource.isCacheable(ctx);
    }

  };
}
 
Example 12
Source Project: lucene-solr   Source File: TestDocsAndPositions.java    License: Apache License 2.0 6 votes vote down vote up
public void testDocsEnumStart() throws Exception {
  Directory dir = newDirectory();
  RandomIndexWriter writer = new RandomIndexWriter(random(), dir);
  Document doc = new Document();
  doc.add(newStringField("foo", "bar", Field.Store.NO));
  writer.addDocument(doc);
  DirectoryReader reader = writer.getReader();
  LeafReader r = getOnlyLeafReader(reader);
  PostingsEnum disi = TestUtil.docs(random(), r, "foo", new BytesRef("bar"), null, PostingsEnum.NONE);
  int docid = disi.docID();
  assertEquals(-1, docid);
  assertTrue(disi.nextDoc() != DocIdSetIterator.NO_MORE_DOCS);
  
  // now reuse and check again
  TermsEnum te = r.terms("foo").iterator();
  assertTrue(te.seekExact(new BytesRef("bar")));
  disi = TestUtil.docs(random(), te, disi, PostingsEnum.NONE);
  docid = disi.docID();
  assertEquals(-1, docid);
  assertTrue(disi.nextDoc() != DocIdSetIterator.NO_MORE_DOCS);
  writer.close();
  r.close();
  dir.close();
}
 
Example 13
Source Project: lucene-solr   Source File: BlockJoin.java    License: Apache License 2.0 6 votes vote down vote up
/** childInput may also contain parents (i.e. a parent or below will all roll up to that parent) */
public static DocSet toParents(DocSet childInput, BitDocSet parentList, QueryContext qcontext) throws IOException {
  FixedBitSet parentBits = parentList.getBits();
  DocSetCollector collector = new DocSetCollector(qcontext.searcher().maxDoc());
  DocIterator iter = childInput.iterator();
  int currentParent = -1;
  while (iter.hasNext()) {
    int childDoc = iter.nextDoc(); // TODO: skipping
    if (childDoc <= currentParent) { // use <= since we also allow parents in the input
      // we already visited this parent
      continue;
    }
    currentParent = parentBits.nextSetBit(childDoc);
    if (currentParent != DocIdSetIterator.NO_MORE_DOCS) {
      // only collect the parent the first time we skip to it
      collector.collect( currentParent );
    }
  }
  return collector.getDocSet();
}
 
Example 14
Source Project: crate   Source File: PerThreadIDVersionAndSeqNoLookup.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * returns the internal lucene doc id for the given id bytes.
 * {@link DocIdSetIterator#NO_MORE_DOCS} is returned if not found
 * */
private int getDocID(BytesRef id, Bits liveDocs) throws IOException {
    // termsEnum can possibly be null here if this leaf contains only no-ops.
    if (termsEnum != null && termsEnum.seekExact(id)) {
        int docID = DocIdSetIterator.NO_MORE_DOCS;
        // there may be more than one matching docID, in the case of nested docs, so we want the last one:
        docsEnum = termsEnum.postings(docsEnum, 0);
        for (int d = docsEnum.nextDoc(); d != DocIdSetIterator.NO_MORE_DOCS; d = docsEnum.nextDoc()) {
            if (liveDocs != null && liveDocs.get(d) == false) {
                continue;
            }
            docID = d;
        }
        return docID;
    } else {
        return DocIdSetIterator.NO_MORE_DOCS;
    }
}
 
Example 15
Source Project: lucene-solr   Source File: DocValuesFacets.java    License: Apache License 2.0 6 votes vote down vote up
/** "typical" single-valued faceting: not too many unique values, no prefixing. maps to global ordinals as a separate step */
static void accumSingleSeg(int counts[], SortedDocValues si, DocIdSetIterator disi, int subIndex, OrdinalMap map) throws IOException {
  // First count in seg-ord space:
  final int segCounts[];
  if (map == null) {
    segCounts = counts;
  } else {
    segCounts = new int[1+si.getValueCount()];
  }
  
  int doc;
  while ((doc = disi.nextDoc()) != DocIdSetIterator.NO_MORE_DOCS) {
    if (si.advanceExact(doc)) {
      segCounts[1+si.ordValue()]++;
    } else {
      segCounts[0]++;
    }
  }
  
  // migrate to global ords (if necessary)
  if (map != null) {
    migrateGlobal(counts, segCounts, subIndex, map);
  }
}
 
Example 16
Source Project: lucene-solr   Source File: TestSegmentTermDocs.java    License: Apache License 2.0 6 votes vote down vote up
public void testTermDocs() throws IOException {
  //After adding the document, we should be able to read it back in
  SegmentReader reader = new SegmentReader(info, Version.LATEST.major, newIOContext(random()));
  assertTrue(reader != null);

  TermsEnum terms = reader.terms(DocHelper.TEXT_FIELD_2_KEY).iterator();
  terms.seekCeil(new BytesRef("field"));
  PostingsEnum termDocs = TestUtil.docs(random(), terms, null, PostingsEnum.FREQS);
  if (termDocs.nextDoc() != DocIdSetIterator.NO_MORE_DOCS)    {
    int docId = termDocs.docID();
    assertTrue(docId == 0);
    int freq = termDocs.freq();
    assertTrue(freq == 3);  
  }
  reader.close();
}
 
Example 17
Source Project: lucene-solr   Source File: SparseFixedBitSet.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public void or(DocIdSetIterator it) throws IOException {
  {
    // specialize union with another SparseFixedBitSet
    final SparseFixedBitSet other = BitSetIterator.getSparseFixedBitSetOrNull(it);
    if (other != null) {
      checkUnpositioned(it);
      or(other);
      return;
    }
  }

  // We do not specialize the union with a FixedBitSet since FixedBitSets are
  // supposed to be used for dense data and sparse fixed bit sets for sparse
  // data, so a sparse set would likely get upgraded by DocIdSetBuilder before
  // being or'ed with a FixedBitSet

  if (it.cost() < indices.length) {
    // the default impl is good for sparse iterators
    super.or(it);
  } else {
    orDense(it);
  }
}
 
Example 18
Source Project: lucene-solr   Source File: DocValuesFieldUpdates.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public final int nextDoc() {
  if (idx >= size) {
    return doc = DocIdSetIterator.NO_MORE_DOCS;
  }
  long longDoc = docs.get(idx);
  ++idx;
  for (; idx < size; idx++) {
    // scan forward to last update to this doc
    final long nextLongDoc = docs.get(idx);
    if ((longDoc >>> 1) != (nextLongDoc >>> 1)) {
      break;
    }
    longDoc = nextLongDoc;
  }
  hasValue = (longDoc & HAS_VALUE_MASK) >  0;
  if (hasValue) {
    set(idx - 1);
  }
  doc = (int)(longDoc >> SHIFT);
  return doc;
}
 
Example 19
Source Project: lucene-solr   Source File: TaxonomyFacetSumValueSource.java    License: Apache License 2.0 6 votes vote down vote up
private void sumValues(List<MatchingDocs> matchingDocs, boolean keepScores, DoubleValuesSource valueSource) throws IOException {

    IntsRef scratch = new IntsRef();
    for(MatchingDocs hits : matchingDocs) {
      OrdinalsReader.OrdinalsSegmentReader ords = ordinalsReader.getReader(hits.context);
      DoubleValues scores = keepScores ? scores(hits) : null;
      DoubleValues functionValues = valueSource.getValues(hits.context, scores);
      DocIdSetIterator docs = hits.bits.iterator();
      
      int doc;
      while ((doc = docs.nextDoc()) != DocIdSetIterator.NO_MORE_DOCS) {
        ords.get(doc, scratch);
        if (functionValues.advanceExact(doc)) {
          float value = (float) functionValues.doubleValue();
          for (int i = 0; i < scratch.length; i++) {
            values[scratch.ints[i]] += value;
          }
        }
      }
    }

    rollup();
  }
 
Example 20
Source Project: ltr4l   Source File: AbstractLTRScorer.java    License: Apache License 2.0 5 votes vote down vote up
public AbstractLTRScorer(Weight luceneWeight, List<FieldFeatureExtractor[]> featuresSpec,
                                   DocIdSetIterator iterator, Ranker ranker){
  super(luceneWeight);
  this.featuresSpec = featuresSpec;
  this.iterator = iterator;
  this.ranker = ranker;
}
 
Example 21
Source Project: lucene-solr   Source File: SortedSetDocValuesWriter.java    License: Apache License 2.0 5 votes vote down vote up
public BufferedSortedSetDocValues(int[] sortedValues, int[] ordMap, BytesRefHash hash, PackedLongValues ords, PackedLongValues ordCounts, int maxCount, DocIdSetIterator docsWithField) {
  this.currentDoc = new int[maxCount];
  this.sortedValues = sortedValues;
  this.ordMap = ordMap;
  this.hash = hash;
  this.ordsIter = ords.iterator();
  this.ordCountsIter = ordCounts.iterator();
  this.docsWithField = docsWithField;
}
 
Example 22
Source Project: lucene-solr   Source File: TestTermVectorsWriter.java    License: Apache License 2.0 5 votes vote down vote up
public void testEndOffsetPositionStopFilter() throws Exception {
  Directory dir = newDirectory();
  IndexWriter w = new IndexWriter(dir, newIndexWriterConfig(new MockAnalyzer(random(), MockTokenizer.SIMPLE, true, MockTokenFilter.ENGLISH_STOPSET)));
  Document doc = new Document();
  FieldType customType = new FieldType(TextField.TYPE_NOT_STORED);
  customType.setStoreTermVectors(true);
  customType.setStoreTermVectorPositions(true);
  customType.setStoreTermVectorOffsets(true);
  Field f = newField("field", "abcd the", customType);
  doc.add(f);
  doc.add(f);
  w.addDocument(doc);
  w.close();

  IndexReader r = DirectoryReader.open(dir);
  TermsEnum termsEnum = r.getTermVectors(0).terms("field").iterator();
  assertNotNull(termsEnum.next());
  PostingsEnum dpEnum = termsEnum.postings(null, PostingsEnum.ALL);
  assertEquals(2, termsEnum.totalTermFreq());

  assertTrue(dpEnum.nextDoc() != DocIdSetIterator.NO_MORE_DOCS);
  dpEnum.nextPosition();
  assertEquals(0, dpEnum.startOffset());
  assertEquals(4, dpEnum.endOffset());

  dpEnum.nextPosition();
  assertEquals(9, dpEnum.startOffset());
  assertEquals(13, dpEnum.endOffset());
  assertEquals(DocIdSetIterator.NO_MORE_DOCS, dpEnum.nextDoc());

  r.close();
  dir.close();
}
 
Example 23
Source Project: lucene-solr   Source File: DocValuesFacets.java    License: Apache License 2.0 5 votes vote down vote up
/** accumulates per-segment multi-valued facet counts */
static void accumMulti(int counts[], int startTermIndex, SortedSetDocValues si, DocIdSetIterator disi, int subIndex, OrdinalMap map) throws IOException {
  if (startTermIndex == -1 && (map == null || si.getValueCount() < disi.cost()*10)) {
    // no prefixing, not too many unique values wrt matching docs (lucene/facets heuristic): 
    //   collect separately per-segment, then map to global ords
    accumMultiSeg(counts, si, disi, subIndex, map);
  } else {
    // otherwise: do collect+map on the fly
    accumMultiGeneric(counts, startTermIndex, si, disi, subIndex, map);
  }
}
 
Example 24
Source Project: mtas   Source File: CodecCollector.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Compute termvector number basic.
 *
 * @param docSet
 *          the doc set
 * @param termDocId
 *          the term doc id
 * @param termsEnum
 *          the terms enum
 * @param r
 *          the r
 * @param lrc
 *          the lrc
 * @param postingsEnum
 *          the postings enum
 * @return the termvector number basic
 * @throws IOException
 *           Signals that an I/O exception has occurred.
 */
private static TermvectorNumberBasic computeTermvectorNumberBasic(
    List<Integer> docSet, int termDocId, TermsEnum termsEnum, LeafReader r,
    LeafReaderContext lrc, PostingsEnum postingsEnum) throws IOException {
  TermvectorNumberBasic result = new TermvectorNumberBasic();
  boolean hasDeletedDocuments = (r.getLiveDocs() != null);
  if ((docSet.size() == r.numDocs()) && !hasDeletedDocuments) {
    try {
      return computeTermvectorNumberBasic(termsEnum, r);
    } catch (IOException e) {
      log.debug("problem", e);
      // problem
    }
  }
  result.docNumber = 0;
  result.valueSum[0] = 0;
  int localTermDocId = termDocId;
  Iterator<Integer> docIterator = docSet.iterator();
  postingsEnum = termsEnum.postings(postingsEnum, PostingsEnum.FREQS);
  int docId;
  while (docIterator.hasNext()) {
    docId = docIterator.next() - lrc.docBase;
    if (docId >= localTermDocId && ((docId == localTermDocId)
        || ((localTermDocId = postingsEnum.advance(docId)) == docId))) {
      result.docNumber++;
      result.valueSum[0] += postingsEnum.freq();
    }
    if (localTermDocId == DocIdSetIterator.NO_MORE_DOCS) {
      break;
    }
  }
  return result;
}
 
Example 25
Source Project: lucene-solr   Source File: TestTermVectorsWriter.java    License: Apache License 2.0 5 votes vote down vote up
public void testDoubleOffsetCounting2() throws Exception {
  Directory dir = newDirectory();
  IndexWriter w = new IndexWriter(dir, newIndexWriterConfig(new MockAnalyzer(random())));
  Document doc = new Document();
  FieldType customType = new FieldType(TextField.TYPE_NOT_STORED);
  customType.setStoreTermVectors(true);
  customType.setStoreTermVectorPositions(true);
  customType.setStoreTermVectorOffsets(true);
  Field f = newField("field", "abcd", customType);
  doc.add(f);
  doc.add(f);
  w.addDocument(doc);
  w.close();

  IndexReader r = DirectoryReader.open(dir);
  TermsEnum termsEnum = r.getTermVectors(0).terms("field").iterator();
  assertNotNull(termsEnum.next());
  PostingsEnum dpEnum = termsEnum.postings(null, PostingsEnum.ALL);
  assertEquals(2, termsEnum.totalTermFreq());

  assertTrue(dpEnum.nextDoc() != DocIdSetIterator.NO_MORE_DOCS);
  dpEnum.nextPosition();
  assertEquals(0, dpEnum.startOffset());
  assertEquals(4, dpEnum.endOffset());

  dpEnum.nextPosition();
  assertEquals(5, dpEnum.startOffset());
  assertEquals(9, dpEnum.endOffset());
  assertEquals(DocIdSetIterator.NO_MORE_DOCS, dpEnum.nextDoc());

  r.close();
  dir.close();
}
 
Example 26
Source Project: lucene-solr   Source File: CheckJoinIndex.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Check that the given index is good to use for block joins.
 * @throws IllegalStateException if the index does not have an appropriate structure
 */
public static void check(IndexReader reader, BitSetProducer parentsFilter) throws IOException {
  for (LeafReaderContext context : reader.leaves()) {
    if (context.reader().maxDoc() == 0) {
      continue;
    }
    final BitSet parents = parentsFilter.getBitSet(context);
    if (parents == null || parents.cardinality() == 0) {
      throw new IllegalStateException("Every segment should have at least one parent, but " + context.reader() + " does not have any");
    }
    if (parents.get(context.reader().maxDoc() - 1) == false) {
      throw new IllegalStateException("The last document of a segment must always be a parent, but " + context.reader() + " has a child as a last doc");
    }
    final Bits liveDocs = context.reader().getLiveDocs();
    if (liveDocs != null) {
      int prevParentDoc = -1;
      DocIdSetIterator it = new BitSetIterator(parents, 0L);
      for (int parentDoc = it.nextDoc(); parentDoc != DocIdSetIterator.NO_MORE_DOCS; parentDoc = it.nextDoc()) {
        final boolean parentIsLive = liveDocs.get(parentDoc);
        for (int child = prevParentDoc + 1; child != parentDoc; child++) {
          final boolean childIsLive = liveDocs.get(child);
          if (parentIsLive != childIsLive) {
            if (childIsLive) {
              throw new IllegalStateException("Parent doc " + parentDoc + " of segment " + context.reader() + " is live but has a deleted child document " + child);
            } else {
              throw new IllegalStateException("Parent doc " + parentDoc + " of segment " + context.reader() + " is deleted but has a live child document " + child);
            }
          }
        }
        prevParentDoc = parentDoc;
      }
    }
  }
}
 
Example 27
Source Project: incubator-retired-blur   Source File: IndexManager.java    License: Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
private static boolean isFiltered(int notAdjustedDocId, IndexReader reader, Filter filter) throws IOException {
  if (filter == null) {
    return false;
  }
  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 + "]");
  }
}
 
Example 28
Source Project: Elasticsearch   Source File: ChildrenQuery.java    License: Apache License 2.0 5 votes vote down vote up
ParentScorer(ParentWeight parentWeight, DocIdSetIterator parentsIterator, ParentCollector collector, SortedDocValues globalOrdinals) {
    super(parentWeight);
    this.parentWeight = parentWeight;
    this.globalOrdinals = globalOrdinals;
    this.parentsIterator = parentsIterator;
    this.parentIds = collector.parentIdxs;
    this.scores = ((ParentScoreCollector) collector).scores;
}
 
Example 29
@Override
public DocIdSetIterator iterator() {
  // add indirection so that if 'it' is updated then it will
  // be taken into account
  return new DocIdSetIterator() {

    @Override
    public int nextDoc() throws IOException {
      return doc = it.nextDoc();
    }

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

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

    @Override
    public int advance(int target) throws IOException {
    return doc = it.advance(target);
    }
  };
}
 
Example 30
Source Project: lucene-solr   Source File: TestFixedBitSet.java    License: Apache License 2.0 5 votes vote down vote up
void doNextSetBit(java.util.BitSet a, FixedBitSet b) {
  assertEquals(a.cardinality(), b.cardinality());
  int aa=-1;
  int bb=-1;
  do {
    aa = a.nextSetBit(aa+1);
    if (aa == -1) {
      aa = DocIdSetIterator.NO_MORE_DOCS;
    }
    bb = bb < b.length()-1 ? b.nextSetBit(bb+1) : DocIdSetIterator.NO_MORE_DOCS;
    assertEquals(aa,bb);
  } while (aa != DocIdSetIterator.NO_MORE_DOCS);
}