Search in sources :

Example 1 with SortingNumericDoubleValues

use of org.opensearch.index.fielddata.SortingNumericDoubleValues in project OpenSearch by opensearch-project.

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) {
        return FieldData.singleton(new NumericDoubleValues() {

            @Override
            public boolean advanceExact(int doc) throws IOException {
                return singleValues.advanceExact(doc);
            }

            @Override
            public double doubleValue() throws IOException {
                final GeoPoint from = fromPoints[0];
                final GeoPoint to = singleValues.geoPointValue();
                return distance.calculate(from.lat(), from.lon(), to.lat(), to.lon(), unit);
            }
        });
    } else {
        return new SortingNumericDoubleValues() {

            @Override
            public boolean advanceExact(int target) throws IOException {
                if (geoPointValues.advanceExact(target)) {
                    resize(geoPointValues.docValueCount() * fromPoints.length);
                    int v = 0;
                    for (int i = 0; i < geoPointValues.docValueCount(); ++i) {
                        final GeoPoint point = geoPointValues.nextValue();
                        for (GeoPoint from : fromPoints) {
                            values[v] = distance.calculate(from.lat(), from.lon(), point.lat(), point.lon(), unit);
                            v++;
                        }
                    }
                    sort();
                    return true;
                } else {
                    return false;
                }
            }
        };
    }
}
Also used : SortingNumericDoubleValues(org.opensearch.index.fielddata.SortingNumericDoubleValues) MultiGeoPointValues(org.opensearch.index.fielddata.MultiGeoPointValues) GeoPointValues(org.opensearch.index.fielddata.GeoPointValues) SortedNumericDoubleValues(org.opensearch.index.fielddata.SortedNumericDoubleValues) SortingNumericDoubleValues(org.opensearch.index.fielddata.SortingNumericDoubleValues) NumericDoubleValues(org.opensearch.index.fielddata.NumericDoubleValues) IOException(java.io.IOException)

Aggregations

IOException (java.io.IOException)1 GeoPointValues (org.opensearch.index.fielddata.GeoPointValues)1 MultiGeoPointValues (org.opensearch.index.fielddata.MultiGeoPointValues)1 NumericDoubleValues (org.opensearch.index.fielddata.NumericDoubleValues)1 SortedNumericDoubleValues (org.opensearch.index.fielddata.SortedNumericDoubleValues)1 SortingNumericDoubleValues (org.opensearch.index.fielddata.SortingNumericDoubleValues)1