-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy path04_rank.sql
58 lines (34 loc) · 1011 Bytes
/
04_rank.sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
-- region Ranking examples
SELECT ts_rank(
to_tsvector('Example document'),
plainto_tsquery('Example document')
); -- 0.099
-- endregion
-- region Weight of document
SELECT setweight(to_tsvector(a.title), 'A') ||
setweight(to_tsvector(a.content), 'C') ||
setweight(to_tsvector(coalesce(c.name, '')), 'D')
FROM article a
LEFT JOIN category c ON c.id = a.categoryid;
-- endregion
-- region Ranking of relevance
SELECT
ts_rank(a.searchcache, searchquery) AS relevance,
a.title,
c.name AS category
FROM article a
LEFT JOIN category c ON c.id = a.categoryid
, plainto_tsquery('twitter') searchquery
WHERE a.searchcache @@ searchquery
ORDER BY relevance DESC;
-- endregion
-- region Headline
SELECT
ts_rank(a.searchcache, searchquery) AS relevance,
ts_headline(a.title, searchquery),
ts_headline(a.content, searchquery)
FROM article a,
plainto_tsquery('twitter') searchquery
WHERE a.searchcache @@ searchquery
ORDER BY relevance DESC;
-- endregion