From 812697ebcf53e089e1d8e18307df5231fe3784c0 Mon Sep 17 00:00:00 2001 From: Ignacio Vera Date: Wed, 2 Jun 2021 13:53:16 +0200 Subject: [PATCH] Specialise ScriptDocValues#GeoPoints for single values (#73656) (#73659) --- .../index/fielddata/ScriptDocValues.java | 52 ++++++++++++------- 1 file changed, 34 insertions(+), 18 deletions(-) diff --git a/server/src/main/java/org/elasticsearch/index/fielddata/ScriptDocValues.java b/server/src/main/java/org/elasticsearch/index/fielddata/ScriptDocValues.java index 4413ef8dd1963..3f35a3f8e9609 100644 --- a/server/src/main/java/org/elasticsearch/index/fielddata/ScriptDocValues.java +++ b/server/src/main/java/org/elasticsearch/index/fielddata/ScriptDocValues.java @@ -273,30 +273,46 @@ public GeoPoints(MultiGeoPointValues in) { public void setNextDocId(int docId) throws IOException { if (in.advanceExact(docId)) { resize(in.docValueCount()); - double centroidLat = 0; - double centroidLon = 0; - double maxLon = Double.NEGATIVE_INFINITY; - double minLon = Double.POSITIVE_INFINITY; - double maxLat = Double.NEGATIVE_INFINITY; - double minLat = Double.POSITIVE_INFINITY; - for (int i = 0; i < count; i++) { - GeoPoint point = in.nextValue(); - values[i].reset(point.lat(), point.lon()); - centroidLat += point.getLat(); - centroidLon += point.getLon(); - maxLon = Math.max(maxLon, values[i].getLon()); - minLon = Math.min(minLon, values[i].getLon()); - maxLat = Math.max(maxLat, values[i].getLat()); - minLat = Math.min(minLat, values[i].getLat()); + if (count == 1) { + setSingleValue(); + } else { + setMultiValue(); } - centroid.reset(centroidLat / count, centroidLon / count); - boundingBox.topLeft().reset(maxLat, minLon); - boundingBox.bottomRight().reset(minLat, maxLon); } else { resize(0); } } + private void setSingleValue() throws IOException { + GeoPoint point = in.nextValue(); + values[0].reset(point.lat(), point.lon()); + centroid.reset(point.lat(), point.lon()); + boundingBox.topLeft().reset(point.lat(), point.lon()); + boundingBox.bottomRight().reset(point.lat(), point.lon()); + } + + private void setMultiValue() throws IOException { + double centroidLat = 0; + double centroidLon = 0; + double maxLon = Double.NEGATIVE_INFINITY; + double minLon = Double.POSITIVE_INFINITY; + double maxLat = Double.NEGATIVE_INFINITY; + double minLat = Double.POSITIVE_INFINITY; + for (int i = 0; i < count; i++) { + GeoPoint point = in.nextValue(); + values[i].reset(point.lat(), point.lon()); + centroidLat += point.getLat(); + centroidLon += point.getLon(); + maxLon = Math.max(maxLon, values[i].getLon()); + minLon = Math.min(minLon, values[i].getLon()); + maxLat = Math.max(maxLat, values[i].getLat()); + minLat = Math.min(minLat, values[i].getLat()); + } + centroid.reset(centroidLat / count, centroidLon / count); + boundingBox.topLeft().reset(maxLat, minLon); + boundingBox.bottomRight().reset(minLat, maxLon); + } + /** * Set the {@link #size()} and ensure that the {@link #values} array can * store at least that many entries.