|
| 1 | +/* |
| 2 | + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one |
| 3 | + * or more contributor license agreements. Licensed under the Elastic License; |
| 4 | + * you may not use this file except in compliance with the Elastic License. |
| 5 | + */ |
| 6 | + |
| 7 | + |
| 8 | +package org.elasticsearch.xpack.spatial.aggregations.metrics; |
| 9 | + |
| 10 | +import org.apache.lucene.index.LeafReaderContext; |
| 11 | +import org.elasticsearch.common.geo.GeoPoint; |
| 12 | +import org.elasticsearch.common.lease.Releasables; |
| 13 | +import org.elasticsearch.common.util.BigArrays; |
| 14 | +import org.elasticsearch.common.util.ByteArray; |
| 15 | +import org.elasticsearch.common.util.DoubleArray; |
| 16 | +import org.elasticsearch.common.util.LongArray; |
| 17 | +import org.elasticsearch.search.aggregations.Aggregator; |
| 18 | +import org.elasticsearch.search.aggregations.InternalAggregation; |
| 19 | +import org.elasticsearch.search.aggregations.LeafBucketCollector; |
| 20 | +import org.elasticsearch.search.aggregations.LeafBucketCollectorBase; |
| 21 | +import org.elasticsearch.search.aggregations.metrics.CompensatedSum; |
| 22 | +import org.elasticsearch.search.aggregations.metrics.InternalGeoCentroid; |
| 23 | +import org.elasticsearch.search.aggregations.metrics.MetricsAggregator; |
| 24 | +import org.elasticsearch.search.internal.SearchContext; |
| 25 | +import org.elasticsearch.xpack.spatial.index.fielddata.DimensionalShapeType; |
| 26 | +import org.elasticsearch.xpack.spatial.index.fielddata.MultiGeoShapeValues; |
| 27 | +import org.elasticsearch.xpack.spatial.search.aggregations.support.GeoShapeValuesSource; |
| 28 | + |
| 29 | +import java.io.IOException; |
| 30 | +import java.util.Map; |
| 31 | + |
| 32 | +/** |
| 33 | + * A geo metric aggregator that computes a geo-centroid from a {@code geo_shape} type field |
| 34 | + */ |
| 35 | +public final class GeoShapeCentroidAggregator extends MetricsAggregator { |
| 36 | + private final GeoShapeValuesSource valuesSource; |
| 37 | + private DoubleArray lonSum, lonCompensations, latSum, latCompensations, weightSum, weightCompensations; |
| 38 | + private LongArray counts; |
| 39 | + private ByteArray dimensionalShapeTypes; |
| 40 | + |
| 41 | + public GeoShapeCentroidAggregator(String name, SearchContext context, Aggregator parent, |
| 42 | + GeoShapeValuesSource valuesSource, Map<String, Object> metadata) throws IOException { |
| 43 | + super(name, context, parent, metadata); |
| 44 | + this.valuesSource = valuesSource; |
| 45 | + if (valuesSource != null) { |
| 46 | + final BigArrays bigArrays = context.bigArrays(); |
| 47 | + lonSum = bigArrays.newDoubleArray(1, true); |
| 48 | + lonCompensations = bigArrays.newDoubleArray(1, true); |
| 49 | + latSum = bigArrays.newDoubleArray(1, true); |
| 50 | + latCompensations = bigArrays.newDoubleArray(1, true); |
| 51 | + weightSum = bigArrays.newDoubleArray(1, true); |
| 52 | + weightCompensations = bigArrays.newDoubleArray(1, true); |
| 53 | + counts = bigArrays.newLongArray(1, true); |
| 54 | + dimensionalShapeTypes = bigArrays.newByteArray(1, true); |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + @Override |
| 59 | + public LeafBucketCollector getLeafCollector(LeafReaderContext ctx, LeafBucketCollector sub) throws IOException { |
| 60 | + if (valuesSource == null) { |
| 61 | + return LeafBucketCollector.NO_OP_COLLECTOR; |
| 62 | + } |
| 63 | + final BigArrays bigArrays = context.bigArrays(); |
| 64 | + final MultiGeoShapeValues values = valuesSource.geoShapeValues(ctx); |
| 65 | + final CompensatedSum compensatedSumLat = new CompensatedSum(0, 0); |
| 66 | + final CompensatedSum compensatedSumLon = new CompensatedSum(0, 0); |
| 67 | + final CompensatedSum compensatedSumWeight = new CompensatedSum(0, 0); |
| 68 | + |
| 69 | + return new LeafBucketCollectorBase(sub, values) { |
| 70 | + @Override |
| 71 | + public void collect(int doc, long bucket) throws IOException { |
| 72 | + latSum = bigArrays.grow(latSum, bucket + 1); |
| 73 | + lonSum = bigArrays.grow(lonSum, bucket + 1); |
| 74 | + weightSum = bigArrays.grow(weightSum, bucket + 1); |
| 75 | + lonCompensations = bigArrays.grow(lonCompensations, bucket + 1); |
| 76 | + latCompensations = bigArrays.grow(latCompensations, bucket + 1); |
| 77 | + weightCompensations = bigArrays.grow(weightCompensations, bucket + 1); |
| 78 | + counts = bigArrays.grow(counts, bucket + 1); |
| 79 | + dimensionalShapeTypes = bigArrays.grow(dimensionalShapeTypes, bucket + 1); |
| 80 | + |
| 81 | + if (values.advanceExact(doc)) { |
| 82 | + final int valueCount = values.docValueCount(); |
| 83 | + // increment by the number of points for this document |
| 84 | + counts.increment(bucket, valueCount); |
| 85 | + // Compute the sum of double values with Kahan summation algorithm which is more |
| 86 | + // accurate than naive summation. |
| 87 | + DimensionalShapeType shapeType = DimensionalShapeType.fromOrdinalByte(dimensionalShapeTypes.get(bucket)); |
| 88 | + double sumLat = latSum.get(bucket); |
| 89 | + double compensationLat = latCompensations.get(bucket); |
| 90 | + double sumLon = lonSum.get(bucket); |
| 91 | + double compensationLon = lonCompensations.get(bucket); |
| 92 | + double sumWeight = weightSum.get(bucket); |
| 93 | + double compensatedWeight = weightCompensations.get(bucket); |
| 94 | + |
| 95 | + compensatedSumLat.reset(sumLat, compensationLat); |
| 96 | + compensatedSumLon.reset(sumLon, compensationLon); |
| 97 | + compensatedSumWeight.reset(sumWeight, compensatedWeight); |
| 98 | + |
| 99 | + // update the sum |
| 100 | + for (int i = 0; i < valueCount; ++i) { |
| 101 | + MultiGeoShapeValues.GeoShapeValue value = values.nextValue(); |
| 102 | + int compares = shapeType.compareTo(value.dimensionalShapeType()); |
| 103 | + if (compares < 0) { |
| 104 | + double coordinateWeight = value.weight(); |
| 105 | + compensatedSumLat.reset(coordinateWeight * value.lat(), 0.0); |
| 106 | + compensatedSumLon.reset(coordinateWeight * value.lon(), 0.0); |
| 107 | + compensatedSumWeight.reset(coordinateWeight, 0.0); |
| 108 | + dimensionalShapeTypes.set(bucket, (byte) value.dimensionalShapeType().ordinal()); |
| 109 | + } else if (compares == 0) { |
| 110 | + double coordinateWeight = value.weight(); |
| 111 | + // weighted latitude |
| 112 | + compensatedSumLat.add(coordinateWeight * value.lat()); |
| 113 | + // weighted longitude |
| 114 | + compensatedSumLon.add(coordinateWeight * value.lon()); |
| 115 | + // weight |
| 116 | + compensatedSumWeight.add(coordinateWeight); |
| 117 | + } |
| 118 | + // else (compares > 0) |
| 119 | + // do not modify centroid calculation since shape is of lower dimension than the running dimension |
| 120 | + |
| 121 | + } |
| 122 | + lonSum.set(bucket, compensatedSumLon.value()); |
| 123 | + lonCompensations.set(bucket, compensatedSumLon.delta()); |
| 124 | + latSum.set(bucket, compensatedSumLat.value()); |
| 125 | + latCompensations.set(bucket, compensatedSumLat.delta()); |
| 126 | + weightSum.set(bucket, compensatedSumWeight.value()); |
| 127 | + weightCompensations.set(bucket, compensatedSumWeight.delta()); |
| 128 | + } |
| 129 | + } |
| 130 | + }; |
| 131 | + } |
| 132 | + |
| 133 | + @Override |
| 134 | + public InternalAggregation buildAggregation(long bucket) { |
| 135 | + if (valuesSource == null || bucket >= counts.size()) { |
| 136 | + return buildEmptyAggregation(); |
| 137 | + } |
| 138 | + final long bucketCount = counts.get(bucket); |
| 139 | + final double bucketWeight = weightSum.get(bucket); |
| 140 | + final GeoPoint bucketCentroid = (bucketWeight > 0) |
| 141 | + ? new GeoPoint(latSum.get(bucket) / bucketWeight, lonSum.get(bucket) / bucketWeight) |
| 142 | + : null; |
| 143 | + return new InternalGeoCentroid(name, bucketCentroid , bucketCount, metadata()); |
| 144 | + } |
| 145 | + |
| 146 | + @Override |
| 147 | + public InternalAggregation buildEmptyAggregation() { |
| 148 | + return new InternalGeoCentroid(name, null, 0L, metadata()); |
| 149 | + } |
| 150 | + |
| 151 | + @Override |
| 152 | + public void doClose() { |
| 153 | + Releasables.close(latSum, latCompensations, lonSum, lonCompensations, counts, weightSum, weightCompensations, |
| 154 | + dimensionalShapeTypes); |
| 155 | + } |
| 156 | +} |
0 commit comments