org.apache.lucene.util.BitSet Java Examples

The following examples show how to use org.apache.lucene.util.BitSet. 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 File: TestQueryBitSetProducer.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
public void testSimple() throws Exception {
  Directory dir = newDirectory();
  IndexWriterConfig iwc = newIndexWriterConfig().setMergePolicy(NoMergePolicy.INSTANCE);
  RandomIndexWriter w = new RandomIndexWriter(random(), dir, iwc);
  w.addDocument(new Document());
  DirectoryReader reader = w.getReader();

  QueryBitSetProducer producer = new QueryBitSetProducer(new MatchNoDocsQuery());
  assertNull(producer.getBitSet(reader.leaves().get(0)));
  assertEquals(1, producer.cache.size());

  producer = new QueryBitSetProducer(new MatchAllDocsQuery());
  BitSet bitSet = producer.getBitSet(reader.leaves().get(0));
  assertEquals(1, bitSet.length());
  assertEquals(true, bitSet.get(0));
  assertEquals(1, producer.cache.size());

  IOUtils.close(reader, w, dir);
}
 
Example #2
Source File: ConjunctionDISI.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
private int doNext(int doc) throws IOException {
  advanceLead: for (;; doc = lead.nextDoc()) {
    if (doc >= minLength) {
      return NO_MORE_DOCS;
    }
    for (BitSet bitSet : bitSets) {
      if (bitSet.get(doc) == false) {
        continue advanceLead;
      }
    }
    for (BitSetIterator iterator : bitSetIterators) {
      iterator.setDocId(doc);
    }
    return doc;
  }
}
 
Example #3
Source File: TestIndexedDISI.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
private void assertAdvanceBeyondEnd(BitSet set, Directory dir) throws IOException {
  final int cardinality = set.cardinality();
  final byte denseRankPower = 9; // Not tested here so fixed to isolate factors
  long length;
  int jumpTableentryCount;
  try (IndexOutput out = dir.createOutput("bar", IOContext.DEFAULT)) {
    jumpTableentryCount = IndexedDISI.writeBitSet(new BitSetIterator(set, cardinality), out, denseRankPower);
  }

  try (IndexInput in = dir.openInput("bar", IOContext.DEFAULT)) {
    BitSetIterator disi2 = new BitSetIterator(set, cardinality);
    int doc = disi2.docID();
    int index = 0;
    while (doc < cardinality) {
      doc = disi2.nextDoc();
      index++;
    }

    IndexedDISI disi = new IndexedDISI(in, 0L, in.length(), jumpTableentryCount, denseRankPower, cardinality);
    // Advance 1 docID beyond end
    assertFalse("There should be no set bit beyond the valid docID range", disi.advanceExact(set.length()));
    disi.advance(doc); // Should be the special docID signifyin NO_MORE_DOCS from the BitSetIterator
    assertEquals("The index when advancing beyond the last defined docID should be correct",
        index, disi.index()+1); // disi.index()+1 as the while-loop also counts the NO_MORE_DOCS
  }
}
 
Example #4
Source File: ToParentBlockJoinQuery.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
public BlockJoinScorer(Weight weight, Scorer childScorer, BitSet parentBits, ScoreMode scoreMode) {
  super(weight);
  //System.out.println("Q.init firstChildDoc=" + firstChildDoc);
  this.parentBits = parentBits;
  this.childScorer = childScorer;
  this.scoreMode = scoreMode;
  childTwoPhase = childScorer.twoPhaseIterator();
  if (childTwoPhase == null) {
    childApproximation = childScorer.iterator();
    parentApproximation = new ParentApproximation(childApproximation, parentBits);
    parentTwoPhase = null;
  } else {
    childApproximation = childTwoPhase.approximation();
    parentApproximation = new ParentApproximation(childTwoPhase.approximation(), parentBits);
    parentTwoPhase = new ParentTwoPhase(parentApproximation, childTwoPhase);
  }
}
 
Example #5
Source File: ToParentBlockJoinSortField.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
private FieldComparator<?> getStringComparator(int numHits) {
  return new FieldComparator.TermOrdValComparator(numHits, getField(), missingValue == STRING_LAST) {

    @Override
    protected SortedDocValues getSortedDocValues(LeafReaderContext context, String field) throws IOException {
      SortedSetDocValues sortedSet = DocValues.getSortedSet(context.reader(), field);
      final BlockJoinSelector.Type type = order
          ? BlockJoinSelector.Type.MAX
          : BlockJoinSelector.Type.MIN;
      final BitSet parents = parentFilter.getBitSet(context);
      final BitSet children = childFilter.getBitSet(context);
      if (children == null) {
        return DocValues.emptySorted();
      }
      return BlockJoinSelector.wrap(sortedSet, type, parents, toIter(children));
    }

  };
}
 
