mirror of https://github.com/apache/lucene.git
SOLR-9558: DIH TemplateTransformerto to support multivalued fields
This commit is contained in:
parent
45628f1a0b
commit
20c3c86475
|
@ -65,6 +65,8 @@ New Features
|
|||
|
||||
* SOLR-9537: Support facet scoring with the scoreNodes expression (Joel Bernstein)
|
||||
|
||||
* SOLR-9558: DIH TemplateTransformerto to support multivalued fields (Ted Sullivan via noble)
|
||||
|
||||
Bug Fixes
|
||||
----------------------
|
||||
|
||||
|
|
|
@ -19,6 +19,7 @@ package org.apache.solr.handler.dataimport;
|
|||
import java.lang.invoke.MethodHandles;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Map;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
@ -54,6 +55,7 @@ public class TemplateTransformer extends Transformer {
|
|||
@SuppressWarnings("unchecked")
|
||||
public Object transformRow(Map<String, Object> row, Context context) {
|
||||
|
||||
|
||||
VariableResolver resolver = (VariableResolver) context
|
||||
.getVariableResolver();
|
||||
// Add current row to the copy of resolver map
|
||||
|
@ -84,15 +86,30 @@ public class TemplateTransformer extends Transformer {
|
|||
if (!resolvable)
|
||||
continue;
|
||||
if(variables.size() == 1 && expr.startsWith("${") && expr.endsWith("}")){
|
||||
row.put(column, resolver.resolve(variables.get(0)));
|
||||
addToRow(column, row, resolver.resolve(variables.get(0)));
|
||||
} else {
|
||||
row.put(column, resolver.replaceTokens(expr));
|
||||
addToRow(column, row, resolver.replaceTokens(expr));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
return row;
|
||||
}
|
||||
|
||||
private void addToRow(String key, Map<String, Object> row, Object value) {
|
||||
Object prevVal = row.get(key);
|
||||
if (prevVal != null) {
|
||||
if (prevVal instanceof List) {
|
||||
((List) prevVal).add(value);
|
||||
} else {
|
||||
ArrayList<Object> valList = new ArrayList<Object>();
|
||||
valList.add(prevVal);
|
||||
valList.add(value);
|
||||
row.put(key, valList);
|
||||
}
|
||||
} else {
|
||||
row.put(key, value);
|
||||
}
|
||||
}
|
||||
|
||||
public static final String TEMPLATE = "template";
|
||||
}
|
||||
|
|
|
@ -69,5 +69,43 @@ public class TestTemplateTransformer extends AbstractDataImportHandlerTestCase {
|
|||
assertEquals("Mr Mangar, Shalin Shekhar", row.get("mrname"));
|
||||
assertEquals(mails,row.get("emails"));
|
||||
}
|
||||
|
||||
@Test
|
||||
@SuppressWarnings("unchecked")
|
||||
public void testTransformRowMultiValue() {
|
||||
List fields = new ArrayList();
|
||||
fields.add(createMap("column", "year"));
|
||||
fields.add(createMap("column", "month"));
|
||||
fields.add(createMap("column", "day"));
|
||||
|
||||
// create three variations of date format
|
||||
fields.add(createMap( "column", "date",
|
||||
TemplateTransformer.TEMPLATE,
|
||||
"${e.day} ${e.month}, ${e.year}" ));
|
||||
fields.add(createMap( "column", "date",
|
||||
TemplateTransformer.TEMPLATE,
|
||||
"${e.month} ${e.day}, ${e.year}" ));
|
||||
fields.add(createMap("column", "date",
|
||||
TemplateTransformer.TEMPLATE,
|
||||
"${e.year}-${e.month}-${e.day}" ));
|
||||
|
||||
Map row = createMap( "year", "2016",
|
||||
"month", "Apr",
|
||||
"day", "30" );
|
||||
VariableResolver resolver = new VariableResolver();
|
||||
resolver.addNamespace("e", row);
|
||||
Map<String, String> entityAttrs = createMap("date", "e");
|
||||
|
||||
Context context = getContext(null, resolver,
|
||||
null, Context.FULL_DUMP, fields, entityAttrs);
|
||||
new TemplateTransformer().transformRow(row, context);
|
||||
assertTrue( row.get( "date" ) instanceof List );
|
||||
|
||||
List<Object> dates = (List<Object>)row.get( "date" );
|
||||
assertEquals( dates.size(), 3 );
|
||||
assertEquals( dates.get(0).toString(), "30 Apr, 2016" );
|
||||
assertEquals( dates.get(1).toString(), "Apr 30, 2016" );
|
||||
assertEquals( dates.get(2).toString(), "2016-Apr-30" );
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue