【问题标题】:SOLR: how to copy data to another field with filtered values?SOLR:如何将数据复制到具有过滤值的另一个字段?
【发布时间】:2020-02-24 11:52:04
【问题描述】:

我在 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?

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

以下是我将新字段类型设置为浮点数时的错误

"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}}

【问题讨论】:

    标签: solr


    【解决方案1】:

    这可以在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 编制索引的数据并实现排序。

    【讨论】:

    • 更新了答案...您是否使用托管架构...?检查价格字段的字段名称
    • 这是否也会从价格字段中删除“澳元”?
    • 我需要“AUD”价格字段中的原始文本
    • 这可以通过早期的解决方案来实现....将原始字段保留为价格...复制名为 copyPrice 的 copyField 的数据....将正则表达式应用于它..名为copyPrice 将有唯一的数字,原始价格将保持原样......
    • 另一种方法可以添加 cloneFieldProcessor,如下所示 pricecopyFloatPrice 处理器>...
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多