Example #6
Source File: ToParentBlockJoinSortField.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
private FieldComparator<?> getIntComparator(int numHits) {
  return new FieldComparator.IntComparator(numHits, getField(), (Integer) missingValue) {
    @Override
    protected NumericDocValues getNumericDocValues(LeafReaderContext context, String field) throws IOException {
      SortedNumericDocValues sortedNumeric = DocValues.getSortedNumeric(context.reader(), field);
      final BlockJoinSelector.Type type = order
          ? BlockJoinSelector.Type.MAX
          : BlockJoinSelector.Type.MIN;
      final BitSet parents = parentFilter.getBitSet(context);
      final BitSet children = childFilter.getBitSet(context);
      if (children == null) {
        return DocValues.emptyNumeric();
      }
      return BlockJoinSelector.wrap(sortedNumeric, type, parents, toIter(children));
    }
  };
}
 
Example #7
Source File: ToParentBlockJoinSortField.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
private FieldComparator<?> getLongComparator(int numHits) {
  return new FieldComparator.LongComparator(numHits, getField(), (Long) missingValue) {
    @Override
    protected NumericDocValues getNumericDocValues(LeafReaderContext context, String field) throws IOException {
      SortedNumericDocValues sortedNumeric = DocValues.getSortedNumeric(context.reader(), field);
      final BlockJoinSelector.Type type = order
          ? BlockJoinSelector.Type.MAX
          : BlockJoinSelector.Type.MIN;
      final BitSet parents = parentFilter.getBitSet(context);
      final BitSet children = childFilter.getBitSet(context);
      if (children == null) {
        return DocValues.emptyNumeric();
      }
      return BlockJoinSelector.wrap(sortedNumeric, type, parents, toIter(children));
    }
  };
}
 
Example #8
Source File: TestIndexedDISI.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
public void testPositionNotZero() throws IOException {
  final int BLOCKS = 10;
  final byte denseRankPower = rarely() ? -1 : (byte) (random().nextInt(7)+7); // sane + chance of disable

  BitSet set = createSetWithRandomBlocks(BLOCKS);
  try (Directory dir = newDirectory()) {
    final int cardinality = set.cardinality();
    int jumpTableEntryCount;
    try (IndexOutput out = dir.createOutput("foo", IOContext.DEFAULT)) {
      jumpTableEntryCount = IndexedDISI.writeBitSet(new BitSetIterator(set, cardinality), out, denseRankPower);
    }
    try (IndexInput fullInput = dir.openInput("foo", IOContext.DEFAULT)) {
      IndexInput blockData =
          IndexedDISI.createBlockSlice(fullInput, "blocks", 0, fullInput.length(), jumpTableEntryCount);
      blockData.seek(random().nextInt((int) blockData.length()));

      RandomAccessInput jumpTable = IndexedDISI.createJumpTable(fullInput, 0, fullInput.length(), jumpTableEntryCount);
      IndexedDISI disi = new IndexedDISI(blockData, jumpTable, jumpTableEntryCount, denseRankPower, cardinality);
      // This failed at some point during LUCENE-8585 development as it did not reset the slice position
      disi.advanceExact(BLOCKS*65536-1);
    }
  }
}
 
Example #9
Source File: ToParentBlockJoinSortField.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
private FieldComparator<?> getFloatComparator(int numHits) {
  return new FieldComparator.FloatComparator(numHits, getField(), (Float) missingValue) {
    @Override
    protected NumericDocValues getNumericDocValues(LeafReaderContext context, String field) throws IOException {
      SortedNumericDocValues sortedNumeric = DocValues.getSortedNumeric(context.reader(), field);
      final BlockJoinSelector.Type type = order
          ? BlockJoinSelector.Type.MAX
          : BlockJoinSelector.Type.MIN;
      final BitSet parents = parentFilter.getBitSet(context);
      final BitSet children = childFilter.getBitSet(context);
      if (children == null) {
        return DocValues.emptyNumeric();
      }
      return new FilterNumericDocValues(BlockJoinSelector.wrap(sortedNumeric, type, parents, toIter(children))) {
        @Override
        public long longValue() throws IOException {
          // undo the numericutils sortability
          return NumericUtils.sortableFloatBits((int) super.longValue());
        }
      };
    }
  };
}
 
