-
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 all 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| /* | ||
| * 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 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 or the Server | ||
| * Side Public License, v 1. | ||
| */ | ||
|
|
||
| package org.elasticsearch.index.mapper; | ||
|
|
||
| /** | ||
| * Defines a MappedFieldType that exposes dynamic child field types | ||
| * | ||
| * If the field is named 'my_field', then a user is able to search on | ||
| * the field in both of the following ways: | ||
| * - Using the field name 'my_field', which will delegate to the field type | ||
| * as usual. | ||
| * - Using any sub-key, for example 'my_field.some_key'. In this case, the | ||
| * search is delegated to {@link #getChildFieldType(String)}, with 'some_key' | ||
| * passed as the argument. The field may create a new field type dynamically | ||
| * in order to handle the search. | ||
| * | ||
| * To prevent conflicts between these dynamic sub-keys and multi-fields, any | ||
| * field mappers generating field types that implement this interface should | ||
| * explicitly disallow multi-fields. | ||
| */ | ||
| public interface DynamicFieldType { | ||
|
|
||
| /** | ||
| * Returns a dynamic MappedFieldType for the given path | ||
| */ | ||
| MappedFieldType getChildFieldType(String path); | ||
|
|
||
| } |
This file was deleted.
This file was deleted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -15,13 +15,15 @@ | |
| import java.util.HashMap; | ||
| import java.util.HashSet; | ||
| import java.util.Map; | ||
| import java.util.Objects; | ||
| import java.util.Set; | ||
|
|
||
| /** | ||
| * An immutable container for looking up {@link MappedFieldType}s by their name. | ||
| */ | ||
| final class FieldTypeLookup { | ||
| private final Map<String, MappedFieldType> fullNameToFieldType = new HashMap<>(); | ||
| private final Map<String, DynamicFieldType> dynamicFieldTypes = new HashMap<>(); | ||
|
|
||
| /** | ||
| * A map from field name to all fields whose content has been copied into it | ||
|
|
@@ -31,23 +33,22 @@ 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<>(); | ||
|
|
||
| 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); | ||
| if (fieldType instanceof DynamicFieldType) { | ||
| dynamicFieldTypes.put(fieldType.name(), (DynamicFieldType) fieldType); | ||
| } | ||
|
|
||
| for (String targetField : fieldMapper.copyTo().copyToFields()) { | ||
| Set<String> sourcePath = fieldToCopiedFields.get(targetField); | ||
| if (sourcePath == null) { | ||
|
|
@@ -59,21 +60,37 @@ final class FieldTypeLookup { | |
| } | ||
| } | ||
|
|
||
| final Map<String, String> aliasToConcreteName = new HashMap<>(); | ||
| int maxParentPathDots = 0; | ||
| for (String dynamicRoot : dynamicFieldTypes.keySet()) { | ||
| maxParentPathDots = Math.max(maxParentPathDots, dotCount(dynamicRoot)); | ||
| } | ||
| this.maxParentPathDots = maxParentPathDots; | ||
|
|
||
| for (FieldAliasMapper fieldAliasMapper : fieldAliasMappers) { | ||
| String aliasName = fieldAliasMapper.name(); | ||
| String path = fieldAliasMapper.path(); | ||
| aliasToConcreteName.put(aliasName, path); | ||
| fullNameToFieldType.put(aliasName, fullNameToFieldType.get(path)); | ||
| MappedFieldType fieldType = fullNameToFieldType.get(path); | ||
| fullNameToFieldType.put(aliasName, fieldType); | ||
| if (fieldType instanceof DynamicFieldType) { | ||
| dynamicFieldTypes.put(aliasName, (DynamicFieldType) fieldType); | ||
| } | ||
| } | ||
|
|
||
| for (RuntimeField runtimeField : runtimeFields) { | ||
| MappedFieldType runtimeFieldType = runtimeField.asMappedFieldType(); | ||
| //this will override concrete fields with runtime fields that have the same name | ||
| fullNameToFieldType.put(runtimeFieldType.name(), runtimeFieldType); | ||
| } | ||
| } | ||
|
|
||
| this.dynamicKeyLookup = new DynamicKeyFieldTypeLookup(dynamicKeyMappers, aliasToConcreteName); | ||
| private static int dotCount(String path) { | ||
| int dotCount = 0; | ||
| for (int i = 0; i < path.length(); i++) { | ||
| if (path.charAt(i) == '.') { | ||
| dotCount++; | ||
| } | ||
| } | ||
| return dotCount; | ||
| } | ||
|
|
||
| /** | ||
|
|
@@ -84,10 +101,42 @@ MappedFieldType get(String field) { | |
| if (fieldType != null) { | ||
| return fieldType; | ||
| } | ||
| return getDynamicField(field); | ||
| } | ||
|
|
||
| // 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); | ||
| // for testing | ||
| int getMaxParentPathDots() { | ||
| return maxParentPathDots; | ||
| } | ||
|
|
||
| // Check if the given field corresponds to a dynamic key mapper of the | ||
| // form 'path_to_field.path_to_key'. If so, returns a field type that | ||
| // can be used to perform searches on this field. Otherwise returns null. | ||
| private MappedFieldType getDynamicField(String field) { | ||
| if (dynamicFieldTypes.isEmpty()) { | ||
| // no parent fields defined | ||
| return null; | ||
| } | ||
| int dotIndex = -1; | ||
| int fieldDepth = -1; | ||
|
|
||
| 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. |
||
| if (++fieldDepth > maxParentPathDots) { | ||
| return null; | ||
| } | ||
|
|
||
| dotIndex = field.indexOf('.', dotIndex + 1); | ||
| if (dotIndex < 0) { | ||
| return null; | ||
| } | ||
|
|
||
| String parentField = field.substring(0, dotIndex); | ||
| DynamicFieldType dft = dynamicFieldTypes.get(parentField); | ||
| if (dft != null && Objects.equals(field, parentField) == false) { | ||
| String key = field.substring(dotIndex + 1); | ||
| return dft.getChildFieldType(key); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /** | ||
|
|
@@ -130,7 +179,10 @@ Set<String> sourcePaths(String field) { | |
| if (fullNameToFieldType.isEmpty()) { | ||
| return Set.of(); | ||
| } | ||
| if (dynamicKeyLookup.get(field) != null) { | ||
|
|
||
| // If the field is dynamically generated then return its full path | ||
| MappedFieldType fieldType = getDynamicField(field); | ||
| if (fieldType != null) { | ||
| 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.
Was there a motivation for changing the lookup approach? The old approach seemed more streamlined -- this one seems to do several passes through the string (in
longestPossibleParent, then thecontainsandlastIndexOfcalls in a loop below).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'm not sure why I re-implemented this, but the original approach is clearly more efficient. I've updated.