Java Code Examples for org.apache.lucene.queries.function.FunctionValues

The following examples show how to use org.apache.lucene.queries.function.FunctionValues. 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: 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 2
Source Project: lucene-solr   Source File: SortedSetFieldSource.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public FunctionValues getValues(Map<Object, Object> context, LeafReaderContext readerContext) throws IOException {
  SortedSetDocValues sortedSet = DocValues.getSortedSet(readerContext.reader(), field);
  SortedDocValues view = SortedSetSelector.wrap(sortedSet, selector);
  return new DocTermsIndexDocValues(this, view) {
    @Override
    protected String toTerm(String readableValue) {
      return readableValue;
    }

    @Override
    public Object objectVal(int doc) throws IOException {
      return strVal(doc);
    }
  };
}
 
Example 3
Source Project: lucene-solr   Source File: ValueSourceParser.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public ValueSource parse(FunctionQParser fp) throws SyntaxError {
  List<ValueSource> sources = fp.parseValueSourceList();
  return new MultiBoolFunction(sources) {
    @Override
    protected String name() {
      return "xor";
    }
    @Override
    protected boolean func(int doc, FunctionValues[] vals) throws IOException {
      int nTrue=0, nFalse=0;
      for (FunctionValues dv : vals) {
        if (dv.boolVal(doc)) nTrue++;
        else nFalse++;
      }
      return nTrue != 0 && nFalse != 0;
    }
  };
}
 
Example 4
Source Project: lucene-solr   Source File: ValueSourceParser.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public ValueSource parse(FunctionQParser fp) throws SyntaxError {
  List<ValueSource> sources = fp.parseValueSourceList();
  return new MultiBoolFunction(sources) {
    @Override
    protected String name() {
      return "or";
    }
    @Override
    protected boolean func(int doc, FunctionValues[] vals) throws IOException {
      for (FunctionValues dv : vals)
        if (dv.boolVal(doc)) return true;
      return false;
    }
  };
}
 
Example 5
Source Project: lucene-solr   Source File: HaversineFunction.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * @param doc  The doc to score
 * @return The haversine distance formula
 */
protected double distance(int doc, FunctionValues p1DV, FunctionValues p2DV) throws IOException {

  double[] p1D = new double[2];
  double[] p2D = new double[2];
  p1DV.doubleVal(doc, p1D);
  p2DV.doubleVal(doc, p2D);
  double y1;
  double x1;
  double y2;
  double x2;
  if (convertToRadians) {
    y1 = p1D[0] * DistanceUtils.DEGREES_TO_RADIANS;
    x1 = p1D[1] * DistanceUtils.DEGREES_TO_RADIANS;
    y2 = p2D[0] * DistanceUtils.DEGREES_TO_RADIANS;
    x2 = p2D[1] * DistanceUtils.DEGREES_TO_RADIANS;
  } else {
    y1 = p1D[0];
    x1 = p1D[1];
    y2 = p2D[0];
    x2 = p2D[1];
  }
  return DistanceUtils.distHaversineRAD(y1,x1,y2,x2)*radius;
}
 
Example 6
Source Project: lucene-solr   Source File: ValueSourceParser.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public ValueSource parse(FunctionQParser fp) throws SyntaxError {
  ValueSource a = fp.parseValueSource();
  ValueSource b = fp.parseValueSource();
  return new DualFloatFunction(a, b) {
    @Override
    protected String name() {
      return "sub";
    }

    @Override
    protected float func(int doc, FunctionValues aVals, FunctionValues bVals) throws IOException {
      return aVals.floatVal(doc) - bVals.floatVal(doc);
    }
  };
}
 
Example 7
Source Project: lucene-solr   Source File: NvlValueSourceParser.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public ValueSource parse(FunctionQParser fp) throws SyntaxError {
  ValueSource source = fp.parseValueSource();
  final float nvl = fp.parseFloat();

  return new SimpleFloatFunction(source) {
    @Override
  protected String name() {
      return "nvl";
    }

    @Override
    protected float func(int doc, FunctionValues vals) throws IOException {
      float v = vals.floatVal(doc);
      if (v == nvlFloatValue) {
        return nvl;
      } else {
        return v;
      }
    }
  };
}
 
