use of org.apache.lucene.queries.function.FunctionValues in project lucene-solr by apache.
the class CachingDoubleValueSource method getValues.
@Override
public FunctionValues getValues(Map context, LeafReaderContext readerContext) throws IOException {
final int base = readerContext.docBase;
final FunctionValues vals = source.getValues(context, readerContext);
return new FunctionValues() {
@Override
public double doubleVal(int doc) throws IOException {
Integer key = Integer.valueOf(base + doc);
Double v = cache.get(key);
if (v == null) {
v = Double.valueOf(vals.doubleVal(doc));
cache.put(key, v);
}
return v.doubleValue();
}
@Override
public float floatVal(int doc) throws IOException {
return (float) doubleVal(doc);
}
@Override
public String toString(int doc) throws IOException {
return doubleVal(doc) + "";
}
};
}
use of org.apache.lucene.queries.function.FunctionValues in project lucene-solr by apache.
the class TestValueSource method parse.
@Override
public ValueSource parse(FunctionQParser fp) throws SyntaxError {
String first = fp.parseArg();
String second = fp.parseArg();
if (first == null)
first = "NOW";
Date d1 = getDate(fp, first);
ValueSource v1 = d1 == null ? getValueSource(fp, first) : null;
Date d2 = getDate(fp, second);
ValueSource v2 = d2 == null ? getValueSource(fp, second) : null;
// d constant
// v field
// dd constant
// dv subtract field from constant
// vd subtract constant from field
// vv subtract fields
final long ms1 = (d1 == null) ? 0 : d1.getTime();
final long ms2 = (d2 == null) ? 0 : d2.getTime();
if (d1 != null && v2 == null) {
return new LongConstValueSource(ms1 - ms2);
}
// "v" just the date field
if (v1 != null && v2 == null && d2 == null) {
return v1;
}
// "dv"
if (d1 != null && v2 != null)
return new DualFloatFunction(new LongConstValueSource(ms1), v2) {
@Override
protected String name() {
return "ms";
}
@Override
protected float func(int doc, FunctionValues aVals, FunctionValues bVals) throws IOException {
return ms1 - bVals.longVal(doc);
}
};
// "vd"
if (v1 != null && d2 != null)
return new DualFloatFunction(v1, new LongConstValueSource(ms2)) {
@Override
protected String name() {
return "ms";
}
@Override
protected float func(int doc, FunctionValues aVals, FunctionValues bVals) throws IOException {
return aVals.longVal(doc) - ms2;
}
};
// "vv"
if (v1 != null && v2 != null)
return new DualFloatFunction(v1, v2) {
@Override
protected String name() {
return "ms";
}
@Override
protected float func(int doc, FunctionValues aVals, FunctionValues bVals) throws IOException {
return aVals.longVal(doc) - bVals.longVal(doc);
}
};
// shouldn't happen
return null;
}
use of org.apache.lucene.queries.function.FunctionValues in project lucene-solr by apache.
the class BBoxValueSource method getValues.
@Override
public FunctionValues getValues(Map context, LeafReaderContext readerContext) throws IOException {
LeafReader reader = readerContext.reader();
final NumericDocValues minX = DocValues.getNumeric(reader, strategy.field_minX);
final NumericDocValues minY = DocValues.getNumeric(reader, strategy.field_minY);
final NumericDocValues maxX = DocValues.getNumeric(reader, strategy.field_maxX);
final NumericDocValues maxY = DocValues.getNumeric(reader, strategy.field_maxY);
//reused
final Rectangle rect = strategy.getSpatialContext().makeRectangle(0, 0, 0, 0);
return new FunctionValues() {
private int lastDocID = -1;
private double getDocValue(NumericDocValues values, int doc) throws IOException {
int curDocID = values.docID();
if (doc > curDocID) {
curDocID = values.advance(doc);
}
if (doc == curDocID) {
return Double.longBitsToDouble(values.longValue());
} else {
return 0.0;
}
}
@Override
public Object objectVal(int doc) throws IOException {
if (doc < lastDocID) {
throw new AssertionError("docs were sent out-of-order: lastDocID=" + lastDocID + " vs doc=" + doc);
}
lastDocID = doc;
double minXValue = getDocValue(minX, doc);
if (minX.docID() != doc) {
return null;
} else {
double minYValue = getDocValue(minY, doc);
double maxXValue = getDocValue(maxX, doc);
double maxYValue = getDocValue(maxY, doc);
rect.reset(minXValue, maxXValue, minYValue, maxYValue);
return rect;
}
}
@Override
public String strVal(int doc) throws IOException {
//TODO support WKT output once Spatial4j does
Object v = objectVal(doc);
return v == null ? null : v.toString();
}
@Override
public boolean exists(int doc) throws IOException {
getDocValue(minX, doc);
return minX.docID() == doc;
}
@Override
public Explanation explain(int doc) throws IOException {
return Explanation.match(Float.NaN, toString(doc));
}
@Override
public String toString(int doc) throws IOException {
return description() + '=' + strVal(doc);
}
};
}
use of org.apache.lucene.queries.function.FunctionValues in project lucene-solr by apache.
the class CompositeVerifyQuery method createWeight.
@Override
public Weight createWeight(IndexSearcher searcher, boolean needsScores, float boost) throws IOException {
//scores aren't unsupported
final Weight indexQueryWeight = indexQuery.createWeight(searcher, false, boost);
final Map valueSourceContext = ValueSource.newContext(searcher);
return new ConstantScoreWeight(this, boost) {
@Override
public Scorer scorer(LeafReaderContext context) throws IOException {
final Scorer indexQueryScorer = indexQueryWeight.scorer(context);
if (indexQueryScorer == null) {
return null;
}
final FunctionValues predFuncValues = predicateValueSource.getValues(valueSourceContext, context);
final TwoPhaseIterator twoPhaseIterator = new TwoPhaseIterator(indexQueryScorer.iterator()) {
@Override
public boolean matches() throws IOException {
return predFuncValues.boolVal(indexQueryScorer.docID());
}
@Override
public float matchCost() {
// TODO: use cost of predFuncValues.boolVal()
return 100;
}
};
return new ConstantScoreScorer(this, score(), twoPhaseIterator);
}
};
}
use of org.apache.lucene.queries.function.FunctionValues in project lucene-solr by apache.
the class ValueSourceGroupSelector method setNextReader.
@Override
public void setNextReader(LeafReaderContext readerContext) throws IOException {
FunctionValues values = valueSource.getValues(context, readerContext);
this.filler = values.getValueFiller();
}
Aggregations