Skip to content

Commit 0486c20

Browse files
committed
Aggregations: Fix geohash grid doc counts computation on multi-valued fields.
Close #8512
1 parent 92fc698 commit 0486c20

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

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -73,14 +73,18 @@ public void collect(int doc, long owningBucketOrdinal) throws IOException {
7373
values.setDocument(doc);
7474
final int valuesCount = values.count();
7575

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

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

Lines changed: 1 addition & 3 deletions
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)