|
17 | 17 | import org.apache.lucene.search.Query;
|
18 | 18 | import org.apache.lucene.store.Directory;
|
19 | 19 | import org.apache.lucene.util.BytesRef;
|
20 |
| -import org.apache.lucene.util.LuceneTestCase; |
21 | 20 | import org.apache.lucene.util.NumericUtils;
|
22 | 21 | import org.elasticsearch.common.CheckedConsumer;
|
23 | 22 | import org.elasticsearch.geo.GeometryTestUtils;
|
|
44 | 43 |
|
45 | 44 | import static org.hamcrest.CoreMatchers.equalTo;
|
46 | 45 |
|
47 |
| -@LuceneTestCase.AwaitsFix(bugUrl = "https://github.com/elastic/elasticsearch/issues/65473") |
48 | 46 | public class GeoLineAggregatorTests extends AggregatorTestCase {
|
49 | 47 |
|
50 | 48 | @Override
|
@@ -112,6 +110,96 @@ public void testDescending() throws IOException {
|
112 | 110 | testAggregator(SortOrder.DESC);
|
113 | 111 | }
|
114 | 112 |
|
| 113 | + public void testComplete() throws IOException { |
| 114 | + // max size is the same as the number of points |
| 115 | + testCompleteForSizeAndNumDocuments(10, 10, true); |
| 116 | + // max size is more than the number of points |
| 117 | + testCompleteForSizeAndNumDocuments(11, 10, true); |
| 118 | + // max size is less than the number of points |
| 119 | + testCompleteForSizeAndNumDocuments(9, 10, false); |
| 120 | + } |
| 121 | + |
| 122 | + public void testCompleteForSizeAndNumDocuments(int size, int numPoints, boolean complete) throws IOException { |
| 123 | + MultiValuesSourceFieldConfig valueConfig = new MultiValuesSourceFieldConfig.Builder() |
| 124 | + .setFieldName("value_field") |
| 125 | + .build(); |
| 126 | + MultiValuesSourceFieldConfig sortConfig = new MultiValuesSourceFieldConfig.Builder().setFieldName("sort_field").build(); |
| 127 | + GeoLineAggregationBuilder lineAggregationBuilder = new GeoLineAggregationBuilder("_name") |
| 128 | + .point(valueConfig) |
| 129 | + .sortOrder(SortOrder.ASC) |
| 130 | + .sort(sortConfig) |
| 131 | + .size(size); |
| 132 | + TermsAggregationBuilder aggregationBuilder = new TermsAggregationBuilder("_name") |
| 133 | + .field("group_id") |
| 134 | + .subAggregation(lineAggregationBuilder); |
| 135 | + |
| 136 | + Map<String, InternalGeoLine> lines = new HashMap<>(1); |
| 137 | + String groupOrd = "0"; |
| 138 | + long[] points = new long[numPoints]; |
| 139 | + double[] sortValues = new double[numPoints]; |
| 140 | + for (int i = 0; i < numPoints; i++) { |
| 141 | + Point point = GeometryTestUtils.randomPoint(false); |
| 142 | + int encodedLat = GeoEncodingUtils.encodeLatitude(point.getLat()); |
| 143 | + int encodedLon = GeoEncodingUtils.encodeLongitude(point.getLon()); |
| 144 | + long lonLat = (((long) encodedLon) << 32) | encodedLat & 0xffffffffL; |
| 145 | + points[i] = lonLat; |
| 146 | + sortValues[i] = i; |
| 147 | + } |
| 148 | + |
| 149 | + int lineSize = Math.min(numPoints, size); |
| 150 | + // re-sort line to be ascending |
| 151 | + long[] linePoints = Arrays.copyOf(points, lineSize); |
| 152 | + double[] lineSorts = Arrays.copyOf(sortValues, lineSize); |
| 153 | + new PathArraySorter(linePoints, lineSorts, SortOrder.ASC).sort(); |
| 154 | + |
| 155 | + lines.put(groupOrd, new InternalGeoLine("_name", |
| 156 | + linePoints, lineSorts, null, complete, true, SortOrder.ASC, size)); |
| 157 | + |
| 158 | + testCase(new MatchAllDocsQuery(), aggregationBuilder, iw -> { |
| 159 | + for (int i = 0; i < points.length; i++) { |
| 160 | + int x = (int) (points[i] >> 32); |
| 161 | + int y = (int) points[i]; |
| 162 | + iw.addDocument(Arrays.asList(new LatLonDocValuesField("value_field", |
| 163 | + GeoEncodingUtils.decodeLatitude(y), |
| 164 | + GeoEncodingUtils.decodeLongitude(x)), |
| 165 | + new SortedNumericDocValuesField("sort_field", NumericUtils.doubleToSortableLong(sortValues[i])), |
| 166 | + new SortedDocValuesField("group_id", new BytesRef(groupOrd)))); |
| 167 | + } |
| 168 | + }, terms -> { |
| 169 | + for (Terms.Bucket bucket : terms.getBuckets()) { |
| 170 | + InternalGeoLine expectedGeoLine = lines.get(bucket.getKeyAsString()); |
| 171 | + InternalGeoLine geoLine = bucket.getAggregations().get("_name"); |
| 172 | + assertThat(geoLine.length(), equalTo(expectedGeoLine.length())); |
| 173 | + assertThat(geoLine.isComplete(), equalTo(expectedGeoLine.isComplete())); |
| 174 | + for (int i = 0; i < geoLine.sortVals().length; i++) { |
| 175 | + geoLine.sortVals()[i] = NumericUtils.sortableLongToDouble((long) geoLine.sortVals()[i]); |
| 176 | + } |
| 177 | + assertArrayEquals(expectedGeoLine.sortVals(), geoLine.sortVals(), 0d); |
| 178 | + assertArrayEquals(expectedGeoLine.line(), geoLine.line()); |
| 179 | + } |
| 180 | + }); |
| 181 | + } |
| 182 | + |
| 183 | + public void testEmpty() throws IOException { |
| 184 | + int size = randomIntBetween(1, GeoLineAggregationBuilder.MAX_PATH_SIZE); |
| 185 | + MultiValuesSourceFieldConfig valueConfig = new MultiValuesSourceFieldConfig.Builder() |
| 186 | + .setFieldName("value_field") |
| 187 | + .build(); |
| 188 | + MultiValuesSourceFieldConfig sortConfig = new MultiValuesSourceFieldConfig.Builder().setFieldName("sort_field").build(); |
| 189 | + GeoLineAggregationBuilder lineAggregationBuilder = new GeoLineAggregationBuilder("_name") |
| 190 | + .point(valueConfig) |
| 191 | + .sortOrder(SortOrder.ASC) |
| 192 | + .sort(sortConfig) |
| 193 | + .size(size); |
| 194 | + TermsAggregationBuilder aggregationBuilder = new TermsAggregationBuilder("_name") |
| 195 | + .field("group_id") |
| 196 | + .subAggregation(lineAggregationBuilder); |
| 197 | + testCase(new MatchAllDocsQuery(), aggregationBuilder, iw -> { |
| 198 | + }, terms -> { |
| 199 | + assertTrue(terms.getBuckets().isEmpty()); |
| 200 | + }); |
| 201 | + } |
| 202 | + |
115 | 203 | private void testAggregator(SortOrder sortOrder) throws IOException {
|
116 | 204 | int size = randomIntBetween(1, GeoLineAggregationBuilder.MAX_PATH_SIZE);
|
117 | 205 | MultiValuesSourceFieldConfig valueConfig = new MultiValuesSourceFieldConfig.Builder()
|
|
0 commit comments