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

别等时光非礼了梦想. 提交于 2020-03-04 21:33:56

问题


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

I have these types of values in solr

"Price":"0.07 AUD"
"Price":"10.00"
"Price":"AUD"

So, I copied the above records into another field as below

"CustomPrice":0.07
"CustomPrice":10.00
"CustomPrice": 0.0

Now I have another field PriceSale, So If the Price is NULL I want to copy PriceSale into CustomPrice

"PriceSale":"45.43 AUD",
"PriceSale":"5.40 AUD",
"PriceSale":"40.30 AUD",

so if the "CustomPrice": 0.0 then PriceSale price should copy into CustomPrice as below

"CustomPrice":0.07
 "CustomPrice":10.00
 "CustomPrice": 40.30

How can I do this?


回答1:


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>


来源:https://stackoverflow.com/questions/60387808/solr-copy-record-from-second-field-if-1st-field-is-null-or-not-available

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!