Postgresql 中带有分数/排名的全文查询

发布于 2024-09-29 07:00:23 字数 248 浏览 1 评论 0原文

我是 Postgres 新手,我不知道如何将此 MySQL 查询转换为 Postgres:

SELECT pictures.id, MATCH (title, cached_tag_list) AGAINST ('phrase') AS score FROM pictures WHERE MATCH (title, cached_tag_list) AGAINST ('phrase') ORDER BY score DESC;

Im new to Postgres and I dont know how to translate this MySQL query to postgres:

SELECT pictures.id, MATCH (title, cached_tag_list) AGAINST ('phrase') AS score FROM pictures WHERE MATCH (title, cached_tag_list) AGAINST ('phrase') ORDER BY score DESC;

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

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

发布评论

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

评论(1

感悟人生的甜 2024-10-06 07:00:23

Postgres 全文搜索与 MySQL 全文搜索略有不同。它有更多的选项,但要按照您喜欢的方式工作可能会有点困难。

本文档告诉您如何对搜索结果进行排名,但我强烈建议您阅读手册中的整个全文部分,以了解可以用它做什么:http://www.postgresql.org/docs/current/interactive/textsearch-controls.html#TEXTSEARCH-RANKING

基本上,您的查询的等价物是这样的:

SELECT pictures.id, ts_rank_cd(textsearch, 'phrase') AS score
FROM pictures
ORDER BY score DESC

如您所见,这使用 textsearch ,这是您必须自己定义的内容。对于简短版本,请阅读:http://www.postgresql.org/ docs/current/interactive/textsearch-tables.html

查询本质上非常简单:

SELECT pictures.id, ts_rank_cd(to_tsvector('english', pictures.title), 'phrase') AS score
FROM pictures
ORDER BY score DESC

但我强烈建议也添加索引:

CREATE INDEX pictures_title ON pictures USING gin(to_tsvector('english', title));

The Postgres fulltext search is a little different from the MySQL fulltext search. It has a lot more options but can be a bit more difficult to get working the way you like it.

This document tells you how to rank your search results, but I strongly recommend you read the entire fulltext section from the manual to get an idea about what you can do with it: http://www.postgresql.org/docs/current/interactive/textsearch-controls.html#TEXTSEARCH-RANKING

Basically, the equivalent of your query would be this:

SELECT pictures.id, ts_rank_cd(textsearch, 'phrase') AS score
FROM pictures
ORDER BY score DESC

As you can see, this uses textsearch which is something you will have to define yourself. For the short version, read: http://www.postgresql.org/docs/current/interactive/textsearch-tables.html

The query is essentially very simple:

SELECT pictures.id, ts_rank_cd(to_tsvector('english', pictures.title), 'phrase') AS score
FROM pictures
ORDER BY score DESC

But I would strongly recommend adding indexes aswell:

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