Example 8
Source Project: lucene-solr   Source File: ValueSourceParser.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public ValueSource parse(FunctionQParser fp) throws SyntaxError {
  List<ValueSource> sources = fp.parseValueSourceList();
  return new MultiBoolFunction(sources) {
    @Override
    protected String name() {
      return "and";
    }
    @Override
    protected boolean func(int doc, FunctionValues[] vals) throws IOException {
      for (FunctionValues dv : vals)
        if (!dv.boolVal(doc)) return false;
      return true;
    }
  };
}
 
Example 9
Source Project: BioSolr   Source File: TestXJoinValueSourceParser.java    License: Apache License 2.0 5 votes vote down vote up
@Test
@SuppressWarnings({ "rawtypes", "unchecked" })
public void testComponentArg() throws Exception {
  NamedList initArgs = new NamedList();
  initArgs.add(XJoinParameters.INIT_ATTRIBUTE, resultAttribute);
  FunctionValues fv = functionValues(initArgs, componentName);
  assertEquals(value, fv.doubleVal(0), 0);
}
 
Example 10
Source Project: lucene-solr   Source File: SumFloatFunction.java    License: Apache License 2.0 5 votes vote down vote up
@Override
protected float func(int doc, FunctionValues[] valsArr) throws IOException {
  float val = 0.0f;
  for (FunctionValues vals : valsArr) {
    val += vals.floatVal(doc);
  }
  return val;
}
 
Example 11
Source Project: BioSolr   Source File: TestXJoinValueSourceParser.java    License: Apache License 2.0 5 votes vote down vote up
@Test
@SuppressWarnings({ "rawtypes", "unchecked" })
public void testAttributeArg() throws Exception {
  NamedList initArgs = new NamedList();
  initArgs.add(XJoinParameters.INIT_XJOIN_COMPONENT_NAME, componentName);
  FunctionValues fv = functionValues(initArgs, resultAttribute);
  assertEquals(value, fv.doubleVal(0), 0);
}
 
Example 12
Source Project: lucene-solr   Source File: IDFValueSource.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public FunctionValues getValues(Map<Object, Object> context, LeafReaderContext readerContext) throws IOException {
  IndexSearcher searcher = (IndexSearcher)context.get("searcher");
  TFIDFSimilarity sim = asTFIDF(searcher.getSimilarity(), field);
  if (sim == null) {
    throw new UnsupportedOperationException("requires a TFIDFSimilarity (such as ClassicSimilarity)");
  }
  int docfreq = searcher.getIndexReader().docFreq(new Term(indexedField, indexedBytes));
  float idf = sim.idf(docfreq, searcher.getIndexReader().maxDoc());
  return new DocFreqValueSource.ConstDoubleDocValues(idf, this);
}
 
Example 13
Source Project: lucene-solr   Source File: ProductFloatFunction.java    License: Apache License 2.0 5 votes vote down vote up
@Override
protected float func(int doc, FunctionValues[] valsArr) throws IOException {
  float val = 1.0f;
  for (FunctionValues vals : valsArr) {
    val *= vals.floatVal(doc);
  }
  return val;
}
 
Example 14
Source Project: lucene-solr   Source File: RandomSortField.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public FunctionValues getValues(@SuppressWarnings({"rawtypes"})Map context, final LeafReaderContext readerContext) throws IOException {
  return new IntDocValues(this) {
      private final int seed = getSeed(field, readerContext);
      @Override
      public int intVal(int doc) {
        return hash(doc+seed);
      }
    };
}
 
