5

I am trying to create a solr document with a child document. I am using solr 8.2.0 In order to comply with the instructions in https://lucene.apache.org/solr/guide/8_0/indexing-nested-documents.html#indexing-nested-documents , I added the following to schema.xml

<field name="_root_" type="string" indexed="true" stored="false"/>
<fieldType name="nest_path" class="solr.NestPathField" />
<field name="_nest_path_" type="nest_path" />
<field name="_nest_parent_" type="string" indexed="true" stored="true"/>

To create a test document, I used the following PHP code:

$solrClient = ...
$solrInputDocument = new SolrInputDocument();
$solrInputDocument->addField('id', 'yirmi1', 1);
$solrInputDocument->addField('test_s', 'this is a parent test', 1);
$childDoc = new SolrInputDocument();
$childDoc->addField('id', 'yirmi2', 1);
$childDoc->addField('test_s', 'this is a child test', 1);
$solrInputDocument->addChildDocument($childDoc);
$solrUpdateResponse = $solrClient->addDocument($solrInputDocument);
$solrClient->commit();

When I query for fq=id: "yirmi1" or fq=id: "yirmi2", the records come up, but there is no indication that there are parent or child documents. Also, when querying for the fields _nest_parent_, _nest_path_, and _root_ do not come up, even when I specify them as query fields.

What else do I have to set up to properly create nested documents.

Yirmiyahu Fischer
  • 503
  • 2
  • 9
  • 22

3 Answers3

1

Apparently "anonymous" or "unlabelled" child documents and _nest_path_ do not mix well together.

I think you have 2 options:

A)

If you want to use addChildDocument, you will need to remove _nest_path_ and _nest_parent_ fields from your schema.

B)

Set the parent-child relation like you would set any other field:

$solrInputDocument->addField('child', $childDoc);
Mico
  • 1,978
  • 2
  • 13
  • 17
  • I tried both with _nest_path_ and without, both with `addChildDocument()` and `addField('child')` -- and still no sign of child document when query returns parent document. – Yirmiyahu Fischer Mar 24 '20 at 14:14
  • If you want to include the children in results you need to use ChildDocTransformerFactory – Mico Mar 24 '20 at 14:34
  • $solrInputDocument->addField('child', $childDoc); this does not work.. do we have any solution for this in php ? – Saurav Kumar Singh Jun 28 '21 at 14:49
0

Have you added fl=*,[child] ? That should return childDocuments .

I will admit that I'm new to SOLR and I haven't had 100% success with this. :(

SilicaGel
  • 459
  • 3
  • 11
0

For me (Solr 8.5.1, documents added via Data Import Handler with nested entities from PostgreSQL) the _childDocuments_ are only included in the response if the schema.xml does not contain the _nest_path_ field and field type.

_nest_parent_ can be removed from the schema as well, since it is not populated as far as I observed.

A _root_ field is required in the schema:

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

It worked best by adding some sort of type information to the documents, e.g. entitytype as in the following example:

...
$solrInputDocument = new SolrInputDocument();
$solrInputDocument->addField('id', 'yirmi1', 1);
$solrInputDocument->addField('entitytype', 'parent', 1);
$solrInputDocument->addField('test_s', 'this is a parent test', 1);
$childDoc = new SolrInputDocument();
$childDoc->addField('id', 'yirmi2', 1);
$childDoc->addField('entitytype', 'child', 1);
$childDoc->addField('test_s', 'this is a child test', 1);
$solrInputDocument->addChildDocument($childDoc);
...

and then querying with the following parameters:

q=entitytype:parent
fl=*,[child parentFilter=entitytype:parent]
Lars Gendner
  • 1,816
  • 2
  • 14
  • 24