2015-10-02 77 views
0

我有两个表:tableA和tableB。这些表格之间有一对多的关系。 tableA中的一行对应tableB中的多行。我的查询:Apache Solr - 索引数据

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`s数据-config.xml文件和索引数据,那么结果是:

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

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

片段我的数据-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中我已经多值设置为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 
    } 

在此先感谢

回答

2

检查schema.xml中的唯一键的值,我怀疑它被设置为“ID”:

<uniqueKey>id</uniqueKey> 

因此,每个随后的具有“1”的ID的记录将覆盖最后的记录,导致仅索引中保留具有ID为“1”的最后一个记录。

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

+0

感谢您的回复。 Solr不会将我的记录识别为唯一,因为每个记录的ID都是相同的。我已经用表B的ID测试了解决方案,看起来不像我想要的。我将不得不使用嵌套实体。 – problemgenerator