在Postgresql有分数/排名的FULLTEXT查询
技术问答
310 人阅读
|
0 人回复
|
2023-09-14
|
我是Postgres新手,我不知道怎么做MySQL查询转换为Postgres:: @. B6 e T4 ^
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;
! l0 @2 S- h- r9 {; W* f9 ] 解决方案:
8 x$ Q% W, o, h, u Postgres全文搜索与MySQL全文搜索略有不同。它有更多的选择,但以你喜欢的方式工作可能更困难。! M! {& ?& B9 W
文档告诉你如何排名搜索结果,但我强烈建议你阅读手册的全文,以了解如何使用它:http : //www.postgresql.org/docs/current/ Interactive / textsearch-controls.html#TEXTSEARCH-RANKING
% F0 h( D" K! a9 o基本上,你的查询会是这样的:
4 O6 F) I h" m8 u* O4 ? J``: s+ X/ {" y& @$ O
SELECT pictures.id,ts_rank_cd(textsearch,‘phrase’) AS score/ W1 w& ]* ~, B z
FROM pictures
+ m) d8 \ l' sORDER BY score DESC
+ E, H" d( \5 ]1 w4 [如你所见,这将被使用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
* Y" u9 D4 {3 P- fFROM pictures; v" S) m9 [$ _1 o8 H5 ^* w
ORDER BY score DESC
) V2 k2 @! h* c6 o但我强烈建议您添加索引:CREATE INDEX pictures_title ON pictures USING gin(to_tsvector(‘english’,title));2 w" U% c) k. L3 I/ _1 q7 @
``` |
|
|
|
|
|