sql - 为什么我的浮点值总是为 postgres 查询中的所有条目返回 0?
问题描述
我一直想在我的网站中加入全文搜索,但从搜索中获得返回结果的排名让我望而却步。文档显示了如何做到这一点,但我一直得到 0 作为我选择的所有帖子条目的结果。
my_app_development=# SELECT content, ts_rank(to_tsvector('microposts.content'), query)
AS rank FROM microposts, to_tsquery('sit') query WHERE microposts.content @@ query
ORDER BY rank DESC LIMIT 10;
content | rank
---------------------------------------------------+------
Dolorem sed omnis iusto sit inventore quia dolor. | 0
Dolorem sed omnis iusto sit inventore quia dolor. | 0
Dolorem sed omnis iusto sit inventore quia dolor. | 0
Dolorem sed omnis iusto sit inventore quia dolor. | 0
Dolorem sed omnis iusto sit inventore quia dolor. | 0
Vel sit ut qui aperiam aut sunt. | 0
Vel sit ut qui aperiam aut sunt. | 0
Vel sit ut qui aperiam aut sunt. | 0
Vel sit ut qui aperiam aut sunt. | 0
Dolorem sed omnis iusto sit inventore quia dolor. | 0
(10 rows)
这是预期的行为吗?如果是,我做错了什么。
干杯。
李。
解决方案
您正在每一行中寻找相同短语的比率to_tsvector('microposts.content')
,而不是您应该使用ts_rank(to_tsvector(content),query)
另外,我假设您应该定义语言以获得准确的结果:
SELECT content, ts_rank(to_tsvector('latin',content), query)
AS rank FROM microposts, to_tsquery('latin','sit') query WHERE microposts.content @@ query
ORDER BY rank DESC LIMIT 10;
我也不确定是否sit
根本不能忽略停用词...
推荐阅读
- php - 如何在 php 数组中添加冒号?
- python - Python 读取 csv 文件
- sql - Oracle/SQL - 基于参数的动态列
- amazon - 亚马逊 MWS CreateShipment 每次都与 Fedex 一起失败,没有真正的错误
- dart - Flutter 中的 ScopedModel
- postgresql - 有没有办法恢复孤立的 Confluence pgsql 数据库?
- android - Firebase dynamic link: can`t create short link
- unity3d - 如何通过单击不拖动对象在场景视图中的鼠标点处创建对象?
- javascript - 缓存存储不完整
- node.js - 续集将所有表字段更改为蛇案例