Apache Solr - 索引数据

时间:2015-10-02 09:31:41

标签: apache solr

我有两个表:tableA和tableB。这些表之间存在一对多的关系。 tableA中的一行对应于tableB中的多行。我有疑问:

select aa.id, aa.first_name, aa.last_name, bb.address_home, bb.address_coresp from testA aa, testB bb where aa.id = bb.testA_fk;

返回许多行 - 例如3:

1   John    Terry   HOME 1      CORESP_1
1   John    Terry   HOME 11     CORESP_11
1   John    Terry   HOME 111    CORESP_111

当我将此查询插入solr的data-config.xml文件和索引数据时,结果是:

{"address_home": ["HOME 111"],
"address_coresp": ["CORESP_111"],
"id": "1",
"LAST_NAME": "Terry",
"FIRST_NAME": "John",
"_version_": 1513906493806608400
}

只有一个地址结果而不是三个。

我的data-config.xml的片段:

<document name="testDoc">

<entity name="testA" query="select aa.id, aa.first_name, aa.last_name, bb.address_home, bb.address_coresp from testA aa, testB bb where aa.id = bb.testA_fk">
    <field column="id" name="id" />
    <field column="first_name" name="first_name" />
    <field column="last_name" name="last_name" />
    <field column="address_home" name="address_home" />
    <field column="address_coresp" name="address_coresp" />
</entity>
</document>

并且在schema.xml中我将multiValued设置为true:

<field name="address_home" type="text_general" indexed="true" stored="true" multiValued="true" /><field name="address_coresp" type="text_general" indexed="true" stored="true" multiValued="true" />

我知道我的问题的解决方案是嵌套的实体元素:

<entity name="testA" query="select * from testA">
field definitions...
    <entity name="testB" query="select * from testB where testB.a_id = '${testA.id}'">
    field definitions...
</entity>
</entity 

,但有一个选项可以在一个查询中执行此操作。我想实现这个结果:

    {"id": "1",
    "LAST_NAME": "Terry",
    "FIRST_NAME": "John",
    "address_home": ["HOME 1","HOME 11","HOME 111"],
    "address_coresp": ["CORESP_1","CORESP_11","CORESP_111"],
    "_version_": 1513905361988354000
    }

提前致谢

1 个答案:

答案 0 :(得分:2)

检查你的schema.xml中是否有uniqueKey的值,我怀疑它被设置为“id”:

 <uniqueKey>id</uniqueKey>

因此,id为“1”的每个后续记录都会覆盖最后一个,导致只有id为“1”的最后一条记录被保留在索引中。

如果您需要能够在数据库中更改数据时更新Solr中的文档,则可以使用TableB中的id,或TableA和TableB中id的组合。如果您不需要更新,可以将id字段映射到不同的Solr字段,并让Solr自动生成唯一ID。