Skip to content

Commit b2619ec

Browse files
authored
Specialise ScriptDocValues#GeoPoints for single values (#73656)
1 parent ceb3099 commit b2619ec

File tree

1 file changed

+34
-18
lines changed

1 file changed

+34
-18
lines changed

server/src/main/java/org/elasticsearch/index/fielddata/ScriptDocValues.java

Lines changed: 34 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -273,30 +273,46 @@ public GeoPoints(MultiGeoPointValues in) {
273273
public void setNextDocId(int docId) throws IOException {
274274
if (in.advanceExact(docId)) {
275275
resize(in.docValueCount());
276-
double centroidLat = 0;
277-
double centroidLon = 0;
278-
double maxLon = Double.NEGATIVE_INFINITY;
279-
double minLon = Double.POSITIVE_INFINITY;
280-
double maxLat = Double.NEGATIVE_INFINITY;
281-
double minLat = Double.POSITIVE_INFINITY;
282-
for (int i = 0; i < count; i++) {
283-
GeoPoint point = in.nextValue();
284-
values[i].reset(point.lat(), point.lon());
285-
centroidLat += point.getLat();
286-
centroidLon += point.getLon();
287-
maxLon = Math.max(maxLon, values[i].getLon());
288-
minLon = Math.min(minLon, values[i].getLon());
289-
maxLat = Math.max(maxLat, values[i].getLat());
290-
minLat = Math.min(minLat, values[i].getLat());
276+
if (count == 1) {
277+
setSingleValue();
278+
} else {
279+
setMultiValue();
291280
}
292-
centroid.reset(centroidLat / count, centroidLon / count);
293-
boundingBox.topLeft().reset(maxLat, minLon);
294-
boundingBox.bottomRight().reset(minLat, maxLon);
295281
} else {
296282
resize(0);
297283
}
298284
}
299285

286+
private void setSingleValue() throws IOException {
287+
GeoPoint point = in.nextValue();
288+
values[0].reset(point.lat(), point.lon());
289+
centroid.reset(point.lat(), point.lon());
290+
boundingBox.topLeft().reset(point.lat(), point.lon());
291+
boundingBox.bottomRight().reset(point.lat(), point.lon());
292+
}
293+
294+
private void setMultiValue() throws IOException {
295+
double centroidLat = 0;
296+
double centroidLon = 0;
297+
double maxLon = Double.NEGATIVE_INFINITY;
298+
double minLon = Double.POSITIVE_INFINITY;
299+
double maxLat = Double.NEGATIVE_INFINITY;
300+
double minLat = Double.POSITIVE_INFINITY;
301+
for (int i = 0; i < count; i++) {
302+
GeoPoint point = in.nextValue();
303+
values[i].reset(point.lat(), point.lon());
304+
centroidLat += point.getLat();
305+
centroidLon += point.getLon();
306+
maxLon = Math.max(maxLon, values[i].getLon());
307+
minLon = Math.min(minLon, values[i].getLon());
308+
maxLat = Math.max(maxLat, values[i].getLat());
309+
minLat = Math.min(minLat, values[i].getLat());
310+
}
311+
centroid.reset(centroidLat / count, centroidLon / count);
312+
boundingBox.topLeft().reset(maxLat, minLon);
313+
boundingBox.bottomRight().reset(minLat, maxLon);
314+
}
315+
300316
/**
301317
* Set the {@link #size()} and ensure that the {@link #values} array can
302318
* store at least that many entries.

0 commit comments

Comments
 (0)