-
Notifications
You must be signed in to change notification settings - Fork 25.2k
Add geo_bounds aggregation support for geo_shape #55328
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 7 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
f953e1a
add geo_bounds for shapes
talevy 1ef925c
add yaml tests
talevy 9e72301
fix
talevy 0491b5f
Merge remote-tracking branch 'elastic/master' into geo_bounds2
talevy 20ec976
inherit from MetricsAggregator and fix tests
talevy 2548f96
revert the upgraded cluster skipping tests
talevy 968a084
Merge remote-tracking branch 'elastic/master' into geo_bounds2
talevy e4b30d4
remove bwc tests to be merged in 7.x. fix scoping
talevy a77692c
Merge remote-tracking branch 'elastic/master' into geo_bounds2
talevy File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
134 changes: 134 additions & 0 deletions
134
...n/java/org/elasticsearch/xpack/spatial/aggregations/metrics/GeoShapeBoundsAggregator.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,134 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License; | ||
* you may not use this file except in compliance with the Elastic License. | ||
*/ | ||
|
||
package org.elasticsearch.xpack.spatial.aggregations.metrics; | ||
|
||
import org.apache.lucene.index.LeafReaderContext; | ||
import org.elasticsearch.common.lease.Releasables; | ||
import org.elasticsearch.common.util.BigArrays; | ||
import org.elasticsearch.common.util.DoubleArray; | ||
import org.elasticsearch.search.aggregations.Aggregator; | ||
import org.elasticsearch.search.aggregations.InternalAggregation; | ||
import org.elasticsearch.search.aggregations.LeafBucketCollector; | ||
import org.elasticsearch.search.aggregations.LeafBucketCollectorBase; | ||
import org.elasticsearch.search.aggregations.metrics.InternalGeoBounds; | ||
import org.elasticsearch.search.aggregations.metrics.MetricsAggregator; | ||
import org.elasticsearch.search.internal.SearchContext; | ||
import org.elasticsearch.xpack.spatial.index.mapper.GeoShapeValuesSource; | ||
import org.elasticsearch.xpack.spatial.index.mapper.MultiGeoShapeValues; | ||
|
||
import java.io.IOException; | ||
import java.util.Map; | ||
|
||
public final class GeoShapeBoundsAggregator extends MetricsAggregator { | ||
private final GeoShapeValuesSource valuesSource; | ||
private final boolean wrapLongitude; | ||
protected DoubleArray tops; | ||
protected DoubleArray bottoms; | ||
protected DoubleArray posLefts; | ||
protected DoubleArray posRights; | ||
protected DoubleArray negLefts; | ||
protected DoubleArray negRights; | ||
|
||
public GeoShapeBoundsAggregator(String name, SearchContext aggregationContext, Aggregator parent, | ||
GeoShapeValuesSource valuesSource, boolean wrapLongitude, Map<String, Object> metadata) throws IOException { | ||
super(name, aggregationContext, parent, metadata); | ||
this.valuesSource = valuesSource; | ||
this.wrapLongitude = wrapLongitude; | ||
if (valuesSource != null) { | ||
final BigArrays bigArrays = context.bigArrays(); | ||
tops = bigArrays.newDoubleArray(1, false); | ||
tops.fill(0, tops.size(), Double.NEGATIVE_INFINITY); | ||
bottoms = bigArrays.newDoubleArray(1, false); | ||
bottoms.fill(0, bottoms.size(), Double.POSITIVE_INFINITY); | ||
posLefts = bigArrays.newDoubleArray(1, false); | ||
posLefts.fill(0, posLefts.size(), Double.POSITIVE_INFINITY); | ||
posRights = bigArrays.newDoubleArray(1, false); | ||
posRights.fill(0, posRights.size(), Double.NEGATIVE_INFINITY); | ||
negLefts = bigArrays.newDoubleArray(1, false); | ||
negLefts.fill(0, negLefts.size(), Double.POSITIVE_INFINITY); | ||
negRights = bigArrays.newDoubleArray(1, false); | ||
negRights.fill(0, negRights.size(), Double.NEGATIVE_INFINITY); | ||
} | ||
} | ||
|
||
@Override | ||
public LeafBucketCollector getLeafCollector(LeafReaderContext ctx, | ||
LeafBucketCollector sub) { | ||
if (valuesSource == null) { | ||
return LeafBucketCollector.NO_OP_COLLECTOR; | ||
} | ||
final BigArrays bigArrays = context.bigArrays(); | ||
final MultiGeoShapeValues values = valuesSource.geoShapeValues(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); | ||
} | ||
|
||
if (values.advanceExact(doc)) { | ||
final int valuesCount = values.docValueCount(); | ||
|
||
for (int i = 0; i < valuesCount; ++i) { | ||
MultiGeoShapeValues.GeoShapeValue value = values.nextValue(); | ||
MultiGeoShapeValues.BoundingBox bounds = value.boundingBox(); | ||
double top = Math.max(tops.get(bucket), bounds.top); | ||
double bottom = Math.min(bottoms.get(bucket), bounds.bottom); | ||
double posLeft = Math.min(posLefts.get(bucket), bounds.posLeft); | ||
double posRight = Math.max(posRights.get(bucket), bounds.posRight); | ||
double negLeft = Math.min(negLefts.get(bucket), bounds.negLeft); | ||
double negRight = Math.max(negRights.get(bucket), bounds.negRight); | ||
tops.set(bucket, top); | ||
bottoms.set(bucket, bottom); | ||
posLefts.set(bucket, posLeft); | ||
posRights.set(bucket, posRight); | ||
negLefts.set(bucket, negLeft); | ||
negRights.set(bucket, negRight); | ||
} | ||
} | ||
} | ||
}; | ||
} | ||
|
||
|
||
@Override | ||
public InternalAggregation buildAggregation(long owningBucketOrdinal) { | ||
if (valuesSource == null) { | ||
return buildEmptyAggregation(); | ||
} | ||
double top = tops.get(owningBucketOrdinal); | ||
double bottom = bottoms.get(owningBucketOrdinal); | ||
double posLeft = posLefts.get(owningBucketOrdinal); | ||
double posRight = posRights.get(owningBucketOrdinal); | ||
double negLeft = negLefts.get(owningBucketOrdinal); | ||
double negRight = negRights.get(owningBucketOrdinal); | ||
return new InternalGeoBounds(name, top, bottom, posLeft, posRight, negLeft, negRight, wrapLongitude, metadata()); | ||
} | ||
|
||
@Override | ||
public InternalAggregation buildEmptyAggregation() { | ||
return new InternalGeoBounds(name, Double.NEGATIVE_INFINITY, Double.POSITIVE_INFINITY, Double.POSITIVE_INFINITY, | ||
Double.NEGATIVE_INFINITY, Double.POSITIVE_INFINITY, Double.NEGATIVE_INFINITY, wrapLongitude, metadata()); | ||
} | ||
|
||
@Override | ||
public void doClose() { | ||
Releasables.close(tops, bottoms, posLefts, posRights, negLefts, negRights); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.