Skip to content

Commit d50961e

Browse files
committed
Add parsing for InternalGeoCentroid
1 parent db07a34 commit d50961e

File tree

4 files changed

+101
-5
lines changed

4 files changed

+101
-5
lines changed

core/src/main/java/org/elasticsearch/search/aggregations/metrics/geocentroid/InternalGeoCentroid.java

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -149,17 +149,13 @@ public Object getProperty(List<String> path) {
149149

150150
static class Fields {
151151
static final ParseField CENTROID = new ParseField("location");
152+
static final ParseField COUNT = new ParseField("count");
152153
static final ParseField CENTROID_LAT = new ParseField("lat");
153154
static final ParseField CENTROID_LON = new ParseField("lon");
154-
static final ParseField COUNT = new ParseField("count");
155155
}
156156

157157
@Override
158158
public XContentBuilder doXContentBody(XContentBuilder builder, Params params) throws IOException {
159-
return renderXContent(builder, params, centroid, count);
160-
}
161-
162-
static XContentBuilder renderXContent(XContentBuilder builder, Params params, GeoPoint centroid, long count) throws IOException {
163159
if (centroid != null) {
164160
builder.startObject(Fields.CENTROID.getPreferredName());
165161
{
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
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.geocentroid;
21+
22+
import org.elasticsearch.common.geo.GeoPoint;
23+
import org.elasticsearch.common.xcontent.ObjectParser;
24+
import org.elasticsearch.common.xcontent.XContentBuilder;
25+
import org.elasticsearch.common.xcontent.XContentParser;
26+
import org.elasticsearch.search.aggregations.ParsedAggregation;
27+
import org.elasticsearch.search.aggregations.metrics.geocentroid.InternalGeoCentroid.Fields;
28+
29+
import java.io.IOException;
30+
31+
/**
32+
* Serialization and merge logic for {@link GeoCentroidAggregator}.
33+
*/
34+
public class ParsedGeoCentroid extends ParsedAggregation implements GeoCentroid {
35+
private GeoPoint centroid;
36+
private long count;
37+
38+
@Override
39+
public GeoPoint centroid() {
40+
return centroid;
41+
}
42+
43+
@Override
44+
public long count() {
45+
return count;
46+
}
47+
48+
@Override
49+
protected String getType() {
50+
return GeoCentroidAggregationBuilder.NAME;
51+
}
52+
53+
@Override
54+
public XContentBuilder doXContentBody(XContentBuilder builder, Params params) throws IOException {
55+
if (centroid != null) {
56+
builder.startObject(Fields.CENTROID.getPreferredName());
57+
{
58+
builder.field(Fields.CENTROID_LAT.getPreferredName(), centroid.lat());
59+
builder.field(Fields.CENTROID_LON.getPreferredName(), centroid.lon());
60+
}
61+
builder.endObject();
62+
}
63+
builder.field(Fields.COUNT.getPreferredName(), count);
64+
return builder;
65+
}
66+
67+
private static final ObjectParser<ParsedGeoCentroid, Void> PARSER = new ObjectParser<>(ParsedGeoCentroid.class.getSimpleName(), true,
68+
ParsedGeoCentroid::new);
69+
70+
private static final ObjectParser<GeoPoint, Void> GEO_POINT_PARSER = new ObjectParser<>(
71+
ParsedGeoCentroid.class.getSimpleName() + "_POINT", true, GeoPoint::new);
72+
73+
static {
74+
declareAggregationFields(PARSER);
75+
PARSER.declareObject((agg, centroid) -> agg.centroid = centroid, GEO_POINT_PARSER, Fields.CENTROID);
76+
PARSER.declareLong((agg, count) -> agg.count = count, Fields.COUNT);
77+
78+
GEO_POINT_PARSER.declareDouble(GeoPoint::resetLat, Fields.CENTROID_LAT);
79+
GEO_POINT_PARSER.declareDouble(GeoPoint::resetLon, Fields.CENTROID_LON);
80+
}
81+
82+
public static ParsedGeoCentroid fromXContent(XContentParser parser, final String name) {
83+
ParsedGeoCentroid geoCentroid = PARSER.apply(parser, null);
84+
geoCentroid.setName(name);
85+
return geoCentroid;
86+
}
87+
}

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@
4040
import org.elasticsearch.search.aggregations.metrics.cardinality.ParsedCardinality;
4141
import org.elasticsearch.search.aggregations.metrics.geobounds.GeoBoundsAggregationBuilder;
4242
import org.elasticsearch.search.aggregations.metrics.geobounds.ParsedGeoBounds;
43+
import org.elasticsearch.search.aggregations.metrics.geocentroid.GeoCentroidAggregationBuilder;
44+
import org.elasticsearch.search.aggregations.metrics.geocentroid.ParsedGeoCentroid;
4345
import org.elasticsearch.search.aggregations.metrics.max.MaxAggregationBuilder;
4446
import org.elasticsearch.search.aggregations.metrics.max.ParsedMax;
4547
import org.elasticsearch.search.aggregations.metrics.min.MinAggregationBuilder;
@@ -118,6 +120,7 @@ static List<NamedXContentRegistry.Entry> getNamedXContents() {
118120
namedXContents.put(ExtendedStatsBucketPipelineAggregationBuilder.NAME,
119121
(p, c) -> ParsedExtendedStatsBucket.fromXContent(p, (String) c));
120122
namedXContents.put(GeoBoundsAggregationBuilder.NAME, (p, c) -> ParsedGeoBounds.fromXContent(p, (String) c));
123+
namedXContents.put(GeoCentroidAggregationBuilder.NAME, (p, c) -> ParsedGeoCentroid.fromXContent(p, (String) c));
121124

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

core/src/test/java/org/elasticsearch/search/aggregations/metrics/geocentroid/InternalGeoCentroidTests.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import org.elasticsearch.common.geo.GeoPoint;
2323
import org.elasticsearch.common.io.stream.Writeable;
2424
import org.elasticsearch.search.aggregations.InternalAggregationTestCase;
25+
import org.elasticsearch.search.aggregations.ParsedAggregation;
2526
import org.elasticsearch.search.aggregations.pipeline.PipelineAggregator;
2627
import org.elasticsearch.test.geo.RandomGeoGenerator;
2728

@@ -70,4 +71,13 @@ protected void assertReduced(InternalGeoCentroid reduced, List<InternalGeoCentro
7071
assertEquals(lonSum/totalCount, reduced.centroid().getLon(), 1E-5D);
7172
assertEquals(totalCount, reduced.count());
7273
}
74+
75+
@Override
76+
protected void assertFromXContent(InternalGeoCentroid aggregation, ParsedAggregation parsedAggregation) {
77+
assertTrue(parsedAggregation instanceof ParsedGeoCentroid);
78+
ParsedGeoCentroid parsed = (ParsedGeoCentroid) parsedAggregation;
79+
80+
assertEquals(aggregation.centroid(), parsed.centroid());
81+
assertEquals(aggregation.count(), parsed.count());
82+
}
7383
}

0 commit comments

Comments
 (0)