Example #10
Source File: ToParentBlockJoinSortField.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
private FieldComparator<?> getDoubleComparator(int numHits) {
  return new FieldComparator.DoubleComparator(numHits, getField(), (Double) missingValue) {
    @Override
    protected NumericDocValues getNumericDocValues(LeafReaderContext context, String field) throws IOException {
      SortedNumericDocValues sortedNumeric = DocValues.getSortedNumeric(context.reader(), field);
      final BlockJoinSelector.Type type = order
          ? BlockJoinSelector.Type.MAX
          : BlockJoinSelector.Type.MIN;
      final BitSet parents = parentFilter.getBitSet(context);
      final BitSet children = childFilter.getBitSet(context);
      if (children == null) {
        return DocValues.emptyNumeric();
      }
      return new FilterNumericDocValues(BlockJoinSelector.wrap(sortedNumeric, type, parents, toIter(children))) {
        @Override
        public long longValue() throws IOException {
          // undo the numericutils sortability
          return NumericUtils.sortableDoubleBits(super.longValue());
        }
      };
    }
  };
}
 
Example #11
Source File: ToChildBlockJoinQuery.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
@Override
public Scorer scorer(LeafReaderContext readerContext) throws IOException {

  final Scorer parentScorer = in.scorer(readerContext);

  if (parentScorer == null) {
    // No matches
    return null;
  }

  // NOTE: this doesn't take acceptDocs into account, the responsibility
  // to not match deleted docs is on the scorer
  final BitSet parents = parentsFilter.getBitSet(readerContext);
  if (parents == null) {
    // No parents
    return null;
  }

  return new ToChildBlockJoinScorer(this, parentScorer, parents, doScores);
}
 
Example #12
Source File: IncludeNestedDocsQuery.java    From Elasticsearch with Apache License 2.0 6 votes vote down vote up
IncludeNestedDocsScorer(Weight weight, Scorer parentScorer, BitSet parentBits, int currentParentPointer) {
    super(weight);
    this.parentScorer = parentScorer;
    this.parentBits = parentBits;
    this.currentParentPointer = currentParentPointer;
    if (currentParentPointer == 0) {
        currentChildPointer = 0;
    } else {
        this.currentChildPointer = this.parentBits.prevSetBit(currentParentPointer - 1);
        if (currentChildPointer == -1) {
            // no previous set parent, we delete from doc 0
            currentChildPointer = 0;
        } else {
            currentChildPointer++; // we only care about children
        }
    }

    currentDoc = currentChildPointer;
}
 
Example #13
Source File: IncludeNestedDocsQuery.java    From Elasticsearch with Apache License 2.0 6 votes vote down vote up
@Override
public Scorer scorer(LeafReaderContext context) throws IOException {
    final Scorer parentScorer = parentWeight.scorer(context);

    // no matches
    if (parentScorer == null) {
        return null;
    }

    BitSet parents = parentsFilter.getBitSet(context);
    if (parents == null) {
        // No matches
        return null;
    }

    int firstParentDoc = parentScorer.iterator().nextDoc();
    if (firstParentDoc == DocIdSetIterator.NO_MORE_DOCS) {
        // No matches
        return null;
    }
    return new IncludeNestedDocsScorer(this, parentScorer, parents, firstParentDoc);
}
 
Example #14
Source File: TestQueryBitSetProducer.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
public void testReaderNotSuitedForCaching() throws IOException{
  Directory dir = newDirectory();
  IndexWriterConfig iwc = newIndexWriterConfig().setMergePolicy(NoMergePolicy.INSTANCE);
  RandomIndexWriter w = new RandomIndexWriter(random(), dir, iwc);
  w.addDocument(new Document());
  DirectoryReader reader = new DummyDirectoryReader(w.getReader());

  QueryBitSetProducer producer = new QueryBitSetProducer(new MatchNoDocsQuery());
  assertNull(producer.getBitSet(reader.leaves().get(0)));
  assertEquals(0, producer.cache.size());

  producer = new QueryBitSetProducer(new MatchAllDocsQuery());
  BitSet bitSet = producer.getBitSet(reader.leaves().get(0));
  assertEquals(1, bitSet.length());
  assertEquals(true, bitSet.get(0));
  assertEquals(0, producer.cache.size());

  IOUtils.close(reader, w, dir);
}
 
Example #15
Source File: BlockJoinParentQParser.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
@Override
public Weight createWeight(IndexSearcher searcher, org.apache.lucene.search.ScoreMode scoreMode, float boost) throws IOException {
  return new ConstantScoreWeight(BitSetProducerQuery.this, boost) {
    @Override
    public Scorer scorer(LeafReaderContext context) throws IOException {
      BitSet bitSet = bitSetProducer.getBitSet(context);
      if (bitSet == null) {
        return null;
      }
      DocIdSetIterator disi = new BitSetIterator(bitSet, bitSet.approximateCardinality());
      return new ConstantScoreScorer(this, boost, scoreMode, disi);
    }

    @Override
    public boolean isCacheable(LeafReaderContext ctx) {
      return getCache();
    }
  };
}
 
Example #16
Source File: RecoverySourcePruneMergePolicy.java    From crate with Apache License 2.0 6 votes vote down vote up
static CodecReader wrapReader(String recoverySourceField, CodecReader reader, Supplier<Query> retainSourceQuerySupplier)
    throws IOException {
    NumericDocValues recoverySource = reader.getNumericDocValues(recoverySourceField);
    if (recoverySource == null || recoverySource.nextDoc() == DocIdSetIterator.NO_MORE_DOCS) {
        return reader; // early terminate - nothing to do here since non of the docs has a recovery source anymore.
    }
    IndexSearcher s = new IndexSearcher(reader);
    s.setQueryCache(null);
    Weight weight = s.createWeight(s.rewrite(retainSourceQuerySupplier.get()), ScoreMode.COMPLETE_NO_SCORES, 1.0f);
    Scorer scorer = weight.scorer(reader.getContext());
    if (scorer != null) {
        BitSet recoverySourceToKeep = BitSet.of(scorer.iterator(), reader.maxDoc());
        // calculating the cardinality is significantly cheaper than skipping all bulk-merging we might do
        // if retentions are high we keep most of it
        if (recoverySourceToKeep.cardinality() == reader.maxDoc()) {
            return reader; // keep all source
        }
        return new SourcePruningFilterCodecReader(recoverySourceField, reader, recoverySourceToKeep);
    } else {
        return new SourcePruningFilterCodecReader(recoverySourceField, reader, null);
    }
}
 
Example #17
Source File: CheckJoinIndex.java    From lucene-solr with 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 #18
Source File: ToChildBlockJoinQuery.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
public ToChildBlockJoinScorer(Weight weight, Scorer parentScorer, BitSet parentBits, boolean doScores) {
  super(weight);
  this.doScores = doScores;
  this.parentBits = parentBits;
  this.parentScorer = parentScorer;
  this.parentIt = parentScorer.iterator();
}
 
Example #19
Source File: TestIndexedDISI.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
public void testOneDoc() throws IOException {
  int maxDoc = TestUtil.nextInt(random(), 1, 100000);
  BitSet set = new SparseFixedBitSet(maxDoc);
  set.set(random().nextInt(maxDoc));
  try (Directory dir = newDirectory()) {
    doTest(set, dir);
  }
}
 
Example #20
Source File: TestIndexedDISI.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
private void doTestAllSingleJump(BitSet set, Directory dir) throws IOException {
  final int cardinality = set.cardinality();
  final byte denseRankPower = rarely() ? -1 : (byte) (random().nextInt(7)+7); // sane + chance of disable
  long length;
  int jumpTableentryCount;
  try (IndexOutput out = dir.createOutput("foo", IOContext.DEFAULT)) {
    jumpTableentryCount = IndexedDISI.writeBitSet(new BitSetIterator(set, cardinality), out, denseRankPower);
    length = out.getFilePointer();
  }

  try (IndexInput in = dir.openInput("foo", IOContext.DEFAULT)) {
    for (int i = 0; i < set.length(); i++) {
      IndexedDISI disi = new IndexedDISI(in, 0L, length, jumpTableentryCount, denseRankPower, cardinality);
      assertEquals("The bit at " + i + " should be correct with advanceExact", set.get(i), disi.advanceExact(i));

      IndexedDISI disi2 = new IndexedDISI(in, 0L, length, jumpTableentryCount, denseRankPower, cardinality);
      disi2.advance(i);
      // Proper sanity check with jump tables as an error could make them seek backwards
      assertTrue("The docID should at least be " + i + " after advance(" + i + ") but was " + disi2.docID(),
          i <= disi2.docID());
      if (set.get(i)) {
        assertEquals("The docID should be present with advance", i, disi2.docID());
      } else {
        assertNotSame("The docID should not be present with advance", i, disi2.docID());
      }
    }
  }
}
 
