org.apache.lucene.index.ReaderUtil Java Examples

The following examples show how to use org.apache.lucene.index.ReaderUtil. 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: ScoreDocRowFunction.java    From Elasticsearch with 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 File: TestMatchesIterator.java    From lucene-solr with 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 #3
Source File: TestMatchesIterator.java    From lucene-solr with 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 #4
Source File: TestMatchesIterator.java    From lucene-solr with 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 #5
Source File: TestMatchesIterator.java    From lucene-solr with 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 #6
Source File: TestMatchesIterator.java    From lucene-solr with 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 #7
Source File: DocumentValueSourceDictionary.java    From lucene-solr with 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 #8
Source File: TestMatchesIterator.java    From lucene-solr with 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 #9
Source File: LTRRescorer.java    From lucene-solr with 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 #10
Source File: TestSelectiveWeightCreation.java    From lucene-solr with 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 #11
Source File: TestLTRScoringQuery.java    From lucene-solr with 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 #12
Source File: ValueSourceAugmenter.java    From lucene-solr with 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 #13
Source File: TestGeo3DPoint.java    From lucene-solr with 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 #14
Source File: TaggerRequestHandler.java    From lucene-solr with 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 File: TaggerRequestHandler.java    From SolrTextTagger with 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 File: ScoreDocRowFunction.java    From crate with 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 File: ReservoirSampler.java    From crate with 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 File: SolrDocumentFetcher.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
/**
 * This will fetch and add the docValues fields to a given SolrDocument/SolrInputDocument
 *
 * @param doc
 *          A SolrDocument or SolrInputDocument instance where docValues will be added
 * @param docid
 *          The lucene docid of the document to be populated
 * @param fields
 *          The fields with docValues to populate the document with.
 *          DocValues fields which do not exist or not decodable will be ignored.
 */
public void decorateDocValueFields(@SuppressWarnings("rawtypes") SolrDocumentBase doc, int docid, Set<String> fields)
    throws IOException {
  final List<LeafReaderContext> leafContexts = searcher.getLeafContexts();
  final int subIndex = ReaderUtil.subIndex(docid, leafContexts);
  final int localId = docid - leafContexts.get(subIndex).docBase;
  final LeafReader leafReader = leafContexts.get(subIndex).reader();
  for (String fieldName : fields) {
    Object fieldValue = decodeDVField(localId, leafReader, fieldName);
    if (fieldValue != null) {
      doc.setField(fieldName, fieldValue);
    }
  }
}
 
Example #19
Source File: GraphTermsQParserPlugin.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
public DocSet getDocSet(IndexSearcher searcher) throws IOException {
  IndexReaderContext top = ReaderUtil.getTopLevelContext(searcher.getTopReaderContext());
  List<LeafReaderContext> segs = top.leaves();
  DocSetBuilder builder = new DocSetBuilder(top.reader().maxDoc(), Math.min(64,(top.reader().maxDoc()>>>10)+4));
  PointValues[] segPoints = new PointValues[segs.size()];
  for (int i=0; i<segPoints.length; i++) {
    segPoints[i] = segs.get(i).reader().getPointValues(field);
  }

  int maxCollect = Math.min(maxDocFreq, top.reader().maxDoc());

  PointSetQuery.CutoffPointVisitor visitor = new PointSetQuery.CutoffPointVisitor(maxCollect);
  PrefixCodedTerms.TermIterator iterator = sortedPackedPoints.iterator();
  outer: for (BytesRef point = iterator.next(); point != null; point = iterator.next()) {
    visitor.setPoint(point);
    for (int i=0; i<segs.size(); i++) {
      if (segPoints[i] == null) continue;
      visitor.setBase(segs.get(i).docBase);
      segPoints[i].intersect(visitor);
      if (visitor.getCount() > maxDocFreq) {
        continue outer;
      }
    }
    int collected = visitor.getCount();
    int[] ids = visitor.getGlobalIds();
    for (int i=0; i<collected; i++) {
      builder.add( ids[i] );
    }
  }

  FixedBitSet liveDocs = getLiveDocs(searcher);
  DocSet set = builder.build(liveDocs);
  return set;
}
 
Example #20
Source File: LTRRescorer.java    From lucene-solr with 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);
}
 
Example #21
Source File: RandomSortField.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
/** 
 * Given a field name and an IndexReader, get a random hash seed.
 * Using dynamic fields, you can force the random order to change 
 */
private static int getSeed(String fieldName, LeafReaderContext context) {
  final DirectoryReader top = (DirectoryReader) ReaderUtil.getTopLevelContext(context).reader();
  // calling getVersion() on a segment will currently give you a null pointer exception, so
  // we use the top-level reader.
  return fieldName.hashCode() + context.docBase + (int)top.getVersion();
}
 
Example #22
Source File: TestIndexSearcher.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings({"unchecked"})
private String getStringVal(SolrQueryRequest sqr, String field, int doc) throws IOException {
  SchemaField sf = sqr.getSchema().getField(field);
  ValueSource vs = sf.getType().getValueSource(sf, null);
  @SuppressWarnings({"rawtypes"})
  Map context = ValueSource.newContext(sqr.getSearcher());
  vs.createWeight(context, sqr.getSearcher());
  IndexReaderContext topReaderContext = sqr.getSearcher().getTopReaderContext();
  List<LeafReaderContext> leaves = topReaderContext.leaves();
  int idx = ReaderUtil.subIndex(doc, leaves);
  LeafReaderContext leaf = leaves.get(idx);
  FunctionValues vals = vs.getValues(context, leaf);
  return vals.strVal(doc-leaf.docBase);
}
 
Example #23
Source File: FetchCollector.java    From crate with Apache License 2.0 5 votes vote down vote up
public StreamBucket collect(IntContainer docIds) {
    StreamBucket.Builder builder = new StreamBucket.Builder(streamers, ramAccounting);
    for (IntCursor cursor : docIds) {
        int docId = cursor.value;
        int readerIndex = ReaderUtil.subIndex(docId, readerContexts);
        LeafReaderContext subReaderContext = readerContexts.get(readerIndex);
        try {
            setNextDocId(subReaderContext, docId - subReaderContext.docBase);
        } catch (IOException e) {
            Exceptions.rethrowRuntimeException(e);
        }
        builder.add(row);
    }
    return builder.build();
}
 
Example #24
Source File: TestMatchesIterator.java    From lucene-solr with 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 #25
Source File: FetchCollector.java    From Elasticsearch with 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 #26
Source File: LRUQueryCache.java    From lucene-solr with 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 #27
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));
}
 
Example #28
Source File: BitsetFilterCache.java    From Elasticsearch with 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 #29
Source File: ConcurrentSortedSetDocValuesFacetCounts.java    From lucene-solr with 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, 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 #30
Source File: SortedSetDocValuesFacetCounts.java    From lucene-solr with 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);
  }
}