实现说明
核心是一条 SELECT 语句,作用于 docs。
语句实际拼装出的结构:先用 WITH … AS 把中间结果命名成 CTE,主查询再引用它。
常见问题
这段 SQL 涉及哪些对象?
引用的是 docs。
代码示例
-- setweight weights title over body:A 段 lexeme 在 ts_rank 默认权重下得分最高
-- ts_rank 默认权重 {0.1, 0.2, 0.4, 1.0} 依次对应 D, C, B, A
WITH docs AS (
SELECT
setweight(to_tsvector('english', 'postgres window functions'), 'A')
||
setweight(to_tsvector('english', 'window functions rank rows inside partitions'), 'B')
AS tsv
)
SELECT
-- 只命中 title 段的查询
ts_rank(tsv, to_tsquery('english', 'postgres')) AS title_hit,
-- 只命中 body 段的查询
ts_rank(tsv, to_tsquery('english', 'partitions')) AS body_hit
FROM docs;