sNi*_*CKY 5 mysql postgresql full-text-search
我是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;
Run Code Online (Sandbox Code Playgroud)
Wol*_*lph 11
Postgres全文搜索与MySQL全文搜索略有不同.它有更多的选择,但可能有点难以按照你喜欢的方式工作.
本文档将告诉您如何对搜索结果进行排名,但我强烈建议您阅读手册中的整个全文部分,以了解您可以使用它做什么:http://www.postgresql.org/docs/current/交互式/ TEXTSEARCH-controls.html#TEXTSEARCH排名
基本上,相当于您的查询将是这样的:
SELECT pictures.id, ts_rank_cd(textsearch, 'phrase') AS score
FROM pictures
ORDER BY score DESC
Run Code Online (Sandbox Code Playgroud)
正如您所看到的,这将使用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
Run Code Online (Sandbox Code Playgroud)
但我强烈建议添加索引以及:
CREATE INDEX pictures_title ON pictures USING gin(to_tsvector('english', title));
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4529 次 |
| 最近记录: |