22

How to write nested schema.xml in solr

The document in schema.xml says

<!-- points to the root document of a block of nested documents. Required for nested
document support, may be removed otherwise
-->
<field name="_root_" type="string" indexed="true" stored="false"/>

http://svn.apache.org/viewvc/lucene/dev/trunk/solr/example/solr/collection1/conf/schema.xml?view=markup

Which can be used in

https://cwiki.apache.org/confluence/display/solr/Other+Parsers#OtherParsers-BlockJoinQueryParsers

What will be schema.xml for nesting the following items:

  • Person string
  • Address
    • city string
    • postcode string
Shishir Kumar
  • 7,981
  • 3
  • 29
  • 45
user2230605
  • 2,390
  • 6
  • 27
  • 45

1 Answers1

17

I know this is an old question, but I ran into a similar issue. Modifying my solution for yours, the fields you need to add to your schema.xml are as follows:

 <field name="person" type="string" indexed="true" stored="true" />
 <field name="address" type="string" indexed="true" stored="true" multiValued="true"/> 
 <field name="address.city" type="string" indexed="true" stored="true" />
 <field name="address.postcode" type="string" indexed="true" stored="true" />

Then when you run it you should be able to add the following JSON to your Solr instance and see the matching output in the query:

{
  "person": "John Smith",
  "address": {
      "city": "San Diego",
      "postcode": 92093
    }
}
jencoston
  • 1,262
  • 7
  • 19
  • 35
  • 9
    if multivalued is true would it not be a list of addresses? – aaa90210 Jun 07 '17 at 04:31
  • 2
    @aaa90210 yes it will be, but thats the whole point right. It should have multiple childs. If there is only one child why to have even the child documents, just flatten child json. Something like: address_city and address_postcode – Bikas Katwal Aug 30 '18 at 11:00
  • it's strange to declare a field as string when that (address) is another document – Pablo Pazos Dec 30 '22 at 16:22