-
Notifications
You must be signed in to change notification settings - Fork 24.9k
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
Create aliases for PassThroughObjectMapper subfields in FieldTypeLookup #106829
Changes from all commits
009ee70
c54fd29
6ec8699
05a6016
cbd6076
c084c31
5109d56
145ba0d
d867e30
0f9b08d
7f2dc2a
45aa214
3cd0f1c
ba3aecc
a72f16d
201b882
f700d59
8553b99
e1241c5
879afde
20ed13d
1a5823c
f7e78aa
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,6 +14,7 @@ | |
import java.util.Collections; | ||
import java.util.HashMap; | ||
import java.util.HashSet; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Objects; | ||
import java.util.Set; | ||
|
@@ -38,9 +39,14 @@ final class FieldTypeLookup { | |
|
||
private final int maxParentPathDots; | ||
|
||
FieldTypeLookup(Collection<FieldMapper> fieldMappers, Collection<FieldAliasMapper> fieldAliasMappers) { | ||
this(fieldMappers, fieldAliasMappers, List.of(), List.of()); | ||
} | ||
|
||
FieldTypeLookup( | ||
Collection<FieldMapper> fieldMappers, | ||
Collection<FieldAliasMapper> fieldAliasMappers, | ||
Collection<PassThroughObjectMapper> passThroughMappers, | ||
Collection<RuntimeField> runtimeFields | ||
) { | ||
|
||
|
@@ -86,6 +92,35 @@ final class FieldTypeLookup { | |
} | ||
} | ||
|
||
// Pass-though subfields can be referenced without the prefix corresponding to the | ||
// PassThroughObjectMapper name. This is achieved by adding a second reference to their | ||
// MappedFieldType using the remaining suffix. | ||
Map<String, PassThroughObjectMapper> passThroughFieldAliases = new HashMap<>(); | ||
for (PassThroughObjectMapper passThroughMapper : passThroughMappers) { | ||
for (Mapper subfield : passThroughMapper.mappers.values()) { | ||
if (subfield instanceof FieldMapper fieldMapper) { | ||
String name = fieldMapper.simpleName(); | ||
// Check for conflict between PassThroughObjectMapper subfields. | ||
PassThroughObjectMapper conflict = passThroughFieldAliases.put(name, passThroughMapper); | ||
if (conflict != null) { | ||
if (conflict.priority() > passThroughMapper.priority()) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. let's reverse the priority? A field from a passthrough field with a higher priority wins. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this is the case? If the inserted object has higher priority we leave it there, otherwise the conflicting one is put back. In both cases, |
||
// Keep the conflicting field if it has higher priority. | ||
passThroughFieldAliases.put(name, conflict); | ||
continue; | ||
} | ||
} else if (fullNameToFieldType.containsKey(name)) { | ||
// There's an existing field or alias for the same field. | ||
continue; | ||
} | ||
MappedFieldType fieldType = fieldMapper.fieldType(); | ||
fullNameToFieldType.put(name, fieldType); | ||
if (fieldType instanceof DynamicFieldType) { | ||
dynamicFieldTypes.put(name, (DynamicFieldType) fieldType); | ||
} | ||
} | ||
} | ||
} | ||
|
||
for (MappedFieldType fieldType : RuntimeField.collectFieldTypes(runtimeFields).values()) { | ||
// this will override concrete fields with runtime fields that have the same name | ||
fullNameToFieldType.put(fieldType.name(), fieldType); | ||
|
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.
Alternatively, you could also sort the passThroughMappers in ascending order and simply let mappers with a higher priority override i.e. put without checking for conflicts.
Your solution works just as well so feel free to ignore.
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.
If priority is going to be required, this does feel simpler to me.
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.
I think it's better to check for conflicts, to avoid cases with inconsistent behavior, depending on the order they show up in the FieldTypeLookup.