Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -99,3 +99,5 @@ gradle-app.setting
### VisualStudioCode template
.vscode

.settings/
.project
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ Please note that [geo_shape data type](https://www.elastic.co/guide/en/elasticse
### Install

Install plugin with:
`./bin/elasticsearch-plugin install https://github.com/opendatasoft/elasticsearch-aggregation-geoclustering/releases/download/v8.19.6.0/geopoint-clustering-aggregation-8.19.6.0.zip`
`./bin/elasticsearch-plugin install https://github.com/opendatasoft/elasticsearch-aggregation-geoclustering/releases/download/v8.19.6.1/geopoint-clustering-aggregation-8.19.6.1.zip`


### Quickstart
Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
es_version = 8.19.6
plugin_version = 8.19.6.0
plugin_version = 8.19.6.1
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.opendatasoft.elasticsearch.search.aggregations.bucket.geopointclustering;

import org.apache.lucene.geo.GeoEncodingUtils;
import org.elasticsearch.common.geo.GeoPoint;
import org.elasticsearch.common.geo.GeoUtils;
import org.elasticsearch.common.io.stream.StreamInput;
Expand Down Expand Up @@ -35,7 +34,7 @@ static class Bucket extends InternalMultiBucketAggregation.InternalBucketWritabl
protected long hashAsLong;
protected GeoPoint centroid;
protected long docCount;
protected InternalAggregations aggregations; // sub-aggregations for this bucket
protected InternalAggregations aggregations; // sub-aggregations for this bucket
protected boolean visited = false;
protected Set<Long> geohashesList;

Expand All @@ -54,10 +53,13 @@ static class Bucket extends InternalMultiBucketAggregation.InternalBucketWritabl
private Bucket(StreamInput in) throws IOException {
hashAsLong = in.readLong();
docCount = in.readVLong();
final long hash = in.readLong();
centroid = new GeoPoint(decodeLatitude(hash), decodeLongitude(hash));
double lat = in.readDouble();
double lon = in.readDouble();
centroid = new GeoPoint(lat, lon);
visited = in.readBoolean();
aggregations = InternalAggregations.readFrom(in);
geohashesList = new HashSet<>();
geohashesList.add(hashAsLong);
Comment thread
5k4nd marked this conversation as resolved.
}

/**
Expand All @@ -67,7 +69,8 @@ private Bucket(StreamInput in) throws IOException {
public void writeTo(StreamOutput out) throws IOException {
out.writeLong(hashAsLong);
out.writeVLong(docCount);
out.writeLong(encodeLatLon(centroid.lat(), centroid.lon()));
out.writeDouble(centroid.lat());
out.writeDouble(centroid.lon());
out.writeBoolean(visited);
aggregations.writeTo(out);
}
Expand Down Expand Up @@ -118,14 +121,13 @@ public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Bucket bucket = (Bucket) o;
return hashAsLong == bucket.hashAsLong && docCount == bucket.docCount && Objects.equals(aggregations, bucket.aggregations);
return (hashAsLong == bucket.hashAsLong && docCount == bucket.docCount && Objects.equals(aggregations, bucket.aggregations));
}

@Override
public int hashCode() {
return Objects.hash(hashAsLong, docCount, aggregations);
}

}

private final double radius;
Expand Down Expand Up @@ -170,20 +172,6 @@ protected void doWriteTo(StreamOutput out) throws IOException {
out.writeCollection((Collection<? extends Writeable>) buckets);
}

public static long encodeLatLon(double lat, double lon) {
return (Integer.toUnsignedLong(GeoEncodingUtils.encodeLatitude(lat)) << 32) | Integer.toUnsignedLong(
GeoEncodingUtils.encodeLongitude(lon)
);
}

public static double decodeLatitude(long encodedLatLon) {
return GeoEncodingUtils.decodeLatitude((int) (encodedLatLon >>> 32));
}

public static double decodeLongitude(long encodedLatLon) {
return GeoEncodingUtils.decodeLongitude((int) (encodedLatLon & 0xFFFFFFFFL));
}

protected Reader<Bucket> getBucketReader() {
return Bucket::new;
}
Expand Down Expand Up @@ -254,7 +242,6 @@ private List<Bucket> mergeBuckets(Bucket[] candidateClusters, AggregationReduceC
@Override
protected AggregatorReducer getLeaderReducer(AggregationReduceContext context, int size) {
return new AggregatorReducer() {

final LongObjectPagedHashMap<BucketReducer> bucketsReducer = new LongObjectPagedHashMap<>(size, context.bigArrays());

/**
Expand Down Expand Up @@ -432,7 +419,7 @@ public int hashCode() {
@Override
public boolean equals(Object obj) {
InternalGeoPointClustering other = (InternalGeoPointClustering) obj;
return Objects.equals(requiredSize, other.requiredSize) && Objects.equals(buckets, other.buckets);
return (Objects.equals(requiredSize, other.requiredSize) && Objects.equals(buckets, other.buckets));
}

static class BucketPriorityQueue<A, B extends Bucket> extends ObjectArrayPriorityQueue<A> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,14 +124,12 @@ setup:
index: test
body: {"aggs": { "gc" : { "geo_point_clustering": {"field": "point", "zoom": 1} } } }


- match: { hits.total: 15 }
- length: { aggregations.gc.buckets: 1 }
- match: { aggregations.gc.buckets.0.doc_count: 15 }
- match: { aggregations.gc.buckets.0.centroid.lat: 48.8468417795375 }
- match: { aggregations.gc.buckets.0.centroid.lon: 2.331401154398918 }


---

"Test Zoom 9":
Expand Down Expand Up @@ -278,3 +276,73 @@ setup:

- gte: { hits.total: 1 }
- length: { aggregations.gc.buckets: 1 }

---
"Test that the cache is working":
- do:
indices.create:
index: test_cache_index
body:
settings:
number_of_shards: 1
mappings:
"properties":
"point":
"type": "geo_point"

- do:
index:
index: test_cache_index
id: 1
body: { "point": [2.253588, 48.864682] }

- do:
index:
index: test_cache_index
id: 2
body: { "point": [2.336643, 48.822493] }

- do:
index:
index: test_cache_index
id: 3
body: { "point": [2.438465, 48.84204] }

- do:
indices.refresh:
index: test_cache_index

- do:
search:
index: test_cache_index
request_cache: true
body:
size: 0
aggs:
gc:
geo_point_clustering:
field: point
zoom: 1

- length: { aggregations.gc.buckets: 1 }
- match: { aggregations.gc.buckets.0.doc_count: 3 }
- match: { aggregations.gc.buckets.0.centroid.lat: 48.84307164698839 }
- match: { aggregations.gc.buckets.0.centroid.lon: 2.3428986221551895 }

# Second strictly identical agg is hitting the cache
- do:
search:
index: test_cache_index
request_cache: true
body:
size: 0
aggs:
gc:
geo_point_clustering:
field: point
zoom: 1

- length: { aggregations.gc.buckets: 1 }
- match: { aggregations.gc.buckets.0.doc_count: 3 }
- match: { aggregations.gc.buckets.0.centroid.lat: 48.84307164698839 }
- match: { aggregations.gc.buckets.0.centroid.lon: 2.3428986221551895 }