Solr嵌套文档创建问题:managed-schema定义及查询失效求助
Let's break down why your nested documents aren't showing up in queries, even though the import doesn't throw errors. Here are the key areas to check:
1. Fix Your managed-schema Configuration
Solr relies heavily on the _root_ field to link parent and child documents in nested structures—this is probably the missing piece here.
- Add the
_root_field to your schema if it's not already present:<field name="_root_" type="string" indexed="true" stored="false"/> - Double-check that all fields you're mapping in your DIH config (like
IdandSampleAtr) have matching definitions inmanaged-schema. For example:
Make sure the<field name="Id" type="string" indexed="true" stored="true" required="true"/> <field name="SampleAtr" type="text_general" indexed="true" stored="true"/>typevalues align with what you've specified in your data import configuration.
2. Validate Your Data Import Handler (DIH) Setup
Your current DIH config defines a single entity, but nested documents require nesting child entities inside the parent entity so Solr can associate them correctly. Here's how to adjust it if you have parent-child relationships in your database:
<document name="sample" stored="true"> <!-- Parent entity: main item records --> <entity name="item" query="select * from item"> <field pk="ID" name="Id" type="string" indexed="true" stored="true" required="true"/> <field column="SAMPLEATR" name="SampleAtr" stored="true" indexed="true"/> <!-- Child entity: linked to parent via parent_id --> <entity name="child_item" query="select * from child_item where parent_id='${item.ID}'"> <field name="Id" type="string" indexed="true" stored="true"/> <field column="CHILDATR" name="ChildAtr" stored="true" indexed="true"/> </entity> </entity> </document>
Also, confirm that solrconfig.xml has the DIH properly configured with the correct path to your data import config file.
3. Verify Documents Are Actually Being Indexed
Just because the import doesn't throw errors doesn't mean documents are making it into the index:
- Use the Solr Admin UI's Query tab and run
*:*to see if any documents are returned. If nothing comes up, check Solr's log files (usuallysolr.log) for hidden warnings or import failures—sometimes silent issues show up here. - Test with the
_root_field: run a query like_root_:"your-parent-item-id"to see if the parent and its children are indexed under the same root value.
4. Use the Correct Query Syntax for Nested Documents
Solr doesn't return nested documents with standard keyword queries by default—you need to use Block Join Query syntax:
- To fetch parent documents that match a child document condition:
{!parent which="Id:[* TO *]"}SampleAtr:your-search-term - To fetch child documents that match a parent document condition:
{!child of="Id:[* TO *]"}ChildAtr:your-search-term
The which/of parameter defines the parent document set (here, all documents with an Id value).
Start with checking the _root_ field in your schema—it's the most common culprit for missing nested document results. Once that's sorted, validate your DIH structure and query syntax, and you should see your documents show up.
内容的提问来源于stack exchange,提问作者Marc Ramiro Alpuente

