-
Notifications
You must be signed in to change notification settings - Fork 25.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Use FallbackSyntheticSourceBlockLoader for point and geo_point #125816
Open
lkts
wants to merge
11
commits into
elastic:main
Choose a base branch
from
lkts:synthetic_source_block_loader_geo3
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+859
−87
Open
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
a55e156
wip
lkts 31802be
integrate into challenge tests
lkts 82e91d7
iter
lkts 85357ed
wip
lkts 2000a61
Update docs/changelog/125816.yaml
lkts 55970d2
Merge branch 'main' into synthetic_source_block_loader_geo3
lkts 3f24d64
remove random file
lkts 9e51dfd
fix checkstyle
lkts d1def39
fix number tests
lkts d601e3d
Merge branch 'main' into synthetic_source_block_loader_geo3
lkts 7135ee7
Add a comment
lkts File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
pr: 125816 | ||
summary: Use `FallbackSyntheticSourceBlockLoader` for point and `geo_point` | ||
area: Mapping | ||
type: enhancement | ||
issues: [] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
207 changes: 207 additions & 0 deletions
207
...c/test/java/org/elasticsearch/index/mapper/blockloader/GeoPointFieldBlockLoaderTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,207 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the "Elastic License | ||
* 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side | ||
* Public License v 1"; you may not use this file except in compliance with, at | ||
* your election, the "Elastic License 2.0", the "GNU Affero General Public | ||
* License v3.0 only", or the "Server Side Public License, v 1". | ||
*/ | ||
|
||
package org.elasticsearch.index.mapper.blockloader; | ||
|
||
import org.apache.lucene.util.BytesRef; | ||
import org.elasticsearch.common.geo.GeoPoint; | ||
import org.elasticsearch.geometry.Point; | ||
import org.elasticsearch.geometry.utils.WellKnownBinary; | ||
import org.elasticsearch.index.mapper.BlockLoaderTestCase; | ||
import org.elasticsearch.index.mapper.MappedFieldType; | ||
|
||
import java.nio.ByteOrder; | ||
import java.util.ArrayList; | ||
import java.util.Comparator; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Objects; | ||
|
||
public class GeoPointFieldBlockLoaderTests extends BlockLoaderTestCase { | ||
public GeoPointFieldBlockLoaderTests(BlockLoaderTestCase.Params params) { | ||
super("geo_point", params); | ||
} | ||
|
||
@Override | ||
@SuppressWarnings("unchecked") | ||
protected Object expected(Map<String, Object> fieldMapping, Object value, TestContext testContext) { | ||
var extractedFieldValues = (ExtractedFieldValues) value; | ||
var values = extractedFieldValues.values(); | ||
|
||
var nullValue = switch (fieldMapping.get("null_value")) { | ||
case String s -> convert(s, null); | ||
case null -> null; | ||
default -> throw new IllegalStateException("Unexpected null_value format"); | ||
}; | ||
|
||
if (params.preference() == MappedFieldType.FieldExtractPreference.DOC_VALUES && hasDocValues(fieldMapping, true)) { | ||
if (values instanceof List<?> == false) { | ||
var point = convert(values, nullValue); | ||
return point != null ? point.getEncoded() : null; | ||
} | ||
|
||
var resultList = ((List<Object>) values).stream() | ||
.map(v -> convert(v, nullValue)) | ||
.filter(Objects::nonNull) | ||
.map(GeoPoint::getEncoded) | ||
.sorted() | ||
.toList(); | ||
return maybeFoldList(resultList); | ||
} | ||
|
||
if (params.syntheticSource() == false) { | ||
return exactValuesFromSource(values, nullValue); | ||
} | ||
|
||
// Usually implementation of block loader from source adjusts values read from source | ||
// so that they look the same as doc_values would (like reducing precision). | ||
// geo_point does not do that and because of that we need to handle all these cases below. | ||
// If we are reading from stored source or fallback synthetic source we get the same exact data as source. | ||
// But if we are using "normal" synthetic source we get lesser precision data from doc_values. | ||
// That is unless "synthetic_source_keep" forces fallback synthetic source again. | ||
|
||
if (testContext.forceFallbackSyntheticSource()) { | ||
return exactValuesFromSource(values, nullValue); | ||
} | ||
|
||
String syntheticSourceKeep = (String) fieldMapping.getOrDefault("synthetic_source_keep", "none"); | ||
if (syntheticSourceKeep.equals("all")) { | ||
return exactValuesFromSource(values, nullValue); | ||
} | ||
if (syntheticSourceKeep.equals("arrays") && extractedFieldValues.documentHasObjectArrays()) { | ||
return exactValuesFromSource(values, nullValue); | ||
} | ||
|
||
// synthetic source and doc_values are present | ||
if (hasDocValues(fieldMapping, true)) { | ||
if (values instanceof List<?> == false) { | ||
return toWKB(normalize(convert(values, nullValue))); | ||
} | ||
|
||
var resultList = ((List<Object>) values).stream() | ||
.map(v -> convert(v, nullValue)) | ||
.filter(Objects::nonNull) | ||
.sorted(Comparator.comparingLong(GeoPoint::getEncoded)) | ||
.map(p -> toWKB(normalize(p))) | ||
.toList(); | ||
return maybeFoldList(resultList); | ||
} | ||
|
||
// synthetic source but no doc_values so using fallback synthetic source | ||
return exactValuesFromSource(values, nullValue); | ||
} | ||
|
||
@SuppressWarnings("unchecked") | ||
private Object exactValuesFromSource(Object value, GeoPoint nullValue) { | ||
if (value instanceof List<?> == false) { | ||
return toWKB(convert(value, nullValue)); | ||
} | ||
|
||
var resultList = ((List<Object>) value).stream().map(v -> convert(v, nullValue)).filter(Objects::nonNull).map(this::toWKB).toList(); | ||
return maybeFoldList(resultList); | ||
} | ||
|
||
private record ExtractedFieldValues(Object values, boolean documentHasObjectArrays) {} | ||
|
||
@Override | ||
protected Object getFieldValue(Map<String, Object> document, String fieldName) { | ||
var extracted = new ArrayList<>(); | ||
var documentHasObjectArrays = processLevel(document, fieldName, extracted, false); | ||
|
||
if (extracted.size() == 1) { | ||
return new ExtractedFieldValues(extracted.get(0), documentHasObjectArrays); | ||
} | ||
|
||
return new ExtractedFieldValues(extracted, documentHasObjectArrays); | ||
} | ||
|
||
@SuppressWarnings("unchecked") | ||
private boolean processLevel(Map<String, Object> level, String field, ArrayList<Object> extracted, boolean documentHasObjectArrays) { | ||
if (field.contains(".") == false) { | ||
var value = level.get(field); | ||
processLeafLevel(value, extracted); | ||
return documentHasObjectArrays; | ||
} | ||
|
||
var nameInLevel = field.split("\\.")[0]; | ||
var entry = level.get(nameInLevel); | ||
if (entry instanceof Map<?, ?> m) { | ||
return processLevel((Map<String, Object>) m, field.substring(field.indexOf('.') + 1), extracted, documentHasObjectArrays); | ||
} | ||
if (entry instanceof List<?> l) { | ||
for (var object : l) { | ||
processLevel((Map<String, Object>) object, field.substring(field.indexOf('.') + 1), extracted, true); | ||
} | ||
return true; | ||
} | ||
|
||
assert false : "unexpected document structure"; | ||
return false; | ||
} | ||
|
||
private void processLeafLevel(Object value, ArrayList<Object> extracted) { | ||
if (value instanceof List<?> l) { | ||
if (l.size() > 0 && l.get(0) instanceof Double) { | ||
// this must be a single point in array form | ||
// we'll put it into a different form here to make our lives a bit easier while implementing `expected` | ||
extracted.add(Map.of("type", "point", "coordinates", l)); | ||
} else { | ||
// this is actually an array of points but there could still be points in array form inside | ||
for (var arrayValue : l) { | ||
processLeafLevel(arrayValue, extracted); | ||
} | ||
} | ||
} else { | ||
extracted.add(value); | ||
} | ||
} | ||
|
||
@SuppressWarnings("unchecked") | ||
private GeoPoint convert(Object value, GeoPoint nullValue) { | ||
if (value == null) { | ||
return nullValue; | ||
} | ||
|
||
if (value instanceof String s) { | ||
try { | ||
return new GeoPoint(s); | ||
} catch (Exception e) { | ||
return null; | ||
} | ||
} | ||
|
||
if (value instanceof Map<?, ?> m) { | ||
if (m.get("type") != null) { | ||
var coordinates = (List<Double>) m.get("coordinates"); | ||
// Order is GeoJSON is lon,lat | ||
return new GeoPoint(coordinates.get(1), coordinates.get(0)); | ||
} else { | ||
return new GeoPoint((Double) m.get("lat"), (Double) m.get("lon")); | ||
} | ||
} | ||
|
||
// Malformed values are excluded | ||
return null; | ||
} | ||
|
||
private GeoPoint normalize(GeoPoint point) { | ||
if (point == null) { | ||
return null; | ||
} | ||
return point.resetFromEncoded(point.getEncoded()); | ||
} | ||
|
||
private BytesRef toWKB(GeoPoint point) { | ||
if (point == null) { | ||
return null; | ||
} | ||
|
||
return new BytesRef(WellKnownBinary.toWKB(new Point(point.getX(), point.getY()), ByteOrder.LITTLE_ENDIAN)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is an interesting case and i suspect it is not intentional.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you elaborate why this isn't intentional?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We don't apply custom logic of
synthetic_source_keep: "arrays"
to fields that haveparsesArrayValue()
set totrue
. But in this context we do.