Search in sources :

Example 16 with FunctionValues

use of org.apache.lucene.queries.function.FunctionValues in project lucene-solr by apache.

the class VectorDistanceFunction method getValues.

@Override
public FunctionValues getValues(Map context, LeafReaderContext readerContext) throws IOException {
    final FunctionValues vals1 = source1.getValues(context, readerContext);
    final FunctionValues vals2 = source2.getValues(context, readerContext);
    return new DoubleDocValues(this) {

        @Override
        public double doubleVal(int doc) throws IOException {
            return distance(doc, vals1, vals2);
        }

        @Override
        public String toString(int doc) throws IOException {
            StringBuilder sb = new StringBuilder();
            sb.append(name()).append('(').append(power).append(',');
            boolean firstTime = true;
            sb.append(vals1.toString(doc)).append(',');
            sb.append(vals2.toString(doc));
            sb.append(')');
            return sb.toString();
        }
    };
}
Also used : DoubleDocValues(org.apache.lucene.queries.function.docvalues.DoubleDocValues) FunctionValues(org.apache.lucene.queries.function.FunctionValues)

Example 17 with FunctionValues

use of org.apache.lucene.queries.function.FunctionValues in project lucene-solr by apache.

the class HaversineConstFunction method getValues.

@Override
public FunctionValues getValues(Map context, LeafReaderContext readerContext) throws IOException {
    final FunctionValues latVals = latSource.getValues(context, readerContext);
    final FunctionValues lonVals = lonSource.getValues(context, readerContext);
    final double latCenterRad = this.latCenter * DEGREES_TO_RADIANS;
    final double lonCenterRad = this.lonCenter * DEGREES_TO_RADIANS;
    final double latCenterRad_cos = this.latCenterRad_cos;
    return new DoubleDocValues(this) {

        @Override
        public double doubleVal(int doc) throws IOException {
            double latRad = latVals.doubleVal(doc) * DEGREES_TO_RADIANS;
            double lonRad = lonVals.doubleVal(doc) * DEGREES_TO_RADIANS;
            double diffX = latCenterRad - latRad;
            double diffY = lonCenterRad - lonRad;
            double hsinX = Math.sin(diffX * 0.5);
            double hsinY = Math.sin(diffY * 0.5);
            double h = hsinX * hsinX + (latCenterRad_cos * Math.cos(latRad) * hsinY * hsinY);
            return (EARTH_MEAN_DIAMETER * Math.atan2(Math.sqrt(h), Math.sqrt(1 - h)));
        }

        @Override
        public String toString(int doc) throws IOException {
            return name() + '(' + latVals.toString(doc) + ',' + lonVals.toString(doc) + ',' + latCenter + ',' + lonCenter + ')';
        }
    };
}
Also used : DoubleDocValues(org.apache.lucene.queries.function.docvalues.DoubleDocValues) FunctionValues(org.apache.lucene.queries.function.FunctionValues)

Example 18 with FunctionValues

use of org.apache.lucene.queries.function.FunctionValues in project lucene-solr by apache.

the class ShapeAreaValueSource method getValues.

@Override
public FunctionValues getValues(Map context, LeafReaderContext readerContext) throws IOException {
    final FunctionValues shapeValues = shapeValueSource.getValues(context, readerContext);
    return new DoubleDocValues(this) {

        @Override
        public double doubleVal(int doc) throws IOException {
            Shape shape = (Shape) shapeValues.objectVal(doc);
            if (shape == null || shape.isEmpty())
                //or NaN?
                return 0;
            //   assuming ctx.isGeo()
            return shape.getArea(geoArea ? ctx : null) * multiplier;
        }

        @Override
        public boolean exists(int doc) throws IOException {
            return shapeValues.exists(doc);
        }

        @Override
        public Explanation explain(int doc) throws IOException {
            Explanation exp = super.explain(doc);
            List<Explanation> details = new ArrayList<>(Arrays.asList(exp.getDetails()));
            details.add(shapeValues.explain(doc));
            return Explanation.match(exp.getValue(), exp.getDescription(), details);
        }
    };
}
Also used : Shape(org.locationtech.spatial4j.shape.Shape) Explanation(org.apache.lucene.search.Explanation) DoubleDocValues(org.apache.lucene.queries.function.docvalues.DoubleDocValues) ArrayList(java.util.ArrayList) FunctionValues(org.apache.lucene.queries.function.FunctionValues)

Example 19 with FunctionValues

use of org.apache.lucene.queries.function.FunctionValues in project lucene-solr by apache.

the class DistanceValueSource method getValues.

/**
   * Returns the FunctionValues used by the function query.
   */
@Override
public FunctionValues getValues(Map context, LeafReaderContext readerContext) throws IOException {
    LeafReader reader = readerContext.reader();
    final NumericDocValues ptX = DocValues.getNumeric(reader, strategy.getFieldNameX());
    final NumericDocValues ptY = DocValues.getNumeric(reader, strategy.getFieldNameY());
    return new FunctionValues() {

        private int lastDocID = -1;

        private final Point from = DistanceValueSource.this.from;

        private final DistanceCalculator calculator = strategy.getSpatialContext().getDistCalc();

        private final double nullValue = (strategy.getSpatialContext().isGeo() ? 180 * multiplier : Double.MAX_VALUE);

        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 float floatVal(int doc) throws IOException {
            return (float) doubleVal(doc);
        }

        @Override
        public double doubleVal(int doc) throws IOException {
            // make sure it has minX and area
            double x = getDocValue(ptX, doc);
            if (ptX.docID() == doc) {
                double y = getDocValue(ptY, doc);
                assert ptY.docID() == doc;
                return calculator.distance(from, x, y) * multiplier;
            }
            return nullValue;
        }

        @Override
        public String toString(int doc) throws IOException {
            return description() + "=" + floatVal(doc);
        }
    };
}
Also used : DistanceCalculator(org.locationtech.spatial4j.distance.DistanceCalculator) NumericDocValues(org.apache.lucene.index.NumericDocValues) LeafReader(org.apache.lucene.index.LeafReader) FunctionValues(org.apache.lucene.queries.function.FunctionValues) Point(org.locationtech.spatial4j.shape.Point) Point(org.locationtech.spatial4j.shape.Point)

Example 20 with FunctionValues

use of org.apache.lucene.queries.function.FunctionValues in project lucene-solr by apache.

the class ValueSourceAugmenter method transform.

@Override
public void transform(SolrDocument doc, int docid, float score) {
    try {
        // TODO: calculate this stuff just once across diff functions
        int idx = ReaderUtil.subIndex(docid, readerContexts);
        LeafReaderContext rcontext = readerContexts.get(idx);
        FunctionValues values = valueSource.getValues(fcontext, rcontext);
        int localId = docid - rcontext.docBase;
        setValue(doc, values.objectVal(localId));
    } catch (IOException e) {
        throw new SolrException(SolrException.ErrorCode.SERVER_ERROR, "exception at docid " + docid + " for valuesource " + valueSource, e);
    }
}
Also used : LeafReaderContext(org.apache.lucene.index.LeafReaderContext) FunctionValues(org.apache.lucene.queries.function.FunctionValues) IOException(java.io.IOException) SolrException(org.apache.solr.common.SolrException)

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