首页 > 解决方案 > 为什么我的浮点值总是为 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)

这是预期的行为吗?如果是,我做错了什么。

干杯。

李。

标签: sqlpostgresqlfull-text-search

解决方案


您正在每一行中寻找相同短语的比率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根本不能忽略停用词...


推荐阅读