Apache Lucene open-source search software
SOLR-8607: The Schema API refuses to add new fields that match existing dynamic fields
Steve Rowe064c0ac00fed
diff --git a/solr/CHANGES.txt b/solr/CHANGES.txtindex 9b085559990..447761fdd9c 100644--- a/solr/CHANGES.txt+++ b/solr/CHANGES.txt@@ -434,6 +434,8 @@ Bug Fixes* SOLR-8605: Regular expression queries starting with escaped forward slash causedan exception. (Scott Blum, yonik)+* SOLR-8607: The Schema API refuses to add new fields that match existing dynamic fields.+ (Jan Høydahl, Steve Rowe)Optimizations----------------------diff --git a/solr/core/src/java/org/apache/solr/schema/ManagedIndexSchema.java b/solr/core/src/java/org/apache/solr/schema/ManagedIndexSchema.javaindex e1140316e36..9db41cd18ac 100644--- a/solr/core/src/java/org/apache/solr/schema/ManagedIndexSchema.java+++ b/solr/core/src/java/org/apache/solr/schema/ManagedIndexSchema.java@@ -395,7 +395,7 @@ public final class ManagedIndexSchema extends IndexSchema {newSchema = shallowCopy(true);for (SchemaField newField : newFields) {- if (null != newSchema.getFieldOrNull(newField.getName())) {+ if (null != newSchema.fields.get(newField.getName())) {String msg = "Field '" + newField.getName() + "' already exists.";throw new FieldExistsException(ErrorCode.BAD_REQUEST, msg);}@@ -1195,7 +1195,7 @@ public final class ManagedIndexSchema extends IndexSchema {String msg = "Can't add dynamic field '" + fieldName + "'.";throw new SolrException(ErrorCode.BAD_REQUEST, msg);}- SchemaField existingFieldWithTheSameName = getFieldOrNull(fieldName);+ SchemaField existingFieldWithTheSameName = fields.get(fieldName);if (null != existingFieldWithTheSameName) {String msg = "Field '" + fieldName + "' already exists.";throw new SolrException(ErrorCode.BAD_REQUEST, msg);diff --git a/solr/core/src/test/org/apache/solr/rest/schema/TestBulkSchemaAPI.java b/solr/core/src/test/org/apache/solr/rest/schema/TestBulkSchemaAPI.javaindex ca6f1fc9f37..bd050153ded 100644--- a/solr/core/src/test/org/apache/solr/rest/schema/TestBulkSchemaAPI.java+++ b/solr/core/src/test/org/apache/solr/rest/schema/TestBulkSchemaAPI.java@@ -164,6 +164,89 @@ public class TestBulkSchemaAPI extends RestTestBase {assertEquals("5.0.0", String.valueOf(analyzer.get("luceneMatchVersion")));}+ public void testAddFieldMatchingExistingDynamicField() throws Exception {+ RestTestHarness harness = restTestHarness;++ String newFieldName = "attr_non_dynamic";++ Map map = getObj(harness, newFieldName, "fields");+ assertNull("Field '" + newFieldName + "' already exists in the schema", map);++ map = getObj(harness, "attr_*", "dynamicFields");+ assertNotNull("'attr_*' dynamic field does not exist in the schema", map);++ map = getObj(harness, "boolean", "fieldTypes");+ assertNotNull("'boolean' field type does not exist in the schema", map);++ String payload = "{\n" ++ " 'add-field' : {\n" ++ " 'name':'" + newFieldName + "',\n" ++ " 'type':'boolean',\n" ++ " 'stored':true,\n" ++ " 'indexed':true\n" ++ " }\n" ++ " }";++ String response = harness.post("/schema?wt=json", json(payload));++ map = (Map)ObjectBuilder.getVal(new JSONParser(new StringReader(response)));+ assertNull(response, map.get("errors"));++ map = getObj(harness, newFieldName, "fields");+ assertNotNull("Field '" + newFieldName + "' is not in the schema", map);+ }++ public void testAddFieldWithExistingCatchallDynamicField() throws Exception {+ RestTestHarness harness = restTestHarness;++ String newFieldName = "NewField1";++ Map map = getObj(harness, newFieldName, "fields");+ assertNull("Field '" + newFieldName + "' already exists in the schema", map);++ map = getObj(harness, "*", "dynamicFields");+ assertNull("'*' dynamic field already exists in the schema", map);++ map = getObj(harness, "string", "fieldTypes");+ assertNotNull("'boolean' field type does not exist in the schema", map);++ map = getObj(harness, "boolean", "fieldTypes");+ assertNotNull("'boolean' field type does not exist in the schema", map);++ String payload = "{\n" ++ " 'add-dynamic-field' : {\n" ++ " 'name':'*',\n" ++ " 'type':'string',\n" ++ " 'stored':true,\n" ++ " 'indexed':true\n" ++ " }\n" ++ "}";++ String response = harness.post("/schema?wt=json", json(payload));++ map = (Map)ObjectBuilder.getVal(new JSONParser(new StringReader(response)));+ assertNull(response, map.get("errors"));++ map = getObj(harness, "*", "dynamicFields");+ assertNotNull("Dynamic field '*' is not in the schema", map);++ payload = "{\n" ++ " 'add-field' : {\n" ++ " 'name':'" + newFieldName + "',\n" ++ " 'type':'boolean',\n" ++ " 'stored':true,\n" ++ " 'indexed':true\n" ++ " }\n" ++ " }";++ response = harness.post("/schema?wt=json", json(payload));++ map = (Map)ObjectBuilder.getVal(new JSONParser(new StringReader(response)));+ assertNull(response, map.get("errors"));++ map = getObj(harness, newFieldName, "fields");+ assertNotNull("Field '" + newFieldName + "' is not in the schema", map);+ }public void testMultipleCommands() throws Exception{RestTestHarness harness = restTestHarness;