Skip to main content

max / makenotwork

4.6 KB · 80 lines History Blame Raw
1 {
2 "db_name": "PostgreSQL",
3 "query": "WITH q AS (\n SELECT websearch_to_tsquery('english', $1) AS tsq\n ),\n thread_matches AS (\n SELECT t.id AS thread_id,\n t.title AS thread_title,\n COALESCE(u.display_name, u.username) AS author_username,\n co.name AS community_name,\n co.slug AS community_slug,\n c.name AS category_name,\n c.slug AS category_slug,\n LEFT(t.title, 200) AS snippet,\n t.last_activity_at,\n (ts_rank(t.search_tsv, q.tsq) * 2.0\n + similarity(t.title, $2)) AS rank\n FROM threads t\n JOIN categories c ON c.id = t.category_id\n JOIN communities co ON co.id = c.community_id\n JOIN users u ON u.mnw_account_id = t.author_id\n CROSS JOIN q\n WHERE t.deleted_at IS NULL\n AND co.suspended_at IS NULL\n AND (t.search_tsv @@ q.tsq\n OR t.title % $2)\n AND ($3::text IS NULL OR co.slug = $3)\n ),\n post_matches AS (\n SELECT DISTINCT ON (t.id)\n t.id AS thread_id,\n t.title AS thread_title,\n COALESCE(pu.display_name, pu.username) AS author_username,\n co.name AS community_name,\n co.slug AS community_slug,\n c.name AS category_name,\n c.slug AS category_slug,\n LEFT(p.body_markdown, 200) AS snippet,\n t.last_activity_at,\n ts_rank(p.search_tsv, q.tsq) AS rank\n FROM posts p\n JOIN threads t ON t.id = p.thread_id\n JOIN categories c ON c.id = t.category_id\n JOIN communities co ON co.id = c.community_id\n -- Author of the matched *reply* (`p.author_id`), not the thread OP\n -- (`t.author_id`): the snippet is the reply's body, so it must be\n -- attributed to whoever wrote it.\n JOIN users pu ON pu.mnw_account_id = p.author_id\n CROSS JOIN q\n WHERE t.deleted_at IS NULL\n AND co.suspended_at IS NULL\n AND p.removed_at IS NULL\n AND p.search_tsv @@ q.tsq\n AND ($3::text IS NULL OR co.slug = $3)\n AND NOT EXISTS (SELECT 1 FROM thread_matches tm WHERE tm.thread_id = t.id)\n ORDER BY t.id, ts_rank(p.search_tsv, q.tsq) DESC\n )\n SELECT\n thread_id AS \"thread_id!\",\n thread_title AS \"thread_title!\",\n author_username AS \"author_username!\",\n community_name AS \"community_name!\",\n community_slug AS \"community_slug!\",\n category_name AS \"category_name!\",\n category_slug AS \"category_slug!\",\n snippet AS \"snippet!\",\n last_activity_at AS \"last_activity_at!: chrono::DateTime<chrono::Utc>\",\n rank AS \"rank!\"\n FROM (\n SELECT * FROM thread_matches\n UNION ALL\n SELECT * FROM post_matches\n ) results\n ORDER BY rank DESC, last_activity_at DESC\n LIMIT $4",
4 "describe": {
5 "columns": [
6 {
7 "ordinal": 0,
8 "name": "thread_id!",
9 "type_info": "Uuid"
10 },
11 {
12 "ordinal": 1,
13 "name": "thread_title!",
14 "type_info": "Text"
15 },
16 {
17 "ordinal": 2,
18 "name": "author_username!",
19 "type_info": "Text"
20 },
21 {
22 "ordinal": 3,
23 "name": "community_name!",
24 "type_info": "Text"
25 },
26 {
27 "ordinal": 4,
28 "name": "community_slug!",
29 "type_info": "Text"
30 },
31 {
32 "ordinal": 5,
33 "name": "category_name!",
34 "type_info": "Text"
35 },
36 {
37 "ordinal": 6,
38 "name": "category_slug!",
39 "type_info": "Text"
40 },
41 {
42 "ordinal": 7,
43 "name": "snippet!",
44 "type_info": "Text"
45 },
46 {
47 "ordinal": 8,
48 "name": "last_activity_at!: chrono::DateTime<chrono::Utc>",
49 "type_info": "Timestamptz"
50 },
51 {
52 "ordinal": 9,
53 "name": "rank!",
54 "type_info": "Float8"
55 }
56 ],
57 "parameters": {
58 "Left": [
59 "Text",
60 "Text",
61 "Text",
62 "Int8"
63 ]
64 },
65 "nullable": [
66 null,
67 null,
68 null,
69 null,
70 null,
71 null,
72 null,
73 null,
74 null,
75 null
76 ]
77 },
78 "hash": "9327e3845bd956638d9589cfbc92ca9259524556d45ec2b954d88dd812ac1a10"
79 }
80