Skip to content

Instantly share code, notes, and snippets.

@lancepantz
Created October 31, 2012 20:32
Show Gist options
  • Save lancepantz/3989651 to your computer and use it in GitHub Desktop.
Save lancepantz/3989651 to your computer and use it in GitHub Desktop.
package com.geni.solr;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;
import java.util.HashMap;
import java.util.Iterator;
import org.apache.solr.handler.dataimport.Context;
import org.apache.solr.handler.dataimport.DataImporter;
import org.apache.solr.handler.dataimport.DataSource;
import com.geni.solr.TemplatedTransformer;
public class CoalesceTransformer extends TemplatedTransformer {
public Map<String,Object> transformRow(Map<String, Object> row, Context context) {
List<Map<String,String>> fields = context.getAllEntityFields();
for (Map<String,String> field : fields) {
String coalesceTo = field.get("coalesce");
if (coalesceTo != null) {
String columnName = field.get(DataImporter.COLUMN);
String value = (String) row.get(columnName);
if (value == null) {
List<String> variables = getVars(coalesceTo);
if (variables.size() != 1)
throw new RuntimeException("coalesce value contains more than one templated variable");
String newValue = (String) context.resolve(variables.get(0));
if (newValue == null)
throw new RuntimeException("coalesce templated value unresolvable");
row.put(columnName, newValue);
}
}
}
return row;
}
}
<field name="parent_id"
coalesce="${document.id}"
column="parent_id"/>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment