SOLR:如何忽略 MinFieldValueUpdateProcessorFactory 处理器中的 0(零)值?

SOLR: how to ignore 0 (zero) value in MinFieldValueUpdateProcessorFactory processor?

我们在另一个字段“all_prices”中添加了自定义价格字段。

<processor class="solr.CloneFieldUpdateProcessorFactory">
  <str name="source">CustomPrice1</str>
  <str name="source">CustomPrice2</str>
  <str name="source">CustomPrice3</str>
  <str name="dest">all_prices</str>
</processor>

所以,在 solr 中,它看起来像

"CustomPrice1":79.95,
"CustomPrice2":69.95,
"CustomPrice3":59.95,
"all_prices":[79.95,69.95,59.95],

所以我们添加了处理器“MinFieldValueUpdateProcessorFactory”,如下所示:

<processor class="solr.MinFieldValueUpdateProcessorFactory">
  <str name="fieldName">all_prices</str>
</processor>

现在,solr 中的数据将如下所示:

"CustomPrice1":79.95,
"CustomPrice2":69.95,
"CustomPrice3":59.95,
"all_prices":[59.95],

现在,在某些情况下,当我们的自定义价格为零时,如下所示:

"CustomPrice1":24.95,
"CustomPrice2":0.0,
"all_prices":[0.0],

所以在那种情况下,我们只想忽略 0(零)值并将第二个最小值取入 all_price。 如何做到这一点?

您可以像下面这样定义 class

package mysolr;

import java.util.Collection;
import java.util.Collections;

import org.apache.solr.common.SolrException;
import org.apache.solr.core.SolrCore;
import org.apache.solr.update.processor.FieldMutatingUpdateProcessor.FieldNameSelector;

import static org.apache.solr.common.SolrException.ErrorCode.BAD_REQUEST;
import static org.apache.solr.update.processor.FieldMutatingUpdateProcessor.SELECT_NO_FIELDS;
    public final class SecondMinFieldValueUpdateProcessorFactory extends FieldValueSubsetUpdateProcessorFactory {
    
      @Override
      @SuppressWarnings({"unchecked"})
      public Collection<Object> pickSubset(@SuppressWarnings({"rawtypes"})Collection values) {
        @SuppressWarnings({"rawtypes"})
        Collection result = values;
        try {
          Object[] array = values.toArray();
          Arrays.sort(array);
          Object res = (int)array[0] == 0 ? array[1] : array[0];
          result.clear();
          result.add(res);
  
         } catch (ClassCastException e) {
          throw new SolrException
            (BAD_REQUEST, 
             "Field values are not mutually comparable: " + e.getMessage(), e);
        }
        return result;
      }
    
      @Override
      public FieldNameSelector getDefaultSelector(SolrCore core) {
        return SELECT_NO_FIELDS;
      }
      
    }

使用此代码,您需要创建一个名为 "SecondMinFieldValueUpdateProcessorFactory .jar".

的 jar

solrConfig.xml 中,请添加以下更改。

<lib dir="${solr.install.dir:../../../..}/plugins/" regex="SecondMinFieldValueUpdateProcessorFactory .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.SecondMinFieldValueUpdateProcessorFactory "/>
    <processor class="solr.LogUpdateProcessorFactory"/>
    <processor class="solr.DistributedUpdateProcessorFactory"/>
    <processor class="solr.RunUpdateProcessorFactory"/>
</updateRequestProcessorChain>