Java Code Examples for org.apache.lucene.index.ReaderUtil

The following examples show how to use org.apache.lucene.index.ReaderUtil. 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: ScoreDocRowFunction.java    License: Apache License 2.0 6 votes vote down vote up
@Nullable
@Override
public Row apply(@Nullable ScoreDoc input) {
    if (input == null) {
        return null;
    }
    FieldDoc fieldDoc = (FieldDoc) input;
    scorer.score(fieldDoc.score);
    for (OrderByCollectorExpression orderByCollectorExpression : orderByCollectorExpressions) {
        orderByCollectorExpression.setNextFieldDoc(fieldDoc);
    }
    List<LeafReaderContext> leaves = indexReader.leaves();
    int readerIndex = ReaderUtil.subIndex(fieldDoc.doc, leaves);
    LeafReaderContext subReaderContext = leaves.get(readerIndex);
    int subDoc = fieldDoc.doc - subReaderContext.docBase;
    for (LuceneCollectorExpression<?> expression : expressions) {
        expression.setNextReader(subReaderContext);
        expression.setNextDocId(subDoc);
    }
    return inputRow;
}
 
Example 2
Source Project: lucene-solr   Source File: TestGeo3DPoint.java    License: Apache License 2.0 6 votes vote down vote up
public static String explain(String fieldName, GeoShape shape, GeoPoint targetDocPoint, GeoPoint scaledDocPoint, IndexReader reader, int docID) throws Exception {

    final XYZBounds bounds = new XYZBounds();
    shape.getBounds(bounds);
    
    // First find the leaf reader that owns this doc:
    int subIndex = ReaderUtil.subIndex(docID, reader.leaves());
    LeafReader leafReader = reader.leaves().get(subIndex).reader();

    StringBuilder b = new StringBuilder();
    b.append("target is in leaf " + leafReader + " of full reader " + reader + "\n");

    DocIdSetBuilder hits = new DocIdSetBuilder(leafReader.maxDoc());
    ExplainingVisitor visitor = new ExplainingVisitor(shape, targetDocPoint, scaledDocPoint,
      new PointInShapeIntersectVisitor(hits, shape, bounds),
      docID - reader.leaves().get(subIndex).docBase, 3, Integer.BYTES, b);

    // Do first phase, where we just figure out the "path" that leads to the target docID:
    leafReader.getPointValues(fieldName).intersect(visitor);

    // Do second phase, where we we see how the wrapped visitor responded along that path:
    visitor.startSecondPhase();
    leafReader.getPointValues(fieldName).intersect(visitor);

    return b.toString();
  }
 
Example 3
Source Project: lucene-solr   Source File: DocumentValueSourceDictionary.java    License: Apache License 2.0 6 votes vote down vote up
/** 
 * Returns the weight for the current <code>docId</code> as computed 
 * by the <code>weightsValueSource</code>
 * */
@Override
protected long getWeight(Document doc, int docId) throws IOException {
  if (currentWeightValues == null) {
    return 0;
  }
  int subIndex = ReaderUtil.subIndex(docId, starts);
  if (subIndex != currentLeafIndex) {
    currentLeafIndex = subIndex;
    currentWeightValues = weightsValueSource.getValues(leaves.get(currentLeafIndex), null);
  }
  if (currentWeightValues.advanceExact(docId - starts[subIndex]))
    return currentWeightValues.longValue();
  else
    return 0;

}
 
Example 4
Source Project: lucene-solr   Source File: TestMatchesIterator.java    License: Apache License 2.0 6 votes vote down vote up
private void checkMatches(Query q, String field, int[][] expected) throws IOException {
  Weight w = searcher.createWeight(searcher.rewrite(q), ScoreMode.COMPLETE, 1);
  for (int i = 0; i < expected.length; i++) {
    LeafReaderContext ctx = searcher.leafContexts.get(ReaderUtil.subIndex(expected[i][0], searcher.leafContexts));
    int doc = expected[i][0] - ctx.docBase;
    Matches matches = w.matches(ctx, doc);
    if (matches == null) {
      assertEquals(expected[i].length, 1);
      continue;
    }
    MatchesIterator it = matches.getMatches(field);
    if (expected[i].length == 1) {
      assertNull(it);
      continue;
    }
    checkFieldMatches(it, expected[i]);
    checkFieldMatches(matches.getMatches(field), expected[i]);  // test multiple calls
  }
}
 
Example 5
Source Project: lucene-solr   Source File: TestMatchesIterator.java    License: Apache License 2.0 6 votes vote down vote up
private void checkLabelCount(Query q, String field, int[] expected) throws IOException {
  Weight w = searcher.createWeight(searcher.rewrite(q), ScoreMode.COMPLETE, 1);
  for (int i = 0; i < expected.length; i++) {
    LeafReaderContext ctx = searcher.leafContexts.get(ReaderUtil.subIndex(i, searcher.leafContexts));
    int doc = i - ctx.docBase;
    Matches matches = w.matches(ctx, doc);
    if (matches == null) {
      assertEquals("Expected to get matches on document " + i, 0, expected[i]);
      continue;
    }
    MatchesIterator it = matches.getMatches(field);
    if (expected[i] == 0) {
      assertNull(it);
      continue;
    }
    else {
      assertNotNull(it);
    }
    IdentityHashMap<Query, Integer> labels = new IdentityHashMap<>();
    while (it.next()) {
      labels.put(it.getQuery(), 1);
    }
    assertEquals(expected[i], labels.size());
  }
}
 
Example 6
Source Project: lucene-solr   Source File: TestMatchesIterator.java    License: Apache License 2.0 6 votes vote down vote up
private void checkSubMatches(Query q, String[][] expectedNames) throws IOException {
  Weight w = searcher.createWeight(searcher.rewrite(q), ScoreMode.COMPLETE_NO_SCORES, 1);
  for (int i = 0; i < expectedNames.length; i++) {
    LeafReaderContext ctx = searcher.leafContexts.get(ReaderUtil.subIndex(i, searcher.leafContexts));
    int doc = i - ctx.docBase;
    Matches matches = w.matches(ctx, doc);
    if (matches == null) {
      assertEquals("Expected to get no matches on document " + i, 0, expectedNames[i].length);
      continue;
    }
    Set<String> expectedQueries = new HashSet<>(Arrays.asList(expectedNames[i]));
    Set<String> actualQueries = NamedMatches.findNamedMatches(matches)
        .stream().map(NamedMatches::getName).collect(Collectors.toSet());

    Set<String> unexpected = new HashSet<>(actualQueries);
    unexpected.removeAll(expectedQueries);
    assertEquals("Unexpected matching leaf queries: " + unexpected, 0, unexpected.size());
    Set<String> missing = new HashSet<>(expectedQueries);
    missing.removeAll(actualQueries);
    assertEquals("Missing matching leaf queries: " + missing, 0, missing.size());
  }
}
 
Example 7
Source Project: lucene-solr   Source File: TestMatchesIterator.java    License: Apache License 2.0 6 votes vote down vote up
private void assertIsLeafMatch(Query q, String field) throws IOException {
  Weight w = searcher.createWeight(searcher.rewrite(q), ScoreMode.COMPLETE, 1);
  for (int i = 0; i < searcher.reader.maxDoc(); i++) {
    LeafReaderContext ctx = searcher.leafContexts.get(ReaderUtil.subIndex(i, searcher.leafContexts));
    int doc = i - ctx.docBase;
    Matches matches = w.matches(ctx, doc);
    if (matches == null) {
      return;
    }
    MatchesIterator mi = matches.getMatches(field);
    if (mi == null) {
      return;
    }
    while (mi.next()) {
      assertNull(mi.getSubMatches());
    }
  }
}
 
Example 8
Source Project: lucene-solr   Source File: TestMatchesIterator.java    License: Apache License 2.0 6 votes vote down vote up
private void checkTermMatches(Query q, String field, TermMatch[][][] expected) throws IOException {
  Weight w = searcher.createWeight(searcher.rewrite(q), ScoreMode.COMPLETE, 1);
  for (int i = 0; i < expected.length; i++) {
    LeafReaderContext ctx = searcher.leafContexts.get(ReaderUtil.subIndex(i, searcher.leafContexts));
    int doc = i - ctx.docBase;
    Matches matches = w.matches(ctx, doc);
    if (matches == null) {
      assertEquals(expected[i].length, 0);
      continue;
    }
    MatchesIterator it = matches.getMatches(field);
    if (expected[i].length == 0) {
      assertNull(it);
      continue;
    }
    checkTerms(expected[i], it);
  }
}
 
Example 9
Source Project: lucene-solr   Source File: TestMatchesIterator.java    License: Apache License 2.0 6 votes vote down vote up
public void testMultipleFields() throws IOException {
  Query q = new BooleanQuery.Builder()
      .add(new TermQuery(new Term("id", "1")), BooleanClause.Occur.SHOULD)
      .add(new TermQuery(new Term(FIELD_WITH_OFFSETS, "w3")), BooleanClause.Occur.MUST)
      .build();
  Weight w = searcher.createWeight(searcher.rewrite(q), ScoreMode.COMPLETE, 1);

  LeafReaderContext ctx = searcher.leafContexts.get(ReaderUtil.subIndex(1, searcher.leafContexts));
  Matches m = w.matches(ctx, 1 - ctx.docBase);
  assertNotNull(m);
  checkFieldMatches(m.getMatches("id"), new int[]{ -1, 0, 0, -1, -1 });
  checkFieldMatches(m.getMatches(FIELD_WITH_OFFSETS), new int[]{ -1, 1, 1, 3, 5, 3, 3, 9, 11 });
  assertNull(m.getMatches("bogus"));

  Set<String> fields = new HashSet<>();
  for (String field : m) {
    fields.add(field);
  }
  assertEquals(2, fields.size());
  assertTrue(fields.contains(FIELD_WITH_OFFSETS));
  assertTrue(fields.contains("id"));

  assertEquals(2, AssertingMatches.unWrap(m).getSubMatches().size());
}
 
Example 10
Source Project: lucene-solr   Source File: LTRRescorer.java    License: Apache License 2.0 6 votes vote down vote up
public static LTRScoringQuery.FeatureInfo[] extractFeaturesInfo(LTRScoringQuery.ModelWeight modelWeight,
    int docid,
    Float originalDocScore,
    List<LeafReaderContext> leafContexts)
        throws IOException {
  final int n = ReaderUtil.subIndex(docid, leafContexts);
  final LeafReaderContext atomicContext = leafContexts.get(n);
  final int deBasedDoc = docid - atomicContext.docBase;
  final LTRScoringQuery.ModelWeight.ModelScorer r = modelWeight.scorer(atomicContext);
  if ( (r == null) || (r.iterator().advance(deBasedDoc) != deBasedDoc) ) {
    return new LTRScoringQuery.FeatureInfo[0];
  } else {
    if (originalDocScore != null) {
      // If results have not been reranked, the score passed in is the original query's
      // score, which some features can use instead of recalculating it
      r.getDocInfo().setOriginalDocScore(originalDocScore);
    }
    r.score();
    return modelWeight.getFeaturesInfo();
  }
}
 
Example 11
Source Project: lucene-solr   Source File: TestSelectiveWeightCreation.java    License: Apache License 2.0 6 votes vote down vote up
private LTRScoringQuery.ModelWeight performQuery(TopDocs hits,
    IndexSearcher searcher, int docid, LTRScoringQuery model) throws IOException,
    ModelException {
  final List<LeafReaderContext> leafContexts = searcher.getTopReaderContext()
      .leaves();
  final int n = ReaderUtil.subIndex(hits.scoreDocs[0].doc, leafContexts);
  final LeafReaderContext context = leafContexts.get(n);
  final int deBasedDoc = hits.scoreDocs[0].doc - context.docBase;

  final Weight weight = searcher.createWeight(searcher.rewrite(model), ScoreMode.COMPLETE, 1);
  final Scorer scorer = weight.scorer(context);

  // rerank using the field final-score
  scorer.iterator().advance(deBasedDoc);
  scorer.score();
  assertTrue(weight instanceof LTRScoringQuery.ModelWeight);
  final LTRScoringQuery.ModelWeight modelWeight = (LTRScoringQuery.ModelWeight) weight;
  return modelWeight;

}
 
Example 12
Source Project: lucene-solr   Source File: TestLTRScoringQuery.java    License: Apache License 2.0 6 votes vote down vote up
private LTRScoringQuery.ModelWeight performQuery(TopDocs hits,
    IndexSearcher searcher, int docid, LTRScoringQuery model) throws IOException,
    ModelException {
  final List<LeafReaderContext> leafContexts = searcher.getTopReaderContext()
      .leaves();
  final int n = ReaderUtil.subIndex(hits.scoreDocs[0].doc, leafContexts);
  final LeafReaderContext context = leafContexts.get(n);
  final int deBasedDoc = hits.scoreDocs[0].doc - context.docBase;

  final Weight weight = searcher.createWeight(searcher.rewrite(model), ScoreMode.COMPLETE, 1);
  final Scorer scorer = weight.scorer(context);

  // rerank using the field final-score
  scorer.iterator().advance(deBasedDoc);
  scorer.score();

  // assertEquals(42.0f, score, 0.0001);
  // assertTrue(weight instanceof AssertingWeight);
  // (AssertingIndexSearcher)
  assertTrue(weight instanceof LTRScoringQuery.ModelWeight);
  final LTRScoringQuery.ModelWeight modelWeight = (LTRScoringQuery.ModelWeight) weight;
  return modelWeight;

}
 
Example 13
Source Project: lucene-solr   Source File: ValueSourceAugmenter.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public void transform(SolrDocument doc, int docid) {
  // This is only good for random-access functions

  try {

    // TODO: calculate this stuff just once across diff functions
    int idx = ReaderUtil.subIndex(docid, readerContexts);
    LeafReaderContext rcontext = readerContexts.get(idx);
    @SuppressWarnings({"unchecked"})
    FunctionValues values = valueSource.getValues(fcontext, rcontext);
    int localId = docid - rcontext.docBase;
    setValue(doc,values.objectVal(localId));
  } catch (IOException e) {
    throw new SolrException(SolrException.ErrorCode.SERVER_ERROR, "exception at docid " + docid + " for valuesource " + valueSource, e);
  }
}
 
Example 14
Source Project: lucene-solr   Source File: TaggerRequestHandler.java    License: Apache License 2.0 6 votes vote down vote up
@SuppressWarnings({"unchecked"})
Object objectVal(int topDocId) throws IOException {
  // lookup segment level stuff:
  int segIdx = ReaderUtil.subIndex(topDocId, readerContexts);
  LeafReaderContext rcontext = readerContexts.get(segIdx);
  int segDocId = topDocId - rcontext.docBase;
  // unfortunately Lucene 7.0 requires forward only traversal (with no reset method).
  //   So we need to track our last docId (per segment) and re-fetch the FunctionValues. :-(
  FunctionValues functionValues = functionValuesPerSeg[segIdx];
  if (functionValues == null || segDocId < functionValuesDocIdPerSeg[segIdx]) {
    functionValues = functionValuesPerSeg[segIdx] = valueSource.getValues(fContext, rcontext);
  }
  functionValuesDocIdPerSeg[segIdx] = segDocId;

  // get value:
  return functionValues.objectVal(segDocId);
}
 
Example 15
Source Project: SolrTextTagger   Source File: TaggerRequestHandler.java    License: Apache License 2.0 6 votes vote down vote up
Object objectVal(int topDocId) throws IOException {
  // lookup segment level stuff:
  int segIdx = ReaderUtil.subIndex(topDocId, readerContexts);
  LeafReaderContext rcontext = readerContexts.get(segIdx);
  int segDocId = topDocId - rcontext.docBase;
  // unfortunately Lucene 7.0 requires forward only traversal (with no reset method).
  //   So we need to track our last docId (per segment) and re-fetch the FunctionValues. :-(
  FunctionValues functionValues = functionValuesPerSeg[segIdx];
  if (functionValues == null || segDocId < functionValuesDocIdPerSeg[segIdx]) {
    functionValues = functionValuesPerSeg[segIdx] = valueSource.getValues(fContext, rcontext);
  }
  functionValuesDocIdPerSeg[segIdx] = segDocId;

  // get value:
  return functionValues.objectVal(segDocId);
}
 
