Search code examples
searchsolrnested-documentssolr-search

How do I represent a Child Document in my Solr schema.xml?


I'm trying to achieve a document hierarchy like so, where the parent is the 'Bundle' and the children are 'Products':

Bundle:
   id
   imageUrl
   Products:
         [
          id:2
          type:"t-shirt"
          sizes:[S,M,L]
          colors:[blue],

          id:3
          type:"hoodie"
          sizes:[M]
          colors:[red]
         ]

So that I can support queries like "M blue products where imageUrl=xyz".

I've configured my managed-schema.xml like so:

<field name="_root_" type="string" docValues="false" indexed="true" stored="false"/>
<field name="image_url" type="text_en" uninvertible="false" indexed="false" stored="true"/>
<field name="id" type="string" multiValued="false" required="true" stored="true"/>

<fieldType name="_nest_path_" class="solr.NestPathField"/>
<field name="_product_" type="_nest_path_">   
    <field name="id" type="string" multiValued="false" required="true" stored="true"/>
    <field name="type" type="string" indexed="true" stored="true"/>
    <field name="colors" type="strings" multiValued="true" indexed="true" stored="true"/>
    <field name="sizes" type="strings" multiValued="true" ndexed="true" stored="true"/>
</field>    

And I'm indexing the document in Java like so:

SolrInputDocument parent = new SolrInputDocument();
parent.addField("id", bundle.id);
parent.addField("imageUrl", bundle.imageUrl);
for (Product product : bundle.products) {
   SolrInputDocument child = new SolrInputDocument();
   child.addField("type", product.type);
   child.addField("colors", product.colors);
   parent.addChildDocument(child);
}

But when I try to index, I'm receiving "org.apache.solr.common.SolrException: ERROR: [doc=347] multiple values encountered for non multiValued field colors: [Black,​ Deep Royal,​ Navy]".

Did I structure my children documents correctly?


Solution

  • First of all you have an error at the sizes field. There you wrote ndexed instead of indexed. Could it be, that the fieldType "strings" your using to store the colors is not declared as multivalue-field in the schema-file?