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

The following examples show how to use org.apache.lucene.queries.function.FunctionValues. 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: ValueSourceParser.java    From lucene-solr with 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 #2
Source File: SortedSetFieldSource.java    From lucene-solr with 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 File: ValueSourceParser.java    From lucene-solr with 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 #4
Source File: HaversineFunction.java    From lucene-solr with 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 #5
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 #6
Source File: ValueSourceParser.java    From lucene-solr with 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 File: ValueSourceParser.java    From lucene-solr with 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 #8
Source File: NvlValueSourceParser.java    From lucene-solr with 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 #9
Source File: XJoinValueSourceParser.java    From BioSolr with Apache License 2.0 5 votes vote down vote up
@Override
@SuppressWarnings("rawtypes")
public FunctionValues getValues(Map context, AtomicReaderContext readerContext) throws IOException {
  final BinaryDocValues joinValues = FieldCache.DEFAULT.getTerms(readerContext.reader(), joinField, false);

  return new DoubleDocValues(this) {

    @Override
    public double doubleVal(int doc) {
      BytesRef joinValue = joinValues.get(doc);
      if (joinValue == null) {
        throw new RuntimeException("No such doc: " + doc);
      }
      Object result = results.getResult(joinValue.utf8ToString());
      if (result == null) {
        return defaultValue;
      }
      if (result instanceof Iterable) {
        Double max = null;
        for (Object object : (Iterable)result) {
          if (object != null) {
            double value = getValue(object);
            if (max == null || value > max) {
              max = value;
            }
          }
        }
        return max != null ? max : defaultValue;
      } else {
        return getValue(result);
      }
    }
    
  };
}
 
Example #10
Source File: GeohashHaversineFunction.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
protected double distance(int doc, FunctionValues gh1DV, FunctionValues gh2DV) throws IOException {
  double result = 0;
  String h1 = gh1DV.strVal(doc);
  String h2 = gh2DV.strVal(doc);
  if (h1 != null && h2 != null && h1.equals(h2) == false){
    //TODO: If one of the hashes is a literal value source, seems like we could cache it
    //and avoid decoding every time
    Point p1 = GeohashUtils.decode(h1,ctx);
    Point p2 = GeohashUtils.decode(h2,ctx);
    result = ctx.getDistCalc().distance(p1, p2) * degreesToDist;
  } else if (h1 == null || h2 == null){
    result = Double.MAX_VALUE;
  }
  return result;
}
 
Example #11
Source File: ValueSourceParser.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
@Override
public FunctionValues getValues(@SuppressWarnings({"rawtypes"})Map context,
                                LeafReaderContext readerContext) throws IOException {
  return new BoolDocValues(this) {
    @Override
    public boolean boolVal(int doc) {
      return constant;
    }
  };
}
 
Example #12
Source File: TestXJoinValueSourceParser.java    From BioSolr with 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 #13
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()];
}
 
Example #14
Source File: EqualFunction.java    From lucene-solr with 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);
  }
}
 
Example #15
Source File: TestXJoinValueSourceParser.java    From BioSolr with 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 #16
Source File: TestXJoinValueSourceParser.java    From BioSolr with 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 #17
Source File: ValueSourceParser.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
@Override
public ValueSource parse(FunctionQParser fp) throws SyntaxError {
  ValueSource vs = fp.parseValueSource();
  return new SimpleBoolFunction(vs) {
    @Override
    protected String name() {
      return "exists";
    }
    @Override
    protected boolean func(int doc, FunctionValues vals) throws IOException {
      return vals.exists(doc);
    }
  };
}
 
Example #18
Source File: ValueSourceParser.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
@Override
public ValueSource parse(FunctionQParser fp) throws SyntaxError {
  ValueSource vs = fp.parseValueSource();
  return new SimpleBoolFunction(vs) {
    @Override
    protected boolean func(int doc, FunctionValues vals) throws IOException {
      return !vals.boolVal(doc);
    }
    @Override
    protected String name() {
      return "not";
    }
  };
}
 
Example #19
Source File: VectorDistanceFunction.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
/**
 * Calculate the distance
 *
 * @param doc The current doc
 * @param dv1 The values from the first MultiValueSource
 * @param dv2 The values from the second MultiValueSource
 * @return The distance
 */
protected double distance(int doc, FunctionValues dv1, FunctionValues dv2) throws IOException {
  //Handle some special cases:
  double[] vals1 = new double[source1.dimension()];
  double[] vals2 = new double[source1.dimension()];
  dv1.doubleVal(doc, vals1);
  dv2.doubleVal(doc, vals2);
  return vectorDistance(vals1, vals2, power, oneOverPower);
}
 
Example #20
Source File: ValueSourceParser.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
@Override
public ValueSource parse(FunctionQParser fp) throws SyntaxError {
  ValueSource source = fp.parseValueSource();
  return new SimpleFloatFunction(source) {
    @Override
    protected String name() {
      return "abs";
    }

    @Override
    protected float func(int doc, FunctionValues vals) throws IOException {
      return Math.abs(vals.floatVal(doc));
    }
  };
}
 
Example #21
Source File: ConcatStringFunction.java    From lucene-solr with 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 #22
Source File: SolrComparisonBoolFunction.java    From lucene-solr with 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 #23
Source File: TestXJoinValueSourceParser.java    From BioSolr with 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 #24
Source File: TestXJoinValueSourceParser.java    From BioSolr with 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 #25
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 #26
Source File: DocBasedVersionConstraintsProcessor.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
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 #27
Source File: DocBasedVersionConstraintsProcessor.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
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 #28
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 #29
Source File: TestXJoinValueSourceParser.java    From BioSolr with 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 #30
Source File: FunctionRangeQuery.java    From lucene-solr with 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());
}