Skip to content

Commit a11f0fa

Browse files
author
Christoph Büscher
committed
WIP some really hacky ideas
1 parent c5bcc5d commit a11f0fa

File tree

2 files changed

+33
-1
lines changed

2 files changed

+33
-1
lines changed

server/src/main/java/org/elasticsearch/search/aggregations/bucket/geogrid/InternalGeoHashGrid.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,13 @@
1919
package org.elasticsearch.search.aggregations.bucket.geogrid;
2020

2121
import org.apache.lucene.util.PriorityQueue;
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;
2526
import org.elasticsearch.common.util.LongObjectPagedHashMap;
2627
import org.elasticsearch.common.xcontent.XContentBuilder;
28+
import org.elasticsearch.rest.action.search.RestSearchAction;
2729
import org.elasticsearch.search.aggregations.Aggregations;
2830
import org.elasticsearch.search.aggregations.InternalAggregation;
2931
import org.elasticsearch.search.aggregations.InternalAggregations;
@@ -154,6 +156,8 @@ public int hashCode() {
154156

155157
}
156158

159+
static final ParseField HASH_TYPE_FIELD = new ParseField("hash_type");
160+
157161
private final int requiredSize;
158162
private final List<Bucket> buckets;
159163

@@ -238,6 +242,17 @@ public InternalGeoHashGrid doReduce(List<InternalAggregation> aggregations, Redu
238242

239243
@Override
240244
public XContentBuilder doXContentBody(XContentBuilder builder, Params params) throws IOException {
245+
// not sure we should re-use RestSearchAction.TYPED_KEYS_PARAM here, but we already use it in
246+
// suggestions and aggregations and it should be switched on by every high level client reqeust
247+
if (params.paramAsBoolean(RestSearchAction.TYPED_KEYS_PARAM, false)) {
248+
if (buckets.isEmpty() == false) {
249+
// TODO this assume all types in the buckets are same, but we should really keep this
250+
// info in the aggregation itself
251+
// TODO the GeoHashType needs a better method to get some sort of ID for it, name() or toString()
252+
// seem to brittle for me tbh
253+
builder.field(HASH_TYPE_FIELD.getPreferredName(), buckets.get(0).type.toString());
254+
}
255+
}
241256
builder.startArray(CommonFields.BUCKETS.getPreferredName());
242257
for (Bucket bucket : buckets) {
243258
bucket.toXContent(builder, params);

server/src/main/java/org/elasticsearch/search/aggregations/bucket/geogrid/ParsedGeoHashGrid.java

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@
3030

3131
public class ParsedGeoHashGrid extends ParsedMultiBucketAggregation<ParsedGeoHashGrid.ParsedBucket> implements GeoHashGrid {
3232

33+
private GeoHashType type = GeoHashType.DEFAULT;
34+
3335
@Override
3436
public String getType() {
3537
return GeoGridAggregationBuilder.NAME;
@@ -40,25 +42,40 @@ public List<? extends GeoHashGrid.Bucket> getBuckets() {
4042
return buckets;
4143
}
4244

45+
private void setType(String type) {
46+
// TODO something less brittle than enum#valueOf() needed I think
47+
this.type = GeoHashType.forString(type);
48+
}
49+
4350
private static ObjectParser<ParsedGeoHashGrid, Void> PARSER =
4451
new ObjectParser<>(ParsedGeoHashGrid.class.getSimpleName(), true, ParsedGeoHashGrid::new);
4552
static {
4653
declareMultiBucketAggregationFields(PARSER, ParsedBucket::fromXContent, ParsedBucket::fromXContent);
54+
PARSER.declareString(ParsedGeoHashGrid::setType, InternalGeoHashGrid.HASH_TYPE_FIELD);
4755
}
4856

4957
public static ParsedGeoHashGrid fromXContent(XContentParser parser, String name) throws IOException {
5058
ParsedGeoHashGrid aggregation = PARSER.parse(parser, null);
5159
aggregation.setName(name);
60+
// transfer hash type info to buckets after parsing, this is horrible IMHO but I couldn't make it work
61+
// otherwise quickly
62+
// TODO don't do it like this!
63+
for (ParsedBucket b : aggregation.buckets) {
64+
b.hashType = aggregation.type;
65+
}
5266
return aggregation;
5367
}
5468

5569
public static class ParsedBucket extends ParsedMultiBucketAggregation.ParsedBucket implements GeoHashGrid.Bucket {
5670

5771
private String geohashAsString;
72+
private GeoHashType hashType = GeoHashType.DEFAULT;
5873

5974
@Override
6075
public GeoPoint getKey() {
61-
return GeoPoint.fromGeohash(geohashAsString);
76+
// TODO in order for this to work, the GeoHashTypeProvider needs to convert back from String -> GeoPoint
77+
// return hashType.getHandler().hashStringAsGeoPoint(geohashAsString);
78+
return null;
6279
}
6380

6481
@Override

0 commit comments

Comments
 (0)