diff --git a/solr/CHANGES.txt b/solr/CHANGES.txt
index 76ae64e7fad..3c4d4d256c1 100644
--- a/solr/CHANGES.txt
+++ b/solr/CHANGES.txt
@@ -161,6 +161,11 @@ New Features
* SOLR-6267: Let user override Interval Faceting key with LocalParams (Tomas Fernandez_Lobbe
via Erick Erickson)
+* SOLR-6020: Auto-generate a unique key in schema-less example if data does not have an id field.
+ The UUIDUpdateProcessor was improved to not require a field name in configuration and generate
+ a UUID into the unique Key field.
+ (Vitaliy Zhovtyuk, hossman, Steve Rowe, Erik Hatcher, shalin)
+
Bug Fixes
----------------------
diff --git a/solr/core/src/java/org/apache/solr/update/processor/AbstractDefaultValueUpdateProcessorFactory.java b/solr/core/src/java/org/apache/solr/update/processor/AbstractDefaultValueUpdateProcessorFactory.java
index 9c87ec978dc..de8491102aa 100644
--- a/solr/core/src/java/org/apache/solr/update/processor/AbstractDefaultValueUpdateProcessorFactory.java
+++ b/solr/core/src/java/org/apache/solr/update/processor/AbstractDefaultValueUpdateProcessorFactory.java
@@ -72,7 +72,7 @@ public abstract class AbstractDefaultValueUpdateProcessorFactory
* to any document which does not already have a value in
* fieldName
*/
- protected static abstract class DefaultValueUpdateProcessor
+ static abstract class DefaultValueUpdateProcessor
extends UpdateRequestProcessor {
final String fieldName;
diff --git a/solr/core/src/java/org/apache/solr/update/processor/UUIDUpdateProcessorFactory.java b/solr/core/src/java/org/apache/solr/update/processor/UUIDUpdateProcessorFactory.java
index 30f84b412d0..3f9ef5aee7d 100644
--- a/solr/core/src/java/org/apache/solr/update/processor/UUIDUpdateProcessorFactory.java
+++ b/solr/core/src/java/org/apache/solr/update/processor/UUIDUpdateProcessorFactory.java
@@ -17,29 +17,28 @@
package org.apache.solr.update.processor;
-import java.io.IOException;
import java.util.UUID;
import java.util.Locale;
+import org.apache.commons.lang.StringUtils;
import org.apache.solr.common.SolrException;
import static org.apache.solr.common.SolrException.ErrorCode.*;
-import org.apache.solr.common.SolrInputDocument;
import org.apache.solr.common.util.NamedList;
import org.apache.solr.request.SolrQueryRequest;
import org.apache.solr.response.SolrQueryResponse;
-import org.apache.solr.update.AddUpdateCommand;
+import org.apache.solr.schema.SchemaField;
/**
*
- * An update processor that adds a newly generated UUID
value
- * to any document being added that does not already have a value in the
+ * An update processor that adds a newly generated UUID
value
+ * to any document being added that does not already have a value in the
* specified field.
*
- * In the example configuration below, if a document does not contain a value
- * in the id
field, a new UUID
will be generated
+ * In the example configuration below, if a document does not contain a value
+ * in the id
field, a new UUID
will be generated
* and added as the value of that field.
*
* @@ -48,19 +47,46 @@ import org.apache.solr.update.AddUpdateCommand; * <str name="fieldName">id</str> * </processor> * - * + * + *
+ * If field name is omitted in processor configuration,
+ * then @{link org.apache.solr.schema.IndexSchema#getUniqueKeyField()}
+ * is used as field and a new UUID
will be generated
+ * and added as the value of that field. The field type of the uniqueKeyField
+ * must be anything which accepts a string or UUID value.
+ *
* @see UUID
*/
-public class UUIDUpdateProcessorFactory
- extends AbstractDefaultValueUpdateProcessorFactory {
+public class UUIDUpdateProcessorFactory extends UpdateRequestProcessorFactory {
- @Override
- public UpdateRequestProcessor getInstance(SolrQueryRequest req,
- SolrQueryResponse rsp,
+ protected String fieldName = null;
+
+ @SuppressWarnings("unchecked")
+ public void init(NamedList args) {
+
+ Object obj = args.remove("fieldName");
+ if (null != obj) {
+ fieldName = obj.toString();
+ }
+
+ if (0 < args.size()) {
+ throw new SolrException(SERVER_ERROR,
+ "Unexpected init param(s): '" +
+ args.getName(0) + "'");
+ }
+ }
+
+ public UpdateRequestProcessor getInstance(SolrQueryRequest req,
+ SolrQueryResponse rsp,
UpdateRequestProcessor next ) {
- return new DefaultValueUpdateProcessor(fieldName, next) {
+ if (StringUtils.isEmpty(fieldName)) {
+ SchemaField schemaField = req.getSchema().getUniqueKeyField();
+ fieldName = schemaField.getName();
+ }
+
+ return new AbstractDefaultValueUpdateProcessorFactory.DefaultValueUpdateProcessor(fieldName, next) {
@Override
- public Object getDefaultValue() {
+ public Object getDefaultValue() {
return UUID.randomUUID().toString().toLowerCase(Locale.ROOT);
}
};
diff --git a/solr/core/src/test-files/solr/collection1/conf/solrconfig-update-processor-chains.xml b/solr/core/src/test-files/solr/collection1/conf/solrconfig-update-processor-chains.xml
index cd80c296877..b7be98e8bea 100644
--- a/solr/core/src/test-files/solr/collection1/conf/solrconfig-update-processor-chains.xml
+++ b/solr/core/src/test-files/solr/collection1/conf/solrconfig-update-processor-chains.xml
@@ -468,6 +468,18 @@
+
+