SOLR:如何将数据复制到具有过滤值的另一个字段?

SOLR: how to copy data to another field with filtered values?

我在 solr 中有 Price 字段,具有以下类型的值。

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

所以,我需要另一个自定义字段 CustomPrice

为了创建这个,我使用复制字段将数据从 Price 复制到 CustomPrice

但是,我只需要 CustomPrice 中的数字值,如下所示

"CustomPrice":"0.07"
"CustomPrice":"10.00"
"CustomPrice":"0"

还需要 CustomPrice 字段类型为 pfloat 以便我们可以按数字对字段进行排序。

我试过 CopyFieldPatternTokenizerFactoryPatternReplaceFilterFactory 来做到这一点。

我的老问题参考:SOLR: How to sort by price when price not added properly?

那么,如何创建一个新的浮点字段,在其中我只能复制价格字段中的数值?

Below is the error when I set new field type as float

"error":{
    "metadata":[
      "error-class","org.apache.solr.common.SolrException",
      "root-error-class","java.lang.NumberFormatException"],
    "msg":"ERROR: [doc=12958142955618] Error adding field 'Price'='129.95 AUD' msg=For input string: \"129.95 AUD\"",
    "code":400}}

这可以在 Update Request Processors 的帮助下实现。 Solr 收到的每个更新请求都是 运行 通过称为 Update Request Processors.

的插件链

这可能很有用,例如,向被索引的文档添加一个字段;更改特定字段的值;或者如果传入文档不符合特定条件则删除更新。

您可以添加一个处理器以达到同样的效果。

处理器是:

<processor class="solr.RegexReplaceProcessorFactory">
            <str name="fieldName">price</str>
            <str name="pattern">[^0-9.]+</str>
            <str name="replacement"></str>
            <bool name="literalReplacement">true</bool>
       </processor>

处理器可以添加到updateRequestProcessorChain如下

<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="solr.RegexReplaceProcessorFactory">
        <str name="fieldName">price</str>
        <str name="pattern">[^0-9.]+</str>
        <str name="replacement"></str>
        <bool name="literalReplacement">true</bool>
   </processor>
    <processor class="solr.LogUpdateProcessorFactory"/>
    <processor class="solr.DistributedUpdateProcessorFactory"/>
    <processor class="solr.RunUpdateProcessorFactory"/>

  </updateRequestProcessorChain>

将此条目添加到您管理的架构文件中。

<field name="copyFloatPrice" type="float" indexed="true" stored="true" multiValued="false" docValues="true"/>
<copyField source="price" dest="copyFloatPrice"/>

当我查询 solr 时,我得到以下数据。我可以在 copyFloatPrice.

上实现排序


这是完成的更改。

solrConfig.xml中的变化。

<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="solr.CloneFieldUpdateProcessorFactory"> 
        <str name="source">price</str>
        <str name="dest">copyFloatPrice</str> 
   </processor>
   <processor class="solr.RegexReplaceProcessorFactory">
        <str name="fieldName">copyFloatPrice</str>
        <str name="pattern">[^0-9.]+</str>
        <str name="replacement"></str>
        <bool name="literalReplacement">true</bool>
   </processor>
   <processor class="solr.RegexReplaceProcessorFactory">
        <str name="fieldName">copyFloatPrice</str>
        <str name="pattern">^$</str>
        <str name="replacement">0</str>
        <bool name="literalReplacement">true</bool>
   </processor>
    <processor class="solr.LogUpdateProcessorFactory"/>
    <processor class="solr.DistributedUpdateProcessorFactory"/>
    <processor class="solr.RunUpdateProcessorFactory"/>
</updateRequestProcessorChain>

managed-schema 文件中所做的更改是:

<field name="price" type="string" indexed="true" stored="true" multiValued="false"/>
    <field name="copyFloatPrice" type="float" indexed="true" stored="true" multiValued="false" docValues="true"/>

solr 查询响应显示为 pricecopyFloatPrice 建立索引的数据和实现的排序。