使用 Sphinx 进行多对多搜索
我想使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
查看文档 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.
最简单的方法应该是只加入表格并按问题 ID 进行分组。 Sphinx 会为您完成剩下的工作:
假设您的表格中有以下列
问题表:
标签表:
question_tag_table:
Easiest should be to just join the tables and group by the question ID. Sphinx does the rest for you:
that assumes, that you've got the following columns in your tables
question table:
tag table:
question_tag_table: