Java Code Examples for org.apache.lucene.util.BitSet

The following examples show how to use org.apache.lucene.util.BitSet. 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: Elasticsearch   Source File: IncludeNestedDocsQuery.java    License: 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 2
Source Project: Elasticsearch   Source File: IncludeNestedDocsQuery.java    License: 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 3
Source Project: lucene-solr   Source File: ToParentBlockJoinQuery.java    License: 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 4
Source Project: lucene-solr   Source File: ToParentBlockJoinSortField.java    License: 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 5
Source Project: lucene-solr   Source File: ToParentBlockJoinSortField.java    License: 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 6
Source Project: lucene-solr   Source File: ToParentBlockJoinSortField.java    License: 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 7
Source Project: lucene-solr   Source File: ToParentBlockJoinSortField.java    License: 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 8
Source Project: lucene-solr   Source File: ToParentBlockJoinSortField.java    License: 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 9
Source Project: lucene-solr   Source File: ToChildBlockJoinQuery.java    License: 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 10
Source Project: lucene-solr   Source File: TestQueryBitSetProducer.java    License: 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 11
Source Project: lucene-solr   Source File: TestQueryBitSetProducer.java    License: 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 12
Source Project: lucene-solr   Source File: ConjunctionDISI.java    License: 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 13
Source Project: lucene-solr   Source File: TestIndexedDISI.java    License: 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 14
Source Project: lucene-solr   Source File: TestIndexedDISI.java    License: 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 15
Source Project: lucene-solr   Source File: BlockJoinParentQParser.java    License: 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 Project: crate   Source File: RecoverySourcePruneMergePolicy.java    License: 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 Project: Elasticsearch   Source File: FetchPhase.java    License: 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 18
Source Project: Elasticsearch   Source File: OrdinalsBuilder.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Builds a {@link BitSet} where each documents bit is that that has one or more ordinals associated with it.
 * if every document has an ordinal associated with it this method returns <code>null</code>
 */
public BitSet buildDocsWithValuesSet() {
    if (numDocsWithValue == maxDoc) {
        return null;
    }
    final FixedBitSet bitSet = new FixedBitSet(maxDoc);
    for (int docID = 0; docID < maxDoc; ++docID) {
        if (ordinals.firstOrdinals.get(docID) != 0) {
            bitSet.set(docID);
        }
    }
    return bitSet;
}
 
Example 19
Source Project: Elasticsearch   Source File: BitsetFilterCache.java    License: Apache License 2.0 5 votes vote down vote up
private BitSet getAndLoadIfNotPresent(final Query query, final LeafReaderContext context) throws IOException, ExecutionException {
    final Object coreCacheReader = context.reader().getCoreCacheKey();
    final ShardId shardId = ShardUtils.extractShardId(context.reader());
    if (shardId != null // can't require it because of the percolator
            && index.getName().equals(shardId.getIndex()) == false) {
        // insanity
        throw new IllegalStateException("Trying to load bit set for index [" + shardId.getIndex()
                + "] with cache of index [" + index.getName() + "]");
    }
    Cache<Query, Value> filterToFbs = loadedFilters.get(coreCacheReader, new Callable<Cache<Query, Value>>() {
        @Override
        public Cache<Query, Value> call() throws Exception {
            context.reader().addCoreClosedListener(BitsetFilterCache.this);
            return CacheBuilder.newBuilder().build();
        }
    });
    return filterToFbs.get(query,new Callable<Value>() {
        @Override
        public Value call() throws Exception {
            final IndexReaderContext topLevelContext = ReaderUtil.getTopLevelContext(context);
            final IndexSearcher searcher = new IndexSearcher(topLevelContext);
            searcher.setQueryCache(null);
            final Weight weight = searcher.createNormalizedWeight(query, false);
            final Scorer s = weight.scorer(context);
            final BitSet bitSet;
            if (s == null) {
                bitSet = null;
            } else {
                bitSet = BitSet.of(s.iterator(), context.reader().maxDoc());
            }

            Value value = new Value(bitSet, shardId);
            listener.onCache(shardId, value.bitset);
            return value;
        }
    }).bitset;
}
 
Example 20
Source Project: Elasticsearch   Source File: BitsetFilterCache.java    License: 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 21
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 22
Source Project: lucene-solr   Source File: ToParentBlockJoinQuery.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public ScorerSupplier scorerSupplier(LeafReaderContext context) throws IOException {
  final ScorerSupplier childScorerSupplier = in.scorerSupplier(context);
  if (childScorerSupplier == null) {
    return null;
  }

  // NOTE: this does not take accept docs into account, the responsibility
  // to not match deleted docs is on the scorer
  final BitSet parents = parentsFilter.getBitSet(context);
  if (parents == null) {
    // No matches
    return null;
  }

  return new ScorerSupplier() {

    @Override
    public Scorer get(long leadCost) throws IOException {
      return new BlockJoinScorer(BlockJoinWeight.this, childScorerSupplier.get(leadCost), parents, scoreMode);
    }

    @Override
    public long cost() {
      return childScorerSupplier.cost();
    }
  };
}
 
Example 23
Source Project: lucene-solr   Source File: BlockJoinSelector.java    License: Apache License 2.0 5 votes vote down vote up
/** Return a {@link Bits} instance that returns true if, and only if, any of
 *  the children of the given parent document has a value. */
public static Bits wrap(final Bits docsWithValue, BitSet parents, BitSet children) {
  return new Bits() {

    @Override
    public boolean get(int docID) {
      assert parents.get(docID) : "this selector may only be used on parent documents";

      if (docID == 0) {
        // no children
        return false;
      }

      final int firstChild = parents.prevSetBit(docID - 1) + 1;
      for (int child = children.nextSetBit(firstChild); child < docID; child = children.nextSetBit(child + 1)) {
        if (docsWithValue.get(child)) {
          return true;
        }
      }
      return false;
    }

    @Override
    public int length() {
      return docsWithValue.length();
    }

  };
}
 
Example 24
Source Project: lucene-solr   Source File: BlockJoinSelector.java    License: Apache License 2.0 5 votes vote down vote up
/** Wraps the provided {@link SortedSetDocValues} in order to only select
 *  one value per parent among its {@code children} using the configured
 *  {@code selection} type. */
public static SortedDocValues wrap(SortedSetDocValues sortedSet, Type selection, BitSet parents, DocIdSetIterator children) {
  SortedDocValues values;
  switch (selection) {
    case MIN:
      values = SortedSetSelector.wrap(sortedSet, SortedSetSelector.Type.MIN);
      break;
    case MAX:
      values = SortedSetSelector.wrap(sortedSet, SortedSetSelector.Type.MAX);
      break;
    default:
      throw new AssertionError();
  }
  return wrap(values, selection, parents, children);
}
 
Example 25
Source Project: lucene-solr   Source File: BlockJoinSelector.java    License: 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 Project: lucene-solr   Source File: BlockJoinSelector.java    License: 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 27
Source Project: lucene-solr   Source File: BlockJoinSelector.java    License: 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 28
Source Project: lucene-solr   Source File: ToChildBlockJoinQuery.java    License: 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 29
Source Project: lucene-solr   Source File: TestBlockJoin.java    License: 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));
}
 
Example 30
Source Project: lucene-solr   Source File: TestJoinUtil.java    License: 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;
}