我正在使用 Solr 9.4.1 和 Solarium 6.3.5。 阅读文档后,我认为我正确编码了实现,但我的文档不是在 Solr 中作为父/子关系创建的。它不是这样工作的吗?我需要更改搜索条件才能使用平面数组吗?我确实在 Solr 中看到,所有记录都有一个 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
是“伪字段”(即不是文档中的实际字段),因此,默认情况下,搜索查询的匹配文档不会包含任何嵌套字段孩子们的反应。
要使用文档的后代丰富结果,您需要通过将 [child]
添加到字段列表参数来使用
Child Doc Transformer,例如。
fl=*,[child]
。您可能还需要将 _nest_path_
字段添加到架构(用于转换器)以启用更多功能。