Ok I wrote a custom Java transformer as below. Can someone confirm if this is
the right way?


import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import org.apache.solr.handler.dataimport.Context;
import org.apache.solr.handler.dataimport.DataImporter;
import org.apache.solr.handler.dataimport.Transformer;

//Custom Concatenation transformer
public class ConcatTransformer extends Transformer {
        @Override
        public Object transformRow(Map<String, Object> row, Context context) {
                List<Map&lt;String, String>> fields = 
context.getAllEntityFields();
                ArrayList<String> arrayValue = new ArrayList<String>();
                String columnName = null;
                for (Map<String, String> field : fields) {
                        // Check if this field has cincatField is specified in 
the
                        // data-config.xml
                        String concat = field.get("concatField");
                        if (concat != null) {
                                String[] fieldNames = concat.split(",");
                                if (fieldNames != null && fieldNames.length > 
0) {
                                        // Get column Name
                                        columnName = 
field.get(DataImporter.COLUMN);
                                        // Get this field's value from the 
current row
                                        ArrayList<?> value1 = (ArrayList<?>) 
row.get(fieldNames[0]);
                                        ArrayList<?> value2 = (ArrayList<?>) 
row.get(fieldNames[1]);
                                        System.out.println("********" + value1 
+ "********"
                                                        + value2);
                                        // Trim and put the updated value back 
in the current row
                                        if (value1 != null && value2 != null && 
!(value1.isEmpty()) &&
!(value2.isEmpty())) {
                                                for (int i = 0; i < 
value1.size(); i++) {
                                                        if (value1.get(i) != 
null && value2.get(i) != null
                                                                        && 
value1.get(i) != ""
                                                                        && 
value2.get(i) != "")
                                                                
arrayValue.add(value1.get(i) + ","
                                                                                
+ value2.get(i));
                                                }
                                        }
                                }
                        }
                }
                row.put(columnName, arrayValue);
                return row;
        }
}




--
View this message in context: 
http://lucene.472066.n3.nabble.com/Best-way-to-concatenate-2-array-pairs-DIH-tp4069784p4069810.html
Sent from the Solr - User mailing list archive at Nabble.com.

Reply via email to