Example 16
Source Project: crate   Source File: ScoreDocRowFunction.java    License: Apache License 2.0 6 votes vote down vote up
@Nullable
@Override
public Row apply(@Nullable ScoreDoc input) {
    onScoreDoc.run();
    if (input == null) {
        return null;
    }
    FieldDoc fieldDoc = (FieldDoc) input;
    scorer.score(fieldDoc.score);
    for (int i = 0; i < orderByCollectorExpressions.size(); i++) {
        orderByCollectorExpressions.get(i).setNextFieldDoc(fieldDoc);
    }
    List<LeafReaderContext> leaves = indexReader.leaves();
    int readerIndex = ReaderUtil.subIndex(fieldDoc.doc, leaves);
    LeafReaderContext subReaderContext = leaves.get(readerIndex);
    int subDoc = fieldDoc.doc - subReaderContext.docBase;
    for (LuceneCollectorExpression<?> expression : expressions) {
        try {
            expression.setNextReader(subReaderContext);
            expression.setNextDocId(subDoc);
        } catch (IOException e) {
            throw new UncheckedIOException(e);
        }
    }
    return inputRow;
}
 
Example 17
Source Project: crate   Source File: ReservoirSampler.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public Object[] apply(Integer docId) {
    List<LeafReaderContext> leaves = searcher.reader().leaves();
    int readerIndex = ReaderUtil.subIndex(docId, leaves);
    LeafReaderContext leafContext = leaves.get(readerIndex);
    int subDoc = docId - leafContext.docBase;
    for (LuceneCollectorExpression<?> expression : expressions) {
        try {
            expression.setNextReader(leafContext);
            expression.setNextDocId(subDoc);
        } catch (IOException e) {
            throw new UncheckedIOException(e);
        }
    }
    Object[] cells = new Object[inputs.size()];
    for (int i = 0; i < cells.length; i++) {
        cells[i] = inputs.get(i).value();
    }
    return cells;
}
 
Example 18
Source Project: Elasticsearch   Source File: FetchCollector.java    License: Apache License 2.0 5 votes vote down vote up
public void collect(IntContainer docIds, StreamBucket.Builder builder) throws IOException {
    for (IntCursor cursor : docIds) {
        final int docId = cursor.value;
        int readerIndex = ReaderUtil.subIndex(docId, readerContexts);
        LeafReaderContext subReaderContext = readerContexts.get(readerIndex);
        setNextReader(subReaderContext);
        setNextDocId(docId - subReaderContext.docBase);
        builder.add(row);
    }
}
 
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: lucene-solr   Source File: JoinDocFreqValueSource.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public FunctionValues getValues(Map<Object, Object> context, LeafReaderContext readerContext) throws IOException
{
  final BinaryDocValues terms = DocValues.getBinary(readerContext.reader(), field);
  final IndexReader top = ReaderUtil.getTopLevelContext(readerContext).reader();
  Terms t = MultiTerms.getTerms(top, qfield);
  final TermsEnum termsEnum = t == null ? TermsEnum.EMPTY : t.iterator();
  
  return new IntDocValues(this) {

    int lastDocID = -1;

    @Override
    public int intVal(int doc) throws IOException {
      if (doc < lastDocID) {
        throw new IllegalArgumentException("docs were sent out-of-order: lastDocID=" + lastDocID + " vs docID=" + doc);
      }
      lastDocID = doc;
      int curDocID = terms.docID();
      if (doc > curDocID) {
        curDocID = terms.advance(doc);
      }
      if (doc == curDocID) {
        BytesRef term = terms.binaryValue();
        if (termsEnum.seekExact(term)) {
          return termsEnum.docFreq();
        }
      }
      return 0;
    }
  };
}
 
