我需要配置通过DIH导入的嵌套实体。用户和地址之间是一对多基数(用户有许多地址)。
这是我们在data-config.xml中的导入定义
<document>
<entity name="user" query="...">
<field column="id" name="id" />
<field column="code" name="code" />
// next fields of user
<entity name="address" child="true" query="..." where="user_id=user.id">
<field column="id" name="id" />
<field column="city" name="city" />
// ... next fields of address
</entity>
</entity>
</document>
和schema.xml这样的配置:
// user fields
<field name="id" type="long" indexed="true" stored="true" />
<field name="name" type="string" indexed="true" stored="true" />
<field name="code" type="string" indexed="true" stored="true" />
// ...
// address fields
<field name="address" type="string" indexed="true" multiValued="true" stored="true" />
<field name="address.id" type="long" indexed="true" stored="true" />
<field name="address.city" type="string" indexed="false" stored="true" />
// ...
此解决方案导致未导入任何地址对象。谢谢您的任何建议。
编辑:
我还发现了许多警告日志
Error creating document : SolrInputDocument(fields: [user_id=122, ... _version_=1671840418228076544,​ _root_=00924553002],​ children: [SolrInputDocument(fields: [address_id=1,​ _root_=00924553002,​ _version_=1671840418228076544]),​ SolrInputDocument(fields: [address_id=20,​ _root_=00924553002,​ _version_=1671840418228076544])])
编辑2:
默认登录应用程序隐藏错误。我检查了计算机上的服务器日志,发现此错误: org.apache.solr.common.SolrException: [doc=null] missing required field: code at org.apache.solr.update.DocumentBuilder.toDocument(DocumentBuilder.java:245)
在schema.xml中,我将此字段设置为用户标识符,但是子级没有此字段。然后,我尝试添加代码字段作为ID的别名,然后发现另一个错误,该错误表明父对象(用户)中使用的必填字段中缺少值。此条件也适用于嵌套对象。所以我也尝试从这些字段中删除这些条件。运行此导入后,但是当我执行select时,所有对象都处于同一级别。 Solr将其导入为平面。
这是预期的选择结果:
{
"reponse": [
{
"id": 1,
"code": "tsdx242-234",
"first_name": "Michael",
"last_name": "Sprox",
"addresses": [
{
"id": 44,
"city": "Paris",
"street": "Champs-Elysees"
},
{
"id": 24,
"city": "Budapest",
"street": "Akácfa utca"
}
]
},
{
"id": 2,
"code": "xx45982-114",
"first_name": "Petra",
"last_name": "Jurka",
"addresses": [
{
"id": 31,
"city": "Vienna",
"street": "Karlsplatz"
},
{
"id": 44,
"city": "Paris",
"street": "Champs-Elysees"
}
]
}
]
}
但是它可以在一个情人中给这个混合用户和地址:
{
"response": [
{
"id": 44,
"city": "Paris",
"street": "Champs-Elysees"
},
{
"id": 24,
"city": "Budapest",
"street": "Akácfa utca"
},
{
"id": 31,
"city": "Vienna",
"street": "Karlsplatz"
},
{
"id": 1,
"code": "tsdx242-234",
"first_name": "Michael",
"last_name": "Sprox"
},
{
"id": 2,
"code": "xx45982-114",
"first_name": "Petra",
"last_name": "Jurka"
}
]
}
答案 0 :(得分:0)
尝试将其重写为类似的内容
<document>
<entity name="user" query="...">
<field column="id" name="id" />
// next fields of user
<entity name="second_entity_name" pk="user_id" query="SELECT ... FROM where user_id = '${user.id}'" >
<field column="id" name="id" />
<field column="city" name="city" />
// ... next fields of address
</entity>
</entity>
</document>