1

我需要一些帮助。网站有 2 种排序类型:按相关性和日期。有时会出现分数最高的问题太旧,而最新的分数很小。所以需要一些基于 2 标记的常见查询。

相关性查询看起来像'ORDER BY' ts_rank(EXTENDED_INDEX, custom_tsquery('english', 'test', 0))'

第二个只是'ORDER BY table.date'

有什么想法可以改善搜索吗?也许是第二个 ts_rank 按日期?

4

1 回答 1

2

基于这个问题,不清楚您使用的是什么数据集作为示例,但您基本上可以ORDER BY rank DESC,date DESC在查询中使用,因此您将在结果集的顶部拥有最“最近”和“排名最高”的数据。

WITH t(id,t,d) AS ( VALUES
  (1,to_tsvector('english','one'),'2016-03-18'::DATE),
  (2,to_tsvector('english','two words'),'2016-03-17'::DATE),
  (3,to_tsvector('english','three words we are looking for'),'2016-03-16'::DATE),
  (4,to_tsvector('english','four words goes here'),'2016-03-15'::DATE)
)
SELECT
  id,
  ts_rank(t,q) AS rank,
  d
FROM t,to_tsquery('english','three | words') AS q
ORDER BY rank DESC NULLS LAST,d DESC;

结果 :

 id |   rank    |     d      
----+-----------+------------
  3 | 0.0607927 | 2016-03-16
  2 | 0.0303964 | 2016-03-17
  4 | 0.0303964 | 2016-03-15
  1 |         0 | 2016-03-18
(4 rows)
于 2016-03-18T17:44:31.250 回答