@@ -12,12 +12,16 @@ def self.valid_options
12
12
13
13
def conditions
14
14
Arel ::Nodes ::Grouping . new (
15
- Arel ::Nodes ::InfixOperation . new ( "@@" , arel_wrap ( tsdocument ) , arel_wrap ( tsquery ) )
15
+ Arel ::Nodes ::InfixOperation . new ( "@@" , tsdocument , tsquery )
16
16
)
17
17
end
18
18
19
19
def rank
20
- arel_wrap ( tsearch_rank )
20
+ Arel ::Nodes ::NamedFunction . new ( "ts_rank" , [
21
+ tsdocument ,
22
+ tsquery ,
23
+ normalization
24
+ ] )
21
25
end
22
26
23
27
def highlight
@@ -30,7 +34,7 @@ def ts_headline
30
34
Arel ::Nodes ::NamedFunction . new ( "ts_headline" , [
31
35
dictionary ,
32
36
arel_wrap ( document ) ,
33
- arel_wrap ( tsquery ) ,
37
+ tsquery ,
34
38
Arel ::Nodes . build_quoted ( ts_headline_options )
35
39
] ) . to_sql
36
40
end
@@ -131,29 +135,28 @@ def tsquery_expression(term_sql, negated:, prefix:)
131
135
end
132
136
133
137
def tsquery
134
- return "''" if query . blank?
138
+ return Arel :: Nodes . build_quoted ( "" ) if query . blank?
135
139
136
140
query_terms = query . split . compact
137
141
tsquery_terms = query_terms . map { |term | tsquery_for_term ( term ) }
138
- tsquery_terms . join ( options [ :any_word ] ? " || " : " && " )
142
+ tsquery_string = tsquery_terms . join ( options [ :any_word ] ? " || " : " && " )
143
+
144
+ arel_wrap ( tsquery_string )
139
145
end
140
146
141
147
def tsdocument
142
- tsdocument_terms = ( columns_to_use || [ ] ) . map do |search_column |
143
- column_to_tsvector ( search_column )
144
- end
148
+ tsdocument_terms = [
149
+ *( columns_to_use || [ ] ) . map { column_to_tsvector ( _1 ) } ,
150
+ *Array . wrap ( options [ :tsvector_column ] ) . map { arel_table [ _1 ] }
151
+ ]
145
152
146
- if options [ :tsvector_column ]
147
- tsvector_columns = Array . wrap ( options [ :tsvector_column ] )
148
-
149
- tsdocument_terms << tsvector_columns . map do |tsvector_column |
150
- column_name = connection . quote_column_name ( tsvector_column )
153
+ return Arel ::Nodes . build_quoted ( nil ) if tsdocument_terms . empty?
151
154
152
- "#{ quoted_table_name } .#{ column_name } "
155
+ Arel ::Nodes ::Grouping . new (
156
+ tsdocument_terms . reduce do |memo , term |
157
+ Arel ::Nodes ::InfixOperation . new ( "||" , memo , term )
153
158
end
154
- end
155
-
156
- tsdocument_terms . join ( " || " )
159
+ )
157
160
end
158
161
159
162
# From http://www.postgresql.org/docs/8.3/static/textsearch-controls.html
@@ -169,14 +172,6 @@ def normalization
169
172
options [ :normalization ] || 0
170
173
end
171
174
172
- def tsearch_rank
173
- Arel ::Nodes ::NamedFunction . new ( "ts_rank" , [
174
- arel_wrap ( tsdocument ) ,
175
- arel_wrap ( tsquery ) ,
176
- normalization
177
- ] ) . to_sql
178
- end
179
-
180
175
def dictionary
181
176
Arel ::Nodes . build_quoted ( options [ :dictionary ] || :simple )
182
177
end
@@ -197,12 +192,13 @@ def column_to_tsvector(search_column)
197
192
tsvector = Arel ::Nodes ::NamedFunction . new (
198
193
"to_tsvector" ,
199
194
[ dictionary , Arel . sql ( normalize ( search_column . to_sql ) ) ]
200
- ) . to_sql
195
+ )
201
196
202
197
if search_column . weight . nil?
203
198
tsvector
204
199
else
205
- "setweight(#{ tsvector } , #{ connection . quote ( search_column . weight ) } )"
200
+ weight = Arel ::Nodes . build_quoted ( search_column . weight )
201
+ Arel ::Nodes ::NamedFunction . new ( "setweight" , [ tsvector , weight ] )
206
202
end
207
203
end
208
204
end
0 commit comments