Java Code Examples for org.apache.lucene.queries.function.ValueSource#newContext()

The following examples show how to use org.apache.lucene.queries.function.ValueSource#newContext() . 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: ValueSourceAugmenter.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
@Override
@SuppressWarnings({"unchecked"})
public void setContext( ResultContext context ) {
  super.setContext(context);
  try {
    searcher = context.getSearcher();
    readerContexts = searcher.getIndexReader().leaves();
    fcontext = ValueSource.newContext(searcher);
    this.valueSource.createWeight(fcontext, searcher);
  } catch (IOException e) {
    throw new SolrException(SolrException.ErrorCode.SERVER_ERROR, e);
  }
}
 
Example 2
Source File: VersionInfo.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
/**
 * Returns the latest version from the index, searched by the given id (bytes) as seen from the realtime searcher.
 * Returns null if no document can be found in the index for the given id.
 */
@SuppressWarnings({"unchecked"})
public Long getVersionFromIndex(BytesRef idBytes) {
  // TODO: we could cache much of this and invalidate during a commit.
  // TODO: most DocValues classes are threadsafe - expose which.

  RefCounted<SolrIndexSearcher> newestSearcher = ulog.uhandler.core.getRealtimeSearcher();
  try {
    SolrIndexSearcher searcher = newestSearcher.get();
    long lookup = searcher.lookupId(idBytes);
    if (lookup < 0) return null; // this means the doc doesn't exist in the index yet

    ValueSource vs = versionField.getType().getValueSource(versionField, null);
    @SuppressWarnings({"rawtypes"})
    Map context = ValueSource.newContext(searcher);
    vs.createWeight(context, searcher);
    FunctionValues fv = vs.getValues(context, searcher.getTopReaderContext().leaves().get((int) (lookup >> 32)));
    long ver = fv.longVal((int) lookup);
    return ver;

  } catch (IOException e) {
    throw new SolrException(SolrException.ErrorCode.SERVER_ERROR, "Error reading version from index", e);
  } finally {
    if (newestSearcher != null) {
      newestSearcher.decref();
    }
  }
}
 
Example 3
Source File: VersionInfo.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
/**
 * Returns the highest version from the index, or 0L if no versions can be found in the index.
 */
@SuppressWarnings({"unchecked"})
public Long getMaxVersionFromIndex(IndexSearcher searcher) throws IOException {

  final String versionFieldName = versionField.getName();

  log.debug("Refreshing highest value of {} for {} version buckets from index", versionFieldName, buckets.length);
  // if indexed, then we have terms to get the max from
  if (versionField.indexed()) {
    if (versionField.getType().isPointField()) {
      return getMaxVersionFromIndexedPoints(searcher);
    } else {
      return getMaxVersionFromIndexedTerms(searcher);
    }
  }
  // else: not indexed, use docvalues via value source ...
  
  long maxVersionInIndex = 0L;
  ValueSource vs = versionField.getType().getValueSource(versionField, null);
  @SuppressWarnings({"rawtypes"})
  Map funcContext = ValueSource.newContext(searcher);
  vs.createWeight(funcContext, searcher);
  // TODO: multi-thread this
  for (LeafReaderContext ctx : searcher.getTopReaderContext().leaves()) {
    int maxDoc = ctx.reader().maxDoc();
    FunctionValues fv = vs.getValues(funcContext, ctx);
    for (int doc = 0; doc < maxDoc; doc++) {
      long v = fv.longVal(doc);
      maxVersionInIndex = Math.max(v, maxVersionInIndex);
    }
  }
  return maxVersionInIndex;
}
 
Example 4
Source File: DocBasedVersionConstraintsProcessor.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings({"unchecked"})
private static FunctionValues getFunctionValues(LeafReaderContext segmentContext,
                                        SchemaField field,
                                        SolrIndexSearcher searcher) throws IOException {
  ValueSource vs = field.getType().getValueSource(field, null);
  @SuppressWarnings({"rawtypes"})
  Map context = ValueSource.newContext(searcher);
  vs.createWeight(context, searcher);
  return vs.getValues(context, segmentContext);
}
 
Example 5
Source File: Grouping.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
@Override
@SuppressWarnings({"unchecked"})
protected void prepare() throws IOException {
  context = ValueSource.newContext(searcher);
  groupBy.createWeight(context, searcher);
  actualGroupsToFind = getMax(offset, numGroups, maxDoc);
}
 
Example 6
Source File: FunctionRangeQuery.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
@Override
public DelegatingCollector getFilterCollector(IndexSearcher searcher) {
  @SuppressWarnings({"rawtypes"})
  Map fcontext = ValueSource.newContext(searcher);
  Weight weight = rangeFilt.createWeight(searcher, ScoreMode.COMPLETE, 1);
  return new FunctionRangeCollector(fcontext, weight);
}
 
Example 7
Source File: SolrConstantScoreQuery.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
public ConstantWeight(IndexSearcher searcher, ScoreMode scoreMode, float boost) throws IOException {
  super(SolrConstantScoreQuery.this, boost);
  this.scoreMode = scoreMode;
  this.context = ValueSource.newContext(searcher);
  if (filter instanceof SolrFilter)
    ((SolrFilter)filter).createWeight(context, searcher);
}
 
Example 8
Source File: TaggerRequestHandler.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
ValueSourceAccessor(IndexSearcher searcher, ValueSource valueSource) {
  readerContexts = searcher.getIndexReader().leaves();
  this.valueSource = valueSource;
  fContext = ValueSource.newContext(searcher);
  functionValuesPerSeg = new FunctionValues[readerContexts.size()];
  functionValuesDocIdPerSeg = new int[readerContexts.size()];
}
 
Example 9
Source File: StatsValuesFactory.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings({"unchecked"})
public void setNextReader(LeafReaderContext ctx) throws IOException {
  if (valueSource == null) {
    // first time we've collected local values, get the right ValueSource
    valueSource = (null == ft)
      ? statsField.getValueSource()
      : ft.getValueSource(sf, null);
    vsContext = ValueSource.newContext(statsField.getSearcher());
  }
  values = valueSource.getValues(vsContext, ctx);
}
 
Example 10
Source File: LatLonType.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings({"unchecked"})
public SpatialWeight(IndexSearcher searcher, float boost) throws IOException {
  super(SpatialDistanceQuery.this, boost);
  this.searcher = searcher;
  this.latContext = ValueSource.newContext(searcher);
  this.lonContext = ValueSource.newContext(searcher);
  latSource.createWeight(latContext, searcher);
  lonSource.createWeight(lonContext, searcher);
}
 
Example 11
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 12
Source File: TaggerRequestHandler.java    From SolrTextTagger with Apache License 2.0 5 votes vote down vote up
ValueSourceAccessor(IndexSearcher searcher, ValueSource valueSource) {
  readerContexts = searcher.getIndexReader().leaves();
  this.valueSource = valueSource;
  fContext = ValueSource.newContext(searcher);
  functionValuesPerSeg = new FunctionValues[readerContexts.size()];
  functionValuesDocIdPerSeg = new int[readerContexts.size()];
}