微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

将具有多行的项从mysql映射到solr

所以我有一个规范化的表,其中包含一些我希望放入Solr索引的数据,类似于此

+----+--------------+--------------+---------+
| id |     name     |  attribute   | value   |
+----+--------------+--------------+---------+
|  1 | Apple        | color        | green   |
|  1 | Apple        | shape        | round   |
|  1 | Apple        | origin       | Belgium |
|  2 | Motorbike    | type         | fast    |
|  2 | Motorbike    | nr of wheels | 2       |
|  3 | Office chair | color        | grayish |
|  3 | Office chair | spins        | yes     |
+----+--------------+--------------+---------+

现在,我希望将它作为每个唯一ID(即项目)的一个文档编入索引.但是我必须将n个属性合并到一个文档中.要做到这一点,我需要用dataConfig做一些魔术.但是我如何存储和映射n个字段?这是使用动态字段的正确时间吗?

这是我目前的尝试.我很确定它无效.

<dataConfig>
    <dataSource 
        type="JdbcDataSource" driver="com.MysqL.jdbc.Driver"
        url="jdbc:MysqL://localhost/mystuff"
        user="root" password="*****"/>

    <document name="doc">
        <entity name="t1" query="select * from item_to_id_table">
            <field name="id" column="id"/>
            <field name="name" column="name"/>
            <entity name="t2" query="select * from my_flat_table" 
                    cacheKey="t1.id"
                    cacheLookup="t2.id">

                <!-- alt 1 -->
                <field name="$(t2.attribute)" column="value" />
                <!-- alt 2 -->
                <entity name="properties" query="select property, value from t2" 
                        cacheKey="$(t2.attribute)"
                        cacheLookup="property">
                    <field name="$(properties.property)" column="value" />
                </entity>
            </entity>
        </entity>

    </document>
</dataConfig>

我很确定这两种选择都不合适,我会很快尝试,除非我能找到更好的东西.也许脚本转换为第三种选择.

这个用例是否合理地与Solr一起使用?

解决方法:

我用here描述的方式解决了这个问题.

简而言之,我使用脚本转换将“属性”实体行转换为带有前缀“p_”的字段.有点像这样(示例代码,可能有bug):

<dataConfig>
    <dataSource 
        type="JdbcDataSource" driver="com.MysqL.jdbc.Driver"
        url="jdbc:MysqL://localhost/mystuff"
        user="root" password="*****"/>

    <script>
    <![CDATA[ 
    function formProperty(row) { 
      var propName = row.get("property");
      var propVal = row.get("value");
      var fieldName = "p_" + propName;
      row.put(fieldName,propVal);
      return row; 
    } 
    ]]>
    </script> 

    <document name="doc">
        <entity name="t1" query="select * from item_to_id_table">
            <field name="id" column="id"/>
            <field name="name" column="name"/>
            <entity name="t2" 
                    query="select * from my_flat_table
                    where my_flat_table.id = ${t1.id}"
                    transformer="script:formProperty">
            </entity>
        </entity>
    </document>
</dataConfig>

然后我将它们映射到schema.xml中的solr schemata作为动态字段

<dynamicField name="p_*" indexed="true" stored="true" type="string"/>

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