我有一个类似的问题:Solr-搜索具有多个子文档的父文档
让我们使用相同的例子:
{
"id": "1",
"bookName_s": "Core Concepts",
"bookDescription_s": "This is the description",
"isbn_s": "ABCD:123",
"type_s":"book"
"reviews": [
{
"id": "2",
"Name_s": "review1",
"detail_s": "sample review",
"type":"review"
}
],
"students": [
{
"id": "3",
"Name_s": "student1",
"type":"student",
"student_s": "test student"
}
]
}
考虑到这种结构,问题是如何检索所有评论为"review1"的家长(书籍(和名为"student1"的学生。
给出的解决方案是:
{!parent which="type:book"}(type:review AND Name_s:review1) OR (type:student AND Name_s:student1)
然而,这也会返回其他书评为"review1"但没有"student1"作为学生的书籍(反之亦然(。我想检索所有既有复习又有学生匹配的书。只有在两者都匹配的情况下,我才希望归还这本书。起初,你可能会认为用OR代替AND就可以了:
{!parent which="type:book"}(type:review AND Name_s:review1) AND (type:student AND Name_s:student1)
但这并没有返回任何结果。这仅仅是因为它现在查找类型为"review"和类型为"student"AND Name_s"review1"AND Name_s"student1"的文档。这些是单值字段,永远不会同时包含两个值。我们如何才能实现我想要的目标?
我找到了一个(?(解决方案。您可以使用多个fq(fieldquery(参数或一个q(query(和fq参数来发送以下信息:
q={!parent which="type:book"}(type:review AND Name_s:review1)
fq={!join from=_root_ to=_root_}(type:student AND Name_s:student1)
在本例中,我定义了一个_root_
字段(由SOLR自动填充父(书(ID(。第二条语句在JoinQueryParser中使用了这一原理,它类似于关系数据库中的子查询。它搜索student1
学生,并使用其结果通过在根字段中将它们连接在一起来过滤ID上的主查询结果。