Skip to content

Commit a6c38b8

Browse files
authored
Add parsing for InternalGeoBounds (#24365)
1 parent 9cb3666 commit a6c38b8

File tree

4 files changed

+153
-15
lines changed

4 files changed

+153
-15
lines changed

core/src/main/java/org/elasticsearch/search/aggregations/metrics/geobounds/InternalGeoBounds.java

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
package org.elasticsearch.search.aggregations.metrics.geobounds;
2121

22+
import org.elasticsearch.common.ParseField;
2223
import org.elasticsearch.common.geo.GeoPoint;
2324
import org.elasticsearch.common.io.stream.StreamInput;
2425
import org.elasticsearch.common.io.stream.StreamOutput;
@@ -32,6 +33,14 @@
3233
import java.util.Objects;
3334

3435
public class InternalGeoBounds extends InternalAggregation implements GeoBounds {
36+
37+
final static ParseField BOUNDS_FIELD = new ParseField("bounds");
38+
final static ParseField TOP_LEFT_FIELD = new ParseField("top_left");
39+
final static ParseField BOTTOM_RIGHT_FIELD = new ParseField("bottom_right");
40+
final static ParseField LAT_FIELD = new ParseField("lat");
41+
final static ParseField LON_FIELD = new ParseField("lon");
42+
43+
3544
final double top;
3645
final double bottom;
3746
final double posLeft;
@@ -170,14 +179,14 @@ public XContentBuilder doXContentBody(XContentBuilder builder, Params params) th
170179
GeoPoint topLeft = topLeft();
171180
GeoPoint bottomRight = bottomRight();
172181
if (topLeft != null) {
173-
builder.startObject("bounds");
174-
builder.startObject("top_left");
175-
builder.field("lat", topLeft.lat());
176-
builder.field("lon", topLeft.lon());
182+
builder.startObject(BOUNDS_FIELD.getPreferredName());
183+
builder.startObject(TOP_LEFT_FIELD.getPreferredName());
184+
builder.field(LAT_FIELD.getPreferredName(), topLeft.lat());
185+
builder.field(LON_FIELD.getPreferredName(), topLeft.lon());
177186
builder.endObject();
178-
builder.startObject("bottom_right");
179-
builder.field("lat", bottomRight.lat());
180-
builder.field("lon", bottomRight.lon());
187+
builder.startObject(BOTTOM_RIGHT_FIELD.getPreferredName());
188+
builder.field(LAT_FIELD.getPreferredName(), bottomRight.lat());
189+
builder.field(LON_FIELD.getPreferredName(), bottomRight.lon());
181190
builder.endObject();
182191
builder.endObject();
183192
}
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
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.geobounds;
21+
22+
import org.elasticsearch.common.collect.Tuple;
23+
import org.elasticsearch.common.geo.GeoPoint;
24+
import org.elasticsearch.common.xcontent.ConstructingObjectParser;
25+
import org.elasticsearch.common.xcontent.ObjectParser;
26+
import org.elasticsearch.common.xcontent.XContentBuilder;
27+
import org.elasticsearch.common.xcontent.XContentParser;
28+
import org.elasticsearch.search.aggregations.ParsedAggregation;
29+
30+
import java.io.IOException;
31+
32+
import static org.elasticsearch.common.xcontent.ConstructingObjectParser.constructorArg;
33+
import static org.elasticsearch.search.aggregations.metrics.geobounds.InternalGeoBounds.BOTTOM_RIGHT_FIELD;
34+
import static org.elasticsearch.search.aggregations.metrics.geobounds.InternalGeoBounds.BOUNDS_FIELD;
35+
import static org.elasticsearch.search.aggregations.metrics.geobounds.InternalGeoBounds.LAT_FIELD;
36+
import static org.elasticsearch.search.aggregations.metrics.geobounds.InternalGeoBounds.LON_FIELD;
37+
import static org.elasticsearch.search.aggregations.metrics.geobounds.InternalGeoBounds.TOP_LEFT_FIELD;
38+
39+
public class ParsedGeoBounds extends ParsedAggregation implements GeoBounds {
40+
private GeoPoint topLeft;
41+
private GeoPoint bottomRight;
42+
43+
@Override
44+
public String getType() {
45+
return GeoBoundsAggregationBuilder.NAME;
46+
}
47+
48+
@Override
49+
public XContentBuilder doXContentBody(XContentBuilder builder, Params params) throws IOException {
50+
if (topLeft != null) {
51+
builder.startObject("bounds");
52+
builder.startObject("top_left");
53+
builder.field("lat", topLeft.getLat());
54+
builder.field("lon", topLeft.getLon());
55+
builder.endObject();
56+
builder.startObject("bottom_right");
57+
builder.field("lat", bottomRight.getLat());
58+
builder.field("lon", bottomRight.getLon());
59+
builder.endObject();
60+
builder.endObject();
61+
}
62+
return builder;
63+
}
64+
65+
@Override
66+
public GeoPoint topLeft() {
67+
return topLeft;
68+
}
69+
70+
@Override
71+
public GeoPoint bottomRight() {
72+
return bottomRight;
73+
}
74+
75+
private static final ObjectParser<ParsedGeoBounds, Void> PARSER = new ObjectParser<>(ParsedGeoBounds.class.getSimpleName(), true,
76+
ParsedGeoBounds::new);
77+
78+
private static final ConstructingObjectParser<Tuple<GeoPoint, GeoPoint>, Void> BOUNDS_PARSER =
79+
new ConstructingObjectParser<>(ParsedGeoBounds.class.getSimpleName() + "_BOUNDS", true,
80+
args -> new Tuple<>((GeoPoint) args[0], (GeoPoint) args[1]));
81+
82+
private static final ObjectParser<GeoPoint, Void> GEO_POINT_PARSER = new ObjectParser<>(
83+
ParsedGeoBounds.class.getSimpleName() + "_POINT", true, GeoPoint::new);
84+
85+
static {
86+
declareAggregationFields(PARSER);
87+
PARSER.declareObject((agg, bbox) -> {
88+
agg.topLeft = bbox.v1();
89+
agg.bottomRight = bbox.v2();
90+
}, BOUNDS_PARSER, BOUNDS_FIELD);
91+
92+
BOUNDS_PARSER.declareObject(constructorArg(), GEO_POINT_PARSER, TOP_LEFT_FIELD);
93+
BOUNDS_PARSER.declareObject(constructorArg(), GEO_POINT_PARSER, BOTTOM_RIGHT_FIELD);
94+
95+
GEO_POINT_PARSER.declareDouble(GeoPoint::resetLat, LAT_FIELD);
96+
GEO_POINT_PARSER.declareDouble(GeoPoint::resetLon, LON_FIELD);
97+
}
98+
99+
public static ParsedGeoBounds fromXContent(XContentParser parser, final String name) {
100+
ParsedGeoBounds geoBounds = PARSER.apply(parser, null);
101+
geoBounds.setName(name);
102+
return geoBounds;
103+
}
104+
105+
}

core/src/test/java/org/elasticsearch/search/aggregations/InternalAggregationTestCase.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@
3838
import org.elasticsearch.search.aggregations.metrics.avg.ParsedAvg;
3939
import org.elasticsearch.search.aggregations.metrics.cardinality.CardinalityAggregationBuilder;
4040
import org.elasticsearch.search.aggregations.metrics.cardinality.ParsedCardinality;
41+
import org.elasticsearch.search.aggregations.metrics.geobounds.GeoBoundsAggregationBuilder;
42+
import org.elasticsearch.search.aggregations.metrics.geobounds.ParsedGeoBounds;
4143
import org.elasticsearch.search.aggregations.metrics.max.MaxAggregationBuilder;
4244
import org.elasticsearch.search.aggregations.metrics.max.ParsedMax;
4345
import org.elasticsearch.search.aggregations.metrics.min.MinAggregationBuilder;
@@ -113,7 +115,9 @@ static List<NamedXContentRegistry.Entry> getNamedXContents() {
113115
namedXContents.put(StatsAggregationBuilder.NAME, (p, c) -> ParsedStats.fromXContent(p, (String) c));
114116
namedXContents.put(StatsBucketPipelineAggregationBuilder.NAME, (p, c) -> ParsedStatsBucket.fromXContent(p, (String) c));
115117
namedXContents.put(ExtendedStatsAggregationBuilder.NAME, (p, c) -> ParsedExtendedStats.fromXContent(p, (String) c));
116-
namedXContents.put(ExtendedStatsBucketPipelineAggregationBuilder.NAME, (p, c) -> ParsedExtendedStatsBucket.fromXContent(p, (String) c));
118+
namedXContents.put(ExtendedStatsBucketPipelineAggregationBuilder.NAME,
119+
(p, c) -> ParsedExtendedStatsBucket.fromXContent(p, (String) c));
120+
namedXContents.put(GeoBoundsAggregationBuilder.NAME, (p, c) -> ParsedGeoBounds.fromXContent(p, (String) c));
117121

118122
return namedXContents.entrySet().stream()
119123
.map(entry -> new NamedXContentRegistry.Entry(Aggregation.class, new ParseField(entry.getKey()), entry.getValue()))

core/src/test/java/org/elasticsearch/search/aggregations/metrics/geobounds/InternalGeoBoundsTests.java

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
import org.elasticsearch.common.io.stream.Writeable;
2323
import org.elasticsearch.search.aggregations.InternalAggregationTestCase;
24+
import org.elasticsearch.search.aggregations.ParsedAggregation;
2425
import org.elasticsearch.search.aggregations.pipeline.PipelineAggregator;
2526

2627
import java.util.Collections;
@@ -35,8 +36,10 @@ public class InternalGeoBoundsTests extends InternalAggregationTestCase<Internal
3536
@Override
3637
protected InternalGeoBounds createTestInstance(String name, List<PipelineAggregator> pipelineAggregators,
3738
Map<String, Object> metaData) {
39+
// we occasionally want to test top = Double.NEGATIVE_INFINITY since this triggers empty xContent object
40+
double top = frequently() ? randomDouble() : Double.NEGATIVE_INFINITY;
3841
InternalGeoBounds geo = new InternalGeoBounds(name,
39-
randomDouble(), randomDouble(), randomDouble(), randomDouble(),
42+
top, randomDouble(), randomDouble(), randomDouble(),
4043
randomDouble(), randomDouble(), randomBoolean(),
4144
pipelineAggregators, Collections.emptyMap());
4245
return geo;
@@ -70,12 +73,29 @@ protected void assertReduced(InternalGeoBounds reduced, List<InternalGeoBounds>
7073
negRight = bounds.negRight;
7174
}
7275
}
73-
assertThat(reduced.top, closeTo(top, GEOHASH_TOLERANCE));
74-
assertThat(reduced.bottom, closeTo(bottom, GEOHASH_TOLERANCE));
75-
assertThat(reduced.posLeft, closeTo(posLeft, GEOHASH_TOLERANCE));
76-
assertThat(reduced.posRight, closeTo(posRight, GEOHASH_TOLERANCE));
77-
assertThat(reduced.negLeft, closeTo(negLeft, GEOHASH_TOLERANCE));
78-
assertThat(reduced.negRight, closeTo(negRight, GEOHASH_TOLERANCE));
76+
assertValueClose(reduced.top, top);
77+
assertValueClose(reduced.bottom, bottom);
78+
assertValueClose(reduced.posLeft, posLeft);
79+
assertValueClose(reduced.posRight, posRight);
80+
assertValueClose(reduced.negLeft, negLeft);
81+
assertValueClose(reduced.negRight, negRight);
82+
}
83+
84+
private static void assertValueClose(double expected, double actual) {
85+
if (Double.isInfinite(expected) == false) {
86+
assertThat(expected, closeTo(actual, GEOHASH_TOLERANCE));
87+
} else {
88+
assertTrue(Double.isInfinite(actual));
89+
}
90+
}
91+
92+
@Override
93+
protected void assertFromXContent(InternalGeoBounds aggregation, ParsedAggregation parsedAggregation) {
94+
assertTrue(parsedAggregation instanceof ParsedGeoBounds);
95+
ParsedGeoBounds parsed = (ParsedGeoBounds) parsedAggregation;
96+
97+
assertEquals(aggregation.topLeft(), parsed.topLeft());
98+
assertEquals(aggregation.bottomRight(), parsed.bottomRight());
7999
}
80100

81101
@Override

0 commit comments

Comments
 (0)