Example 21
Source Project: lucene-solr   Source File: ScaleFloatFunction.java    License: Apache License 2.0 5 votes vote down vote up
private ScaleInfo createScaleInfo(Map<Object, Object> context, LeafReaderContext readerContext) throws IOException {
  final List<LeafReaderContext> leaves = ReaderUtil.getTopLevelContext(readerContext).leaves();

  float minVal = Float.POSITIVE_INFINITY;
  float maxVal = Float.NEGATIVE_INFINITY;

  for (LeafReaderContext leaf : leaves) {
    int maxDoc = leaf.reader().maxDoc();
    FunctionValues vals =  source.getValues(context, leaf);
    for (int i=0; i<maxDoc; i++) {
      if ( ! vals.exists(i) ) {
        continue;
      }
      float val = vals.floatVal(i);
      if ((Float.floatToRawIntBits(val) & (0xff<<23)) == 0xff<<23) {
        // if the exponent in the float is all ones, then this is +Inf, -Inf or NaN
        // which don't make sense to factor into the scale function
        continue;
      }
      if (val < minVal) {
        minVal = val;
      }
      if (val > maxVal) {
        maxVal = val;
      }
    }
  }

  if (minVal == Float.POSITIVE_INFINITY) {
  // must have been an empty index
    minVal = maxVal = 0;
  }

  ScaleInfo scaleInfo = new ScaleInfo();
  scaleInfo.minVal = minVal;
  scaleInfo.maxVal = maxVal;
  context.put(ScaleFloatFunction.this, scaleInfo);
  return scaleInfo;
}
 
Example 22
Source Project: lucene-solr   Source File: TestIntervals.java    License: Apache License 2.0 5 votes vote down vote up
private void assertGaps(IntervalsSource source, int doc, String field, int[] expectedGaps) throws IOException {
  int ord = ReaderUtil.subIndex(doc, searcher.getIndexReader().leaves());
  LeafReaderContext ctx = searcher.getIndexReader().leaves().get(ord);
  IntervalIterator it = source.intervals(field, ctx);
  doc = doc - ctx.docBase;
  assertEquals(doc, it.advance(doc));
  for (int expectedGap : expectedGaps) {
    if (it.nextInterval() == IntervalIterator.NO_MORE_INTERVALS) {
      fail("Unexpected interval " + it);
    }
    assertEquals(expectedGap, it.gaps());
  }
}
 
Example 23
Source Project: lucene-solr   Source File: ExpressionRescorer.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public Explanation explain(IndexSearcher searcher, Explanation firstPassExplanation, int docID) throws IOException {
  Explanation superExpl = super.explain(searcher, firstPassExplanation, docID);

  List<LeafReaderContext> leaves = searcher.getIndexReader().leaves();
  int subReader = ReaderUtil.subIndex(docID, leaves);
  LeafReaderContext readerContext = leaves.get(subReader);
  int docIDInSegment = docID - readerContext.docBase;

  return expression.getDoubleValuesSource(bindings).explain(readerContext, docIDInSegment, superExpl);
}
 
Example 24
Source Project: lucene-solr   Source File: TestGrouping.java    License: Apache License 2.0 5 votes vote down vote up
private void verifyShards(int[] docStarts, TopGroups<BytesRef> topGroups) {
  for(GroupDocs<?> group : topGroups.groups) {
    for(int hitIDX=0;hitIDX<group.scoreDocs.length;hitIDX++) {
      final ScoreDoc sd = group.scoreDocs[hitIDX];
      assertEquals("doc=" + sd.doc + " wrong shard",
                   ReaderUtil.subIndex(sd.doc, docStarts),
                   sd.shardIndex);
    }
  }
}
 
Example 25
Source Project: lucene-solr   Source File: SortedSetDocValuesFacetCounts.java    License: Apache License 2.0 5 votes vote down vote up
/** Does all the "real work" of tallying up the counts. */
private final void count(List<MatchingDocs> matchingDocs) throws IOException {
  //System.out.println("ssdv count");

  OrdinalMap ordinalMap;

  // TODO: is this right?  really, we need a way to
  // verify that this ordinalMap "matches" the leaves in
  // matchingDocs...
  if (dv instanceof MultiDocValues.MultiSortedSetDocValues && matchingDocs.size() > 1) {
    ordinalMap = ((MultiSortedSetDocValues) dv).mapping;
  } else {
    ordinalMap = null;
  }
  
  IndexReader reader = state.getReader();

  for(MatchingDocs hits : matchingDocs) {

    // LUCENE-5090: make sure the provided reader context "matches"
    // the top-level reader passed to the
    // SortedSetDocValuesReaderState, else cryptic
    // AIOOBE can happen:
    if (ReaderUtil.getTopLevelContext(hits.context).reader() != reader) {
      throw new IllegalStateException("the SortedSetDocValuesReaderState provided to this class does not match the reader being searched; you must create a new SortedSetDocValuesReaderState every time you open a new IndexReader");
    }

    countOneSegment(ordinalMap, hits.context.reader(), hits.context.ord, hits);
  }
}
 
