【问题标题】:Solr: Get DataImportHandler to ignore missing elementsSolr:获取 DataImportHandler 以忽略缺失的元素
【发布时间】:2017-11-14 18:00:51
【问题描述】:

我正在尝试使用 DIH 从我不维护的 XML 源导入数据。此 XML 具有分组为属性的可选元素,例如颜色或风味。并非所有实体都具有所有属性,这是非常好的和有效的。可悲的是,当我仍然需要这些实体时,DIH 会跳过它们。这是我的 data-config.xml

<dataConfig>
  <dataSource type="FileDataSource" name="datasource"/>
  <document>
   <entity
     name="files"
     processor="FileListEntityProcessor"
     baseDir="C:\\"
     fileName="recipe_page.*xml"
     recursive="false"
     rootEntity="false"
     dataSource="null">
     <entity
      name="file"
      processor="XPathEntityProcessor"
      url="${files.fileAbsolutePath}"
      forEach="/results|/results/recipe"
      stream="true"
      transformer="TemplateTransformer">
       <field column="recipe_id" xpath="/results/recipe/recipeID" />
       <field column="recipe_title" xpath="/results/recipe/recipeTitle" />     
       <field column="color" xpath="/results/recipe/attributes/Color" default="" />
       <field column="drink_classification" xpath="/results/recipe/attributes/DrinkClassification" default="" />
       <field column="flavor" xpath="/results/recipe/attributes/Flavor" default="" />        
       <field column="uid" template="recipe_${file.recipe_id}" />
       <field column="document_type" template="recipe" />
    </entity>
   </entity>
  </document>
</dataConfig>

如何让 DIH 忽略缺失的元素或至少为这些元素设置默认值?

【问题讨论】:

    标签: xml solr dataimporthandler dih


    【解决方案1】:

    我不确定,我知道如何让XPathEntityProcessor 知道忽略丢失的 xpath 语句,但我有一个想法,您可以使用 Transformer 和特别是 TemplateTransformer 设置字段的默认值

    您可以使用模板转换器来构造或修改字段 值,也许使用其他字段的值。您可以插入额外的 文本到模板中。

    我希望这样的东西对你有用:

    <entity name="en" pk="id" transformer="TemplateTransformer" ...>
    
      <!-- generate a full address from fields containing the component parts -->
      <field column="color" template="default-color" />
    </entity>
    

    我在这里尝试用一个值替换字段值,但是我不确定它是否会有所帮助(我希望它可以替换值,但是如果它存在,则应该仔细检查)

    在这种情况下,可以创建自定义 Transformer:

    public class DefaultValueTransformer {
            public Object transformRow(Map<String, Object> row) {
                    String color= row.get("color");
                    if (artist != null)             
                            row.put("color", "default-color-value");
    
                    return row;
            }
    }
    

    以后可以这样使用:

    <entity name="entity" query="..." transformer="my.package.DefaultValueTransformer">
    

    【讨论】:

    • 我一直在寻找一种全 xml 方法。如果我要使用 Java,我可以绕过整个 DIH 方法。我想这是不可能的:(
    • 尝试一下 TemplateTransformer,尽管我不确定它是否会有所帮助
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-11
    • 2014-02-04
    • 2020-08-11
    • 1970-01-01
    • 1970-01-01
    • 2013-01-27
    相关资源
    最近更新 更多