【发布时间】:2020-07-12 06:06:02
【问题描述】:
我正在努力实现 Sphinx 作为我网站的搜索服务器。该网站允许教师构建评估项目——我希望将其编入索引——可以包括:
- 一个上下文(每个项目一个)
- 一个或多个问题(每个项目很多)
- 每个问题的答案(每个问题多个)
- 每个问题的类别(多对多)
我对 Sphinx 和直接使用 MySQL 都是新手,因为我的其他搜索是通过 CakePHP 的函数进行的。
在构建可以索引数据库中的问题的视图时,我提出了以下 SQL 查询(它很大,但我不会要求您调试它):
SELECT
questions.id,
CONCAT(
IFNULL(questions.context_template, ''),
IFNULL(context_answers_concat.context_answer_templates, ''),
IFNULL(parts_concat.full_parts, '')
) as full_question,
parts_concat.all_categories
FROM
questions
LEFT JOIN(
SELECT
question_answers.question_id,
GROUP_CONCAT(
question_answers.answer_template
) AS context_answer_templates
FROM
question_answers
GROUP BY
question_answers.question_id
) AS context_answers_concat
ON
context_answers_concat.question_id = questions.id
LEFT JOIN(
SELECT
question_parts.question_id,
GROUP_CONCAT(
CONCAT(
question_parts.question_template,
IFNULL(answers_concat.answer_templates, '')
)
) AS full_parts,
GROUP_CONCAT(
categories_concat.part_categories
) AS all_categories
FROM
question_parts
LEFT JOIN(
SELECT
question_answers.question_part_id,
GROUP_CONCAT(
question_answers.answer_template
) AS answer_templates
FROM
question_answers
GROUP BY
question_answers.question_part_id
) AS answers_concat
ON
answers_concat.question_part_id = question_parts.id
LEFT JOIN(
SELECT
question_categories.question_part_id,
GROUP_CONCAT(DISTINCT categories.type) AS part_categories
FROM
question_categories,
categories
WHERE
question_categories.category_id = categories.id
GROUP BY
question_categories.question_part_id
) AS categories_concat
ON
categories_concat.question_part_id = question_parts.id
GROUP BY
question_parts.question_id
) AS parts_concat
ON
parts_concat.question_id = questions.id
我在 phpMyAdmin 中运行了查询,它生成了我期望的视图——一个 id 列,一个包含问题和答案文本的列,以及一个类别列。
我的问题是,因为它太大了,这是一个合理的查询,用于在 Sphinx 中索引类似的东西吗?如果没有,我是否应该使用 Sphinx 的其他部分来完成其中的一些工作?还是不同的 SQL 函数?
【问题讨论】: