如何按点击频率对关键词搜索结果进行排序?
我构建了一个搜索索引(两列表),它将单个关键字分配给其内容 ID。
现在搜索单个关键字可以工作并且速度非常快。
但是,搜索多个关键字,例如 SELECT media_id, keywords FROM search_index WHERE keywords = 'b' OR keywords = 'a'
将按关键字字母顺序返回结果(首先是 'a'
上的所有匹配,然后 <代码>'b')。
我尝试在结果页面上使用 PHP 来完成此操作,但这需要我从数据库加载比我想要显示的更多的数据,以获得可接受的排序。使用该技术,脚本甚至可能会在分配更频繁的关键字上超时。
我还尝试了 GROUP BY ,它似乎将按字母顺序排列的结果分组到它们的单个 ID 中。
编辑: 最后,我发现类似 SELECT DISTINCT media_id, keywords, COUNT(media_id) AS num FROM search_index GROUP BY media_id ORDER BY num DESC 的东西,效果不错,但速度非常慢。
因此,如果我搜索“b”和“a”,我希望结果中首先出现带有“a”和“b”的 ID。
那么我怎样才能告诉MySQL按照一个ID的命中频率对输出进行排序,同时仍然非常快呢?
I've built a search index (two column table) which assigns single keywords to their content ID.
Now searching for single keywords works and is damn fast.
However, searching for multiple keywords likeSELECT media_id, keyword FROM search_index WHERE keyword = 'b' OR keyword = 'a'
will return results in alphabetical keyword order (all hits on 'a'
first, then 'b'
).
I tried doing it with PHP on the results page, but that requires me to load a lot more data from the database than I want to display, to get an acceptable sorting. With that technique the script might even time out on a keywords that been assigned more frequently.
I also tried GROUP BY
which seemed to group the result from the alphabetical order together to their single IDs.
Edit:
Lastly I found something like SELECT DISTINCT media_id, keyword, COUNT(media_id) AS num FROM search_index GROUP BY media_id ORDER BY num DESC
, which works kinda well but is really slow.
So if i search for 'b' and 'a', I want the IDs with 'a' and 'b' first in the result.
So how can I tell MySQL to sort the output by the frequency of hits on one ID while still being damn fast?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
像这样的事情怎么样?
SELECT *, CAST(keyword='a' AS SIGNED)+CAST(keyword='b' AS SIGNED) AS 排名 FROM search_index ORDER BY RANK DESC
How about something like this?
SELECT *, CAST(keyword='a' AS SIGNED)+CAST(keyword='b' AS SIGNED) AS rank FROM search_index ORDER BY RANK DESC
MySQL 有全文搜索,它会返回相关性分数。
MySQL has full text search which returns a relevancy score.
好吧,我自己想出来了。
最快的似乎是执行
SELECT media_id, keywords, COUNT(media_id) AS num WHERE ... GROUP BY media_id
,然后使用简单的 php 遍历num
字段-usort函数。Okay I figured it out myself.
The fastest seems to be to do
SELECT media_id, keyword, COUNT(media_id) AS num WHERE ... GROUP BY media_id
and then going over thenum
field with a simple php-usort function.