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

wor*_*shi 4 mysql solr nosql key-value-store

所以我有一个规范化的表,其中包含一些我希望放入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     |
+----+--------------+--------------+---------+
Run Code Online (Sandbox Code Playgroud)

现在,我希望将它作为每个唯一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>
Run Code Online (Sandbox Code Playgroud)

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

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

wor*_*shi 5

我解决了这个描述的方式在这里.

简而言之,我使用脚本转换将"属性"实体行转换为带有前缀"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>
Run Code Online (Sandbox Code Playgroud)

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

<dynamicField name="p_*" indexed="true" stored="true" type="string"/>
Run Code Online (Sandbox Code Playgroud)