赋予某些字段更多的相关性,并在mysql全文搜索中按相关性排序

发布于 2024-10-13 15:04:50 字数 188 浏览 5 评论 0原文

我的帖子表中有两个字段 - post_titlepost_content。现在,我使用标准全文搜索来匹配两个字段的一些关键字。我需要给标题字段比内容字段更多的相关性,并且比按相关性对结果进行排序...

要实现此目标,mysql 语法会是什么样子?我用的是mysql 5.1

I have two fields in posts table - post_title and post_content. Now I use standard full text search to match some keywords against both fields. I need to give the title field more relevance than the content field and than order the results by relevance...

What would the mysql syntax look like to achieve this goal? I use mysql 5.1

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

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

发布评论

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

评论(1

绮筵 2024-10-20 15:04:50

首先,创建三个 FULLTEXT 索引:

* one on the title column
* one on the body column
* one on both title and body columns

然后,按以下方式构建查询:

SELECT field1, field2, field3, title, body,
MATCH (title) AGAINST ('word_to_search') AS rel_title,
MATCH (body) AGAINST ('word_to_search') AS rel_body
FROM table_to_use
WHERE MATCH (title,body) AGAINST ('word_to_search')
ORDER BY (rel_title*2)+(rel_body)

这将使标题的相关性比正文高 2 倍。

当您需要允许对内容进行排序(例如按标签(用户不会查看))时,这非常方便,因为它允许您在幕后调整结果。

First, create three FULLTEXT indexes:

* one on the title column
* one on the body column
* one on both title and body columns

Then, build your query in the following manner:

SELECT field1, field2, field3, title, body,
MATCH (title) AGAINST ('word_to_search') AS rel_title,
MATCH (body) AGAINST ('word_to_search') AS rel_body
FROM table_to_use
WHERE MATCH (title,body) AGAINST ('word_to_search')
ORDER BY (rel_title*2)+(rel_body)

This will give the title 2 times more relevance than the body.

This is quite handy when you need to allow the content to be sorted, for instance, by tags (which are not viewed by the users) because it allows you to tweak the results from behind the scenes.

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