Apache Lucene open-source search software
SOLR-10858: Make UUIDUpdateProcessorFactory as Runtime URP
Noble Paul741b49e83984
diff --git a/solr/CHANGES.txt b/solr/CHANGES.txtindex 8446b46d23c..b493a1c9699 100644--- a/solr/CHANGES.txt+++ b/solr/CHANGES.txt@@ -61,6 +61,8 @@ New Features* SOLR-11046: Add residuals Stream Evaluator (Joel Bernstein)+* SOLR-10858: Make UUIDUpdateProcessorFactory as Runtime URP (Amit Sarkar, noble)+Bug Fixes----------------------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.javaindex ff8ff838239..12b96948f38 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@@ -47,36 +47,55 @@ import org.apache.solr.schema.SchemaField;* </processor>* </pre>*+ ** <p>- * If field name is omitted in processor configuration,+ * You can also incoke the processor with request handler param(s)+ * as <code>uuid.fieldname</code> with <code>processor=uuid</code>+ *+ * curl -X POST -H Content-Type: application/json+ * http://localhost:8983/solr/test/update/json/docs?processor=uuid;ampersand;uuid.fieldName=id;ampersand;commit=true+ * --data-binary {"id":"1","title": "titleA"}+ *+ * NOTE: The param(s) provided in request handler will override / supersede processor's config.+ *+ * If field name is omitted in processor configuration and not provided in request handler param(s),* then @{link org.apache.solr.schema.IndexSchema#getUniqueKeyField()}* is used as field and a new <code>UUID</code> 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 UpdateRequestProcessorFactory {+ private static final String PREFIX_PARAM = "uuid.";+ public static final String NAME = "uuid";+ private static final String FIELD_PARAM = "fieldName";++protected String fieldName = null;@SuppressWarnings("unchecked")public void init(NamedList args) {- Object obj = args.remove("fieldName");+ Object obj = args.remove(FIELD_PARAM);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 ) {+ String fieldName = this.fieldName;++ String fname = req.getParams().get(PREFIX_PARAM+FIELD_PARAM);+ if (!StringUtils.isEmpty(fname)) {+ fieldName = fname;+ }+if (StringUtils.isEmpty(fieldName)) {SchemaField schemaField = req.getSchema().getUniqueKeyField();fieldName = schemaField.getName();diff --git a/solr/core/src/java/org/apache/solr/update/processor/UpdateRequestProcessorChain.java b/solr/core/src/java/org/apache/solr/update/processor/UpdateRequestProcessorChain.javaindex 6bb212cbc46..bb0c129e5b5 100644--- a/solr/core/src/java/org/apache/solr/update/processor/UpdateRequestProcessorChain.java+++ b/solr/core/src/java/org/apache/solr/update/processor/UpdateRequestProcessorChain.java@@ -321,6 +321,7 @@ public final class UpdateRequestProcessorChain implements PluginInfoInitializedpublic static final Map<String, Class> implicits = new ImmutableMap.Builder().put(TemplateUpdateProcessorFactory.NAME, TemplateUpdateProcessorFactory.class).put(AtomicUpdateProcessorFactory.NAME, AtomicUpdateProcessorFactory.class)+ .put(UUIDUpdateProcessorFactory.NAME, UUIDUpdateProcessorFactory.class).build();}diff --git a/solr/core/src/test/org/apache/solr/update/processor/UUIDUpdateProcessorFallbackTest.java b/solr/core/src/test/org/apache/solr/update/processor/UUIDUpdateProcessorFallbackTest.javaindex 2b10953fc05..9561d5ae245 100644--- a/solr/core/src/test/org/apache/solr/update/processor/UUIDUpdateProcessorFallbackTest.java+++ b/solr/core/src/test/org/apache/solr/update/processor/UUIDUpdateProcessorFallbackTest.java@@ -20,6 +20,7 @@ import org.apache.solr.SolrTestCaseJ4;import org.apache.solr.common.SolrInputDocument;import org.apache.solr.common.SolrInputField;import org.apache.solr.common.params.ModifiableSolrParams;+import org.apache.solr.common.params.SolrParams;import org.apache.solr.core.SolrCore;import org.apache.solr.request.LocalSolrQueryRequest;import org.apache.solr.request.SolrQueryRequest;@@ -27,24 +28,29 @@ import org.apache.solr.request.SolrRequestInfo;import org.apache.solr.response.SolrQueryResponse;import org.apache.solr.update.AddUpdateCommand;import org.junit.BeforeClass;+import org.slf4j.Logger;+import org.slf4j.LoggerFactory;import java.io.IOException;+import java.lang.invoke.MethodHandles;import java.util.Date;+import java.util.List;import java.util.UUID;public class UUIDUpdateProcessorFallbackTest extends SolrTestCaseJ4 {+ Date now = new Date();+@BeforeClasspublic static void beforeClass() throws Exception {initCore("solrconfig-update-processor-chains.xml", "schema.xml");}public void testFallbackToUnique() throws Exception {- Date now = new Date();// get all defaultsSolrInputDocument d = processAdd("default-values-fallback-to-unique",- doc(f("name", "Existing", "Values")));+ doc(f("name", "Existing", "Values")));assertNotNull(d);@@ -62,18 +68,18 @@ public class UUIDUpdateProcessorFallbackTest extends SolrTestCaseJ4 {// defaults already specifiedd = processAdd("default-values-fallback-to-unique",- doc(f("timestamp", now),- f("id", "550e8400-e29b-41d4-a716-446655440000"),- f("processor_default_s", "I HAVE A VALUE"),- f("processor_default_i", 12345),- f("name", "Existing", "Values")));+ doc(f("timestamp", now),+ f("id", "550e8400-e29b-41d4-a716-446655440000"),+ f("processor_default_s", "I HAVE A VALUE"),+ f("processor_default_i", 12345),+ f("name", "Existing", "Values")));assertNotNull(d);assertEquals("550e8400-e29b-41d4-a716-446655440000",- d.getFieldValue("id"));+ d.getFieldValue("id"));- // defaults already specified+ // defaults already specified //both config and request param not passed.d = processAdd("default-values-fallback-to-unique-automatically",doc(f("timestamp", now),f("id", "550e8400-e29b-41d4-a716-446655440000"),@@ -88,8 +94,52 @@ public class UUIDUpdateProcessorFallbackTest extends SolrTestCaseJ4 {assertEquals(121, d.getFieldValue("processor_default_i"));}+ public void testRequesTParams() throws Exception {+ SolrInputDocument d = processAdd(null,+ doc(f("name", "Existing", "Values"), f( "id","75765")), params("processor", "uuid", "uuid.fieldName", "id_s"));++ assertNotNull(d);++ assertNotNull(d.getFieldValue("id_s"));+ assertNotNull(UUID.fromString(d.getFieldValue("id_s").toString()));++++ // defaults already specified+ d = processAdd(null,+ doc(f("timestamp", now),+ f("id", "454435"),+ f("id_s", "550e8400-e29b-41d4-a716-446655440000"),+ f("processor_default_s", "I HAVE A VALUE"),+ f("processor_default_i", 121),+ f("name", "Existing", "Values"))+ , params("processor", "uuid", "uuid.fieldName", "id_s"));++ assertNotNull(d);++ assertEquals("550e8400-e29b-41d4-a716-446655440000",+ d.getFieldValue("id_s"));+ assertEquals(121, d.getFieldValue("processor_default_i"));+ }++ public void testProcessorPrefixReqParam() throws Exception {+ List<UpdateRequestProcessorFactory> processors = UpdateRequestProcessorChain.getReqProcessors("uuid", h.getCore());+ UpdateRequestProcessorFactory processorFactory = processors.get(0);+ assertTrue(processorFactory instanceof UUIDUpdateProcessorFactory);- /**+ SolrQueryResponse rsp = new SolrQueryResponse();+ SolrQueryRequest req = new LocalSolrQueryRequest(h.getCore(), new ModifiableSolrParams());+ AddUpdateCommand cmd = new AddUpdateCommand(req);+ cmd.solrDoc = new SolrInputDocument();+ cmd.solrDoc.addField("random_s", "random_val");++ processorFactory.getInstance(req, rsp, null).processAdd(cmd);+ assertNotNull(cmd.solrDoc);+ assertNotNull(cmd.solrDoc.get("id"));+ assertNotNull(cmd.solrDoc.get("id").getValue());+ }++ /*** Convenience method for building up SolrInputDocuments*/SolrInputDocument doc(SolrInputField... fields) {@@ -100,7 +150,7 @@ public class UUIDUpdateProcessorFallbackTest extends SolrTestCaseJ4 {return d;}- /**+ /*** Convenience method for building up SolrInputFields*/SolrInputField field(String name, float boost, Object... values) {@@ -111,7 +161,7 @@ public class UUIDUpdateProcessorFallbackTest extends SolrTestCaseJ4 {return f;}- /**+ /*** Convenience method for building up SolrInputFields with default boost*/SolrInputField f(String name, Object... values) {@@ -120,22 +170,30 @@ public class UUIDUpdateProcessorFallbackTest extends SolrTestCaseJ4 {/**- * Runs a document through the specified chain, and returns the final- * document used when the chain is completed (NOTE: some chains may+ * Runs a document through the specified chain, and returns the final+ * document used when the chain is completed (NOTE: some chains may* modify the document in place*/- SolrInputDocument processAdd(final String chain,- final SolrInputDocument docIn)- throws IOException {++ SolrInputDocument processAdd(final String chain,+ final SolrInputDocument docIn) throws IOException {+ return processAdd(chain, docIn, params());+ }++ SolrInputDocument processAdd(final String chain,+ final SolrInputDocument docIn, SolrParams params)+ throws IOException {SolrCore core = h.getCore();- UpdateRequestProcessorChain pc = core.getUpdateProcessingChain(chain);+ UpdateRequestProcessorChain pc = chain == null ?+ core.getUpdateProcessorChain(params) :+ core.getUpdateProcessingChain(chain);assertNotNull("No Chain named: " + chain, pc);SolrQueryResponse rsp = new SolrQueryResponse();SolrQueryRequest req = new LocalSolrQueryRequest- (core, new ModifiableSolrParams());+ (core, params);try {SolrRequestInfo.setRequestInfo(new SolrRequestInfo(req,rsp));AddUpdateCommand cmd = new AddUpdateCommand(req);diff --git a/solr/solr-ref-guide/src/update-request-processors.adoc b/solr/solr-ref-guide/src/update-request-processors.adocindex a11d74ad34a..981c2e1dedf 100644--- a/solr/solr-ref-guide/src/update-request-processors.adoc+++ b/solr/solr-ref-guide/src/update-request-processors.adoc@@ -398,3 +398,13 @@ The above parameters convert a normal `update` operation on* `field2` to an atomic `set` operation* `field3` to an atomic `inc` operation* `field4` to an atomic `remove` operation++==== UUIDUpdateProcessorFactory++Name of the processor is `uuid` . Use it to add a UUID to a field+example:++[source,bash]+----+processor=uuid&uuid.fieldName=somefield_name+----