The UpdateRequestProcessor defines how an update request is processed before it is indexed by the update handler.
Configuring UpdateRequestProcessors
solrconfig.xml will load a UpdateRequestProcessorChain
<updateRequestProcessorChain name="mychain" >
<processor class="solr.CustomUpdateRequestProcessorFactory" >
<lst name="name">
<str name="n1">x1</str>
<str name="n2">x2</str>
</lst>
</processor>
<processor class="solr.RunUpdateProcessorFactory" />
<processor class="solr.LogUpdateProcessorFactory" />
</updateRequestProcessorChain>
Selecting the UpdateChain for your request
Once one or more update chains are defined, you may select one on the update request through the parameter update.chain (
Note that for pre-Solr3.2 you need to use update.processor instead). Example: http://localhost:8983/solr/update/xml?update.chain=mychain. You may also choose to set a default UpdateChain for a certain UpdateRequestHandler:
<!-- referencing it in an update handler -->
<requestHandler name="/update/processortest" class="solr.JsonUpdateRequestHandler" >
<lst name="defaults">
<str name="update.chain">mychain</str>
</lst>
</requestHandler>
Implementing a conditional copyField
Here is a quick example that adds the 'cat' 'popular' if the value of 'popularity' is > 5
package my.solr;
import java.io.IOException;
import org.apache.solr.common.SolrInputDocument;
import org.apache.solr.request.SolrQueryRequest;
import org.apache.solr.response.SolrQueryResponse;
import org.apache.solr.update.AddUpdateCommand;
import org.apache.solr.update.processor.UpdateRequestProcessor;
import org.apache.solr.update.processor.UpdateRequestProcessorFactory;
public class ConditionalCopyProcessorFactory extends UpdateRequestProcessorFactory
{
@Override
public UpdateRequestProcessor getInstance(SolrQueryRequest req, SolrQueryResponse rsp, UpdateRequestProcessor next)
{
return new ConditionalCopyProcessor(next);
}
}
class ConditionalCopyProcessor extends UpdateRequestProcessor
{
public ConditionalCopyProcessor( UpdateRequestProcessor next) {
super( next );
}
@Override
public void processAdd(AddUpdateCommand cmd) throws IOException {
SolrInputDocument doc = cmd.getSolrInputDocument();
Object v = doc.getFieldValue( "popularity" );
if( v != null ) {
int pop = Integer.parseInt( v.toString() );
if( pop > 5 ) {
doc.addField( "cat", "popular" );
}
}
// pass it up the chain
super.processAdd(cmd);
}
}