|
| 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 | +package org.elasticsearch.xpack.spatial.search.aggregations; |
| 7 | + |
| 8 | +import org.apache.lucene.geo.GeoEncodingUtils; |
| 9 | +import org.apache.lucene.index.LeafReaderContext; |
| 10 | +import org.apache.lucene.search.ScoreMode; |
| 11 | +import org.apache.lucene.util.ArrayUtil; |
| 12 | +import org.apache.lucene.util.NumericUtils; |
| 13 | +import org.elasticsearch.common.geo.GeoPoint; |
| 14 | +import org.elasticsearch.common.lease.Releasables; |
| 15 | +import org.elasticsearch.common.util.BigArrays; |
| 16 | +import org.elasticsearch.common.util.IntArray; |
| 17 | +import org.elasticsearch.common.util.ObjectArray; |
| 18 | +import org.elasticsearch.index.fielddata.MultiGeoPointValues; |
| 19 | +import org.elasticsearch.index.fielddata.SortedNumericDoubleValues; |
| 20 | +import org.elasticsearch.search.aggregations.AggregationExecutionException; |
| 21 | +import org.elasticsearch.search.aggregations.Aggregator; |
| 22 | +import org.elasticsearch.search.aggregations.InternalAggregation; |
| 23 | +import org.elasticsearch.search.aggregations.LeafBucketCollector; |
| 24 | +import org.elasticsearch.search.aggregations.LeafBucketCollectorBase; |
| 25 | +import org.elasticsearch.search.aggregations.metrics.MetricsAggregator; |
| 26 | +import org.elasticsearch.search.aggregations.pipeline.PipelineAggregator; |
| 27 | +import org.elasticsearch.search.aggregations.support.MultiValuesSource; |
| 28 | +import org.elasticsearch.search.internal.SearchContext; |
| 29 | + |
| 30 | +import java.io.IOException; |
| 31 | +import java.util.List; |
| 32 | +import java.util.Map; |
| 33 | + |
| 34 | +import static org.elasticsearch.xpack.spatial.search.aggregations.GeoLineAggregationBuilder.GEO_POINT_FIELD; |
| 35 | +import static org.elasticsearch.xpack.spatial.search.aggregations.GeoLineAggregationBuilder.SORT_FIELD; |
| 36 | + |
| 37 | +/** |
| 38 | + * Metric Aggregation for computing the pearson product correlation coefficient between multiple fields |
| 39 | + **/ |
| 40 | +final class GeoLineAggregator extends MetricsAggregator { |
| 41 | + /** Multiple ValuesSource with field names */ |
| 42 | + private final MultiValuesSource.AnyMultiValuesSource valuesSources; |
| 43 | + |
| 44 | + private ObjectArray<long[]> paths; |
| 45 | + private ObjectArray<double[]> sortValues; |
| 46 | + private IntArray idxs; |
| 47 | + |
| 48 | + GeoLineAggregator(String name, MultiValuesSource.AnyMultiValuesSource valuesSources, SearchContext context, |
| 49 | + Aggregator parent, List<PipelineAggregator> pipelineAggregators, |
| 50 | + Map<String,Object> metaData) throws IOException { |
| 51 | + super(name, context, parent, pipelineAggregators, metaData); |
| 52 | + this.valuesSources = valuesSources; |
| 53 | + if (valuesSources != null) { |
| 54 | + paths = context.bigArrays().newObjectArray(1); |
| 55 | + sortValues = context.bigArrays().newObjectArray(1); |
| 56 | + idxs = context.bigArrays().newIntArray(1); |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + @Override |
| 61 | + public ScoreMode scoreMode() { |
| 62 | + if (valuesSources != null && valuesSources.needsScores()) { |
| 63 | + return ScoreMode.COMPLETE; |
| 64 | + } |
| 65 | + return super.scoreMode(); |
| 66 | + } |
| 67 | + |
| 68 | + @Override |
| 69 | + public LeafBucketCollector getLeafCollector(LeafReaderContext ctx, |
| 70 | + final LeafBucketCollector sub) throws IOException { |
| 71 | + if (valuesSources == null) { |
| 72 | + return LeafBucketCollector.NO_OP_COLLECTOR; |
| 73 | + } |
| 74 | + final BigArrays bigArrays = context.bigArrays(); |
| 75 | + MultiGeoPointValues docGeoPointValues = valuesSources.getGeoPointField(GEO_POINT_FIELD.getPreferredName(), ctx); |
| 76 | + SortedNumericDoubleValues docSortValues = valuesSources.getNumericField(SORT_FIELD.getPreferredName(), ctx); |
| 77 | + |
| 78 | + return new LeafBucketCollectorBase(sub, docGeoPointValues) { |
| 79 | + @Override |
| 80 | + public void collect(int doc, long bucket) throws IOException { |
| 81 | + paths = bigArrays.grow(paths, bucket + 1); |
| 82 | + if (docGeoPointValues.advanceExact(doc) && docSortValues.advanceExact(doc)) { |
| 83 | + if (docSortValues.docValueCount() > 1) { |
| 84 | + throw new AggregationExecutionException("Encountered more than one sort value for a " + |
| 85 | + "single document. Use a script to combine multiple sort-values-per-doc into a single value."); |
| 86 | + } |
| 87 | + if (docGeoPointValues.docValueCount() > 1) { |
| 88 | + throw new AggregationExecutionException("Encountered more than one geo_point value for a " + |
| 89 | + "single document. Use a script to combine multiple geo_point-values-per-doc into a single value."); |
| 90 | + } |
| 91 | + |
| 92 | + // There should always be one weight if advanceExact lands us here, either |
| 93 | + // a real weight or a `missing` weight |
| 94 | + assert docSortValues.docValueCount() == 1; |
| 95 | + assert docGeoPointValues.docValueCount() == 1; |
| 96 | + final double sort = docSortValues.nextValue(); |
| 97 | + final GeoPoint point = docGeoPointValues.nextValue(); |
| 98 | + |
| 99 | + int idx = idxs.get(bucket); |
| 100 | + long[] bucketLine = paths.get(bucket); |
| 101 | + double[] sortVals = sortValues.get(bucket); |
| 102 | + if (bucketLine == null) { |
| 103 | + bucketLine = new long[10]; |
| 104 | + } else { |
| 105 | + bucketLine = ArrayUtil.grow(bucketLine, idx + 1); |
| 106 | + } |
| 107 | + |
| 108 | + |
| 109 | + if (sortVals == null) { |
| 110 | + sortVals = new double[10]; |
| 111 | + } else { |
| 112 | + sortVals = ArrayUtil.grow(sortVals, idx + 1); |
| 113 | + } |
| 114 | + |
| 115 | + int encodedLat = GeoEncodingUtils.encodeLatitude(point.lat()); |
| 116 | + int encodedLon = GeoEncodingUtils.encodeLongitude(point.lon()); |
| 117 | + long lonLat = (((long) encodedLon) << 32) | (encodedLat & 0xffffffffL); |
| 118 | + |
| 119 | + sortVals[idx] = sort; |
| 120 | + bucketLine[idx] = lonLat; |
| 121 | + |
| 122 | + paths.set(bucket, bucketLine); |
| 123 | + sortValues.set(bucket, sortVals); |
| 124 | + idxs.set(bucket, idx + 1); |
| 125 | + } |
| 126 | + } |
| 127 | + }; |
| 128 | + } |
| 129 | + |
| 130 | + @Override |
| 131 | + public InternalAggregation buildAggregation(long bucket) { |
| 132 | + if (valuesSources == null) { |
| 133 | + return buildEmptyAggregation(); |
| 134 | + } |
| 135 | + long[] bucketLine = paths.get(bucket); |
| 136 | + double[] sortVals = sortValues.get(bucket); |
| 137 | + int length = idxs.get(bucket); |
| 138 | + new PathArraySorter(bucketLine, sortVals, length).sort(); |
| 139 | + return new InternalGeoLine(name, bucketLine, sortVals, length, pipelineAggregators(), metaData()); |
| 140 | + } |
| 141 | + |
| 142 | + @Override |
| 143 | + public InternalAggregation buildEmptyAggregation() { |
| 144 | + return new InternalGeoLine(name, null, null, 0, pipelineAggregators(), metaData()); |
| 145 | + } |
| 146 | + |
| 147 | + @Override |
| 148 | + public void doClose() { |
| 149 | + Releasables.close(paths, idxs, sortValues); |
| 150 | + } |
| 151 | +} |
0 commit comments