From 4f9bc97179257b71eecee784188befcb347075c1 Mon Sep 17 00:00:00 2001 From: Shalin Shekhar Mangar Date: Sun, 17 Aug 2014 17:51:13 +0000 Subject: [PATCH] SOLR-6383: RegexTransformer returns no results after replaceAll if regex does not match a value git-svn-id: https://svn.apache.org/repos/asf/lucene/dev/trunk@1618488 13f79535-47bb-0310-9956-ffa450edef68 --- solr/CHANGES.txt | 3 +++ .../handler/dataimport/RegexTransformer.java | 2 +- .../dataimport/TestRegexTransformer.java | 19 +++++++++++++++++-- 3 files changed, 21 insertions(+), 3 deletions(-) diff --git a/solr/CHANGES.txt b/solr/CHANGES.txt index dad64acb467..f3b274a8bf2 100644 --- a/solr/CHANGES.txt +++ b/solr/CHANGES.txt @@ -282,6 +282,9 @@ Bug Fixes * SOLR-6380: Added missing context info to log message if IOException occurs in processing tlog (Steven Bower via hossman) +* SOLR-6383: RegexTransformer returns no results after replaceAll if regex does not match a value. + (Alexander Kingson, shalin) + Optimizations --------------------- diff --git a/solr/contrib/dataimporthandler/src/java/org/apache/solr/handler/dataimport/RegexTransformer.java b/solr/contrib/dataimporthandler/src/java/org/apache/solr/handler/dataimport/RegexTransformer.java index 24bf9df4236..c5ccaad7fd8 100644 --- a/solr/contrib/dataimporthandler/src/java/org/apache/solr/handler/dataimport/RegexTransformer.java +++ b/solr/contrib/dataimporthandler/src/java/org/apache/solr/handler/dataimport/RegexTransformer.java @@ -122,7 +122,7 @@ public class RegexTransformer extends Transformer { } else if (replaceWith != null) { Pattern p = getPattern(reStr); Matcher m = p.matcher(value); - return m.find()? m.replaceAll(replaceWith): null; + return m.find() ? m.replaceAll(replaceWith) : value; } else { return readfromRegExp(reStr, value, col, groupNames); } diff --git a/solr/contrib/dataimporthandler/src/test/org/apache/solr/handler/dataimport/TestRegexTransformer.java b/solr/contrib/dataimporthandler/src/test/org/apache/solr/handler/dataimport/TestRegexTransformer.java index 50da4f164ec..e792804634f 100644 --- a/solr/contrib/dataimporthandler/src/test/org/apache/solr/handler/dataimport/TestRegexTransformer.java +++ b/solr/contrib/dataimporthandler/src/test/org/apache/solr/handler/dataimport/TestRegexTransformer.java @@ -98,6 +98,20 @@ public class TestRegexTransformer extends AbstractDataImportHandlerTestCase { Map result = new RegexTransformer().transformRow(src, context); assertEquals("D''souza", result.get("name")); + + fld = getField("title_underscore", "string", "\\s+", "title", null); + fld.put(REPLACE_WITH, "_"); + fields.clear(); + fields.add(fld); + context = getContext(null, null, null, Context.FULL_DUMP, fields, null); + src.clear(); + src.put("title", "value with spaces"); // a value which will match the regex + result = new RegexTransformer().transformRow(src, context); + assertEquals("value_with_spaces", result.get("title_underscore")); + src.clear(); + src.put("title", "valueWithoutSpaces"); // value which will not match regex + result = new RegexTransformer().transformRow(src, context); + assertEquals("valueWithoutSpaces", result.get("title_underscore")); // value should be returned as-is } @Test @@ -117,7 +131,7 @@ public class TestRegexTransformer extends AbstractDataImportHandlerTestCase { fld = getField("t1", "string","duff", "rowdata", null); fields.add(fld); - // **ATTEMPTS** a match WITH a replaceWith + // **ATTEMPTS** a match WITH a replaceWith (should return original data) // fld = getField("t2", "string","duff", "rowdata", null); fld.put(REPLACE_WITH, "60"); @@ -140,7 +154,8 @@ public class TestRegexTransformer extends AbstractDataImportHandlerTestCase { Context context = getContext(null, resolver, null, Context.FULL_DUMP, fields, eAttrs); Map result = new RegexTransformer().transformRow(row, context); - assertEquals(5, result.size()); + assertEquals(6, result.size()); + assertEquals(s, result.get("t2")); assertEquals(s, result.get("rowdata")); assertEquals("26", result.get("highway_mileage")); assertEquals("19", result.get("city_mileage"));