Skip to content

Commit ec57ecc

Browse files
tlrxjavanna
authored andcommitted
Add parsing method to GeoHashGrid aggregation (elastic#24589)
1 parent 6d653f4 commit ec57ecc

File tree

4 files changed

+141
-0
lines changed

4 files changed

+141
-0
lines changed
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
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.bucket.geogrid;
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.ParsedMultiBucketAggregation;
27+
28+
import java.io.IOException;
29+
import java.util.List;
30+
31+
public class ParsedGeoHashGrid extends ParsedMultiBucketAggregation<ParsedGeoHashGrid.ParsedBucket> implements GeoHashGrid {
32+
33+
@Override
34+
public String getType() {
35+
return GeoGridAggregationBuilder.NAME;
36+
}
37+
38+
@Override
39+
public List<? extends GeoHashGrid.Bucket> getBuckets() {
40+
return buckets;
41+
}
42+
43+
private static ObjectParser<ParsedGeoHashGrid, Void> PARSER =
44+
new ObjectParser<>(ParsedGeoHashGrid.class.getSimpleName(), true, ParsedGeoHashGrid::new);
45+
static {
46+
declareMultiBucketAggregationFields(PARSER, ParsedBucket::fromXContent, ParsedBucket::fromXContent);
47+
}
48+
49+
public static ParsedGeoHashGrid fromXContent(XContentParser parser, String name) throws IOException {
50+
ParsedGeoHashGrid aggregation = PARSER.parse(parser, null);
51+
aggregation.setName(name);
52+
return aggregation;
53+
}
54+
55+
public static class ParsedBucket extends ParsedMultiBucketAggregation.ParsedBucket implements GeoHashGrid.Bucket {
56+
57+
private String geohashAsString;
58+
59+
@Override
60+
public GeoPoint getKey() {
61+
return GeoPoint.fromGeohash(geohashAsString);
62+
}
63+
64+
@Override
65+
public String getKeyAsString() {
66+
return geohashAsString;
67+
}
68+
69+
@Override
70+
protected XContentBuilder keyToXContent(XContentBuilder builder) throws IOException {
71+
return builder.field(CommonFields.KEY.getPreferredName(), geohashAsString);
72+
}
73+
74+
static ParsedBucket fromXContent(XContentParser parser) throws IOException {
75+
return parseXContent(parser, false, ParsedBucket::new, (p, bucket) -> bucket.geohashAsString = p.textOrNull());
76+
}
77+
}
78+
}

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import org.elasticsearch.rest.action.search.RestSearchAction;
2929
import org.elasticsearch.search.aggregations.bucket.filter.InternalFilterTests;
3030
import org.elasticsearch.search.aggregations.bucket.global.InternalGlobalTests;
31+
import org.elasticsearch.search.aggregations.bucket.geogrid.InternalGeoHashGridTests;
3132
import org.elasticsearch.search.aggregations.bucket.histogram.InternalDateHistogramTests;
3233
import org.elasticsearch.search.aggregations.bucket.histogram.InternalHistogramTests;
3334
import org.elasticsearch.search.aggregations.bucket.missing.InternalMissingTests;
@@ -114,6 +115,7 @@ private static List<InternalAggregationTestCase> getAggsTests() {
114115
aggsTests.add(new InternalGlobalTests());
115116
aggsTests.add(new InternalFilterTests());
116117
aggsTests.add(new InternalSamplerTests());
118+
aggsTests.add(new InternalGeoHashGridTests());
117119
return Collections.unmodifiableList(aggsTests);
118120
}
119121

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
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+
package org.elasticsearch.search.aggregations.bucket.geogrid;
20+
21+
import org.apache.lucene.index.IndexWriter;
22+
import org.elasticsearch.common.geo.GeoHashUtils;
23+
import org.elasticsearch.common.io.stream.Writeable;
24+
import org.elasticsearch.search.aggregations.InternalAggregations;
25+
import org.elasticsearch.search.aggregations.InternalMultiBucketAggregationTestCase;
26+
import org.elasticsearch.search.aggregations.ParsedMultiBucketAggregation;
27+
import org.elasticsearch.search.aggregations.pipeline.PipelineAggregator;
28+
29+
import java.util.ArrayList;
30+
import java.util.HashMap;
31+
import java.util.List;
32+
import java.util.Map;
33+
34+
public class InternalGeoHashGridTests extends InternalMultiBucketAggregationTestCase<InternalGeoHashGrid> {
35+
36+
@Override
37+
protected InternalGeoHashGrid createTestInstance(String name,
38+
List<PipelineAggregator> pipelineAggregators,
39+
Map<String, Object> metaData,
40+
InternalAggregations aggregations) {
41+
int size = randomIntBetween(1, 3);
42+
List<InternalGeoHashGrid.Bucket> buckets = new ArrayList<>(size);
43+
for (int i = 0; i < size; i++) {
44+
double latitude = randomDoubleBetween(-90.0, 90.0, false);
45+
double longitude = randomDoubleBetween(-180.0, 180.0, false);
46+
47+
long geoHashAsLong = GeoHashUtils.longEncode(longitude, latitude, 4);
48+
buckets.add(new InternalGeoHashGrid.Bucket(geoHashAsLong, randomInt(IndexWriter.MAX_DOCS), aggregations));
49+
}
50+
return new InternalGeoHashGrid(name, size, buckets, pipelineAggregators, metaData);
51+
}
52+
53+
@Override
54+
protected Class<? extends ParsedMultiBucketAggregation> implementationClass() {
55+
return ParsedGeoHashGrid.class;
56+
}
57+
}

test/framework/src/main/java/org/elasticsearch/test/InternalAggregationTestCase.java

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

2020
package org.elasticsearch.test;
2121

22+
import com.carrotsearch.randomizedtesting.annotations.Repeat;
2223
import org.elasticsearch.common.ParseField;
2324
import org.elasticsearch.common.bytes.BytesReference;
2425
import org.elasticsearch.common.xcontent.ContextParser;
@@ -36,6 +37,8 @@
3637
import org.elasticsearch.search.aggregations.bucket.filter.ParsedFilter;
3738
import org.elasticsearch.search.aggregations.bucket.global.GlobalAggregationBuilder;
3839
import org.elasticsearch.search.aggregations.bucket.global.ParsedGlobal;
40+
import org.elasticsearch.search.aggregations.bucket.geogrid.GeoGridAggregationBuilder;
41+
import org.elasticsearch.search.aggregations.bucket.geogrid.ParsedGeoHashGrid;
3942
import org.elasticsearch.search.aggregations.bucket.histogram.DateHistogramAggregationBuilder;
4043
import org.elasticsearch.search.aggregations.bucket.histogram.HistogramAggregationBuilder;
4144
import org.elasticsearch.search.aggregations.bucket.histogram.ParsedDateHistogram;
@@ -146,6 +149,7 @@ public static List<NamedXContentRegistry.Entry> getNamedXContents() {
146149
namedXContents.put(GlobalAggregationBuilder.NAME, (p, c) -> ParsedGlobal.fromXContent(p, (String) c));
147150
namedXContents.put(FilterAggregationBuilder.NAME, (p, c) -> ParsedFilter.fromXContent(p, (String) c));
148151
namedXContents.put(InternalSampler.NAME, (p, c) -> ParsedSampler.fromXContent(p, (String) c));
152+
namedXContents.put(GeoGridAggregationBuilder.NAME, (p, c) -> ParsedGeoHashGrid.fromXContent(p, (String) c));
149153

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

0 commit comments

Comments
 (0)