我们在另一个字段中添加了自定义价格字段"所有价格";。
<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。如何做到这一点?
您可以定义如下所示的类
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>