Apache Solr open-source search software
SOLR-4864: RegexReplaceProcessorFactory should support pattern capture group substitution in replacement string.
Steven Rowef58f711f73d4
diff --git a/solr/CHANGES.txt b/solr/CHANGES.txtindex d23c902e438..f7001bcb34d 100644--- a/solr/CHANGES.txt+++ b/solr/CHANGES.txt@@ -175,6 +175,10 @@ New Featuresjava -Durl="http://username:password@hostname:8983/solr/update" -jar post.jar sample.xml(Sameer Maggon via Uwe Schindler)+* SOLR-4864: RegexReplaceProcessorFactory should support pattern capture group+ substitution in replacement string.+ (Sunil Srinivasan, Jack Krupansky via Steve Rowe)+Bug Fixes----------------------diff --git a/solr/core/src/java/org/apache/solr/update/processor/RegexReplaceProcessorFactory.java b/solr/core/src/java/org/apache/solr/update/processor/RegexReplaceProcessorFactory.javaindex f1561b445ac..129a147621a 100644--- a/solr/core/src/java/org/apache/solr/update/processor/RegexReplaceProcessorFactory.java+++ b/solr/core/src/java/org/apache/solr/update/processor/RegexReplaceProcessorFactory.java@@ -35,12 +35,22 @@ import org.slf4j.LoggerFactory;/*** An updated processor that applies a configured regex to any* CharSequence values found in the selected fields, and replaces- * any matches with the configured replacement string+ * any matches with the configured replacement string.+ ** <p>* By default this processor applies itself to no fields.* </p>** <p>+ * By default, <code>literalReplacement</code> is set to true, in which+ * case, the <code>replacement</code> string will be treated literally by+ * quoting via {@link Matcher#quoteReplacement(String)}. And hence, '\'+ * and '$' signs will not be processed. When <code>literalReplacement</code>+ * is set to false, one can perform backreference operations and capture+ * group substitutions.+ * </p>+ *+ * <p>* For example, with the configuration listed below, any sequence of multiple* whitespace characters found in values for field named <code>title</code>* or <code>content</code> will be replaced by a single space character.@@ -52,8 +62,9 @@ import org.slf4j.LoggerFactory;* <str name="fieldName">title</str>* <str name="pattern">\s+</str>* <str name="replacement"> </str>+ * <bool name="literalReplacement">true</bool>* </processor></pre>- *+ ** @see java.util.regex.Pattern*/public final class RegexReplaceProcessorFactory extends FieldMutatingUpdateProcessorFactory {@@ -62,9 +73,12 @@ public final class RegexReplaceProcessorFactory extends FieldMutatingUpdateProceprivate static final String REPLACEMENT_PARAM = "replacement";private static final String PATTERN_PARAM = "pattern";+ private static final String LITERAL_REPLACEMENT_PARAM = "literalReplacement";private Pattern pattern;private String replacement;+ // by default, literalReplacementEnabled is set to true to allow backward compatibility+ private boolean literalReplacementEnabled = true;@SuppressWarnings("unchecked")@Override@@ -72,7 +86,7 @@ public final class RegexReplaceProcessorFactory extends FieldMutatingUpdateProceObject patternParam = args.remove(PATTERN_PARAM);- if(patternParam == null) {+ if (patternParam == null) {throw new SolrException(ErrorCode.SERVER_ERROR,"Missing required init parameter: " + PATTERN_PARAM);}@@ -85,11 +99,22 @@ public final class RegexReplaceProcessorFactory extends FieldMutatingUpdateProce}Object replacementParam = args.remove(REPLACEMENT_PARAM);- if(replacementParam == null) {+ if (replacementParam == null) {throw new SolrException(ErrorCode.SERVER_ERROR,"Missing required init parameter: " + REPLACEMENT_PARAM);}- replacement = Matcher.quoteReplacement(replacementParam.toString());++ Boolean literalReplacement = args.removeBooleanArg(LITERAL_REPLACEMENT_PARAM);++ if (literalReplacement != null) {+ literalReplacementEnabled = literalReplacement;+ }++ if (literalReplacementEnabled) {+ replacement = Matcher.quoteReplacement(replacementParam.toString());+ } else {+ replacement = replacementParam.toString();+ }super.init(args);}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.xmlindex 1b99f61dc36..a5ffdd4488a 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@@ -412,6 +412,45 @@</processor></updateRequestProcessorChain>+ <updateRequestProcessorChain name="regex-replace-literal-true">+ <processor class="solr.RegexReplaceProcessorFactory">+ <str name="fieldName">content</str>+ <str name="fieldName">title</str>+ <str name="pattern">(try)</str>+ <str name="replacement"><$1></str>+ <bool name="literalReplacement">true</bool>+ </processor>+ </updateRequestProcessorChain>++ <updateRequestProcessorChain name="regex-replace-literal-default-true">+ <processor class="solr.RegexReplaceProcessorFactory">+ <str name="fieldName">content</str>+ <str name="fieldName">title</str>+ <str name="pattern">(try)</str>+ <str name="replacement"><$1></str>+ </processor>+ </updateRequestProcessorChain>++ <updateRequestProcessorChain name="regex-replace-literal-false">+ <processor class="solr.RegexReplaceProcessorFactory">+ <str name="fieldName">content</str>+ <str name="fieldName">title</str>+ <str name="pattern">(try)</str>+ <str name="replacement"><$1></str>+ <bool name="literalReplacement">false</bool>+ </processor>+ </updateRequestProcessorChain>++ <updateRequestProcessorChain name="regex-replace-literal-str-true">+ <processor class="solr.RegexReplaceProcessorFactory">+ <str name="fieldName">content</str>+ <str name="fieldName">title</str>+ <str name="pattern">(try)</str>+ <str name="replacement"><$1></str>+ <str name="literalReplacement">true</str>+ </processor>+ </updateRequestProcessorChain>+<updateRequestProcessorChain name="default-values"><processor class="solr.DefaultValueUpdateProcessorFactory"><str name="fieldName">processor_default_s</str>diff --git a/solr/core/src/test/org/apache/solr/update/processor/FieldMutatingUpdateProcessorTest.java b/solr/core/src/test/org/apache/solr/update/processor/FieldMutatingUpdateProcessorTest.javaindex 48ac66c9f78..d731dc9c643 100644--- a/solr/core/src/test/org/apache/solr/update/processor/FieldMutatingUpdateProcessorTest.java+++ b/solr/core/src/test/org/apache/solr/update/processor/FieldMutatingUpdateProcessorTest.java@@ -367,6 +367,59 @@ public class FieldMutatingUpdateProcessorTest extends UpdateProcessorTestBase {d.getFieldValue("content"));assertEquals("ThisXtitleXhasXaXlotXofXspaces",d.getFieldValue("title"));++ // literalReplacement = true+ d = processAdd("regex-replace-literal-true",+ doc(f("id", "doc2"),+ f("content", "Let's try this one"),+ f("title", "Let's try try this one")));++ assertNotNull(d);++ assertEquals("Let's <$1> this one",+ d.getFieldValue("content"));+ assertEquals("Let's <$1> <$1> this one",+ d.getFieldValue("title"));++ // literalReplacement is not specified, defaults to true+ d = processAdd("regex-replace-literal-default-true",+ doc(f("id", "doc3"),+ f("content", "Let's try this one"),+ f("title", "Let's try try this one")));++ assertNotNull(d);++ assertEquals("Let's <$1> this one",+ d.getFieldValue("content"));+ assertEquals("Let's <$1> <$1> this one",+ d.getFieldValue("title"));++ // if user passes literalReplacement as a string param instead of boolean+ d = processAdd("regex-replace-literal-str-true",+ doc(f("id", "doc4"),+ f("content", "Let's try this one"),+ f("title", "Let's try try this one")));++ assertNotNull(d);++ assertEquals("Let's <$1> this one",+ d.getFieldValue("content"));+ assertEquals("Let's <$1> <$1> this one",+ d.getFieldValue("title"));++ // This is with literalReplacement = false+ d = processAdd("regex-replace-literal-false",+ doc(f("id", "doc5"),+ f("content", "Let's try this one"),+ f("title", "Let's try try this one")));++ assertNotNull(d);++ assertEquals("Let's <try> this one",+ d.getFieldValue("content"));+ assertEquals("Let's <try> <try> this one",+ d.getFieldValue("title"));+}public void testFirstValue() throws Exception {