将具有多行的项从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"/>

相关文章

校园网上订餐系统系统主要功能模块包括公告内容(公告栏、轮...
从今天开始,正式进入项目阶段。本次的项目是跟着黑马的瑞吉...
在可预见的将来,IT不会萎缩,只会越发展越庞大,最终会渗透...
vulntarget-b靶场最详细通关记录。
MongoDB是一个开源、高性能、支持海量数据存储的。
【NoSQL数据库技术与应用】课本代码、课后答案(持续更新)