Search in sources :

Example 1 with MultiGeoPointValues

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

the class GeoLatitudeValueSource method getValues.

@Override
// ValueSource uses a rawtype
@SuppressWarnings("rawtypes")
public FunctionValues getValues(Map context, LeafReaderContext leaf) throws IOException {
    AtomicGeoPointFieldData leafData = (AtomicGeoPointFieldData) fieldData.load(leaf);
    final MultiGeoPointValues values = leafData.getGeoPointValues();
    return new DoubleDocValues(this) {

        @Override
        public double doubleVal(int doc) {
            values.setDocument(doc);
            if (values.count() == 0) {
                return 0.0;
            } else {
                return values.valueAt(0).getLat();
            }
        }
    };
}
Also used : AtomicGeoPointFieldData(org.elasticsearch.index.fielddata.AtomicGeoPointFieldData) DoubleDocValues(org.apache.lucene.queries.function.docvalues.DoubleDocValues) MultiGeoPointValues(org.elasticsearch.index.fielddata.MultiGeoPointValues)

Example 2 with MultiGeoPointValues

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

the class GeoLongitudeValueSource method getValues.

@Override
// ValueSource uses a rawtype
@SuppressWarnings("rawtypes")
public FunctionValues getValues(Map context, LeafReaderContext leaf) throws IOException {
    AtomicGeoPointFieldData leafData = (AtomicGeoPointFieldData) fieldData.load(leaf);
    final MultiGeoPointValues values = leafData.getGeoPointValues();
    return new DoubleDocValues(this) {

        @Override
        public double doubleVal(int doc) {
            values.setDocument(doc);
            if (values.count() == 0) {
                return 0.0;
            } else {
                return values.valueAt(0).getLon();
            }
        }
    };
}
Also used : AtomicGeoPointFieldData(org.elasticsearch.index.fielddata.AtomicGeoPointFieldData) DoubleDocValues(org.apache.lucene.queries.function.docvalues.DoubleDocValues) MultiGeoPointValues(org.elasticsearch.index.fielddata.MultiGeoPointValues)

Example 3 with MultiGeoPointValues

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

the class GeoDistanceSortBuilder method build.

@Override
public SortFieldAndFormat build(QueryShardContext context) throws IOException {
    final boolean indexCreatedBeforeV2_0 = context.indexVersionCreated().before(Version.V_2_0_0);
    // validation was not available prior to 2.x, so to support bwc percolation queries we only ignore_malformed
    // on 2.x created indexes
    GeoPoint[] localPoints = points.toArray(new GeoPoint[points.size()]);
    if (!indexCreatedBeforeV2_0 && !GeoValidationMethod.isIgnoreMalformed(validation)) {
        for (GeoPoint point : localPoints) {
            if (GeoUtils.isValidLatitude(point.lat()) == false) {
                throw new ElasticsearchParseException("illegal latitude value [{}] for [GeoDistanceSort] for field [{}].", point.lat(), fieldName);
            }
            if (GeoUtils.isValidLongitude(point.lon()) == false) {
                throw new ElasticsearchParseException("illegal longitude value [{}] for [GeoDistanceSort] for field [{}].", point.lon(), fieldName);
            }
        }
    }
    if (GeoValidationMethod.isCoerce(validation)) {
        for (GeoPoint point : localPoints) {
            GeoUtils.normalizePoint(point, true, true);
        }
    }
    boolean reverse = (order == SortOrder.DESC);
    final MultiValueMode finalSortMode;
    if (sortMode == null) {
        finalSortMode = reverse ? MultiValueMode.MAX : MultiValueMode.MIN;
    } else {
        finalSortMode = MultiValueMode.fromString(sortMode.toString());
    }
    MappedFieldType fieldType = context.fieldMapper(fieldName);
    if (fieldType == null) {
        throw new IllegalArgumentException("failed to find mapper for [" + fieldName + "] for geo distance based sort");
    }
    final IndexGeoPointFieldData geoIndexFieldData = context.getForField(fieldType);
    final Nested nested = resolveNested(context, nestedPath, nestedFilter);
    if (// only works with 5.x geo_point
    geoIndexFieldData.getClass() == LatLonPointDVIndexFieldData.class && nested == null && // LatLonDocValuesField internally picks the closest point
    finalSortMode == MultiValueMode.MIN && unit == DistanceUnit.METERS && reverse == false && localPoints.length == 1) {
        return new SortFieldAndFormat(LatLonDocValuesField.newDistanceSort(fieldName, localPoints[0].lat(), localPoints[0].lon()), DocValueFormat.RAW);
    }
    IndexFieldData.XFieldComparatorSource geoDistanceComparatorSource = new IndexFieldData.XFieldComparatorSource() {

        @Override
        public SortField.Type reducedType() {
            return SortField.Type.DOUBLE;
        }

        @Override
        public FieldComparator<?> newComparator(String fieldname, int numHits, int sortPos, boolean reversed) {
            return new FieldComparator.DoubleComparator(numHits, null, null) {

                @Override
                protected NumericDocValues getNumericDocValues(LeafReaderContext context, String field) throws IOException {
                    final MultiGeoPointValues geoPointValues = geoIndexFieldData.load(context).getGeoPointValues();
                    final SortedNumericDoubleValues distanceValues = GeoUtils.distanceValues(geoDistance, unit, geoPointValues, localPoints);
                    final NumericDoubleValues selectedValues;
                    if (nested == null) {
                        selectedValues = finalSortMode.select(distanceValues, Double.POSITIVE_INFINITY);
                    } else {
                        final BitSet rootDocs = nested.rootDocs(context);
                        final DocIdSetIterator innerDocs = nested.innerDocs(context);
                        selectedValues = finalSortMode.select(distanceValues, Double.POSITIVE_INFINITY, rootDocs, innerDocs, context.reader().maxDoc());
                    }
                    return selectedValues.getRawDoubleValues();
                }
            };
        }
    };
    return new SortFieldAndFormat(new SortField(fieldName, geoDistanceComparatorSource, reverse), DocValueFormat.RAW);
}
Also used : IndexGeoPointFieldData(org.elasticsearch.index.fielddata.IndexGeoPointFieldData) Nested(org.elasticsearch.index.fielddata.IndexFieldData.XFieldComparatorSource.Nested) BitSet(org.apache.lucene.util.BitSet) SortField(org.apache.lucene.search.SortField) NumericDoubleValues(org.elasticsearch.index.fielddata.NumericDoubleValues) SortedNumericDoubleValues(org.elasticsearch.index.fielddata.SortedNumericDoubleValues) MultiValueMode(org.elasticsearch.search.MultiValueMode) GeoPoint(org.elasticsearch.common.geo.GeoPoint) LatLonPointDVIndexFieldData(org.elasticsearch.index.fielddata.plain.AbstractLatLonPointDVIndexFieldData.LatLonPointDVIndexFieldData) ElasticsearchParseException(org.elasticsearch.ElasticsearchParseException) MappedFieldType(org.elasticsearch.index.mapper.MappedFieldType) LeafReaderContext(org.apache.lucene.index.LeafReaderContext) LatLonPointDVIndexFieldData(org.elasticsearch.index.fielddata.plain.AbstractLatLonPointDVIndexFieldData.LatLonPointDVIndexFieldData) IndexFieldData(org.elasticsearch.index.fielddata.IndexFieldData) SortedNumericDoubleValues(org.elasticsearch.index.fielddata.SortedNumericDoubleValues) MultiGeoPointValues(org.elasticsearch.index.fielddata.MultiGeoPointValues) DocIdSetIterator(org.apache.lucene.search.DocIdSetIterator)

Example 4 with MultiGeoPointValues

use of org.elasticsearch.index.fielddata.MultiGeoPointValues 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)

Example 5 with MultiGeoPointValues

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

the class GeoBoundsAggregator method getLeafCollector.