Example 26
/** Does all the "real work" of tallying up the counts. */
private final void count(List<MatchingDocs> matchingDocs) throws IOException, InterruptedException {

  OrdinalMap ordinalMap;

  // TODO: is this right?  really, we need a way to
  // verify that this ordinalMap "matches" the leaves in
  // matchingDocs...
  if (dv instanceof MultiDocValues.MultiSortedSetDocValues && matchingDocs.size() > 1) {
    ordinalMap = ((MultiSortedSetDocValues) dv).mapping;
  } else {
    ordinalMap = null;
  }
  
  IndexReader reader = state.getReader();
  List<Future<Void>> results = new ArrayList<>();

  for (MatchingDocs hits : matchingDocs) {
    // LUCENE-5090: make sure the provided reader context "matches"
    // the top-level reader passed to the
    // SortedSetDocValuesReaderState, else cryptic
    // AIOOBE can happen:
    if (ReaderUtil.getTopLevelContext(hits.context).reader() != reader) {
      throw new IllegalStateException("the SortedSetDocValuesReaderState provided to this class does not match the reader being searched; you must create a new SortedSetDocValuesReaderState every time you open a new IndexReader");
    }
    
    results.add(exec.submit(new CountOneSegment(hits.context.reader(), hits, ordinalMap, hits.context.ord)));
  }

  for (Future<Void> result : results) {
    try {
      result.get();
    } catch (ExecutionException ee) {
      // Theoretically cause can be null; guard against that.
      Throwable cause = ee.getCause();
      throw IOUtils.rethrowAlways(cause != null ? cause : ee);
    }
  }
}
 
Example 27
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 28
Source Project: lucene-solr   Source File: LRUQueryCache.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public boolean test(LeafReaderContext context) {
  final int maxDoc = context.reader().maxDoc();
  if (maxDoc < minSize) {
    return false;
  }
  final IndexReaderContext topLevelContext = ReaderUtil.getTopLevelContext(context);
  final float sizeRatio = (float) context.reader().maxDoc() / topLevelContext.reader().maxDoc();
  return sizeRatio >= minSizeRatio;
}
 
Example 29
Source Project: lucene-solr   Source File: TestMatchesIterator.java    License: Apache License 2.0 5 votes vote down vote up
private void checkNoPositionsMatches(Query q, String field, boolean[] expected) throws IOException {
  Weight w = searcher.createWeight(searcher.rewrite(q), ScoreMode.COMPLETE, 1);
  for (int i = 0; i < expected.length; i++) {
    LeafReaderContext ctx = searcher.leafContexts.get(ReaderUtil.subIndex(i, searcher.leafContexts));
    int doc = i - ctx.docBase;
    Matches matches = w.matches(ctx, doc);
    if (expected[i]) {
      MatchesIterator mi = matches.getMatches(field);
      assertNull(mi);
    }
    else {
      assertNull(matches);
    }
  }
}
 
Example 30
Source Project: lucene-solr   Source File: LTRRescorer.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public Explanation explain(IndexSearcher searcher,
    Explanation firstPassExplanation, int docID) throws IOException {

  final List<LeafReaderContext> leafContexts = searcher.getTopReaderContext()
      .leaves();
  final int n = ReaderUtil.subIndex(docID, leafContexts);
  final LeafReaderContext context = leafContexts.get(n);
  final int deBasedDoc = docID - context.docBase;
  final Weight modelWeight = searcher.createWeight(searcher.rewrite(scoringQuery),
      ScoreMode.COMPLETE, 1);
  return modelWeight.explain(context, deBasedDoc);
}