diff --git a/docs/changelog/114624.yaml b/docs/changelog/114624.yaml new file mode 100644 index 0000000000000..8d79023339c65 --- /dev/null +++ b/docs/changelog/114624.yaml @@ -0,0 +1,6 @@ +pr: 114624 +summary: Empty mappings equals empty properties +area: Search +type: enhancement +issues: + - 107031 diff --git a/server/src/internalClusterTest/java/org/elasticsearch/action/admin/indices/create/CreateIndexIT.java b/server/src/internalClusterTest/java/org/elasticsearch/action/admin/indices/create/CreateIndexIT.java index b32c9941d8c4b..d4071f2c25a54 100644 --- a/server/src/internalClusterTest/java/org/elasticsearch/action/admin/indices/create/CreateIndexIT.java +++ b/server/src/internalClusterTest/java/org/elasticsearch/action/admin/indices/create/CreateIndexIT.java @@ -170,6 +170,22 @@ public void testTwoEmptyEqualMappings() throws Exception { assertEquals(fieldCapsResp1.get(), fieldCapsResp2.get()); } + public void testEmptyMappingsEqualsEmptyProperties() throws Exception { + assertAcked( + prepareCreate("test1").setMapping(XContentFactory.jsonBuilder().startObject().startObject("properties").endObject().endObject()) + ); + assertAcked(prepareCreate("test2").setMapping(XContentFactory.jsonBuilder().startObject().endObject())); + FieldCapabilitiesRequest fieldCapsReq1 = new FieldCapabilitiesRequest(); + fieldCapsReq1.indices("test1"); + fieldCapsReq1.fields("*"); + FieldCapabilitiesResponse fieldCapsResp1 = internalCluster().coordOnlyNodeClient().fieldCaps(fieldCapsReq1).actionGet(); + FieldCapabilitiesRequest fieldCapsReq2 = new FieldCapabilitiesRequest(); + fieldCapsReq2.indices("test2"); + fieldCapsReq2.fields("*"); + FieldCapabilitiesResponse fieldCapsResp2 = internalCluster().coordOnlyNodeClient().fieldCaps(fieldCapsReq2).actionGet(); + assertEquals(fieldCapsResp1.get(), fieldCapsResp2.get()); + } + public void testInvalidShardCountSettings() throws Exception { int value = randomIntBetween(-10, 0); try { diff --git a/server/src/main/java/org/elasticsearch/action/admin/indices/create/CreateIndexRequest.java b/server/src/main/java/org/elasticsearch/action/admin/indices/create/CreateIndexRequest.java index f191ab3827347..e406f02018da5 100644 --- a/server/src/main/java/org/elasticsearch/action/admin/indices/create/CreateIndexRequest.java +++ b/server/src/main/java/org/elasticsearch/action/admin/indices/create/CreateIndexRequest.java @@ -284,9 +284,9 @@ private CreateIndexRequest mapping(BytesReference source, XContentType xContentT } private CreateIndexRequest mapping(String type, Map source) { - if (source.isEmpty()) { + if (isSourceEffectivelyEmpty(source)) { // If no source is provided we return empty mappings - return mapping(EMPTY_MAPPINGS); + source = Map.of(); } else if (source.size() != 1 || source.containsKey(type) == false) { // wrap it in a type map if its not source = Map.of(MapperService.SINGLE_MAPPING_NAME, source); @@ -303,6 +303,27 @@ private CreateIndexRequest mapping(String type, Map source) { } } + /** + * Checks if the source map is effectively empty. + * + * @param source the source map to check + * @return true if the source map is empty or contains only an empty "properties" key, false otherwise + */ + private boolean isSourceEffectivelyEmpty(Map source) { + if (source == null) { + return true; + } + if (source.isEmpty()) { + return true; + } + if (source.size() == 1 && source.containsKey("properties")) { + if (source.get("properties") instanceof Map properties) { + return properties.isEmpty(); + } + } + return false; + } + /** * The cause for this index creation. */