Followed by this question: SOLR: how to copy data to another field with filtered values?
I have these types of values in solr
<
Below is the way you can try and get the things working for you. Implementing a conditional copyField implemenation as below.
package mysolr;
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( "Price" );
if( v == "AUD" ) {
Object priceSaleObject = doc.getFieldValue( "PriceSale" );
float priceSale = Float.parseFloat( priceSaleObject.toString() );
doc.addField("CustomPrice", priceSale);
//addField(String name,Object value)
}
// pass it up the chain
super.processAdd(cmd);
}
}
With this code you need to create a jar named "ConditionalCopyProcessorFactory.jar"
.
In the solrConfig.xml
, please add the following changes.
<lib dir="${solr.install.dir:../../../..}/plugins/" regex="ConditionalCopyProcessorFactory.jar" />
<updateRequestProcessorChain name="add-unknown-fields-to-the-schema" default="${update.autoCreateFields:true}"
processor="uuid,remove-blank,field-name-mutating,parse-boolean,parse-long,parse-double,parse-date,add-schema-fields">
<processor class="mysolr.ConditionalCopyProcessorFactory"/>
<processor class="solr.LogUpdateProcessorFactory"/>
<processor class="solr.DistributedUpdateProcessorFactory"/>
<processor class="solr.RunUpdateProcessorFactory"/>
</updateRequestProcessorChain>