From d3325de0d2c9666f20e5f1e767bf4f6a88d1b648 Mon Sep 17 00:00:00 2001 From: pgomulka Date: Mon, 19 Jul 2021 12:24:40 +0200 Subject: [PATCH] [Rest Api Compatibility] Do not return _doc for empty mappings in template Previously the compatibility layer was alwayre returning an _doc for get template. This commit does not return _doc in mappings when mappings are empty. Returning just {} empty object also moving term lookups tests which are already fixed (relates #74544) relates #70966 relates main meta issue #51816 relates types removal meta #54160 --- rest-api-spec/build.gradle | 8 ++------ .../cluster/metadata/IndexTemplateMetadata.java | 13 +++++++++++++ 2 files changed, 15 insertions(+), 6 deletions(-) diff --git a/rest-api-spec/build.gradle b/rest-api-spec/build.gradle index 56aabfc0092ab..f19ad81839b9e 100644 --- a/rest-api-spec/build.gradle +++ b/rest-api-spec/build.gradle @@ -78,8 +78,9 @@ def v7compatibilityNotSupportedTests = { 'indices.create/10_basic/Create index without soft deletes', //Make soft-deletes mandatory in 8.0 #51122 - settings changes are note supported in Rest Api compatibility - 'field_caps/30_filter/Field caps with index filter', //behaviour change after #63692 4digits dates are parsed as epoch and in quotes as year + + 'indices.forcemerge/10_basic/Check deprecation warning when incompatible only_expunge_deletes and max_num_segments values are both set', //#44761 bug fix ] } tasks.named("yamlRestCompatTest").configure { @@ -91,12 +92,7 @@ tasks.named("yamlRestCompatTest").configure { 'cluster.voting_config_exclusions/10_basic/Throw exception when adding voting config exclusion and specifying both node_ids and node_names', 'cluster.voting_config_exclusions/10_basic/Throw exception when adding voting config exclusion without specifying nodes', 'indices.flush/10_basic/Index synced flush rest test', - 'indices.forcemerge/10_basic/Check deprecation warning when incompatible only_expunge_deletes and max_num_segments values are both set', - // not fixing this in #70966 - 'indices.put_template/11_basic_with_types/Put template with empty mappings', 'search.aggregation/200_top_hits_metric/top_hits aggregation with sequence numbers', - 'search.aggregation/51_filter_with_types/Filter aggs with terms lookup and ensure it\'s cached', - 'search/150_rewrite_on_coordinator/Ensure that we fetch the document only once', //terms_lookup 'search/310_match_bool_prefix/multi_match multiple fields with cutoff_frequency throws exception', //cutoff_frequency 'search/340_type_query/type query', // type_query - probably should behave like match_all ] + v7compatibilityNotSupportedTests()) diff --git a/server/src/main/java/org/elasticsearch/cluster/metadata/IndexTemplateMetadata.java b/server/src/main/java/org/elasticsearch/cluster/metadata/IndexTemplateMetadata.java index 3d92f55e79b21..ba4b26b5d7058 100644 --- a/server/src/main/java/org/elasticsearch/cluster/metadata/IndexTemplateMetadata.java +++ b/server/src/main/java/org/elasticsearch/cluster/metadata/IndexTemplateMetadata.java @@ -27,6 +27,7 @@ import org.elasticsearch.common.xcontent.XContentHelper; import org.elasticsearch.common.xcontent.XContentParser; import org.elasticsearch.common.xcontent.json.JsonXContent; +import org.elasticsearch.index.mapper.MapperService; import java.io.IOException; import java.io.UncheckedIOException; @@ -391,6 +392,8 @@ private static void toInnerXContent(IndexTemplateMetadata indexTemplateMetadata, Map documentMapping = XContentHelper.convertToMap(m.uncompressed(), true).v2(); if (includeTypeName == false) { documentMapping = reduceMapping(documentMapping); + } else { + documentMapping = reduceEmptyMapping(documentMapping); } builder.field("mappings"); builder.map(documentMapping); @@ -405,6 +408,16 @@ private static void toInnerXContent(IndexTemplateMetadata indexTemplateMetadata, builder.endObject(); } + @SuppressWarnings("unchecked") + private static Map reduceEmptyMapping(Map mapping) { + if(mapping.keySet().size() == 1 && mapping.containsKey(MapperService.SINGLE_MAPPING_NAME) && + ((Map)mapping.get(MapperService.SINGLE_MAPPING_NAME)).size() == 0){ + return (Map) mapping.values().iterator().next(); + } else { + return mapping; + } + } + @SuppressWarnings("unchecked") private static Map reduceMapping(Map mapping) { assert mapping.keySet().size() == 1 : mapping.keySet();