|
@@ -1,5 +1,8 @@
|
1
|
1
|
require 'sequel'
|
2
|
2
|
|
|
3
|
+require_relative './models/post'
|
|
4
|
+require_relative './models/thread'
|
|
5
|
+
|
3
|
6
|
RESULTS_PER_PAGE = 50
|
4
|
7
|
|
5
|
8
|
def search(params)
|
|
@@ -8,6 +11,8 @@ def search(params)
|
8
|
11
|
username = params[:username].strip
|
9
|
12
|
from_date = params[:from_date].strip
|
10
|
13
|
to_date = params[:to_date].strip
|
|
14
|
+ sort = params[:sort].strip
|
|
15
|
+ exact_match = params[:exact_match].strip == "yes"
|
11
|
16
|
|
12
|
17
|
errors = Array.new
|
13
|
18
|
|
|
@@ -39,18 +44,9 @@ def search(params)
|
39
|
44
|
|
40
|
45
|
results = case params[:type]
|
41
|
46
|
when 'threads'
|
42
|
|
- sort =
|
43
|
|
- case params[:sort]
|
44
|
|
- when 'thread'
|
45
|
|
- 'created_at DESC'
|
46
|
|
- when 'post'
|
47
|
|
- 'last_post_created_at DESC'
|
48
|
|
- else
|
49
|
|
- 'created_at DESC'
|
50
|
|
- end
|
51
|
|
- search_threads(query, username, from_date, to_date, sort, offset)
|
|
47
|
+ search_threads(query, username, from_date, to_date, sort, offset, exact_match)
|
52
|
48
|
when 'posts'
|
53
|
|
- search_posts(query, username, from_date, to_date, offset)
|
|
49
|
+ search_posts(query, username, from_date, to_date, offset, exact_match)
|
54
|
50
|
else
|
55
|
51
|
Array.new
|
56
|
52
|
end
|
|
@@ -58,39 +54,40 @@ def search(params)
|
58
|
54
|
{results: results, errors: errors}
|
59
|
55
|
end
|
60
|
56
|
|
61
|
|
-def search_threads(query, username, from_date, to_date, sort, offset)
|
62
|
|
- DB[<<-SQL, query, username, username, from_date, from_date, to_date, to_date, offset]
|
63
|
|
- SELECT
|
64
|
|
- threads.*,
|
65
|
|
- count(*) OVER() AS full_count
|
66
|
|
- FROM threads
|
67
|
|
- WHERE
|
68
|
|
- to_tsvector(title) @@ plainto_tsquery(?)
|
69
|
|
- AND (LOWER(threads.creator) = LOWER(?) OR ? = '')
|
70
|
|
- AND (created_at >= ? OR ? IS NULL)
|
71
|
|
- AND (created_at <= ? OR ? IS NULL)
|
72
|
|
- ORDER BY #{sort}
|
73
|
|
- LIMIT #{RESULTS_PER_PAGE}
|
74
|
|
- OFFSET ?;
|
75
|
|
- SQL
|
|
57
|
+def search_threads(q, username, from_date, to_date, sort, offset, exact_match)
|
|
58
|
+ sort = Sequel.desc(params[:sort] == 'post' ? :last_post_created_at : :created_at)
|
|
59
|
+ query = VLV::Thread
|
|
60
|
+ .select(Sequel.lit('threads.*, count(*) OVER() AS full_count'))
|
|
61
|
+ .where(Sequel.lit("(LOWER(threads.creator) = LOWER(?) OR ? = '')", username, username))
|
|
62
|
+ .where(Sequel.lit("created_at >= ? OR ? IS NULL", from_date, from_date))
|
|
63
|
+ .where(Sequel.lit("created_at >= ? OR ? IS NULL", to_date, to_date))
|
|
64
|
+ .order(sort)
|
|
65
|
+ .limit(RESULTS_PER_PAGE)
|
|
66
|
+ .offset(Sequel.lit('?', offset))
|
|
67
|
+
|
|
68
|
+ if exact_match
|
|
69
|
+ query.full_text_search(:title, q, phrase: true)
|
|
70
|
+ else
|
|
71
|
+ query.full_text_search(:title, Sequel.lit("websearch_to_tsquery(?)", q), tsquery: true)
|
|
72
|
+ end
|
76
|
73
|
end
|
77
|
74
|
|
78
|
|
-def search_posts(query, username, from_date, to_date, offset)
|
79
|
|
- DB[<<-SQL, query, username, username, from_date, from_date, to_date, to_date, offset]
|
80
|
|
- SELECT
|
81
|
|
- posts.*,
|
82
|
|
- threads.title as thread_title,
|
83
|
|
- threads.remote_id as remote_thread_id,
|
84
|
|
- count(*) OVER() AS full_count
|
85
|
|
- FROM posts
|
86
|
|
- INNER JOIN threads on posts.thread_id = threads.id
|
87
|
|
- WHERE
|
88
|
|
- tsv @@ plainto_tsquery(?)
|
89
|
|
- AND ((LOWER(posts.creator) = LOWER(?)) OR (? = ''))
|
90
|
|
- AND (posts.created_at >= ? OR ? IS NULL)
|
91
|
|
- AND (posts.created_at <= ? OR ? IS NULL)
|
92
|
|
- ORDER BY posts.created_at DESC
|
93
|
|
- LIMIT #{RESULTS_PER_PAGE}
|
94
|
|
- OFFSET ?;
|
95
|
|
- SQL
|
|
75
|
+def search_posts(q, username, from_date, to_date, offset, exact_match)
|
|
76
|
+ query = VLV::Post
|
|
77
|
+ .select(Sequel.lit('posts.*, threads.title as thread_title, threads.remote_id as remote_thread_id, count(*) OVER() AS full_count'))
|
|
78
|
+ .join(Sequel.lit('threads on posts.thread_id = threads.id'))
|
|
79
|
+ .where(Sequel.lit("(LOWER(posts.creator) = LOWER(?) OR (? = ''))", username, username))
|
|
80
|
+ .where(Sequel.lit("posts.created_at >= ? OR ? IS NULL", from_date, from_date))
|
|
81
|
+ .where(Sequel.lit("posts.created_at >= ? OR ? IS NULL", to_date, to_date))
|
|
82
|
+ .limit(RESULTS_PER_PAGE)
|
|
83
|
+ .offset(Sequel.lit('?', offset))
|
|
84
|
+
|
|
85
|
+ if exact_match
|
|
86
|
+ query.full_text_search(:body, q, phrase: true)
|
|
87
|
+ else
|
|
88
|
+ query.full_text_search(:tsv, Sequel.lit("websearch_to_tsquery(?)", q), {
|
|
89
|
+ tsquery: true,
|
|
90
|
+ tsvector: true
|
|
91
|
+ })
|
|
92
|
+ end
|
96
|
93
|
end
|