From 05aa06f88bc2741ee886a9e67fd744e976170812 Mon Sep 17 00:00:00 2001 From: carlosdelest Date: Thu, 21 Mar 2024 13:54:50 +0100 Subject: [PATCH] Add getMapper(field) to Mapper, so both field with multifields and object mappers can provide underlying field mappers --- .../elasticsearch/index/mapper/FieldAliasMapper.java | 5 +++++ .../org/elasticsearch/index/mapper/FieldMapper.java | 11 +++++++++++ .../java/org/elasticsearch/index/mapper/Mapper.java | 8 ++++++++ 3 files changed, 24 insertions(+) diff --git a/server/src/main/java/org/elasticsearch/index/mapper/FieldAliasMapper.java b/server/src/main/java/org/elasticsearch/index/mapper/FieldAliasMapper.java index 8aa29e6317d51..2cbdb79c4ce45 100644 --- a/server/src/main/java/org/elasticsearch/index/mapper/FieldAliasMapper.java +++ b/server/src/main/java/org/elasticsearch/index/mapper/FieldAliasMapper.java @@ -69,6 +69,11 @@ public Iterator iterator() { return Collections.emptyIterator(); } + @Override + public Mapper getMapper(String field) { + return null; + } + @Override public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { return builder.startObject(simpleName()).field("type", CONTENT_TYPE).field(Names.PATH, path).endObject(); diff --git a/server/src/main/java/org/elasticsearch/index/mapper/FieldMapper.java b/server/src/main/java/org/elasticsearch/index/mapper/FieldMapper.java index 71fd9edd49903..53985393aa42f 100644 --- a/server/src/main/java/org/elasticsearch/index/mapper/FieldMapper.java +++ b/server/src/main/java/org/elasticsearch/index/mapper/FieldMapper.java @@ -306,6 +306,17 @@ public Iterator iterator() { return multiFieldsIterator(); } + @Override + public Mapper getMapper(String field) { + while (iterator().hasNext()) { + Mapper mapper = iterator().next(); + if (mapper.simpleName().equals(field)) { + return mapper; + } + } + return null; + } + protected Iterator multiFieldsIterator() { return Iterators.forArray(multiFields.mappers); } diff --git a/server/src/main/java/org/elasticsearch/index/mapper/Mapper.java b/server/src/main/java/org/elasticsearch/index/mapper/Mapper.java index 7c047125a80d3..64d36c1928499 100644 --- a/server/src/main/java/org/elasticsearch/index/mapper/Mapper.java +++ b/server/src/main/java/org/elasticsearch/index/mapper/Mapper.java @@ -145,4 +145,12 @@ public static FieldType freezeAndDeduplicateFieldType(FieldType fieldType) { * Defines how this mapper counts towards {@link MapperService#INDEX_MAPPING_TOTAL_FIELDS_LIMIT_SETTING}. */ public abstract int getTotalFieldsCount(); + + /** + * Returns a submapper for this mapper, if it exists. + * + * @param field field name from which to obtain the mapper + * @return submapper + */ + public abstract Mapper getMapper(String field); }