Solr:DIH的MySQL查询具有multiValued字段吗?

时间:2018-08-06 16:26:55

标签: solr dih

我正在尝试在Solr中设置多值字段,但在我的情况下却失败了!

数据库查询结果(示例)

|id  | another_id    | name          | phone       | type        |
|----------------------------------------------------------------|
|'1' | '11'          | 'F. Brown'    | '112233440' | 'employee'  |
|'2' | '22'          | 'Jhon Smith'  | '123123123' | 'guest'     |
|'2' | '22'          | 'Jhon Smith'  | '321321321' | 'guest'     |

Solr-data-config.xml

<?xml version="1.0" encoding="UTF-8"?>
<dataConfig>
  <dataSource   type="JdbcDataSource"
                driver="com.mysql.jdbc.Driver"
                url="jdbc:mysql://localhost:3306/servme_prd"
                user="root"
                password="root" />
  <document>
    <entity name="person_cards" query="SELECT table1.id, table2.id AS another_id, table1.name, table2.phone, table1.type 
        FROM table1
        INNER JOIN table2 ON table1.id = table2.fk_id">
        <field column="id" name="uid" />
        <field column="another_id" name="pid" />
        <field column="name" name="name" />
        <field column="phone" name="phone" />
        <field column="type" name="type"/>
    </entity>
</document>
</dataConfig>

managed-schema.xml

<uniqueKey>uid</uniqueKey>
<field name="_version_" type="plong" indexed="false" stored="false"/>
<field name="uid" type="string" docValues="false" multiValued="false" indexed="true" required="true" stored="true"/>
<field name="pid" type="string" docValues="false" multiValued="false" indexed="true" required="true" stored="true"/>
<field name="name" type="string" indexed="true" stored="true"/>
<field name="phone" type="string" docValues="false" multiValued="true" indexed="true" stored="true"/>
<field name="type" type="string" indexed="true" stored="true"/>

每当我进行一次完全导入时,我都不会把手机当作多值字段使用;样本solr查询响应:

{
    "name":"F. Brown",
    "uid":"1",
    "pid":"11",
    "phone":["112233440"],
    "type":"employee" 
    "_version_":1608065390436417536
},
{
    "name":"Jhon Smith",
    "uid":"2",
    "pid":"22",
    "phone":["123123123"],
    "type":"guest" 
    "_version_":1608065390436417536
},
{
    "name":"Jhon Smith",
    "uid":"2",
    "pid":"22",
    "phone":["321321321"],
    "type":"guest" 
    "_version_":1608065390436417536
}

我想从solr查询搜索中获得以下响应:

{
    "name":"F. Brown",
    "uid":"1",
    "pid":"11",
    "phone":["112233440"],
    "type":"employee" 
    "_version_":1608065390436417536
},
{
    "name":"Jhon Smith",
    "uid":"2",
    "pid":"22",
    "phone":["123123123", "321321321"],
    "type":"guest" 
    "_version_":1608065390436417536
}

solr配置部分缺少任何内容,因此我无法使多值字段正常工作吗?

顺便说一句,我正在使用安装在ubuntu 14服务器上的Solr 7.4。 谢谢

1 个答案:

答案 0 :(得分:1)

由于您使用的是MySQL,因此快速解决方案是使用GROUP_CONCAT,然后拆分列with DIH's RegexTransformer

<entity transformer="RegexTransformer" name="person_cards" query="SELECT 
        table1.id, 
        table2.id AS another_id, 
        table1.name, 
        GROUP_CONCAT(table2.phone) AS phone, table1.type 
    FROM table1
    INNER JOIN table2 ON table1.id = table2.fk_id
    GROUP BY uid
    ">
    ...
    <field column="phone" name="phone" splitBy="," />
    ...
</entity>