Skip to content

Commit 5ae3f19

Browse files
authored
Wire up geo_bounds aggregation to ValuesSourceRegistry (#53034)
This commit updates the geo_bounds aggregation to depend on registering itself in the ValuesSourceRegistry relates #42949.
1 parent 29463c4 commit 5ae3f19

File tree

4 files changed

+63
-5
lines changed

4 files changed

+63
-5
lines changed

server/src/main/java/org/elasticsearch/search/SearchModule.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -446,7 +446,8 @@ private void registerAggregations(List<SearchPlugin> plugins) {
446446
registerAggregation(new AggregationSpec(TopHitsAggregationBuilder.NAME, TopHitsAggregationBuilder::new,
447447
TopHitsAggregationBuilder::parse).addResultReader(InternalTopHits::new));
448448
registerAggregation(new AggregationSpec(GeoBoundsAggregationBuilder.NAME, GeoBoundsAggregationBuilder::new,
449-
GeoBoundsAggregationBuilder::parse).addResultReader(InternalGeoBounds::new));
449+
GeoBoundsAggregationBuilder::parse).addResultReader(InternalGeoBounds::new)
450+
.setAggregatorRegistrar(GeoBoundsAggregationBuilder::registerAggregators));
450451
registerAggregation(new AggregationSpec(GeoCentroidAggregationBuilder.NAME, GeoCentroidAggregationBuilder::new,
451452
GeoCentroidAggregationBuilder::parse).addResultReader(InternalGeoCentroid::new));
452453
registerAggregation(new AggregationSpec(ScriptedMetricAggregationBuilder.NAME, ScriptedMetricAggregationBuilder::new,

server/src/main/java/org/elasticsearch/search/aggregations/metrics/GeoBoundsAggregationBuilder.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
import org.elasticsearch.search.aggregations.support.ValuesSourceAggregationBuilder;
3333
import org.elasticsearch.search.aggregations.support.ValuesSourceConfig;
3434
import org.elasticsearch.search.aggregations.support.ValuesSourceParserHelper;
35+
import org.elasticsearch.search.aggregations.support.ValuesSourceRegistry;
3536
import org.elasticsearch.search.aggregations.support.ValuesSourceType;
3637

3738
import java.io.IOException;
@@ -52,6 +53,10 @@ public static AggregationBuilder parse(String aggregationName, XContentParser pa
5253
return PARSER.parse(parser, new GeoBoundsAggregationBuilder(aggregationName), null);
5354
}
5455

56+
public static void registerAggregators(ValuesSourceRegistry valuesSourceRegistry) {
57+
GeoBoundsAggregatorFactory.registerAggregators(valuesSourceRegistry);
58+
}
59+
5560
private boolean wrapLongitude = true;
5661

5762
public GeoBoundsAggregationBuilder(String name) {

server/src/main/java/org/elasticsearch/search/aggregations/metrics/GeoBoundsAggregatorFactory.java

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,12 @@
2525
import org.elasticsearch.search.aggregations.AggregatorFactories;
2626
import org.elasticsearch.search.aggregations.AggregatorFactory;
2727
import org.elasticsearch.search.aggregations.pipeline.PipelineAggregator;
28+
import org.elasticsearch.search.aggregations.support.AggregatorSupplier;
29+
import org.elasticsearch.search.aggregations.support.CoreValuesSourceType;
2830
import org.elasticsearch.search.aggregations.support.ValuesSource;
2931
import org.elasticsearch.search.aggregations.support.ValuesSourceAggregatorFactory;
3032
import org.elasticsearch.search.aggregations.support.ValuesSourceConfig;
33+
import org.elasticsearch.search.aggregations.support.ValuesSourceRegistry;
3134
import org.elasticsearch.search.internal.SearchContext;
3235

3336
import java.io.IOException;
@@ -64,11 +67,22 @@ protected Aggregator doCreateInternal(ValuesSource valuesSource,
6467
boolean collectsFromSingleBucket,
6568
List<PipelineAggregator> pipelineAggregators,
6669
Map<String, Object> metaData) throws IOException {
67-
if (valuesSource instanceof ValuesSource.GeoPoint == false) {
68-
throw new AggregationExecutionException("ValuesSource type " + valuesSource.toString() + "is not supported for aggregation " +
69-
this.name());
70+
AggregatorSupplier aggregatorSupplier = queryShardContext.getValuesSourceRegistry()
71+
.getAggregator(config.valueSourceType(), GeoBoundsAggregationBuilder.NAME);
72+
73+
if (aggregatorSupplier instanceof GeoBoundsAggregatorSupplier == false) {
74+
throw new AggregationExecutionException("Registry miss-match - expected "
75+
+ GeoBoundsAggregatorSupplier.class.getName() + ", found [" + aggregatorSupplier.getClass().toString() + "]");
7076
}
71-
return new GeoBoundsAggregator(name, searchContext, parent, (ValuesSource.GeoPoint) valuesSource, wrapLongitude,
77+
78+
return ((GeoBoundsAggregatorSupplier) aggregatorSupplier).build(name, searchContext, parent, valuesSource, wrapLongitude,
7279
pipelineAggregators, metaData);
7380
}
81+
82+
static void registerAggregators(ValuesSourceRegistry valuesSourceRegistry) {
83+
valuesSourceRegistry.register(GeoBoundsAggregationBuilder.NAME, CoreValuesSourceType.GEOPOINT,
84+
(GeoBoundsAggregatorSupplier) (name, aggregationContext, parent, valuesSource, wrapLongitude, pipelineAggregators, metaData)
85+
-> new GeoBoundsAggregator(name, aggregationContext, parent, (ValuesSource.GeoPoint) valuesSource,
86+
wrapLongitude, pipelineAggregators, metaData));
87+
}
7488
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
* Licensed to Elasticsearch under one or more contributor
3+
* license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright
5+
* ownership. Elasticsearch licenses this file to you under
6+
* the Apache License, Version 2.0 (the "License"); you may
7+
* not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
package org.elasticsearch.search.aggregations.metrics;
21+
22+
import org.elasticsearch.search.aggregations.Aggregator;
23+
import org.elasticsearch.search.aggregations.pipeline.PipelineAggregator;
24+
import org.elasticsearch.search.aggregations.support.AggregatorSupplier;
25+
import org.elasticsearch.search.aggregations.support.ValuesSource;
26+
import org.elasticsearch.search.internal.SearchContext;
27+
28+
import java.io.IOException;
29+
import java.util.List;
30+
import java.util.Map;
31+
32+
@FunctionalInterface
33+
public interface GeoBoundsAggregatorSupplier extends AggregatorSupplier {
34+
35+
GeoBoundsAggregator build(String name, SearchContext aggregationContext, Aggregator parent,
36+
ValuesSource valuesSource, boolean wrapLongitude, List<PipelineAggregator> pipelineAggregators,
37+
Map<String, Object> metaData) throws IOException;
38+
}

0 commit comments

Comments
 (0)