How can you retrieve a full nested document in Solr?

蓝咒 提交于 2019-12-23 21:13:23

问题


In my instance of Solr 4.10.3 I would like to index JSONs with a nested structure.

Example:

{
  "id": "myDoc",
  "title": "myTitle"
  "nestedDoc": {
    "name": "test name"
    "nestedAttribute": {
      "attr1": "attr1Val"
    }
  }
}

I am able to store it correctly through the admin interface:

/solr/#/mySchema/documents

and I'm also able to search and retrieve the document.

The problem I'm facing is that when I get the response document from my Solr search, I cannot see the nested attributes. I only see:

{
  "id": "myDoc",
  "title": "myTitle"
}
Is there a way to include ALL the nested fields in the returned documents?

I tried with : "fl=[child parentFilter=title:myTitle]" but it's not working (ChildDocTransformerFactory from:https://cwiki.apache.org/confluence/display/solr/Transforming+Result+Documents). Is that the right way to do it or is there any other way?

I'm using: Solr 4.10.3!!!!!!


回答1:


To get returned all the nested structure, you indeed need to use ChildDocTransformerFactor. However, you first need to properly index your documents.

If you just passed your structure as it is, Solr will index them as separate documents and won't know that they're actually connected. If you want to be able to correctly query nested documents, you'll have to pre-process your data structure as described in this post or try using (modifying as needed) a pre-processing script. Unfortunately, including the latest Solr 6.0, there's no nice and smooth solution on indexing and returning nested document structures, so everything is done through "workarounds".

Particularly in your case, you'll need to transform your document structure into this:

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

Then, the following ChildDocTransformerFactor query will return you all subdocuments (btw, although it says it's available since Solr 4.9, I've actually only seen it in Solr 5.3... so you need to test):

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

Note, although it returns all nested documents, the returned document structure will be flattend (alas!), i.e., you'll get:

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

Probably, not really what you've expected but... this is the unfortunate Solr's behavior that will be fixed in a nearest future release.




回答2:


You can put q={!parent which=} and in fl field :"fl=*,[child parentFilter=title:myTitle].

It will give you all parent field and children field of title:mytitle



来源:https://stackoverflow.com/questions/36587990/how-can-you-retrieve-a-full-nested-document-in-solr

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!