使用 Sphinx 进行多对多搜索

发布于 2024-09-30 05:12:07 字数 332 浏览 3 评论 0原文

我想使用 Sphinx 进行多对多匹配。我在 StackOverflow 中引用了问题和标签案例来进行说明。

一个问题可以与多个标签相关联,反之亦然。

所以在mysql中,我有3个表:question、tag、question_tag。

我想搜索包含尽可能多的“java”、“sphinx”、“mysql”、“hibernate”集合中的标签的问题。所以结果可能会出现 3 场比赛、2 场比赛或 1 场比赛的问题。

目前,我创建了一个字段,将所有这些标签与空格连接起来,并让 sphinx 搜索该字段。但这听起来很愚蠢,并且在添加和删除标签时会产生大量开销。会有一些更聪明的方法,对吧?

I would like to use Sphinx for many to many matching. I took the questions and tags case here in StackOverflow for illustration.

A question can be associated with many tags and vice versa for a tag.

So in the mysql, I have 3 tables: question, tag, question_tag.

I would like to search for questions which contains as many tags in the set "java", "sphinx", "mysql", "hibernate" as possible. So the result might come up with questions with 3 matches, 2 matches or 1 match.

Currently, I create a field which concat all these tags with space and have sphinx search that field. But it sounds silly and create a lot of overhead when adding and removing tags. There gonna be some smarter way, right?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

一袭白衣梦中忆 2024-10-07 05:12:07

查看文档 MVA 中的 MVA - 多值属性
sql_attr_multi

我过去曾用它来搜索针对“人”的“兴趣”。所以我可能喜欢摇滚乐和看橄榄球。 Sphinx 可以在数组中对其进行索引,您可以使用 OR 或 AND 匹配来搜索它们。

Take a look at MVA - Multi Value Attributes, in the documentation MVA and
sql_attr_multi.

I have used this in the past to search "interests" against a "person". So I may like rock music and watching rugby. Sphinx can index this in an array and you can search them using OR or AND matching.

岁月打碎记忆 2024-10-07 05:12:07

最简单的方法应该是只加入表格并按问题 ID 进行分组。 Sphinx 会为您完成剩下的工作:

source src_questions{
  select question_id, question_subject, question_body, tag_value from question \
  JOIN question_tag on question.question_id = question_tag.question_id \
  JOIN tag on question_tag.tag_id = tag.tag_id
  GROUP BY question_id 
}

假设您的表格中有以下列

问题表:

question_id INT
question_subject VARCHAR or TEXT
question_body VARCHAR or TEXT

标签表:

tag_id INT
tag_value VARCHAR or TEXT

question_tag_table:

question_id INT
tag_id INT

Easiest should be to just join the tables and group by the question ID. Sphinx does the rest for you:

source src_questions{
  select question_id, question_subject, question_body, tag_value from question \
  JOIN question_tag on question.question_id = question_tag.question_id \
  JOIN tag on question_tag.tag_id = tag.tag_id
  GROUP BY question_id 
}

that assumes, that you've got the following columns in your tables

question table:

question_id INT
question_subject VARCHAR or TEXT
question_body VARCHAR or TEXT

tag table:

tag_id INT
tag_value VARCHAR or TEXT

question_tag_table:

question_id INT
tag_id INT
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文