我需要配置通过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" />
// ...此解决方案导致不导入address对象。谢谢你的建议。
编辑:我还发现了很多警告日志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中,我将这个字段设置为用户的标识符,但是孩子没有这个字段。然后我尝试添加code字段作为ID的别名,我发现了另一个错误,它表明父对象(user)中使用的必填字段中缺少值。此条件也适用于嵌套对象。因此,我还尝试从这些字段中删除这些条件。在此导入运行后,但当我执行select时,所有对象都在同一级别上。Solr将其作为平面导入。
这是预期的select结果:
{
"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"
}
]
}
]
}但它在一个lovel中提供了混合用户和地址:
{
"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"
}
]
}发布于 2020-07-10 20:35:00
试着把它重写成像这样的东西
<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>https://stackoverflow.com/questions/62833874
复制相似问题