DynamicField名称来自SQL值

时间:2011-10-13 11:20:45

标签: solr

我在schema.xml中有“catch all”字段:

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

在下面的示例中,假设我有一个包含2个字段的表:“custom_value”和“custom_key”,其中包含以下值:

custom_key:“mykey”

custom_value:“myvalue”

我的目标是索引一个名为“mykey”的字段和值“myvalue”的文档。我怎么能这样做?

<dataConfig>
<dataSource type="JdbcDataSource" 
driver="com.mysql.jdbc.Driver"
url="jdbc:mysql://localhost/MY_DB" 
user="MYUSER" 
password="MYPASS"
batchSize="-1"/>

<document>
    <entity name="article" query="SELECT id, custom_key, custom_value FROM mytable">
        <field column="id" name="id"/>
        <field column="custom_value" name=":::WHAT TO PUT HERE?:::_s"/>
    </entity>
</document>

1 个答案:

答案 0 :(得分:1)

找到了一个(hacky?)解决方案,这个解决方案适用于我的目的,我不会将这个问题标记为几天的答案,因为有人想出一个更清洁/更好的解决方案。

 <dataConfig>
    <script><![CDATA[
            function insertVariants(row)        {
                row.put(row.get('custom_key') + '_custom', row.get('custom_value'));
                return row;
            }
    ]]></script>

    <dataSource type="JdbcDataSource" 
driver="com.mysql.jdbc.Driver"
url="jdbc:mysql://localhost/MY_DB" 
user="MYUSER" 
password="MYPASS"
batchSize="-1"/>

<document>
    <entity name="article" query="SELECT id, custom_key, custom_value FROM mytable" transformer="script:insertVariants">
        <field column="id" name="id"/>
    </entity>
</document>

</dataConfig>