Search in sources :

Example 1 with SortingNumericDoubleValues

use of org.elasticsearch.index.fielddata.SortingNumericDoubleValues in project elasticsearch by elastic.

the class GeoUtils method distanceValues.

/**
     * Return a {@link SortedNumericDoubleValues} instance that returns the distances to a list of geo-points
     * for each document.
     */
public static SortedNumericDoubleValues distanceValues(final GeoDistance distance, final DistanceUnit unit, final MultiGeoPointValues geoPointValues, final GeoPoint... fromPoints) {
    final GeoPointValues singleValues = FieldData.unwrapSingleton(geoPointValues);
    if (singleValues != null && fromPoints.length == 1) {
        final Bits docsWithField = FieldData.unwrapSingletonBits(geoPointValues);
        return FieldData.singleton(new NumericDoubleValues() {

            @Override
            public double get(int docID) {
                if (docsWithField != null && !docsWithField.get(docID)) {
                    return 0d;
                }
                final GeoPoint to = singleValues.get(docID);
                final GeoPoint from = fromPoints[0];
                return distance.calculate(from.lat(), from.lon(), to.lat(), to.lon(), unit);
            }
        }, docsWithField);
    } else {
        return new SortingNumericDoubleValues() {

            @Override
            public void setDocument(int doc) {
                geoPointValues.setDocument(doc);
                resize(geoPointValues.count() * fromPoints.length);
                int v = 0;
                for (GeoPoint from : fromPoints) {
                    for (int i = 0; i < geoPointValues.count(); ++i) {
                        final GeoPoint point = geoPointValues.valueAt(i);
                        values[v] = distance.calculate(from.lat(), from.lon(), point.lat(), point.lon(), unit);
                        v++;
                    }
                }
                sort();
            }
        };
    }
}
Also used : SortingNumericDoubleValues(org.elasticsearch.index.fielddata.SortingNumericDoubleValues) Bits(org.apache.lucene.util.Bits) MultiGeoPointValues(org.elasticsearch.index.fielddata.MultiGeoPointValues) GeoPointValues(org.elasticsearch.index.fielddata.GeoPointValues) SortingNumericDoubleValues(org.elasticsearch.index.fielddata.SortingNumericDoubleValues) NumericDoubleValues(org.elasticsearch.index.fielddata.NumericDoubleValues) SortedNumericDoubleValues(org.elasticsearch.index.fielddata.SortedNumericDoubleValues)

Aggregations

Bits (org.apache.lucene.util.Bits)1 GeoPointValues (org.elasticsearch.index.fielddata.GeoPointValues)1 MultiGeoPointValues (org.elasticsearch.index.fielddata.MultiGeoPointValues)1 NumericDoubleValues (org.elasticsearch.index.fielddata.NumericDoubleValues)1 SortedNumericDoubleValues (org.elasticsearch.index.fielddata.SortedNumericDoubleValues)1 SortingNumericDoubleValues (org.elasticsearch.index.fielddata.SortingNumericDoubleValues)1