|
@@ -11,7 +11,7 @@ def search(params)
|
11
|
11
|
username = params[:username].strip
|
12
|
12
|
from_date = params[:from_date].strip
|
13
|
13
|
to_date = params[:to_date].strip
|
14
|
|
- sort = params[:sort].strip
|
|
14
|
+ sort = (params[:sort] || "").strip
|
15
|
15
|
exact_match = params[:exact_match].strip == "yes"
|
16
|
16
|
|
17
|
17
|
errors = Array.new
|
|
@@ -65,10 +65,23 @@ def search_threads(q, username, from_date, to_date, sort, offset, exact_match)
|
65
|
65
|
.limit(RESULTS_PER_PAGE)
|
66
|
66
|
.offset(Sequel.lit('?', offset))
|
67
|
67
|
|
|
68
|
+ count_query = VLV::Thread
|
|
69
|
+ .select(Sequel.lit('count(*) as full_count'))
|
|
70
|
+ .where(Sequel.lit("(LOWER(threads.creator) = LOWER(?) OR ? = '')", username, username))
|
|
71
|
+ .where(Sequel.lit("created_at >= ? OR ? IS NULL", from_date, from_date))
|
|
72
|
+ .where(Sequel.lit("created_at >= ? OR ? IS NULL", to_date, to_date))
|
|
73
|
+
|
68
|
74
|
if exact_match
|
69
|
|
- query.where(Sequel.ilike(:title, "%#{q}%"))
|
|
75
|
+ filter = Sequel.ilike(:title, "%#{q}%")
|
|
76
|
+ result = query.where(filter)
|
|
77
|
+ full_count = count_query.where(filter).first.values[:full_count]
|
|
78
|
+ [full_count, result]
|
70
|
79
|
else
|
71
|
|
- query.full_text_search(:title, Sequel.lit("websearch_to_tsquery(?)", q), tsquery: true, language: 'english')
|
|
80
|
+ result = query.full_text_search(:title, Sequel.lit("websearch_to_tsquery(?)", q), tsquery: true, language: 'english')
|
|
81
|
+ full_count = count_query.full_text_search(
|
|
82
|
+ :title, Sequel.lit("websearch_to_tsquery(?)", q), tsquery: true, language: 'english'
|
|
83
|
+ ).first.values[:full_count]
|
|
84
|
+ [full_count, result]
|
72
|
85
|
end
|
73
|
86
|
end
|
74
|
87
|
|
|
@@ -83,13 +96,28 @@ def search_posts(q, username, from_date, to_date, offset, exact_match)
|
83
|
96
|
.offset(Sequel.lit('?', offset))
|
84
|
97
|
.order(Sequel.desc(Sequel.lit('posts.created_at')))
|
85
|
98
|
|
|
99
|
+ count_query = VLV::Post
|
|
100
|
+ .select(Sequel.lit('count(*) as full_count'))
|
|
101
|
+ .join(Sequel.lit('threads on posts.thread_id = threads.id'))
|
|
102
|
+ .where(Sequel.lit("(LOWER(posts.creator) = LOWER(?) OR (? = ''))", username, username))
|
|
103
|
+ .where(Sequel.lit("posts.created_at >= ? OR ? IS NULL", from_date, from_date))
|
|
104
|
+ .where(Sequel.lit("posts.created_at >= ? OR ? IS NULL", to_date, to_date))
|
|
105
|
+
|
86
|
106
|
if exact_match
|
87
|
|
- query.where(Sequel.ilike(:body, "%#{q}%"))
|
|
107
|
+ result = query.where(Sequel.ilike(:body, "%#{q}%"))
|
|
108
|
+ full_count = count_query.where(Sequel.ilike(:body, "%#{q}%")).first.values[:full_count]
|
|
109
|
+ [full_count, result]
|
88
|
110
|
else
|
89
|
|
- query.full_text_search(:tsv, Sequel.lit("websearch_to_tsquery(?)", q), {
|
|
111
|
+ result = query.full_text_search(:tsv, Sequel.lit("websearch_to_tsquery(?)", q), {
|
90
|
112
|
tsquery: true,
|
91
|
113
|
tsvector: true,
|
92
|
114
|
language: 'english'
|
93
|
115
|
})
|
|
116
|
+ full_count = count_query.full_text_search(:tsv, Sequel.lit("websearch_to_tsquery(?)", q), {
|
|
117
|
+ tsquery: true,
|
|
118
|
+ tsvector: true,
|
|
119
|
+ language: 'english'
|
|
120
|
+ }).first.values[:full_count]
|
|
121
|
+ [full_count, result]
|
94
|
122
|
end
|
95
|
123
|
end
|