SOLR: Copy record from second field if 1st field is null or not available

前端 未结 1 1331
感动是毒
感动是毒 2021-01-23 11:31

Followed by this question: SOLR: how to copy data to another field with filtered values?

I have these types of values in solr

<         


        
1条回答
  •  刺人心
    刺人心 (楼主)
    2021-01-23 11:56

    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.

    
    
    
        
        
        
        
    
    

    0 讨论(0)
提交回复
热议问题