use of org.apache.solr.update.processor.FieldMutatingUpdateProcessor.FieldNameSelector in project lucene-solr by apache.
the class CloneFieldUpdateProcessorFactory method getInstance.
@Override
public final UpdateRequestProcessor getInstance(SolrQueryRequest req, SolrQueryResponse rsp, UpdateRequestProcessor next) {
final FieldNameSelector srcSelector = getSourceSelector();
return new UpdateRequestProcessor(next) {
@Override
public void processAdd(AddUpdateCommand cmd) throws IOException {
final SolrInputDocument doc = cmd.getSolrInputDocument();
// destination may be regex replace string, which can cause multiple output fields.
Map<String, SolrInputField> destMap = new HashMap<>();
// preserve initial values and boost (if any)
for (final String fname : doc.getFieldNames()) {
if (!srcSelector.shouldMutate(fname))
continue;
Collection<Object> srcFieldValues = doc.getFieldValues(fname);
if (srcFieldValues == null || srcFieldValues.isEmpty())
continue;
String resolvedDest = dest;
if (pattern != null) {
Matcher matcher = pattern.matcher(fname);
if (matcher.find()) {
resolvedDest = matcher.replaceAll(dest);
} else {
log.debug("CloneFieldUpdateProcessor.srcSelector.shouldMutate(\"{}\") returned true, " + "but replacement pattern did not match, field skipped.", fname);
continue;
}
}
SolrInputField destField;
if (doc.containsKey(resolvedDest)) {
destField = doc.getField(resolvedDest);
} else {
SolrInputField targetField = destMap.get(resolvedDest);
if (targetField == null) {
destField = new SolrInputField(resolvedDest);
} else {
destField = targetField;
}
}
for (Object val : srcFieldValues) {
destField.addValue(val);
}
// put it in map to avoid concurrent modification...
destMap.put(resolvedDest, destField);
}
for (String dest : destMap.keySet()) {
doc.put(dest, destMap.get(dest));
}
super.processAdd(cmd);
}
};
}
Aggregations