Skip to content

Commit abc0bc4

Browse files
committed
Aggregations: Fix geohash grid doc counts computation on multi-valued fields.
Close #8512
1 parent 98641ff commit abc0bc4

File tree

2 files changed

+11
-9
lines changed

2 files changed

+11
-9
lines changed

src/main/java/org/elasticsearch/search/aggregations/bucket/geogrid/GeoHashGridAggregator.java

+10-6
Original file line numberDiff line numberDiff line change
@@ -74,14 +74,18 @@ public void collect(int doc, long owningBucketOrdinal) throws IOException {
7474
values.setDocument(doc);
7575
final int valuesCount = values.count();
7676

77+
long previous = Long.MAX_VALUE;
7778
for (int i = 0; i < valuesCount; ++i) {
7879
final long val = values.valueAt(i);
79-
long bucketOrdinal = bucketOrds.add(val);
80-
if (bucketOrdinal < 0) { // already seen
81-
bucketOrdinal = - 1 - bucketOrdinal;
82-
collectExistingBucket(doc, bucketOrdinal);
83-
} else {
84-
collectBucket(doc, bucketOrdinal);
80+
if (previous != val || i == 0) {
81+
long bucketOrdinal = bucketOrds.add(val);
82+
if (bucketOrdinal < 0) { // already seen
83+
bucketOrdinal = - 1 - bucketOrdinal;
84+
collectExistingBucket(doc, bucketOrdinal);
85+
} else {
86+
collectBucket(doc, bucketOrdinal);
87+
}
88+
previous = val;
8589
}
8690
}
8791
}

src/test/java/org/elasticsearch/search/aggregations/bucket/GeoHashGridTests.java

+1-3
Original file line numberDiff line numberDiff line change
@@ -106,9 +106,7 @@ public void setupSuiteScopeCluster() throws Exception {
106106
for (int i = 0; i < numDocs; i++) {
107107
final int numPoints = random.nextInt(4);
108108
List<String> points = new ArrayList<>();
109-
// TODO (#8512): this should be a Set, not a List. Currently if a document has two positions that have
110-
// the same geo hash, it will increase the doc_count for this geo hash by 2 instead of 1
111-
List<String> geoHashes = new ArrayList<>();
109+
Set<String> geoHashes = new HashSet<>();
112110
for (int j = 0; j < numPoints; ++j) {
113111
double lat = (180d * random.nextDouble()) - 90d;
114112
double lng = (360d * random.nextDouble()) - 180d;

0 commit comments

Comments
 (0)