Example #21
Source File: BitsetFilterCache.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
@Override
public BitSet getBitSet(LeafReaderContext context) throws IOException {
    try {
        return getAndLoadIfNotPresent(query, context);
    } catch (ExecutionException e) {
        throw ExceptionsHelper.convertToElastic(e);
    }
}
 
Example #22
Source File: TestIndexedDISI.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
private BitSet createSetWithRandomBlocks(int blockCount) {
  final int B = 65536;
  BitSet set = new SparseFixedBitSet(blockCount * B);
  for (int block = 0; block < blockCount; block++) {
    switch (random().nextInt(4)) {
      case 0: { // EMPTY
        break;
      }
      case 1: { // ALL
        for (int docID = block* B; docID < (block+1)* B; docID++) {
          set.set(docID);
        }
        break;
      }
      case 2: { // SPARSE ( < 4096 )
        for (int docID = block* B; docID < (block+1)* B; docID += 101) {
          set.set(docID);
        }
        break;
      }
      case 3: { // DENSE ( >= 4096 )
        for (int docID = block* B; docID < (block+1)* B; docID += 3) {
          set.set(docID);
        }
        break;
      }
      default: throw new IllegalStateException("Modulo logic error: there should only be 4 possibilities");
    }
  }
  return set;
}
 
Example #23
Source File: BlockJoinSelector.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
/** Wraps the provided {@link SortedNumericDocValues} in order to only select
 *  one value per parent among its {@code children} using the configured
 *  {@code selection} type. */
public static NumericDocValues wrap(SortedNumericDocValues sortedNumerics, Type selection, BitSet parents, DocIdSetIterator children) {
  NumericDocValues values;
  switch (selection) {
    case MIN:
      values = SortedNumericSelector.wrap(sortedNumerics, SortedNumericSelector.Type.MIN, SortField.Type.LONG);
      break;
    case MAX:
      values = SortedNumericSelector.wrap(sortedNumerics, SortedNumericSelector.Type.MAX, SortField.Type.LONG);
      break;
    default:
      throw new AssertionError();
  }
  return wrap(values, selection, parents, children);
}
 
Example #24
Source File: BlockJoinSelector.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
/** Wraps the provided {@link NumericDocValues}, iterating over only
 *  child documents, in order to only select one value per parent among
 *  its {@code children} using the configured {@code selection} type. */
public static NumericDocValues wrap(final NumericDocValues values, Type selection, BitSet parents, DocIdSetIterator children) {
  if (values.docID() != -1) {
    throw new IllegalArgumentException("values iterator was already consumed: values.docID=" + values.docID());
  }
  return ToParentDocValues.wrap(values,selection, parents, children);
}
 
Example #25
Source File: BlockJoinSelector.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
/** Wraps the provided {@link SortedDocValues} in order to only select
 *  one value per parent among its {@code children} using the configured
 *  {@code selection} type. */
public static SortedDocValues wrap(final SortedDocValues values, Type selection, BitSet parents, DocIdSetIterator children) {
  if (values.docID() != -1) {
    throw new IllegalArgumentException("values iterator was already consumed: values.docID=" + values.docID());
  }
  return ToParentDocValues.wrap(values, selection, parents, children);
}
 
