This repository has been archived by the owner on Jun 30, 2018. It is now read-only.
/
search_query_test.rb
397 lines (312 loc) · 13.7 KB
/
search_query_test.rb
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
require 'test_helper'
module Tire::Search
class QueryTest < Test::Unit::TestCase
context "Query" do
should "be serialized to JSON" do
assert_respond_to Query.new, :to_json
end
should "return itself as a Hash" do
assert_respond_to Query.new, :to_hash
assert_equal( { :term => { :foo => { :term => 'bar' } } }, Query.new.term(:foo, 'bar').to_hash )
end
should "allow a block to be given" do
assert_equal( { :term => { :foo => { :term => 'bar' } } }.to_json, Query.new do
term(:foo, 'bar')
end.to_json)
end
should "have accessor for value" do
assert_equal( {}, Query.new.value )
end
end
context "Term query" do
should "allow search for single term" do
assert_equal( { :term => { :foo => { :term => 'bar' } } }, Query.new.term(:foo, 'bar') )
end
should "allow search for single term passing an options hash" do
assert_equal( { :term => { :foo => { :term => 'bar', :boost => 2.0 } } }, Query.new.term(:foo, 'bar', :boost => 2.0) )
end
should "allow complex term queries" do
assert_equal( { :term => { :foo => { :field => 'bar', :boost => 2.0 } } }, Query.new.term(:foo, {:field => 'bar', :boost => 2.0}) )
end
should "allow complex term queries with Hash-like objects" do
assert_equal(
{ :term => { :foo => { :field => 'bar', :boost => 2.0 } } },
Query.new.term(:foo, Hashr.new( :field => 'bar', :boost => 2.0 ))
)
end
end
context "Terms query" do
should "allow search for multiple terms" do
assert_equal( { :terms => { :foo => ['bar', 'baz'] } }, Query.new.terms(:foo, ['bar', 'baz']) )
end
should "allow set minimum match when searching for multiple terms" do
assert_equal( { :terms => { :foo => ['bar', 'baz'], :minimum_match => 2 } },
Query.new.terms(:foo, ['bar', 'baz'], :minimum_match => 2) )
end
end
context "Range query" do
should "allow search for a range" do
assert_equal( { :range => { :age => { :gte => 21 } } }, Query.new.range(:age, { :gte => 21 }) )
end
end
context "Text query" do
should "allow search with a text search" do
assert_equal( { :text => {'field' => {:query => 'foo'}}}, Query.new.text('field', 'foo'))
end
should "allow search with a different operator for text search" do
assert_equal( { :text => {'field' => {:query => 'foo', :operator => 'and'}}},
Query.new.text('field', 'foo', :operator => 'and'))
end
end
context "Query String query" do
should "allow search with a query string" do
assert_equal( { :query_string => { :query => 'title:foo' } },
Query.new.string('title:foo') )
end
should "allow set default field when searching with a query string" do
assert_equal( { :query_string => { :query => 'foo', :default_field => 'title' } },
Query.new.string('foo', :default_field => 'title') )
end
should "allow set default operator when searching with a query string" do
assert_equal( { :query_string => { :query => 'foo', :default_operator => 'AND' } },
Query.new.string('foo', :default_operator => 'AND') )
end
should "allow to set options when searching with a query string" do
assert_equal( { :query_string => { :query => 'foo', :fields => ['title.*'], :use_dis_max => true } },
Query.new.string('foo', :fields => ['title.*'], :use_dis_max => true) )
end
end
context "Custom Score query" do
should "allow to set script for custom score queries" do
query = Query.new.custom_score(:script => "_score * doc['price'].value") do
string 'foo'
end
assert_equal "_score * doc['price'].value", query[:custom_score][:script]
end
should "allow to pass parameters for custom score queries" do
query = Query.new.custom_score(:script => "_score * doc['price'].value / max(a, b)",
:params => { :a => 1, :b => 2 }) do
string 'foo'
end
assert_equal 1, query[:custom_score][:params][:a]
assert_equal 2, query[:custom_score][:params][:b]
end
end
context "All query" do
should "search for all documents" do
assert_equal( { :match_all => { } }, Query.new.all )
end
should "allow passing arguments" do
assert_equal( { :match_all => {:boost => 1.2} }, Query.new.all(:boost => 1.2) )
end
end
context "IDs query" do
should "search for documents by IDs" do
assert_equal( { :ids => { :values => [1, 2], :type => 'foo' } },
Query.new.ids([1, 2], 'foo') )
end
end
context "FuzzyQuery" do
should "allow a fuzzy search" do
assert_equal( { :fuzzy => { :foo => { :term => 'bar' } } }, Query.new.fuzzy(:foo, 'bar') )
end
should "allow a fuzzy search with an options hash" do
assert_equal( { :term => { :foo => { :term => 'bar', :boost => 1.0, :min_similarity => 0.5 } } }, Query.new.term(:foo, 'bar', :boost => 1.0, :min_similarity => 0.5 ) )
end
end
context "BooleanQuery" do
should "not raise an error when no block is given" do
assert_nothing_raised { Query.new.boolean }
end
should "encode options" do
query = Query.new.boolean(:minimum_number_should_match => 1) do
must { string 'foo' }
end
assert_equal 1, query[:bool][:minimum_number_should_match]
end
should "wrap single query" do
assert_equal( { :bool => {:must => [{ :query_string => { :query => 'foo' } }] }},
Query.new.boolean { must { string 'foo' } } )
end
should "wrap multiple queries for the same condition" do
query = Query.new.boolean do
must { string 'foo' }
must { string 'bar' }
end
assert_equal( 2, query[:bool][:must].size, query[:bool][:must].inspect )
assert_equal( { :query_string => {:query => 'foo'} }, query[:bool][:must].first )
assert_equal( { :query_string => {:query => 'bar'} }, query[:bool][:must].last )
end
should "wrap queries for multiple conditions" do
query = Query.new.boolean do
should { string 'foo' }
must { string 'bar' }
must { string 'baz' }
must_not { string 'fuu' }
end
assert_equal 2, query[:bool][:must].size
assert_equal 1, query[:bool][:should].size
assert_equal 1, query[:bool][:must_not].size
assert_equal( { :query_string => {:query => 'foo'} }, query[:bool][:should].first )
assert_equal( { :query_string => {:query => 'bar'} }, query[:bool][:must].first )
assert_equal( { :query_string => {:query => 'baz'} }, query[:bool][:must].last )
assert_equal( { :query_string => {:query => 'fuu'} }, query[:bool][:must_not].first )
end
should "allow passing variables from outer scope" do
@q1 = 'foo'
@q2 = 'bar'
query = Query.new.boolean do |boolean|
boolean.must { |query| query.string @q1 }
boolean.must { |query| query.string @q2 }
end
assert_equal( 2, query[:bool][:must].size, query[:bool][:must].inspect )
assert_equal( { :query_string => {:query => 'foo'} }, query[:bool][:must].first )
assert_equal( { :query_string => {:query => 'bar'} }, query[:bool][:must].last )
end
end
context "FilteredQuery" do
should "not raise an error when no block is given" do
assert_nothing_raised { Query.new.filtered }
end
should "properly encode filter" do
query = Query.new.filtered do
query { term :foo, 'bar' }
filter :terms, :tags => ['ruby']
end
query[:filtered].tap do |f|
assert_equal( { :term => { :foo => { :term => 'bar' } } }, f[:query].to_hash )
assert_equal( { :tags => ['ruby'] }, f[:filter][:and].first[:terms] )
end
end
should "properly encode multiple filters" do
query = Query.new.filtered do
query { term :foo, 'bar' }
filter :terms, :tags => ['ruby']
filter :terms, :tags => ['python']
end
query[:filtered][:filter].tap do |filter|
assert_equal 1, filter.size
assert_equal( { :tags => ['ruby'] }, filter[:and].first[:terms] )
assert_equal( { :tags => ['python'] }, filter[:and].last[:terms] )
end
end
should "allow passing variables from outer scope" do
@my_query = 'bar'
@my_filter = { :tags => ['ruby'] }
query = Query.new.filtered do |f|
f.query { |q| q.term :foo, @my_query }
f.filter :terms, @my_filter
end
query[:filtered].tap do |f|
assert_equal( { :term => { :foo => { :term => 'bar' } } }, f[:query].to_hash )
assert_equal( { :tags => ['ruby'] }, f[:filter][:and].first[:terms] )
end
end
context "Prefix query" do
should "allow search for a prefix" do
assert_equal( { :prefix => { :user => "foo" } }, Query.new.prefix(:user, "foo") )
end
should "allow setting boost for prefix" do
assert_equal( { :prefix => {:user => {:prefix => "foo", :boost => 2.0 } } },
Query.new.prefix(:user, "foo", :boost => 2.0) )
end
end
end
context "DisMaxQuery" do
should "not raise an error when no block is given" do
assert_nothing_raised { Query.new.dis_max }
end
should "encode options" do
query = Query.new.dis_max(:tie_breaker => 0.7) do
query { string 'foo' }
end
assert_equal 0.7, query[:dis_max][:tie_breaker]
end
should "wrap single query" do
assert_equal( { :dis_max => {:queries => [{ :query_string => { :query => 'foo' } }] }},
Query.new.dis_max { query { string 'foo' } } )
end
should "wrap multiple queries" do
query = Query.new.dis_max do
query { string 'foo' }
query { string 'bar' }
query { string 'baz' }
end
assert_equal 3, query[:dis_max][:queries].size
assert_equal( { :query_string => {:query => 'foo'} }, query[:dis_max][:queries][0] )
assert_equal( { :query_string => {:query => 'bar'} }, query[:dis_max][:queries][1] )
assert_equal( { :query_string => {:query => 'baz'} }, query[:dis_max][:queries][2] )
end
should "allow passing variables from outer scope" do
@q1 = 'foo'
@q2 = 'bar'
query = Query.new.dis_max do |dis_max|
dis_max.query { |query| query.string @q1 }
dis_max.query { |query| query.string @q2 }
end
assert_equal( 2, query[:dis_max][:queries].size, query[:dis_max][:queries].inspect )
assert_equal( { :query_string => {:query => 'foo'} }, query[:dis_max][:queries].first )
assert_equal( { :query_string => {:query => 'bar'} }, query[:dis_max][:queries].last )
end
end
context "BoostingQuery" do
should "not raise an error when no block is given" do
assert_nothing_raised { Query.new.boosting }
end
should "encode options" do
query = Query.new.boosting(:negative_boost => 0.2) do
positive { string 'foo' }
end
assert_equal 0.2, query[:boosting][:negative_boost]
end
should "wrap positive query" do
assert_equal( { :boosting => {:positive => [{ :query_string => { :query => 'foo' } }] }},
Query.new.boosting { positive { string 'foo' } } )
end
should "wrap negative query" do
assert_equal( { :boosting => {:negative => [{ :query_string => { :query => 'foo' } }] }},
Query.new.boosting { negative { string 'foo' } } )
end
should "wrap multiple queries for the same condition" do
query = Query.new.boosting do
positive { string 'foo' }
positive { term('bar', 'baz') }
end
assert_equal( 2, query[:boosting][:positive].size, query[:boosting][:positive].inspect )
assert_equal( { :query_string => {:query => 'foo'} }, query[:boosting][:positive].first )
assert_equal( { :term => { "bar" => { :term => "baz" } } }, query[:boosting][:positive].last )
end
should "allow passing variables from outer scope" do
@q1 = 'foo'
@q2 = 'bar'
query = Query.new.boosting do |boosting|
boosting.positive { |query| query.string @q1 }
boosting.negative { |query| query.string @q2 }
end
assert_equal( { :query_string => {:query => 'foo'} }, query[:boosting][:positive].first )
assert_equal( { :query_string => {:query => 'bar'} }, query[:boosting][:negative].last )
end
end
context "MatchQuery" do
should "allow searching in single field" do
assert_equal( { :match => { :foo => { :query => 'bar' } } },
Query.new.match(:foo, 'bar') )
end
should "allow searching in multiple fields with multi_match" do
assert_equal( { :multi_match => { :query => 'bar', :fields => [:foo, :moo] } },
Query.new.match([:foo, :moo], 'bar') )
end
should "encode options" do
query = Query.new.match(:foo, 'bar', :type => 'phrase_prefix')
assert_equal 'phrase_prefix', query[:match][:foo][:type]
end
should "automatically construct a boolean query" do
query = Query.new
query.match(:foo, 'bar')
query.match(:moo, 'bar')
assert_not_nil query.to_hash[:bool]
assert_equal 2, query.to_hash[:bool][:must].size
end
end
end
end