如何按点击频率对关键词搜索结果进行排序?

发布于 2024-11-17 16:06:39 字数 646 浏览 2 评论 0原文

我构建了一个搜索索引(两列表),它将单个关键字分配给其内容 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 like
SELECT 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 技术交流群。

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

发布评论

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

评论(3

油焖大侠 2024-11-24 16:06:39

像这样的事情怎么样?

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

姜生凉生 2024-11-24 16:06:39

MySQL 有全文搜索,它会返回相关性分数。

MySQL has full text search which returns a relevancy score.

梦毁影碎の 2024-11-24 16:06:39

好吧,我自己想出来了。

最快的似乎是执行 SELECT media_id, keywords, COUNT(media_id) AS num WHERE ... GROUP BY media_id ,然后使用简单的 php 遍历 num 字段-usort函数。

function SortFrequency($a, $b)
{
     if ($a['num'] == $b['num']) return 0;
     return ($a['num'] > $b['num']) ? -1 : 1;
}

usort($results, 'SortFrequency');

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 the num field with a simple php-usort function.

function SortFrequency($a, $b)
{
     if ($a['num'] == $b['num']) return 0;
     return ($a['num'] > $b['num']) ? -1 : 1;
}

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