Skip to content

Commit

Permalink
Teach runtime geopoint to parse arrays (#67985) (#67997)
Browse files Browse the repository at this point in the history
The default runtime geopoint parser should be able to handle point arrays. This PR adds the ability to the parser.
  • Loading branch information
iverase committed Jan 26, 2021
1 parent 5152b96 commit 5b196cb
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,28 @@ public interface LeafFactory {
@Override
public void execute() {
try {
Object v = XContentMapValues.extractValue(field, leafSearchLookup.source().loadSourceIfNeeded());
GeoUtils.parseGeoPoint(v, scratch, true);
emit(scratch.lat(), scratch.lon());
Object value = XContentMapValues.extractValue(field, leafSearchLookup.source().loadSourceIfNeeded());
if (value instanceof List<?>) {
List<?> values = (List<?>) value;
if (values.size() > 0 && values.get(0) instanceof Number) {
parsePoint(value);
} else {
for (Object point : values) {
parsePoint(point);
}
}
} else {
parsePoint(value);
}
} catch (Exception e) {
// ignore
}
}

private void parsePoint(Object point) {
GeoUtils.parseGeoPoint(point, scratch, true);
emit(scratch.lat(), scratch.lon());
}
};

public GeoPointFieldScript(String fieldName, Map<String, Object> params, SearchLookup searchLookup, LeafReaderContext ctx) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,20 @@ setup:
{"index":{}}
{"timestamp": "1998-04-30T14:30:17-05:00", "location" : {"lat": 13.5, "lon" : 34.89}}
{"index":{}}
{"timestamp": "1998-04-30T14:30:27-05:00", "location" : [{"lat": 13.0, "lon" : 34.0}, {"lat": 14.0, "lon" : 35.0}]}
{"index":{}}
{"timestamp": "1998-04-30T14:30:53-05:00", "location" : "-7.9, 120.78"}
{"index":{}}
{"timestamp": "1998-04-30T14:30:55-05:00", "location" : ["-8, 120", "-7, 121.0"]}
{"index":{}}
{"timestamp": "1998-04-30T14:31:12-05:00", "location" : [-173.45, 45.78]}
{"index":{}}
{"timestamp": "1998-04-30T14:31:18-05:00", "location" : [[-174.45, 46.78], [0.0, 1.0]]}
{"index":{}}
{"timestamp": "1998-04-30T14:31:19-05:00", "location" : "POINT(45.6 32.45)"}
{"index":{}}
{"timestamp": "1998-04-30T14:31:20-05:00", "location" : ["POINT(46.5 33.45)", "POINT(45.4 32.75)"]}
{"index":{}}
{"timestamp": "1998-04-30T14:31:22-05:00", "location" : {"lat": -63.24, "lon" : 31.0}}
{"index":{}}
{"timestamp": "1998-04-30T14:31:27-05:00", "location" : {"lat": 0.0, "lon" : 0.0}}
Expand All @@ -51,7 +59,7 @@ setup:
body:
sort: timestamp
fields: [location]
- match: {hits.total.value: 6}
- match: {hits.total.value: 10}
- match: {hits.hits.0.fields.location: ["13.499999991618097, 34.889999935403466"] }

---
Expand All @@ -63,7 +71,7 @@ setup:
query:
exists:
field: location
- match: {hits.total.value: 6}
- match: {hits.total.value: 10}

---
"geo bounding box query":
Expand All @@ -80,7 +88,7 @@ setup:
bottom_right:
lat: -10
lon: 10
- match: {hits.total.value: 1}
- match: {hits.total.value: 2}

---
"geo shape query":
Expand All @@ -94,7 +102,7 @@ setup:
shape:
type: "envelope"
coordinates: [ [ -10, 10 ], [ 10, -10 ] ]
- match: {hits.total.value: 1}
- match: {hits.total.value: 2}

---
"geo distance query":
Expand All @@ -108,7 +116,7 @@ setup:
location:
lat: 0
lon: 0
- match: {hits.total.value: 1}
- match: {hits.total.value: 2}

---
"bounds agg":
Expand All @@ -121,11 +129,11 @@ setup:
geo_bounds:
field: "location"
wrap_longitude: false
- match: {hits.total.value: 6}
- match: {aggregations.bounds.bounds.top_left.lat: 45.7799999602139 }
- match: {aggregations.bounds.bounds.top_left.lon: -173.4500000718981 }
- match: {hits.total.value: 10}
- match: {aggregations.bounds.bounds.top_left.lat: 46.77999998442829 }
- match: {aggregations.bounds.bounds.top_left.lon: -174.45000001229346 }
- match: {aggregations.bounds.bounds.bottom_right.lat: -63.240000014193356 }
- match: {aggregations.bounds.bounds.bottom_right.lon: 120.77999993227422 }
- match: {aggregations.bounds.bounds.bottom_right.lon: 120.99999999627471 }

---
"geo_distance sort":
Expand All @@ -138,12 +146,19 @@ setup:
location:
lat: 0.0
lon: 0.0
- match: {hits.total.value: 6}
- match: {hits.total.value: 10}
- match: {hits.hits.0._source.location.lat: 0.0 }
- match: {hits.hits.0._source.location.lon: 0.0 }
- match: {hits.hits.1._source.location.lat: 13.5 }
- match: {hits.hits.1._source.location.lon: 34.89 }
- match: {hits.hits.2._source.location: "POINT(45.6 32.45)" }
- match: {hits.hits.3._source.location.lat: -63.24 }
- match: {hits.hits.3._source.location.lon: 31.0 }
- match: {hits.hits.1._source.location.0.0: -174.45 }
- match: {hits.hits.1._source.location.0.1: 46.78 }
- match: {hits.hits.1._source.location.1.0: 0.0 }
- match: {hits.hits.1._source.location.1.1: 1.0 }
- match: {hits.hits.2._source.location.0.lat: 13.0 }
- match: {hits.hits.2._source.location.0.lon: 34.0 }
- match: {hits.hits.2._source.location.1.lat: 14.0 }
- match: {hits.hits.2._source.location.1.lon: 35.0 }
- match: {hits.hits.3._source.location.lat: 13.5 }
- match: {hits.hits.3._source.location.lon: 34.89 }
- match: {hits.hits.4._source.location.0: "POINT(46.5 33.45)" }
- match: {hits.hits.4._source.location.1: "POINT(45.4 32.75)" }

Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,24 @@ setup:
{"index":{}}
{"timestamp": "1998-04-30T14:30:17-05:00", "location" : {"lat": 13.5, "lon" : 34.89}}
{"index":{}}
{"timestamp": "1998-04-30T14:30:27-05:00", "location" : [{"lat": 13.0, "lon" : 34.0}, {"lat": 14.0, "lon" : 35.0}]}
{"index":{}}
{"timestamp": "1998-04-30T14:30:53-05:00", "location" : "-7.9, 120.78"}
{"index":{}}
{"timestamp": "1998-04-30T14:30:55-05:00", "location" : ["-8, 120", "-7, 121.0"]}
{"index":{}}
{"timestamp": "1998-04-30T14:31:12-05:00", "location" : [-173.45, 45.78]}
{"index":{}}
{"timestamp": "1998-04-30T14:31:18-05:00", "location" : [[-174.45, 46.78], [0.0, 1.0]]}
{"index":{}}
{"timestamp": "1998-04-30T14:31:19-05:00", "location" : "POINT(45.6 32.45)"}
{"index":{}}
{"timestamp": "1998-04-30T14:31:20-05:00", "location" : ["POINT(46.5 33.45)", "POINT(45.4 32.75)"]}
{"index":{}}
{"timestamp": "1998-04-30T14:31:22-05:00", "location" : {"lat": -63.24, "lon" : 31.0}}
{"index":{}}
{"timestamp": "1998-04-30T14:31:27-05:00", "location" : {"lat": 0.0, "lon" : 0.0}}
---
"fetch fields from source":
- do:
Expand All @@ -43,7 +50,7 @@ setup:
type: geo_point
sort: timestamp
fields: [location]
- match: {hits.total.value: 6}
- match: {hits.total.value: 10}
- match: {hits.hits.0.fields.location: ["13.499999991618097, 34.889999935403466"] }

---
Expand All @@ -58,7 +65,7 @@ setup:
query:
exists:
field: location
- match: {hits.total.value: 6}
- match: {hits.total.value: 10}

---
"geo bounding box query":
Expand All @@ -78,7 +85,7 @@ setup:
bottom_right:
lat: -10
lon: 10
- match: {hits.total.value: 1}
- match: {hits.total.value: 2}

---
"geo shape query":
Expand All @@ -95,7 +102,7 @@ setup:
shape:
type: "envelope"
coordinates: [ [ -10, 10 ], [ 10, -10 ] ]
- match: {hits.total.value: 1}
- match: {hits.total.value: 2}

---
"geo distance query":
Expand All @@ -112,7 +119,7 @@ setup:
location:
lat: 0
lon: 0
- match: {hits.total.value: 1}
- match: {hits.total.value: 2}

---
"bounds agg":
Expand All @@ -128,11 +135,11 @@ setup:
geo_bounds:
field: "location"
wrap_longitude: false
- match: {hits.total.value: 6}
- match: {aggregations.bounds.bounds.top_left.lat: 45.7799999602139 }
- match: {aggregations.bounds.bounds.top_left.lon: -173.4500000718981 }
- match: {hits.total.value: 10}
- match: {aggregations.bounds.bounds.top_left.lat: 46.77999998442829 }
- match: {aggregations.bounds.bounds.top_left.lon: -174.45000001229346 }
- match: {aggregations.bounds.bounds.bottom_right.lat: -63.240000014193356 }
- match: {aggregations.bounds.bounds.bottom_right.lon: 120.77999993227422 }
- match: {aggregations.bounds.bounds.bottom_right.lon: 120.99999999627471 }

---
"geo_distance sort":
Expand All @@ -148,12 +155,19 @@ setup:
location:
lat: 0.0
lon: 0.0
- match: {hits.total.value: 6}
- match: {hits.total.value: 10}
- match: {hits.hits.0._source.location.lat: 0.0 }
- match: {hits.hits.0._source.location.lon: 0.0 }
- match: {hits.hits.1._source.location.lat: 13.5 }
- match: {hits.hits.1._source.location.lon: 34.89 }
- match: {hits.hits.2._source.location: "POINT(45.6 32.45)" }
- match: {hits.hits.3._source.location.lat: -63.24 }
- match: {hits.hits.3._source.location.lon: 31.0 }
- match: {hits.hits.1._source.location.0.0: -174.45 }
- match: {hits.hits.1._source.location.0.1: 46.78 }
- match: {hits.hits.1._source.location.1.0: 0.0 }
- match: {hits.hits.1._source.location.1.1: 1.0 }
- match: {hits.hits.2._source.location.0.lat: 13.0 }
- match: {hits.hits.2._source.location.0.lon: 34.0 }
- match: {hits.hits.2._source.location.1.lat: 14.0 }
- match: {hits.hits.2._source.location.1.lon: 35.0 }
- match: {hits.hits.3._source.location.lat: 13.5 }
- match: {hits.hits.3._source.location.lon: 34.89 }
- match: {hits.hits.4._source.location.0: "POINT(46.5 33.45)" }
- match: {hits.hits.4._source.location.1: "POINT(45.4 32.75)" }

0 comments on commit 5b196cb

Please sign in to comment.