Solr Nested Documents: Can't make non-anon nested documents (_nest_path_) work

Viewed 12

I am trying to create an index with non-anonymous nested classes. My desired output from solr is:

 "responseHeader":{
    "status":0,
    "QTime":8,
    "params":{
      "q":"discriminator:project",
      "indent":"true",
      "fl":"*,[child]",
      "q.op":"OR",
      "_":"1660714908720"}},
  "response":{"numFound":1003,"start":0,"numFoundExact":true,"docs":[
        {"name":"Project 1",
        "id":"315500",
        "discriminator":"project",
        "_version_":1741444763087798272,
        "publicContacts":[
        {
          "name":"Gurney Halleck",
          "id":"315520",
          "discriminator":"publicContact",
          "_version_":1741444763087798272},
        {
          "name":"Thufir Hawat",
          "id":"315530",
          "discriminator":"publicContact",
          "_version_":1741444763087798272}]},

I have read and followed: https://solr.apache.org/guide/8_0/indexing-nested-documents.html and https://solr.apache.org/guide/8_11/indexing-nested-documents.html#indexing-nested-documents

If I add /just/

<field name="_root_" type="string" indexed="true" stored="false" docValues="false" />

to my schema.xml I can perform a query and get a result with anonymous nested documents returned as childDocuments

 "responseHeader":{
    "status":0,
    "QTime":8,
    "params":{
      "q":"discriminator:project",
      "indent":"true",
      "fl":"*,[child]",
      "q.op":"OR",
      "_":"1660714908720"}},
  "response":{"numFound":1003,"start":0,"numFoundExact":true,"docs":[ 
        "name":"Project 1",
        "id":"315500",
        "discriminator":"project",
        "_version_":1741444763087798272,
        "_childDocuments_":[
        {
          "name":"Gurney Halleck",
          "id":"315520",
          "discriminator":"publicContact",
          "_version_":1741444763087798272},
        {
          "name":"Thufir Hawat",
          "id":"315530",
          "discriminator":"publicContact",
          "_version_":1741444763087798272}]
        },

However, if I add

<fieldType name="_nest_path_" class="solr.NestPathField" />
<field name="_nest_path_" type="_nest_path_" stored="true" />

the nesting relationships are not created at all (not even anonymous childDocuments!) but my nexted documents are put in the index.

I am using DIH to index the documents:

        <entity transformer="RegexTransformer" name="project" query="select * from project">
            <!-- universal fields -->
            <field column="discriminator"/>
            <field column="id"/>
            <field column="name"/>

            <entity child="true" name="publicContacts" query="select * from project_public_contacts where project_id='${project.id}'">
                <field column="discriminator"/>
                <field column="id"/>
                <field column="name"/>
            </entity>
        </entity>

What am I doing wrong?

1 Answers

After digging into this, I have found this is a defect in Solr's DIH. As of 8/29/20, Apache has determined that this defect will not be fixed due to the deprecation of DIH.

https://issues.apache.org/jira/browse/SOLR-14490?page=com.atlassian.jira.plugin.system.issuetabpanels%3Aall-tabpanel

I did find a work around, which is to populate the nest_path yourself in db-data-config.xml. For example:

        <entity name="project" query="select * from project">
            <!-- universal fields -->
            <field column="discriminator"/>
            <field column="id"/>
            <field column="name"/>

            <entity child="true" name="publicContacts" query="select * from project_public_contacts where project_id='${project.id}'">
                <field column="discriminator"/>
                <field column="id"/>
                <field column="name"/>
                <field column="nest" name="_nest_path_"/>
            </entity>

            <entity child="true" name="privateContacts" query="select * from project_private_contacts where project_id='${project.id}'">
                <field column="discriminator"/>
                <field column="id"/>
                <field column="name"/>
                <field column="nest" name="_nest_path_"/>
            </entity>
        </entity>

where the value looks like:

/publicContacts

or whatever you want the property to be named. For more details about how/what the _nest_path_ field should be set to, you can set the field to be stored in schema.xml and then populate the data with the SOLR REST endpoints or other means that are not DIH to see how it's populated. This is how I debugged this issue.

<field name="_nest_path_" type="_nest_path_" stored="true"/>

I also noted that the documentation is incorrect in the SOLR links I provided in my original post. You DO need to have fields defined in schema.xml for the named child documents. I received errors when trying to index through REST endpoints without them. My definitions are:

<field name="publicContacts" type="string" indexed="true" stored="true" required="false" multiValued="true"/>
<field name="privateContacts" type="string" indexed="true" stored="true" required="false" multiValued="true"/>
Related