@Override
public LeafBucketCollector getLeafCollector(LeafReaderContext ctx, LeafBucketCollector sub) {
    if (valuesSource == null) {
        return LeafBucketCollector.NO_OP_COLLECTOR;
    }
    final BigArrays bigArrays = context.bigArrays();
    final MultiGeoPointValues values = valuesSource.geoPointValues(ctx);
    return new LeafBucketCollectorBase(sub, values) {

        @Override
        public void collect(int doc, long bucket) throws IOException {
            if (bucket >= tops.size()) {
                long from = tops.size();
                tops = bigArrays.grow(tops, bucket + 1);
                tops.fill(from, tops.size(), Double.NEGATIVE_INFINITY);
                bottoms = bigArrays.resize(bottoms, tops.size());
                bottoms.fill(from, bottoms.size(), Double.POSITIVE_INFINITY);
                posLefts = bigArrays.resize(posLefts, tops.size());
                posLefts.fill(from, posLefts.size(), Double.POSITIVE_INFINITY);
                posRights = bigArrays.resize(posRights, tops.size());
                posRights.fill(from, posRights.size(), Double.NEGATIVE_INFINITY);
                negLefts = bigArrays.resize(negLefts, tops.size());
                negLefts.fill(from, negLefts.size(), Double.POSITIVE_INFINITY);
                negRights = bigArrays.resize(negRights, tops.size());
                negRights.fill(from, negRights.size(), Double.NEGATIVE_INFINITY);
            }
            values.setDocument(doc);
            final int valuesCount = values.count();
            for (int i = 0; i < valuesCount; ++i) {
                GeoPoint value = values.valueAt(i);
                double top = tops.get(bucket);
                if (value.lat() > top) {
                    top = value.lat();
                }
                double bottom = bottoms.get(bucket);
                if (value.lat() < bottom) {
                    bottom = value.lat();
                }
                double posLeft = posLefts.get(bucket);
                if (value.lon() >= 0 && value.lon() < posLeft) {
                    posLeft = value.lon();
                }
                double posRight = posRights.get(bucket);
                if (value.lon() >= 0 && value.lon() > posRight) {
                    posRight = value.lon();
                }
                double negLeft = negLefts.get(bucket);
                if (value.lon() < 0 && value.lon() < negLeft) {
                    negLeft = value.lon();
                }
                double negRight = negRights.get(bucket);
                if (value.lon() < 0 && value.lon() > negRight) {
                    negRight = value.lon();
                }
                tops.set(bucket, top);
                bottoms.set(bucket, bottom);
                posLefts.set(bucket, posLeft);
                posRights.set(bucket, posRight);
                negLefts.set(bucket, negLeft);
                negRights.set(bucket, negRight);
            }
        }
    };
}
Also used : GeoPoint(org.elasticsearch.common.geo.GeoPoint) BigArrays(org.elasticsearch.common.util.BigArrays) LeafBucketCollectorBase(org.elasticsearch.search.aggregations.LeafBucketCollectorBase) MultiGeoPointValues(org.elasticsearch.index.fielddata.MultiGeoPointValues) GeoPoint(org.elasticsearch.common.geo.GeoPoint)

Aggregations

MultiGeoPointValues (org.elasticsearch.index.fielddata.MultiGeoPointValues)10 GeoPoint (org.elasticsearch.common.geo.GeoPoint)6 DoubleDocValues (org.apache.lucene.queries.function.docvalues.DoubleDocValues)3 AtomicGeoPointFieldData (org.elasticsearch.index.fielddata.AtomicGeoPointFieldData)3 BigArrays (org.elasticsearch.common.util.BigArrays)2 NumericDoubleValues (org.elasticsearch.index.fielddata.NumericDoubleValues)2 SortedNumericDoubleValues (org.elasticsearch.index.fielddata.SortedNumericDoubleValues)2 LeafBucketCollectorBase (org.elasticsearch.search.aggregations.LeafBucketCollectorBase)2 LeafReaderContext (org.apache.lucene.index.LeafReaderContext)1 DocIdSetIterator (org.apache.lucene.search.DocIdSetIterator)1 SortField (org.apache.lucene.search.SortField)1 BitSet (org.apache.lucene.util.BitSet)1 Bits (org.apache.lucene.util.Bits)1 ElasticsearchParseException (org.elasticsearch.ElasticsearchParseException)1 GeoPointValues (org.elasticsearch.index.fielddata.GeoPointValues)1 IndexFieldData (org.elasticsearch.index.fielddata.IndexFieldData)1 Nested (org.elasticsearch.index.fielddata.IndexFieldData.XFieldComparatorSource.Nested)1 IndexGeoPointFieldData (org.elasticsearch.index.fielddata.IndexGeoPointFieldData)1 SortingNumericDoubleValues (org.elasticsearch.index.fielddata.SortingNumericDoubleValues)1 LatLonPointDVIndexFieldData (org.elasticsearch.index.fielddata.plain.AbstractLatLonPointDVIndexFieldData.LatLonPointDVIndexFieldData)1