如何在Solr中检索完整的嵌套文档?

时间:2016-04-13 03:26:17

标签: solr nested documents

在我的 Solr 4.10.3 的实例中,我想用嵌套结构索引JSON。

示例:

this.state.place >1 ? :.

我可以通过管理界面正确存储它:

/ solr的/#/ MYSCHEMA /文件

我也可以搜索和检索文档。

我面临的问题是,当我从Solr搜索中获取响应文档时,我看不到嵌套属性。我只看到:

{
  "id": "myDoc",
  "title": "myTitle"
  "nestedDoc": {
    "name": "test name"
    "nestedAttribute": {
      "attr1": "attr1Val"
    }
  }
}
有没有办法在返回的文档中包含所有嵌套字段?

我尝试过:“fl = [child parentFilter = title:myTitle]”但它无法正常工作(来自https://cwiki.apache.org/confluence/display/solr/Transforming+Result+Documents的ChildDocTransformerFactory)。这是正确的做法还是有其他办法吗?

我正在使用: Solr 4.10.3 !!!!!!

2 个答案:

答案 0 :(得分:2)

要返回所有嵌套结构,您确实需要使用 ChildDocTransformerFactor 。但是,您首先需要正确索引文档。

如果你刚刚通过你的结构,Solr会将它们作为单独的文件编入索引,并且不知道它们实际连接了。如果您希望能够正确查询嵌套文档,则必须按照this post中的说明预处理数据结构,或尝试使用(根据需要修改)预处理script 。不幸的是,包括最新的Solr 6.0,在索引和返回嵌套文档结构方面没有很好的平滑解决方案,所以一切都通过" workarounds"来完成。

特别是在您的情况下,您需要将文档结构转换为:

{
  "type": "parentDoc", 
  "id": "myDoc",
  "title": "myTitle"
  "_childDocuments_": [ 
  {
     "type": "nestedDoc",
     "name": "test name",
     "_childDocuments_" :[
     { 
      "type": "nestedAttribute"
       "attr1": "attr1Val"
     }]
   }]    
} 

然后,下面的ChildDocTransformerFactor查询将返回所有子文档(顺便说一句,虽然它说自从Solr 4.9以来它可用,我实际上只在Solr 5.3中看到它...所以你需要测试):

q=title:myTitle&fl=*,[child parentFilter=type:parentDoc limit=50]

注意,虽然它返回所有嵌套文档,但返回的文档结构将是 flattend (唉!),即你得到:

{
  "type": "parentDoc", 
  "id": "myDoc",
  "title": "myTitle"
  "_childDocuments_": [ 
  {
     "type": "nestedDoc",
     "name": "test name"
  },   
  { 
      "type": "nestedAttribute"
       "attr1": "attr1Val"
  }]    
} 

可能不是你真正想要的......但这是不幸的Solr的行为,将在最近的版本中修复。

答案 1 :(得分:1)

你可以放 q = {!parent which =}  在fl字段中:“fl = *,[child parentFilter = title:myTitle]。

它将为您提供标题的所有父字段和子字段:mytitle