|
12 | 12 | import org.apache.lucene.document.StoredField; |
13 | 13 | import org.apache.lucene.index.DirectoryReader; |
14 | 14 | import org.apache.lucene.index.LeafReaderContext; |
| 15 | +import org.apache.lucene.index.NoMergePolicy; |
15 | 16 | import org.apache.lucene.search.Collector; |
16 | 17 | import org.apache.lucene.search.IndexSearcher; |
17 | 18 | import org.apache.lucene.search.LeafCollector; |
|
23 | 24 | import org.apache.lucene.tests.index.RandomIndexWriter; |
24 | 25 | import org.apache.lucene.util.BytesRef; |
25 | 26 | import org.elasticsearch.common.geo.GeoPoint; |
| 27 | +import org.elasticsearch.common.geo.GeoUtils; |
| 28 | +import org.elasticsearch.common.geo.GeometryFormatterFactory; |
26 | 29 | import org.elasticsearch.common.lucene.search.function.ScriptScoreQuery; |
| 30 | +import org.elasticsearch.common.settings.Settings; |
27 | 31 | import org.elasticsearch.geo.GeometryTestUtils; |
28 | 32 | import org.elasticsearch.index.IndexVersion; |
29 | 33 | import org.elasticsearch.index.fielddata.GeoPointScriptFieldData; |
|
37 | 41 | import org.elasticsearch.script.ScriptFactory; |
38 | 42 | import org.elasticsearch.script.ScriptType; |
39 | 43 | import org.elasticsearch.search.MultiValueMode; |
| 44 | +import org.elasticsearch.search.lookup.SearchLookup; |
40 | 45 | import org.elasticsearch.search.lookup.Source; |
41 | 46 |
|
42 | 47 | import java.io.IOException; |
43 | 48 | import java.util.ArrayList; |
| 49 | +import java.util.Arrays; |
44 | 50 | import java.util.List; |
45 | 51 | import java.util.Map; |
| 52 | +import java.util.function.Function; |
46 | 53 |
|
47 | 54 | import static java.util.Collections.emptyMap; |
| 55 | +import static org.elasticsearch.index.mapper.GeoPointFieldMapper.GeoPointFieldType.GEO_FORMATTER_FACTORY; |
48 | 56 | import static org.hamcrest.Matchers.containsInAnyOrder; |
49 | 57 | import static org.hamcrest.Matchers.equalTo; |
| 58 | +import static org.hamcrest.Matchers.instanceOf; |
| 59 | +import static org.hamcrest.Matchers.nullValue; |
50 | 60 |
|
51 | 61 | public class GeoPointScriptFieldTypeTests extends AbstractNonTextScriptFieldTypeTestCase { |
52 | 62 |
|
| 63 | + private static final GeoPoint EMPTY_POINT = null; |
| 64 | + private static final GeoPoint MALFORMED_POINT = null; |
| 65 | + |
| 66 | + private static final Function<List<GeoPoint>, List<Object>> FORMATTER = GEO_FORMATTER_FACTORY.getFormatter( |
| 67 | + GeometryFormatterFactory.WKB, |
| 68 | + p -> { |
| 69 | + if (p != null) { |
| 70 | + return new org.elasticsearch.geometry.Point(p.getLon(), p.getLat()); |
| 71 | + } |
| 72 | + return null; |
| 73 | + } |
| 74 | + ); |
| 75 | + |
53 | 76 | @Override |
54 | 77 | protected ScriptFactory parseFromSource() { |
55 | 78 | return GeoPointFieldScript.PARSE_FROM_SOURCE; |
@@ -225,6 +248,140 @@ public void testTermsQuery() { |
225 | 248 |
|
226 | 249 | } |
227 | 250 |
|
| 251 | + public void testBlockLoaderSourceOnlyRuntimeFieldWithSyntheticSource() throws IOException { |
| 252 | + try ( |
| 253 | + Directory directory = newDirectory(); |
| 254 | + RandomIndexWriter iw = new RandomIndexWriter(random(), directory, newIndexWriterConfig().setMergePolicy(NoMergePolicy.INSTANCE)) |
| 255 | + ) { |
| 256 | + // given |
| 257 | + iw.addDocuments( |
| 258 | + List.of( |
| 259 | + createDocumentWithIgnoredSource(""" |
| 260 | + {"type": "Point", "coordinates": [11.22, -33.44]} |
| 261 | + """), |
| 262 | + createDocumentWithIgnoredSource(""" |
| 263 | + ["POINT (23.45 -56.78)"] |
| 264 | + """), |
| 265 | + createDocumentWithIgnoredSource(""" |
| 266 | + {"lat": 22.33, "lon" : -44.55}} |
| 267 | + """), |
| 268 | + createDocumentWithIgnoredSource(""" |
| 269 | + [-33.44, 55.66] |
| 270 | + """), |
| 271 | + createDocumentWithIgnoredSource(""" |
| 272 | + [-44.55, 66.77, 88.99] |
| 273 | + """), |
| 274 | + createDocumentWithIgnoredSource(""" |
| 275 | + ["55.66,77.88"] |
| 276 | + """), |
| 277 | + createDocumentWithIgnoredSource(""" |
| 278 | + ["drm3btev3e86"] |
| 279 | + """), |
| 280 | + // ensure empty points can be parsed |
| 281 | + createDocumentWithIgnoredSource(""" |
| 282 | + [] |
| 283 | + """), |
| 284 | + // ensure a malformed value doesn't crash |
| 285 | + createDocumentWithIgnoredSource(""" |
| 286 | + ["potato", "tomato"] |
| 287 | + """) |
| 288 | + ) |
| 289 | + ); |
| 290 | + |
| 291 | + Settings settings = Settings.builder().put("index.mapping.source.mode", "synthetic").build(); |
| 292 | + GeoPointScriptFieldType fieldType = simpleSourceOnlyMappedFieldType(); |
| 293 | + |
| 294 | + // note, the order of coordinates here differs from the documents above. This is expected - x and y coordinates get mapped to |
| 295 | + // lon and lat differently depending on the underlying type of the geo point itself. Sometimes they match 1:1, others they swap |
| 296 | + List<Object> expectedPoints = FORMATTER.apply( |
| 297 | + Arrays.asList( |
| 298 | + new GeoPoint(-33.44, 11.22), |
| 299 | + new GeoPoint(-56.78, 23.45), |
| 300 | + new GeoPoint(22.33, -44.55), |
| 301 | + new GeoPoint(55.66, -33.44), |
| 302 | + new GeoPoint(66.77, -44.55), |
| 303 | + new GeoPoint(55.66, 77.88), |
| 304 | + new GeoPoint("drm3btev3e86") |
| 305 | + ) |
| 306 | + ); |
| 307 | + |
| 308 | + // add these separately because the formatter doesn't like nulls |
| 309 | + expectedPoints.add(EMPTY_POINT); |
| 310 | + expectedPoints.add(MALFORMED_POINT); |
| 311 | + |
| 312 | + // expected points converted to BytesRef |
| 313 | + List<BytesRef> expected = expectedPoints.stream().map(gp -> { |
| 314 | + if (gp instanceof byte[] wkb) { |
| 315 | + return new BytesRef(wkb); |
| 316 | + } |
| 317 | + return null; |
| 318 | + }).toList(); |
| 319 | + |
| 320 | + try (DirectoryReader reader = iw.getReader()) { |
| 321 | + // when |
| 322 | + BlockLoader loader = fieldType.blockLoader(blContext(settings, true)); |
| 323 | + |
| 324 | + // then |
| 325 | + |
| 326 | + // assert loader is of expected instance type |
| 327 | + assertThat(loader, instanceOf(FallbackSyntheticSourceBlockLoader.class)); |
| 328 | + |
| 329 | + // ignored source doesn't support column at a time loading: |
| 330 | + var columnAtATimeLoader = loader.columnAtATimeReader(reader.leaves().getFirst()); |
| 331 | + assertThat(columnAtATimeLoader, nullValue()); |
| 332 | + |
| 333 | + var rowStrideReader = loader.rowStrideReader(reader.leaves().getFirst()); |
| 334 | + assertThat( |
| 335 | + rowStrideReader.getClass().getName(), |
| 336 | + equalTo("org.elasticsearch.index.mapper.FallbackSyntheticSourceBlockLoader$IgnoredSourceRowStrideReader") |
| 337 | + ); |
| 338 | + |
| 339 | + // assert values |
| 340 | + assertThat(blockLoaderReadValuesFromRowStrideReader(settings, reader, fieldType, true), equalTo(expected)); |
| 341 | + } |
| 342 | + } |
| 343 | + } |
| 344 | + |
| 345 | + /** |
| 346 | + * Returns a source only mapped field type. This is useful, since the available build() function doesn't override isParsedFromSource() |
| 347 | + */ |
| 348 | + private GeoPointScriptFieldType simpleSourceOnlyMappedFieldType() { |
| 349 | + Script script = new Script(ScriptType.INLINE, "test", "", emptyMap()); |
| 350 | + GeoPointFieldScript.Factory factory = new GeoPointFieldScript.Factory() { |
| 351 | + @Override |
| 352 | + public GeoPointFieldScript.LeafFactory newFactory( |
| 353 | + String fieldName, |
| 354 | + Map<String, Object> params, |
| 355 | + SearchLookup searchLookup, |
| 356 | + OnScriptError onScriptError |
| 357 | + ) { |
| 358 | + return ctx -> new GeoPointFieldScript(fieldName, params, searchLookup, onScriptError, ctx) { |
| 359 | + @Override |
| 360 | + @SuppressWarnings("unchecked") |
| 361 | + public void execute() { |
| 362 | + Map<String, Object> source = (Map<String, Object>) this.getParams().get("_source"); |
| 363 | + for (Object foo : (List<?>) source.get("test")) { |
| 364 | + try { |
| 365 | + // ignore the Z coordinate because we don't care about it anyway |
| 366 | + // this conversion matches GeoPointFieldScript.emitPoint() |
| 367 | + GeoPoint gp = GeoUtils.parseGeoPoint(foo, true); |
| 368 | + emit(gp.lat(), gp.lon()); |
| 369 | + } catch (Exception e) { |
| 370 | + // skip |
| 371 | + } |
| 372 | + } |
| 373 | + } |
| 374 | + }; |
| 375 | + } |
| 376 | + |
| 377 | + @Override |
| 378 | + public boolean isParsedFromSource() { |
| 379 | + return true; |
| 380 | + } |
| 381 | + }; |
| 382 | + return new GeoPointScriptFieldType("test", factory, script, emptyMap(), OnScriptError.FAIL); |
| 383 | + } |
| 384 | + |
228 | 385 | @Override |
229 | 386 | protected Query randomTermsQuery(MappedFieldType ft, SearchExecutionContext ctx) { |
230 | 387 | return ft.termsQuery(randomList(100, GeometryTestUtils::randomPoint), mockContext()); |
|
0 commit comments