浏览代码

Don't keep track of field aliases in FieldTypeLookup (#64884)

We currently keep track of field aliases which causes us to do two lookups for every field lookup. Instead, we could keep track of field aliases in the same map where ordinary fields are, by adding an entry that has the name of the field alias, and its corresponding field type is the field that that the alias points to.
Luca Cavanna 5 年之前
父节点
当前提交
3f3c05cbbb
共有 1 个文件被更改,包括 6 次插入11 次删除
  1. 6 11
      server/src/main/java/org/elasticsearch/index/mapper/FieldTypeLookup.java

+ 6 - 11
server/src/main/java/org/elasticsearch/index/mapper/FieldTypeLookup.java

@@ -35,7 +35,6 @@ import java.util.Set;
 final class FieldTypeLookup implements Iterable<MappedFieldType> {
 final class FieldTypeLookup implements Iterable<MappedFieldType> {
 
 
     private final Map<String, MappedFieldType> fullNameToFieldType = new HashMap<>();
     private final Map<String, MappedFieldType> fullNameToFieldType = new HashMap<>();
-    private final Map<String, String> aliasToConcreteName = new HashMap<>();
 
 
     /**
     /**
      * A map from field name to all fields whose content has been copied into it
      * A map from field name to all fields whose content has been copied into it
@@ -70,10 +69,12 @@ final class FieldTypeLookup implements Iterable<MappedFieldType> {
             }
             }
         }
         }
 
 
+        final Map<String, String> aliasToConcreteName = new HashMap<>();
         for (FieldAliasMapper fieldAliasMapper : fieldAliasMappers) {
         for (FieldAliasMapper fieldAliasMapper : fieldAliasMappers) {
             String aliasName = fieldAliasMapper.name();
             String aliasName = fieldAliasMapper.name();
             String path = fieldAliasMapper.path();
             String path = fieldAliasMapper.path();
             aliasToConcreteName.put(aliasName, path);
             aliasToConcreteName.put(aliasName, path);
+            fullNameToFieldType.put(aliasName, fullNameToFieldType.get(path));
         }
         }
 
 
         this.dynamicKeyLookup = new DynamicKeyFieldTypeLookup(dynamicKeyMappers, aliasToConcreteName);
         this.dynamicKeyLookup = new DynamicKeyFieldTypeLookup(dynamicKeyMappers, aliasToConcreteName);
@@ -83,8 +84,7 @@ final class FieldTypeLookup implements Iterable<MappedFieldType> {
      * Returns the mapped field type for the given field name.
      * Returns the mapped field type for the given field name.
      */
      */
     MappedFieldType get(String field) {
     MappedFieldType get(String field) {
-        String concreteField = aliasToConcreteName.getOrDefault(field, field);
-        MappedFieldType fieldType = fullNameToFieldType.get(concreteField);
+        MappedFieldType fieldType = fullNameToFieldType.get(field);
         if (fieldType != null) {
         if (fieldType != null) {
             return fieldType;
             return fieldType;
         }
         }
@@ -99,14 +99,9 @@ final class FieldTypeLookup implements Iterable<MappedFieldType> {
      */
      */
     Set<String> simpleMatchToFullName(String pattern) {
     Set<String> simpleMatchToFullName(String pattern) {
         Set<String> fields = new HashSet<>();
         Set<String> fields = new HashSet<>();
-        for (MappedFieldType fieldType : this) {
-            if (Regex.simpleMatch(pattern, fieldType.name())) {
-                fields.add(fieldType.name());
-            }
-        }
-        for (String aliasName : aliasToConcreteName.keySet()) {
-            if (Regex.simpleMatch(pattern, aliasName)) {
-                fields.add(aliasName);
+        for (String field : fullNameToFieldType.keySet()) {
+            if (Regex.simpleMatch(pattern, field)) {
+                fields.add(field);
             }
             }
         }
         }
         return fields;
         return fields;