Skip to content

Apache Lucene open-source search software

SOLR-5000: ManagedIndexSchema doesn't persist uniqueKey tag after calling addFields method

Steven Rowee76d6599d96e
diff --git a/solr/CHANGES.txt b/solr/CHANGES.txt
index fa16ed32302..1980ed83e99 100644
--- a/solr/CHANGES.txt
+++ b/solr/CHANGES.txt
@@ -242,6 +242,9 @@ Bug Fixes
* SOLR-4452: Hunspell stemmer should not merge duplicate dictionary entries (janhoy)
+* SOLR-5000: ManagedIndexSchema doesn't persist uniqueKey tag after calling addFields
+ method. (Jun Ohtani, Steve Rowe)
+
Optimizations
----------------------
diff --git a/solr/core/src/java/org/apache/solr/schema/IndexSchema.java b/solr/core/src/java/org/apache/solr/schema/IndexSchema.java
index ad0422d5aac..fbd5c708244 100644
--- a/solr/core/src/java/org/apache/solr/schema/IndexSchema.java
+++ b/solr/core/src/java/org/apache/solr/schema/IndexSchema.java
@@ -311,8 +311,8 @@ public class IndexSchema {
*/
public SchemaField getUniqueKeyField() { return uniqueKeyField; }
- private String uniqueKeyFieldName;
- private FieldType uniqueKeyFieldType;
+ protected String uniqueKeyFieldName;
+ protected FieldType uniqueKeyFieldType;
/**
* The raw (field type encoded) value of the Unique Key field for
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 56b86698633..f3fa92ce171 100644
--- a/solr/core/src/java/org/apache/solr/schema/ManagedIndexSchema.java
+++ b/solr/core/src/java/org/apache/solr/schema/ManagedIndexSchema.java
@@ -334,6 +334,8 @@ public final class ManagedIndexSchema extends IndexSchema {
newSchema.similarityFactory = similarityFactory;
newSchema.isExplicitSimilarity = isExplicitSimilarity;
newSchema.uniqueKeyField = uniqueKeyField;
+ newSchema.uniqueKeyFieldName = uniqueKeyFieldName;
+ newSchema.uniqueKeyFieldType = uniqueKeyFieldType;
if (includeFieldDataStructures) {
// These need new collections, since addFields() can add members to them
diff --git a/solr/core/src/test-files/solr/collection1/conf/schema-one-field-no-dynamic-field-unique-key.xml b/solr/core/src/test-files/solr/collection1/conf/schema-one-field-no-dynamic-field-unique-key.xml
new file mode 100644
index 00000000000..783ae77c958
--- /dev/null
+++ b/solr/core/src/test-files/solr/collection1/conf/schema-one-field-no-dynamic-field-unique-key.xml
@@ -0,0 +1,29 @@
+<?xml version="1.0" encoding="UTF-8" ?>
+<!--
+ Licensed to the Apache Software Foundation (ASF) under one or more
+ contributor license agreements. See the NOTICE file distributed with
+ this work for additional information regarding copyright ownership.
+ The ASF licenses this file to You under the Apache License, Version 2.0
+ (the "License"); you may not use this file except in compliance with
+ the License. You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing, software
+ distributed under the License is distributed on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ See the License for the specific language governing permissions and
+ limitations under the License.
+-->
+<schema name="one_field_no_dynamic_field_unique_key" version="1.1">
+ <types>
+ <fieldType name="string" class="solr.StrField"/>
+ <fieldType name="text" class="solr.TextField">
+ <analyzer class="org.apache.lucene.analysis.standard.StandardAnalyzer"/>
+ </fieldType>
+ </types>
+ <fields>
+ <field name="str" type="string" indexed="true" stored="true"/>
+ </fields>
+ <uniqueKey>str</uniqueKey>
+</schema>
diff --git a/solr/core/src/test/org/apache/solr/schema/TestManagedSchema.java b/solr/core/src/test/org/apache/solr/schema/TestManagedSchema.java
index 16fa650bba4..8bc29c9f8ac 100644
--- a/solr/core/src/test/org/apache/solr/schema/TestManagedSchema.java
+++ b/solr/core/src/test/org/apache/solr/schema/TestManagedSchema.java
@@ -57,6 +57,7 @@ public class TestManagedSchema extends AbstractBadConfigTestBase {
FileUtils.copyFileToDirectory(new File(testHomeConfDir, "solrconfig-basic.xml"), tmpConfDir);
FileUtils.copyFileToDirectory(new File(testHomeConfDir, "solrconfig.snippet.randomindexconfig.xml"), tmpConfDir);
FileUtils.copyFileToDirectory(new File(testHomeConfDir, "schema-one-field-no-dynamic-field.xml"), tmpConfDir);
+ FileUtils.copyFileToDirectory(new File(testHomeConfDir, "schema-one-field-no-dynamic-field-unique-key.xml"), tmpConfDir);
FileUtils.copyFileToDirectory(new File(testHomeConfDir, "schema-minimal.xml"), tmpConfDir);
FileUtils.copyFileToDirectory(new File(testHomeConfDir, "schema_codec.xml"), tmpConfDir);
FileUtils.copyFileToDirectory(new File(testHomeConfDir, "schema-bm25.xml"), tmpConfDir);
@@ -386,4 +387,39 @@ public class TestManagedSchema extends AbstractBadConfigTestBase {
assertQ(req(fieldName + ":thing"), "//*[@numFound='1']");
}
+ public void testPersistUniqueKey() throws Exception {
+ assertSchemaResource(collection, "managed-schema");
+ deleteCore();
+ File managedSchemaFile = new File(tmpConfDir, "managed-schema");
+ assertTrue(managedSchemaFile.delete()); // Delete managed-schema so it won't block parsing a new schema
+ initCore("solrconfig-mutable-managed-schema.xml", "schema-one-field-no-dynamic-field-unique-key.xml", tmpSolrHome.getPath());
+
+ assertTrue(managedSchemaFile.exists());
+ String managedSchemaContents = FileUtils.readFileToString(managedSchemaFile, "UTF-8");
+ assertFalse(managedSchemaContents.contains("\"new_field\""));
+
+ Map<String,Object> options = new HashMap<String,Object>();
+ options.put("stored", "false");
+ IndexSchema oldSchema = h.getCore().getLatestSchema();
+ assertEquals("str", oldSchema.getUniqueKeyField().getName());
+ String fieldName = "new_field";
+ String fieldType = "string";
+ SchemaField newField = oldSchema.newField(fieldName, fieldType, options);
+ IndexSchema newSchema = oldSchema.addField(newField);
+ assertEquals("str", newSchema.getUniqueKeyField().getName());
+ h.getCore().setLatestSchema(newSchema);
+ log.info("####close harness");
+ h.close();
+ log.info("####close harness end");
+ initCore();
+
+ assertTrue(managedSchemaFile.exists());
+ FileInputStream stream = new FileInputStream(managedSchemaFile);
+ managedSchemaContents = IOUtils.toString(stream, "UTF-8");
+ stream.close(); // Explicitly close so that Windows can delete this file
+ assertTrue(managedSchemaContents.contains("<field name=\"new_field\" type=\"string\" stored=\"false\"/>"));
+ IndexSchema newNewSchema = h.getCore().getLatestSchema();
+ assertNotNull(newNewSchema.getUniqueKeyField());
+ assertEquals("str", newNewSchema.getUniqueKeyField().getName());
+ }
}