Search in sources :

Example 31 with FunctionValues

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) + "";
        }
    };
}
Also used : FunctionValues(org.apache.lucene.queries.function.FunctionValues)

Example 32 with FunctionValues

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;
}
Also used : AggValueSource(org.apache.solr.search.facet.AggValueSource) ValueSource(org.apache.lucene.queries.function.ValueSource) FunctionValues(org.apache.lucene.queries.function.FunctionValues) IOException(java.io.IOException) Date(java.util.Date)

Example 33 with FunctionValues

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);
        }
    };
}
Also used : NumericDocValues(org.apache.lucene.index.NumericDocValues) LeafReader(org.apache.lucene.index.LeafReader) Rectangle(org.locationtech.spatial4j.shape.Rectangle) FunctionValues(org.apache.lucene.queries.function.FunctionValues)

Example 34 with FunctionValues

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);
        }
    };
}
Also used : TwoPhaseIterator(org.apache.lucene.search.TwoPhaseIterator) ConstantScoreScorer(org.apache.lucene.search.ConstantScoreScorer) LeafReaderContext(org.apache.lucene.index.LeafReaderContext) ConstantScoreScorer(org.apache.lucene.search.ConstantScoreScorer) Scorer(org.apache.lucene.search.Scorer) FunctionValues(org.apache.lucene.queries.function.FunctionValues) Map(java.util.Map) Weight(org.apache.lucene.search.Weight) ConstantScoreWeight(org.apache.lucene.search.ConstantScoreWeight) ConstantScoreWeight(org.apache.lucene.search.ConstantScoreWeight)

Example 35 with FunctionValues

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();
}
Also used : FunctionValues(org.apache.lucene.queries.function.FunctionValues)

Aggregations

FunctionValues (org.apache.lucene.queries.function.FunctionValues)45 ValueSource (org.apache.lucene.queries.function.ValueSource)10 DoubleDocValues (org.apache.lucene.queries.function.docvalues.DoubleDocValues)9 LeafReaderContext (org.apache.lucene.index.LeafReaderContext)7 Map (java.util.Map)6 LeafReader (org.apache.lucene.index.LeafReader)6 NumericDocValues (org.apache.lucene.index.NumericDocValues)6 IOException (java.io.IOException)5 FloatDocValues (org.apache.lucene.queries.function.docvalues.FloatDocValues)5 Explanation (org.apache.lucene.search.Explanation)4 ArrayList (java.util.ArrayList)3 Terms (org.apache.lucene.index.Terms)3 BoolDocValues (org.apache.lucene.queries.function.docvalues.BoolDocValues)3 BytesRef (org.apache.lucene.util.BytesRef)3 BytesRefBuilder (org.apache.lucene.util.BytesRefBuilder)3 Point (org.locationtech.spatial4j.shape.Point)3 Rectangle (org.locationtech.spatial4j.shape.Rectangle)3 Date (java.util.Date)2 TermsEnum (org.apache.lucene.index.TermsEnum)2 SimpleFloatFunction (org.apache.lucene.queries.function.valuesource.SimpleFloatFunction)2