Example #26
Source File: TestBlockJoinSelector.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
public void testSortedSelector() throws IOException {
  final BitSet parents = new FixedBitSet(20);
  parents.set(0);
  parents.set(5);
  parents.set(6);
  parents.set(10);
  parents.set(15);
  parents.set(19);

  final BitSet children = new FixedBitSet(20);
  children.set(2);
  children.set(3);
  children.set(4);
  children.set(12);
  children.set(17);

  final int[] ords = new int[20];
  Arrays.fill(ords, -1);
  ords[2] = 5;
  ords[3] = 7;
  ords[4] = 3;
  ords[12] = 10;
  ords[18] = 10;

  final SortedDocValues mins = BlockJoinSelector.wrap(DocValues.singleton(new CannedSortedDocValues(ords)), BlockJoinSelector.Type.MIN, parents, toIter(children));
  assertEquals(5, nextDoc(mins,5));
  assertEquals(3, mins.ordValue());
  assertEquals(15, nextDoc(mins,15));
  assertEquals(10, mins.ordValue());
  assertNoMoreDoc(mins, 20);

  final SortedDocValues maxs = BlockJoinSelector.wrap(DocValues.singleton(new CannedSortedDocValues(ords)), BlockJoinSelector.Type.MAX, parents, toIter(children));
  assertEquals(5, nextDoc(maxs,5));
  assertEquals(7, maxs.ordValue());
  assertEquals(15, nextDoc(maxs,15));
  assertEquals(10, maxs.ordValue());
  assertNoMoreDoc( maxs,20);
}
 
Example #27
Source File: FetchPhase.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
private int findRootDocumentIfNested(SearchContext context, LeafReaderContext subReaderContext, int subDocId) throws IOException {
    if (context.mapperService().hasNested()) {
        BitSet bits = context.bitsetFilterCache().getBitSetProducer(Queries.newNonNestedFilter()).getBitSet(subReaderContext);
        if (!bits.get(subDocId)) {
            return bits.nextSetBit(subDocId);
        }
    }
    return -1;
}
 
Example #28
Source File: TestJoinUtil.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
private BitSet createExpectedResult(String queryValue, boolean from, IndexReader topLevelReader, IndexIterationContext context) throws IOException {
  final Map<String, List<RandomDoc>> randomValueDocs;
  final Map<String, List<RandomDoc>> linkValueDocuments;
  if (from) {
    randomValueDocs = context.randomValueFromDocs;
    linkValueDocuments = context.toDocuments;
  } else {
    randomValueDocs = context.randomValueToDocs;
    linkValueDocuments = context.fromDocuments;
  }

  BitSet expectedResult = new FixedBitSet(topLevelReader.maxDoc());
  List<RandomDoc> matchingDocs = randomValueDocs.get(queryValue);
  if (matchingDocs == null) {
    return new FixedBitSet(topLevelReader.maxDoc());
  }

  for (RandomDoc matchingDoc : matchingDocs) {
    for (String linkValue : matchingDoc.linkValues) {
      List<RandomDoc> otherMatchingDocs = linkValueDocuments.get(linkValue);
      if (otherMatchingDocs == null) {
        continue;
      }

      for (RandomDoc otherSideDoc : otherMatchingDocs) {
        PostingsEnum postingsEnum = MultiTerms.getTermPostingsEnum(topLevelReader, "id", new BytesRef(otherSideDoc.id), 0);
        assert postingsEnum != null;
        int doc = postingsEnum.nextDoc();
        expectedResult.set(doc);
      }
    }
  }
  return expectedResult;
}
 
Example #29
Source File: TestBlockJoinSelector.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
public void testDocsWithValue() {
  final BitSet parents = new FixedBitSet(20);
  parents.set(0);
  parents.set(5);
  parents.set(6);
  parents.set(10);
  parents.set(15);
  parents.set(19);

  final BitSet children = new FixedBitSet(20);
  children.set(2);
  children.set(3);
  children.set(4);
  children.set(12);
  children.set(17);

  final BitSet childDocsWithValue = new FixedBitSet(20);
  childDocsWithValue.set(2);
  childDocsWithValue.set(3);
  childDocsWithValue.set(4);
  childDocsWithValue.set(8);
  childDocsWithValue.set(16);

  final Bits docsWithValue = BlockJoinSelector.wrap(childDocsWithValue, parents, children);
  assertFalse(docsWithValue.get(0));
  assertTrue(docsWithValue.get(5));
  assertFalse(docsWithValue.get(6));
  assertFalse(docsWithValue.get(10));
  assertFalse(docsWithValue.get(15));
  assertFalse(docsWithValue.get(19));
}
 
Example #30
Source File: TestBlockJoin.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
private Document getParentDoc(IndexReader reader, BitSetProducer parents, int childDocID) throws IOException {
  final List<LeafReaderContext> leaves = reader.leaves();
  final int subIndex = ReaderUtil.subIndex(childDocID, leaves);
  final LeafReaderContext leaf = leaves.get(subIndex);
  final BitSet bits = parents.getBitSet(leaf);
  return leaf.reader().document(bits.nextSetBit(childDocID - leaf.docBase));
}