Example 15
Source Project: lucene-solr   Source File: MultiFunction.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Helper utility for {@link FunctionValues}
 *
 * @return true if <em>all</em> of the specified <code>values</code>
 *         {@link FunctionValues#exists} for the specified doc, else false.
 */
public static boolean allExists(int doc, FunctionValues[] values) throws IOException {
  for (FunctionValues v : values) {
    if ( ! v.exists(doc) ) {
      return false;
    }
  }
  return true;
}
 
Example 16
Source Project: BioSolr   Source File: TestXJoinValueSourceParser.java    License: Apache License 2.0 5 votes vote down vote up
@SuppressWarnings({ "rawtypes" })
private FunctionValues functionValues(NamedList initArgs, String arg) throws Exception {
  FunctionQParser fqp = mockFunctionQParser(arg);
  XJoinValueSourceParser vsp = new XJoinValueSourceParser();
  vsp.init(initArgs);
  ValueSource vs = vsp.parse(fqp);
  return vs.getValues(null, searcher.getAtomicReader().getContext());
}
 
Example 17
Source Project: BioSolr   Source File: TestXJoinValueSourceParser.java    License: Apache License 2.0 5 votes vote down vote up
@Test
@SuppressWarnings({ "rawtypes", "unchecked" })
public void testDefault() throws Exception {
  NamedList initArgs = new NamedList();
  initArgs.add(XJoinParameters.INIT_ATTRIBUTE, resultAttribute);
  initArgs.add(XJoinParameters.INIT_DEFAULT_VALUE, defaultValue);
  FunctionValues fv = functionValues(initArgs, componentName);
  assertEquals(defaultValue, fv.doubleVal(missingDoc), 0);
}
 
Example 18
Source Project: lucene-solr   Source File: MultiFunction.java    License: Apache License 2.0 5 votes vote down vote up
public static String toString(String name, FunctionValues[] valsArr, int doc) throws IOException {
  StringBuilder sb = new StringBuilder();
  sb.append(name).append('(');
  boolean firstTime=true;
  for (FunctionValues vals : valsArr) {
    if (firstTime) {
      firstTime=false;
    } else {
      sb.append(',');
    }
    sb.append(vals.toString(doc));
  }
  sb.append(')');
  return sb.toString();
}
 
Example 19
Source Project: lucene-solr   Source File: MinFloatFunction.java    License: Apache License 2.0 5 votes vote down vote up
@Override
protected float func(int doc, FunctionValues[] valsArr) throws IOException {
  if ( ! exists(doc, valsArr) ) return 0.0f;

  float val = Float.POSITIVE_INFINITY;
  for (FunctionValues vals : valsArr) {
    if (vals.exists(doc)) {
      val = Math.min(vals.floatVal(doc), val);
    }
  }
  return val;
}
 
Example 20
Source Project: BioSolr   Source File: TestXJoinValueSourceParser.java    License: Apache License 2.0 5 votes vote down vote up
@Test
@SuppressWarnings({ "rawtypes", "unchecked" })
public void testDefault() throws Exception {
  NamedList initArgs = new NamedList();
  initArgs.add(XJoinParameters.INIT_ATTRIBUTE, resultAttribute);
  initArgs.add(XJoinParameters.INIT_DEFAULT_VALUE, defaultValue);
  FunctionValues fv = functionValues(initArgs, componentName);
  assertEquals(defaultValue, fv.doubleVal(missingDoc), 0);
}
 
Example 21
Source Project: BioSolr   Source File: TestXJoinValueSourceParser.java    License: Apache License 2.0 5 votes vote down vote up
@Test
@SuppressWarnings({ "rawtypes", "unchecked" })
public void testAttributeArg() throws Exception {
  NamedList initArgs = new NamedList();
  initArgs.add(XJoinParameters.INIT_XJOIN_COMPONENT_NAME, componentName);
  FunctionValues fv = functionValues(initArgs, resultAttribute);
  assertEquals(value, fv.doubleVal(0), 0);
}
 
Example 22
Source Project: lucene-solr   Source File: VersionInfo.java    License: 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 23
private static Object[] getObjectValues(LeafReaderContext segmentContext,
                                        SchemaField[] fields,
                                        SolrIndexSearcher searcher,
                                        int docIdInSegment) throws IOException {
  FunctionValues[] functionValues = getManyFunctionValues(segmentContext, fields, searcher);
  Object[] objectValues = new Object[functionValues.length];
  for (int i = 0; i < functionValues.length; i++) {
    objectValues[i] = functionValues[i].objectVal(docIdInSegment);
  }
  return objectValues;
}
 
Example 24
private static FunctionValues[] getManyFunctionValues(LeafReaderContext segmentContext,
                                              SchemaField[] fields,
                                              SolrIndexSearcher searcher) throws IOException {
  FunctionValues[] values = new FunctionValues[fields.length];
  for (int i = 0; i < fields.length; i++) {
    values[i] = getFunctionValues(segmentContext, fields[i], searcher);
  }
  return values;
}
 
Example 25
@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 26
Source Project: lucene-solr   Source File: FunctionRangeQuery.java    License: Apache License 2.0 5 votes vote down vote up
@Override
protected void doSetNextReader(LeafReaderContext context) throws IOException {
  super.doSetNextReader(context);
  maxdoc = context.reader().maxDoc();
  @SuppressWarnings({"unchecked"})
  FunctionValues dv = rangeFilt.getValueSource().getValues(fcontext, context);
  scorer = dv.getRangeScorer(weight, context, rangeFilt.getLowerVal(), rangeFilt.getUpperVal(), rangeFilt.isIncludeLower(), rangeFilt.isIncludeUpper());
}
 
Example 27
Source Project: BioSolr   Source File: TestXJoinValueSourceParser.java    License: Apache License 2.0 5 votes vote down vote up
@SuppressWarnings({ "rawtypes" })
private FunctionValues functionValues(NamedList initArgs, String arg) throws Exception {
  FunctionQParser fqp = mockFunctionQParser(arg);
  XJoinValueSourceParser vsp = new XJoinValueSourceParser();
  vsp.init(initArgs);
  ValueSource vs = vsp.parse(fqp);
  return vs.getValues(null, searcher.getLeafReader().getContext());
}
 
Example 28
Source Project: lucene-solr   Source File: SolrComparisonBoolFunction.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public boolean compare(int doc, FunctionValues lhs, FunctionValues rhs) throws IOException {
  // TODO consider a separate FunctionValues impl, one for Long, one for Double
  // performs the safest possible numeric comparison, if both lhs and rhs are Longs, then
  // we perform a Long comparison to avoid the issues with precision when casting to doubles
  boolean lhsAnInt = (lhs instanceof LongDocValues || lhs instanceof IntDocValues);
  boolean rhsAnInt = (rhs instanceof LongDocValues || rhs instanceof IntDocValues);
  if (lhsAnInt && rhsAnInt) {
    return cmp.compare(Long.compare(lhs.longVal(doc), rhs.longVal(doc)));
  } else {
    return cmp.compare(Double.compare(lhs.doubleVal(doc), rhs.doubleVal(doc)));
  }
}
 
Example 29
Source Project: lucene-solr   Source File: ConcatStringFunction.java    License: Apache License 2.0 5 votes vote down vote up
@Override
protected String func(int doc, FunctionValues[] valsArr) throws IOException {
  StringBuilder sb = new StringBuilder();
  for (FunctionValues val : valsArr) {
    String v = val.strVal(doc);
    if(v == null){
      return null;
    } else {
      sb.append(v);
    }
  }
  return sb.toString();
}
 
Example 30
Source Project: lucene-solr   Source File: EqualFunction.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public boolean compare(int doc, FunctionValues lhs, FunctionValues rhs) throws IOException {
  Object objL = lhs.objectVal(doc);
  Object objR = rhs.objectVal(doc);
  if (isNumeric(objL) && isNumeric(objR)) {
    if (isInteger(objL) && isInteger(objR)) {
      return Long.compare(((Number)objL).longValue(), ((Number)objR).longValue()) == 0;
    } else {
      return Double.compare(((Number)objL).doubleValue(), ((Number)objR).doubleValue()) == 0;
    }
  } else {
    return Objects.equals(objL, objR);
  }
}