-
Notifications
You must be signed in to change notification settings - Fork 25.7k
Merge dynamic field type lookup into FieldTypeLookup #72024
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
Changes from 2 commits
4ba25b6
d9cbecb
63d427b
538831f
3385c36
63e9cb0
828214d
3630a3c
f0de3fe
673ae38
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
This file was deleted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -9,6 +9,7 @@ | |
| package org.elasticsearch.index.mapper; | ||
|
|
||
| import org.elasticsearch.common.regex.Regex; | ||
| import org.elasticsearch.index.mapper.flattened.FlattenedFieldMapper; | ||
|
|
||
| import java.util.Collection; | ||
| import java.util.Collections; | ||
|
|
@@ -31,23 +32,21 @@ final class FieldTypeLookup { | |
| * For convenience, the set of copied fields includes the field itself. | ||
| */ | ||
| private final Map<String, Set<String>> fieldToCopiedFields = new HashMap<>(); | ||
| private final DynamicKeyFieldTypeLookup dynamicKeyLookup; | ||
|
|
||
| private final int maxParentPathDots; | ||
|
|
||
| FieldTypeLookup( | ||
| Collection<FieldMapper> fieldMappers, | ||
| Collection<FieldAliasMapper> fieldAliasMappers, | ||
| Collection<RuntimeField> runtimeFields | ||
| ) { | ||
| Map<String, DynamicKeyFieldMapper> dynamicKeyMappers = new HashMap<>(); | ||
|
|
||
| int maxParentPathDots = 0; | ||
| for (FieldMapper fieldMapper : fieldMappers) { | ||
| String fieldName = fieldMapper.name(); | ||
| MappedFieldType fieldType = fieldMapper.fieldType(); | ||
| fullNameToFieldType.put(fieldType.name(), fieldType); | ||
| if (fieldMapper instanceof DynamicKeyFieldMapper) { | ||
| dynamicKeyMappers.put(fieldName, (DynamicKeyFieldMapper) fieldMapper); | ||
| } | ||
|
|
||
| maxParentPathDots = Math.max(maxParentPathDots, dotCount(fieldType.name())); | ||
| for (String targetField : fieldMapper.copyTo().copyToFields()) { | ||
| Set<String> sourcePath = fieldToCopiedFields.get(targetField); | ||
| if (sourcePath == null) { | ||
|
|
@@ -58,12 +57,11 @@ final class FieldTypeLookup { | |
| fieldToCopiedFields.get(targetField).add(fieldName); | ||
| } | ||
| } | ||
| this.maxParentPathDots = maxParentPathDots; | ||
|
|
||
| final Map<String, String> aliasToConcreteName = new HashMap<>(); | ||
| for (FieldAliasMapper fieldAliasMapper : fieldAliasMappers) { | ||
| String aliasName = fieldAliasMapper.name(); | ||
| String path = fieldAliasMapper.path(); | ||
| aliasToConcreteName.put(aliasName, path); | ||
| fullNameToFieldType.put(aliasName, fullNameToFieldType.get(path)); | ||
| } | ||
|
|
||
|
|
@@ -72,8 +70,30 @@ final class FieldTypeLookup { | |
| //this will override concrete fields with runtime fields that have the same name | ||
| fullNameToFieldType.put(runtimeFieldType.name(), runtimeFieldType); | ||
| } | ||
| } | ||
|
|
||
| private static int dotCount(String path) { | ||
| int dotCount = 0; | ||
| for (int i = 0; i < path.length(); i++) { | ||
| if (path.charAt(i) == '.') { | ||
| dotCount++; | ||
| } | ||
| } | ||
| return dotCount; | ||
| } | ||
|
|
||
| this.dynamicKeyLookup = new DynamicKeyFieldTypeLookup(dynamicKeyMappers, aliasToConcreteName); | ||
| // for testing | ||
| String longestPossibleParent(String path) { | ||
| int dotCount = 0; | ||
| for (int i = 0; i < path.length(); i++) { | ||
| if (path.charAt(i) == '.') { | ||
| dotCount++; | ||
| if (dotCount > maxParentPathDots) { | ||
| return path.substring(0, i); | ||
| } | ||
| } | ||
| } | ||
| return path; | ||
| } | ||
|
|
||
| /** | ||
|
|
@@ -85,9 +105,20 @@ MappedFieldType get(String field) { | |
| return fieldType; | ||
| } | ||
|
|
||
| // If the mapping contains fields that support dynamic sub-key lookup, check | ||
| // if this could correspond to a keyed field of the form 'path_to_field.path_to_key'. | ||
| return dynamicKeyLookup.get(field); | ||
| // Try parent fields instead! | ||
| String parentField = longestPossibleParent(field); | ||
| while (true) { | ||
|
Member
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. For some reason this loop makes me nervous that we may do more work than needed. Effectively we could stop once we encounter an object. But maybe that should not be a concern.
Contributor
Author
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.
Not quite - you can have a dynamic field nested inside an object. That's why we calculate the
Member
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. but you go backwards analyzing the path, and you can't have a dynamic field pointing to an object, right? so once you find an object, you should be done and there is no need to look at its parent and so on? Am I missing something?
Member
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. My comment is off, because when you look up field types, you can not find an object :) so I think this is a non-issue, like I said I am nervous that we go ahead and analyze the path when it's not needed, but I am not sure that would be a problem and how to avoid it. |
||
| fieldType = fullNameToFieldType.get(parentField); | ||
| if (fieldType != null) { | ||
| return fieldType.childFieldType(field.substring(parentField.length() + 1)); | ||
| } | ||
| if (parentField.contains(".") == false) { | ||
| break; | ||
| } | ||
| parentField = parentField.substring(0, parentField.lastIndexOf(".")); | ||
| } | ||
|
|
||
| return null; | ||
| } | ||
|
|
||
| /** | ||
|
|
@@ -130,7 +161,10 @@ Set<String> sourcePaths(String field) { | |
| if (fullNameToFieldType.isEmpty()) { | ||
| return Set.of(); | ||
| } | ||
| if (dynamicKeyLookup.get(field) != null) { | ||
|
|
||
| // TODO there must be a nicer way of doing this... | ||
| MappedFieldType fieldType = get(field); | ||
| if (fieldType instanceof FlattenedFieldMapper.KeyedFlattenedFieldType) { | ||
| return Set.of(field); | ||
| } | ||
|
|
||
|
|
||
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 this is now just used in
testMaxDynamicKeyDepthand should be removed. It looks like we need to rework this test.