solr index nested document

2019-08-07 03:49发布

问题:

Does solr support nested documents? And is there a better way to achieve this sort of document?

<doc>
    <field name="name">Mr. Test</field>
    <field name="case">
        <field name="link">http://foo.com</field>
        <field name="date">1-2-1234</filed>
        <field name="title">My title</filed>
    </field>
    <field name="case">
        <field name="link">http://foo.com/2/</field>
        <field name="date">1-2-1234</filed>
        <field name="title">My title 2</filed>
    </field>
</doc>

What I have is a person that has been part of multiple cases. Is this form of schema legal with solr? A different person can also be part of the same case. So it does look like a task for a relational database, but I'm using solr for this project.

回答1:

No, Solr doesn't support that nested structure. Have a look at this other question too.



回答2:

Newer versions of Solr provides support for Nested Documents

Index this Json

[
  {
    "id": "1",
    "title": "Solr adds block join support",
    "content_type": "parentDocument",
    "_childDocuments_": [
      {
        "id": "2",
        "comments": "SolrCloud supports it too!"
      }
    ]
  },
  {
    "id": "3",
    "title": "Lucene and Solr 4.5 is out",
    "content_type": "parentDocument",
    "_childDocuments_": [
      {
        "id": "4",
        "comments": "Lots of new features"
      }
    ]
  }
]

Into schema.xml, u have to add all the fields which are getting used here that is "title","content_type","comments". The parameter "childDocuments" is the parameter which solr takes care and with which it understands this is a child document and "content_type": "parentDocument" is the identifier for solr to understand that this is parent document. After indexing this Json if we query

"*":"*"

We should see 4 documents in all. Now we can get parent or child documents with the help of Block and join query parsers. Try this queries

http://localhost:8983/solr/collection_test/select?q={!child%20of=%22content_type:parentDocument%22}title:lucene

and this one

http://localhost:8983/solr/collection_test/select?q={!parent%20which=%22content_type:parentDocument%22}comments:SolrCloud