2

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.

user9418
  • 395
  • 2
  • 4
  • 13

2 Answers2

5

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
Gunjan
  • 2,775
  • 27
  • 30
  • There is some error in your json format. There should be a "[" after "_childDocuments_": – Lijo Abraham Oct 26 '15 at 12:46
  • @Lijo: thnks..I changed it. – Gunjan Oct 27 '15 at 04:35
  • :- If I want to index like below in solr,How can I do it? [{ "id": "1", "company_id": "1", "company_name" : "company_1", "meta_categories": [ { "cat_id": "1", "cat_name": "fashion" }, { "cat_id": "2", "cat_name": "sports" } ], "main_categories": [ { "cat_name": "1", "cat_name": "fashion" }, { "cat_name": "2", "cat_name": "sports" } ] – Lijo Abraham Oct 28 '15 at 05:05
3

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

Community
  • 1
  • 1
javanna
  • 59,145
  • 14
  • 144
  • 125