【发布时间】:2015-10-02 09:31:40
【问题描述】:
我有两个表: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": "特里", "FIRST_NAME": "约翰", "address_home": ["HOME 1","HOME 11","HOME 111"], "address_coresp": ["CORESP_1","CORESP_11","CORESP_111"], “_版本_”:1513905361988354000 }提前致谢
【问题讨论】: