从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一起使用?

是否合理

i在此处描述的方式解决了。

简而言之,我使用脚本转换将"属性"实体行转换为具有前缀" p_"的字段。有点像这样(示例代码,可能有错误):

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

i然后将它们映射到schema.xml中的solr架构中,为动态字段

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

最新更新