diff --git a/contrib/dataimporthandler/CHANGES.txt b/contrib/dataimporthandler/CHANGES.txt index f1609c04a2b..7bb2e2987e6 100644 --- a/contrib/dataimporthandler/CHANGES.txt +++ b/contrib/dataimporthandler/CHANGES.txt @@ -22,6 +22,7 @@ New Features ---------------------- * SOLR-1525 allow DIH to refer to core properties (noble) +* SOLR-1547 TemplateTransformer copy objects more intelligently when there when the template is a single variable (noble) Optimizations ---------------------- diff --git a/contrib/dataimporthandler/src/main/java/org/apache/solr/handler/dataimport/TemplateTransformer.java b/contrib/dataimporthandler/src/main/java/org/apache/solr/handler/dataimport/TemplateTransformer.java index cc55c8e2da1..a13721bb48a 100644 --- a/contrib/dataimporthandler/src/main/java/org/apache/solr/handler/dataimport/TemplateTransformer.java +++ b/contrib/dataimporthandler/src/main/java/org/apache/solr/handler/dataimport/TemplateTransformer.java @@ -49,6 +49,7 @@ import org.slf4j.LoggerFactory; public class TemplateTransformer extends Transformer { private static final Logger LOG = LoggerFactory.getLogger(TemplateTransformer.class); + private Map> templateVsVars = new HashMap>(); @SuppressWarnings("unchecked") public Object transformRow(Map row, Context context) { @@ -67,7 +68,7 @@ public class TemplateTransformer extends Transformer { // Verify if all variables can be resolved or not boolean resolvable = true; - List variables = TemplateString.getVariables(expr); + List variables = getVars(expr); for (String v : variables) { if (resolver.resolve(v) == null) { LOG.warn("Unable to resolve variable: " + v @@ -78,13 +79,26 @@ public class TemplateTransformer extends Transformer { if (!resolvable) continue; + if(variables.size() == 1 && expr.startsWith("${") && expr.endsWith("}")){ + row.put(column, resolver.resolve(variables.get(0))); + } else { + row.put(column, resolver.replaceTokens(expr)); + } - row.put(column, resolver.replaceTokens(expr)); } return row; } + private List getVars(String expr) { + List result = this.templateVsVars.get(expr); + if(result == null){ + result = TemplateString.getVariables(expr); + this.templateVsVars.put(expr, result); + } + return result; + } + public static final String TEMPLATE = "template"; } diff --git a/contrib/dataimporthandler/src/test/java/org/apache/solr/handler/dataimport/TestTemplateTransformer.java b/contrib/dataimporthandler/src/test/java/org/apache/solr/handler/dataimport/TestTemplateTransformer.java index 78b7b094b0e..7fbf9ac9e23 100644 --- a/contrib/dataimporthandler/src/test/java/org/apache/solr/handler/dataimport/TestTemplateTransformer.java +++ b/contrib/dataimporthandler/src/test/java/org/apache/solr/handler/dataimport/TestTemplateTransformer.java @@ -22,6 +22,7 @@ import org.junit.Test; import java.util.ArrayList; import java.util.List; import java.util.Map; +import java.util.Arrays; /** *

@@ -43,14 +44,20 @@ public class TestTemplateTransformer { fields.add(AbstractDataImportHandlerTest.createMap("column", "name", TemplateTransformer.TEMPLATE, "${e.lastName}, ${e.firstName} ${e.middleName}")); + fields.add(AbstractDataImportHandlerTest.createMap("column", "emails", + TemplateTransformer.TEMPLATE, + "${e.mail}")); + // test reuse of template output in another template fields.add(AbstractDataImportHandlerTest.createMap("column", "mrname", TemplateTransformer.TEMPLATE,"Mr ${e.name}")); - + + List mails = Arrays.asList(new String[]{"a@b.com", "c@d.com"}); Map row = AbstractDataImportHandlerTest.createMap( "firstName", "Shalin", "middleName", "Shekhar", - "lastName", "Mangar"); + "lastName", "Mangar", + "mail", mails); VariableResolverImpl resolver = new VariableResolverImpl(); resolver.addNamespace("e", row); @@ -62,6 +69,7 @@ public class TestTemplateTransformer { new TemplateTransformer().transformRow(row, context); Assert.assertEquals("Mangar, Shalin Shekhar", row.get("name")); Assert.assertEquals("Mr Mangar, Shalin Shekhar", row.get("mrname")); + Assert.assertEquals(mails,row.get("emails")); } }