Skip to content

Commit

Permalink
Fix bug 94733 geo_line not respecting sort_order (#94734)
Browse files Browse the repository at this point in the history
* Move unused geoline tests into correct location

The original geo_line PR included two yaml test files, one of which
was never used. This commit moves that test into the correct place,
and fixes a syntax error in the test.

* Fix geo_line sort order (#94733)

The code was re-sorting to ASC explicitly in the reduce phase.
No explanation was given. Removing this extra sort causes the
new yaml test to pass. Two failing java tests were fixed by no
longer explicitly expected ASC data when order was DESC.

* Update docs/changelog/94734.yaml

* Improve changelog text

* Added test for size in geo_line

* Add geo_line sort and limit test
  • Loading branch information
craigtaverner authored Mar 28, 2023
1 parent 5e327e2 commit e5f03ad
Show file tree
Hide file tree
Showing 6 changed files with 153 additions and 69 deletions.
6 changes: 6 additions & 0 deletions docs/changelog/94734.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
pr: 94734
summary: Fix bug where `geo_line` does not respect `sort_order`
area: Geo
type: bug
issues:
- 94733
Original file line number Diff line number Diff line change
Expand Up @@ -110,10 +110,6 @@ public InternalAggregation reduce(List<InternalAggregation> aggregations, Aggreg

MergedGeoLines mergedGeoLines = new MergedGeoLines(internalGeoLines, finalSize, sortOrder);
mergedGeoLines.merge();
// the final reduce should always be in ascending order
if (reduceContext.isFinalReduce() && SortOrder.DESC.equals(sortOrder)) {
new PathArraySorter(mergedGeoLines.getFinalPoints(), mergedGeoLines.getFinalSortValues(), SortOrder.ASC).sort();
}
return new InternalGeoLine(
name,
mergedGeoLines.getFinalPoints(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -323,13 +323,13 @@ private void testAggregator(SortOrder sortOrder) throws IOException {
int encodedLon = GeoEncodingUtils.encodeLongitude(point.getLon());
long lonLat = (((long) encodedLon) << 32) | encodedLat & 0xffffffffL;
points[i] = lonLat;
sortValues[i] = SortOrder.ASC.equals(sortOrder) ? i : numPoints - i;
sortValues[i] = i;
}
int lineSize = Math.min(numPoints, size);
// re-sort line to be ascending
long[] linePoints = Arrays.copyOf(points, lineSize);
double[] lineSorts = Arrays.copyOf(sortValues, lineSize);
new PathArraySorter(linePoints, lineSorts, SortOrder.ASC).sort();
new PathArraySorter(linePoints, lineSorts, sortOrder).sort();

lines.put(String.valueOf(groupOrd), new InternalGeoLine("_name", linePoints, lineSorts, null, complete, true, sortOrder, size));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,10 +134,6 @@ protected void assertReduced(InternalGeoLine reduced, List<InternalGeoLine> inpu
long[] finalCappedPoints = Arrays.copyOf(finalList, Math.min(reduced.size(), mergedLength));
double[] finalCappedSortVals = Arrays.copyOf(finalSortVals, Math.min(reduced.size(), mergedLength));

if (SortOrder.DESC.equals(reduced.sortOrder())) {
new PathArraySorter(finalCappedPoints, finalCappedSortVals, SortOrder.ASC).sort();
}

assertArrayEquals(finalCappedSortVals, reduced.sortVals(), 0d);
assertArrayEquals(finalCappedPoints, reduced.line());
}
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,17 @@ setup:
position:
type: geo_point

- do:
indices.create:
index: locations
body:
mappings:
properties:
location:
type: geo_point
rank:
type: double

- do:
indices.create:
index: test1
Expand Down Expand Up @@ -49,6 +60,18 @@ setup:
{"index":{}}
{"position": "POINT(4.914722 52.371667)", "race_id": "Amsterdam", "timestamp": 10}
- do:
bulk:
index: locations
refresh: true
body: |
{"index":{}}
{"location": [13.37139831, 47.82930284], "rank": 2.0 }
{"index":{}}
{"location": [13.3784208402, 47.88832084022], "rank": 0.0 }
{"index":{}}
{"location": [13.371830148701, 48.2084200148], "rank": 1.2 }
- do:
bulk:
index: test1
Expand Down Expand Up @@ -79,6 +102,128 @@ setup:
- do:
indices.refresh: { }

---
"Test geo_line aggregation on geo points sort by double and include_sort":
- do:
search:
rest_total_hits_as_int: true
index: locations
size: 0
body:
aggs:
path:
geo_line:
include_sort: true
point:
field: location
sort:
field: rank
- match: { hits.total: 3 }
- match: { aggregations.path.type: "Feature" }
- match: { aggregations.path.geometry.type: "LineString" }
- length: { aggregations.path.geometry.coordinates: 3 }
- match: { aggregations.path.geometry.coordinates.0.0: 13.378421 }
- match: { aggregations.path.geometry.coordinates.0.1: 47.888321 }
- match: { aggregations.path.geometry.coordinates.1.0: 13.37183 }
- match: { aggregations.path.geometry.coordinates.1.1: 48.20842 }
- match: { aggregations.path.geometry.coordinates.2.0: 13.371398 }
- match: { aggregations.path.geometry.coordinates.2.1: 47.829303 }
- is_true: aggregations.path.properties.complete
- length: { aggregations.path.properties.sort_values: 3 }
- match: { aggregations.path.properties.sort_values.0: 0.0 }
- match: { aggregations.path.properties.sort_values.1: 1.2 }
- match: { aggregations.path.properties.sort_values.2: 2.0 }

---
"Test geo_line aggregation on geo points reverse sort by double and include_sort":
- do:
search:
rest_total_hits_as_int: true
index: locations
size: 0
body:
aggs:
path:
geo_line:
include_sort: true
sort_order: "desc"
point:
field: location
sort:
field: rank
- match: { hits.total: 3 }
- match: { aggregations.path.type: "Feature" }
- match: { aggregations.path.geometry.type: "LineString" }
- length: { aggregations.path.geometry.coordinates: 3 }
- match: { aggregations.path.geometry.coordinates.0.0: 13.371398 }
- match: { aggregations.path.geometry.coordinates.0.1: 47.829303 }
- match: { aggregations.path.geometry.coordinates.1.0: 13.37183 }
- match: { aggregations.path.geometry.coordinates.1.1: 48.20842 }
- match: { aggregations.path.geometry.coordinates.2.0: 13.378421 }
- match: { aggregations.path.geometry.coordinates.2.1: 47.888321 }
- is_true: aggregations.path.properties.complete
- length: { aggregations.path.properties.sort_values: 3 }
- match: { aggregations.path.properties.sort_values.0: 2.0 }
- match: { aggregations.path.properties.sort_values.1: 1.2 }
- match: { aggregations.path.properties.sort_values.2: 0.0 }

---
"Test geo_line aggregation on geo points limit size":
- do:
search:
rest_total_hits_as_int: true
index: locations
size: 0
body:
aggs:
path:
geo_line:
size: 2
point:
field: location
sort:
field: rank
- match: { hits.total: 3 }
- match: { aggregations.path.type: "Feature" }
- match: { aggregations.path.geometry.type: "LineString" }
- length: { aggregations.path.geometry.coordinates: 2 }
- match: { aggregations.path.geometry.coordinates.0.0: 13.378421 }
- match: { aggregations.path.geometry.coordinates.0.1: 47.888321 }
- match: { aggregations.path.geometry.coordinates.1.0: 13.37183 }
- match: { aggregations.path.geometry.coordinates.1.1: 48.20842 }
- is_false: aggregations.path.properties.complete

---
"Test geo_line aggregation on geo points limit and sort":
- do:
search:
rest_total_hits_as_int: true
index: locations
size: 0
body:
aggs:
path:
geo_line:
size: 2
include_sort: true
sort_order: "desc"
point:
field: location
sort:
field: rank
- match: { hits.total: 3 }
- match: { aggregations.path.type: "Feature" }
- match: { aggregations.path.geometry.type: "LineString" }
- length: { aggregations.path.geometry.coordinates: 2 }
- match: { aggregations.path.geometry.coordinates.0.0: 13.371398 }
- match: { aggregations.path.geometry.coordinates.0.1: 47.829303 }
- match: { aggregations.path.geometry.coordinates.1.0: 13.37183 }
- match: { aggregations.path.geometry.coordinates.1.1: 48.20842 }
- is_false: aggregations.path.properties.complete
- length: { aggregations.path.properties.sort_values: 2 }
- match: { aggregations.path.properties.sort_values.0: 2.0 }
- match: { aggregations.path.properties.sort_values.1: 1.2 }

---
"Test geo_line aggregation on geo points with no grouping":
- do:
Expand Down

0 comments on commit e5f03ad

Please sign in to comment.