使用Solr 9.4.1与Solarium 6.3.5创建父子文档失败求助
Solr父子文档关系创建失败排查求助
我正在使用Solr 9.4.1和Solarium 6.3.5,参考文档编写代码后,发现Solr中未正确创建文档的父子关系。目前所有记录均包含root键,父文档的root值匹配自身id,子文档的root值匹配父文档id,但这是唯一能表明关系可能生效的标识。我是否需要将查询逻辑改为适配扁平数组?以下是我的代码,希望熟悉Solr/Solarium的开发者帮忙排查问题:
public function syncForum() { $client = $this->solrclient; $update = $client->createUpdate(); $db = db_connect(); // Create arrays to store topic and comment documents $topicQuery = $db->query("SELECT forum_topics.id AS topic_id, forum_topics.topic_subject AS topic_subject, forum_topics.topic_message AS topic_message, forum_topics.created_at AS created_at, forum_categories.title AS forum_category, users.username AS topic_author FROM forum_topics INNER JOIN forum_posts ON forum_posts.post_topic = forum_topics.id LEFT JOIN users ON users.id = forum_posts.post_by LEFT JOIN forum_categories ON forum_categories.id = forum_topics.topic_cat"); $topicResult = $topicQuery->getResultArray(); $documentArray = []; foreach ($topicResult as &$topic) { //Loop through topics and create a topic document $document = $update->createDocument(); $document->id = $topic['topic_id']; $document->topic_subject = $topic['topic_subject']; $document->topic_message = $topic['topic_message']; $document->category = $topic['forum_category']; $document->author = $topic['topic_author']; // $document->_root_ = 'true'; $topicDate = $topic['created_at']; $dateTime = new \DateTime($topicDate); $document->created_at = $dateTime->format("Y-m-d\TH:i:s\Z"); $documentArray[] = $document; //For each topic, loop through the comments and create a comment document $commentQuery = $db->query("SELECT forum_posts.id, forum_posts.post_content AS comment_message, forum_posts.created_at, users.username AS author FROM forum_posts LEFT JOIN users ON users.id = forum_posts.post_by WHERE post_topic =" . $topic['topic_id']); $comments = $commentQuery->getResultArray(); $commArray = []; foreach ($comments as $i => $comment) { $commArray[$i]['id'] = $comment['id']; $commArray[$i]['comment_message'] = $comment['comment_message']; $commArray[$i]['author'] = $comment['author']; $originalDate = $comment['created_at']; $dateTime = new \DateTime($originalDate); $formattedDate = $dateTime->format("Y-m-d\TH:i:s\Z"); $commArray[$i]['created_at'] = $formattedDate; } $document->childdocs = $commArray; } // add the documents and a commit command to the update query $update->addDocuments($documentArray); $update->addCommit(); // this executes the query and returns the result $result = $client->update($update); // Output status information echo "Status: " . $result->getStatus() . " | Query Time: " . $result->getQueryTime() . PHP_EOL; }
问题排查与修复建议
- 子文档创建方式错误:你现在将子文档作为普通关联数组赋值给
childdocs,但Solarium要求子文档必须是通过$update->createDocument()创建的Solarium文档对象,而非普通数组。修改子文档创建逻辑如下:
// 替换原有子文档循环代码 foreach ($comments as $comment) { $childDoc = $update->createDocument(); $childDoc->id = $comment['id']; $childDoc->comment_message = $comment['comment_message']; $childDoc->author = $comment['author']; $originalDate = $comment['created_at']; $dateTime = new \DateTime($originalDate); $childDoc->created_at = $dateTime->format("Y-m-d\TH:i:s\Z"); $document->childdocs[] = $childDoc; }
检查Solr核心配置:确保你的Solr core已正确配置嵌套文档支持:
- 确认
managed-schema(或schema.xml)中存在_root_字段,类型为string且multiValued="false"(默认已包含,但需确认未被修改) - 确保更新请求处理器包含
NestedUpdateProcessorFactory(默认/update处理器已集成,若自定义处理器需手动添加)
- 确认
无需改为扁平数组:只要修复子文档的创建方式,嵌套结构即可正常工作,无需切换到扁平数组。
_root_字段的自动维护是正常现象,但只有当子文档以正确的Solarium文档对象格式提交时,Solr才会真正建立父子关联,支持后续的嵌套查询(如{!child}或{!parent}语法)。验证关联有效性:提交后可通过Solr Admin查询页验证:
- 执行
q=*:*&fl=id,_root_,[child parentFilter=*:*]查看父子关联数据 - 执行
q={!child of='id:父文档ID'}查询指定父文档的所有子文档
- 执行
内容的提问来源于stack exchange,提问作者James
相关产品推荐
相关产品推荐

