You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@lucene.apache.org by cp...@apache.org on 2016/02/02 10:53:28 UTC

[16/21] lucene-solr git commit: SOLR-8607: The Schema API refuses to add new fields that match existing dynamic fields

SOLR-8607: The Schema API refuses to add new fields that match existing dynamic fields


Project: http://git-wip-us.apache.org/repos/asf/lucene-solr/repo
Commit: http://git-wip-us.apache.org/repos/asf/lucene-solr/commit/064c0ac0
Tree: http://git-wip-us.apache.org/repos/asf/lucene-solr/tree/064c0ac0
Diff: http://git-wip-us.apache.org/repos/asf/lucene-solr/diff/064c0ac0

Branch: refs/heads/master-solr-8621
Commit: 064c0ac00fed662183c4cb117f6aeb6d0f9fe1a1
Parents: 8e27c14
Author: Steve Rowe <sa...@apache.org>
Authored: Mon Feb 1 09:15:17 2016 -0500
Committer: Steve Rowe <sa...@apache.org>
Committed: Mon Feb 1 09:15:17 2016 -0500

----------------------------------------------------------------------
 solr/CHANGES.txt                                |  2 +
 .../apache/solr/schema/ManagedIndexSchema.java  |  4 +-
 .../solr/rest/schema/TestBulkSchemaAPI.java     | 83 ++++++++++++++++++++
 3 files changed, 87 insertions(+), 2 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/lucene-solr/blob/064c0ac0/solr/CHANGES.txt
----------------------------------------------------------------------
diff --git a/solr/CHANGES.txt b/solr/CHANGES.txt
index 9b08555..447761f 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 caused
   an 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
 ----------------------

http://git-wip-us.apache.org/repos/asf/lucene-solr/blob/064c0ac0/solr/core/src/java/org/apache/solr/schema/ManagedIndexSchema.java
----------------------------------------------------------------------
diff --git a/solr/core/src/java/org/apache/solr/schema/ManagedIndexSchema.java b/solr/core/src/java/org/apache/solr/schema/ManagedIndexSchema.java
index e114031..9db41cd 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);

http://git-wip-us.apache.org/repos/asf/lucene-solr/blob/064c0ac0/solr/core/src/test/org/apache/solr/rest/schema/TestBulkSchemaAPI.java
----------------------------------------------------------------------
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.java
index ca6f1fc..bd05015 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;