tire 0.4.0 → 0.4.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,68 @@
1
+ require 'test_helper'
2
+
3
+ module Tire
4
+
5
+ class IndexAliasesIntegrationTest < Test::Unit::TestCase
6
+ include Test::Integration
7
+
8
+ context "With a filtered alias" do
9
+ setup do
10
+
11
+ @index = Tire.index 'index-original' do
12
+ delete
13
+ create
14
+ end
15
+
16
+ end
17
+
18
+ teardown { Tire.index('index-original').delete }
19
+
20
+ should "create the alias" do
21
+ @index.add_alias 'index-aliased'
22
+ assert_equal 1, @index.aliases.size
23
+ end
24
+
25
+ should "find only portion of documents in the filtered alias" do
26
+ Tire.index 'index-original' do
27
+ add_alias 'index-aliased', :filter => { :term => { :user => 'anne' } }
28
+ store :title => 'Document 1', :user => 'anne'
29
+ store :title => 'Document 2', :user => 'mary'
30
+
31
+ refresh
32
+ end
33
+
34
+ assert_equal 2, Tire.search('index-original') { query { all } }.results.size
35
+ assert_equal 1, Tire.search('index-aliased') { query { all } }.results.size
36
+ end
37
+
38
+ should "remove the alias" do
39
+ @index.add_alias 'index-aliased'
40
+ assert_equal 1, @index.aliases.size
41
+
42
+ @index.remove_alias 'index-aliased'
43
+ assert_equal 0, @index.aliases.size
44
+
45
+ assert_raise Tire::Search::SearchRequestFailed do
46
+ Tire.search('index-aliased') { query { all } }.results
47
+ end
48
+ end
49
+
50
+ should "retrieve a list of aliases for an index" do
51
+ @index.add_alias 'index-aliased'
52
+
53
+ assert_equal ['index-aliased'], @index.aliases
54
+ end
55
+
56
+ should "retrieve the properties of an alias" do
57
+ @index.add_alias 'index-aliased', :routing => '1'
58
+
59
+ assert_equal(
60
+ { 'index_routing' => '1',
61
+ 'search_routing' => '1' },
62
+ @index.aliases('index-aliased') )
63
+ end
64
+ end
65
+
66
+ end
67
+
68
+ end
@@ -0,0 +1,46 @@
1
+ require 'test_helper'
2
+
3
+ module Tire
4
+
5
+ class ReindexIntegrationTest < Test::Unit::TestCase
6
+ include Test::Integration
7
+
8
+ context "Reindex" do
9
+ setup do
10
+ Tire.index('reindex-test-new').delete
11
+
12
+ documents = (1..100).map { |i| { id: i, type: 'test', title: "Document #{i}" } }
13
+
14
+ Tire.index 'reindex-test' do
15
+ delete
16
+ create :settings => { :number_of_shards => 1, :number_of_replicas => 0 }
17
+ import documents
18
+ refresh
19
+ end
20
+ end
21
+
22
+ teardown do
23
+ Index.new('reindex-test').delete
24
+ Index.new('reindex-test-new').delete
25
+ end
26
+
27
+ should "reindex the index into a new index with different settings" do
28
+ Tire.index('reindex-test').reindex 'reindex-test-new', settings: { number_of_shards: 3 }
29
+
30
+ Tire.index('reindex-test-new').refresh
31
+ assert_equal 100, Tire.search('reindex-test-new').results.total
32
+ assert_equal '3', Tire.index('reindex-test-new').settings['index.number_of_shards']
33
+ end
34
+
35
+ should "reindex a portion of an index into a new index" do
36
+ Tire.index('reindex-test').reindex('reindex-test-new') { query { string '10*' } }
37
+
38
+ Tire.index('reindex-test-new').refresh
39
+ assert_equal 2, Tire.search('reindex-test-new').results.total
40
+ end
41
+
42
+ end
43
+
44
+ end
45
+
46
+ end
@@ -0,0 +1,56 @@
1
+ require 'test_helper'
2
+
3
+ module Tire
4
+
5
+ class ScanIntegrationTest < Test::Unit::TestCase
6
+ include Test::Integration
7
+
8
+ context "Scan" do
9
+ setup do
10
+ documents = (1..100).map { |i| { id: i, type: 'test', title: "Document #{i}" } }
11
+
12
+ Tire.index 'scantest' do
13
+ delete
14
+ create :settings => { :number_of_shards => 1, :number_of_replicas => 0 }
15
+ import documents
16
+ refresh
17
+ end
18
+ end
19
+
20
+ teardown { Index.new('scantest').delete }
21
+
22
+ should "iterate over batches of documents" do
23
+ count = 0
24
+
25
+ s = Tire.scan 'scantest'
26
+ s.each { |results| count += 1 }
27
+
28
+ assert_equal 10, count
29
+ end
30
+
31
+ should "iterate over individual documents" do
32
+ count = 0
33
+
34
+ s = Tire.scan 'scantest'
35
+ s.each_document { |results| count += 1 }
36
+
37
+ assert_equal 100, count
38
+ end
39
+
40
+ should "limit the returned results by query" do
41
+ count = 0
42
+
43
+ s = Tire.scan('scantest') { query { string '10*' } }
44
+ s.each do |results|
45
+ count += 1
46
+ assert_equal ['Document 10', 'Document 100'], results.map(&:title)
47
+ end
48
+
49
+ assert_equal 1, count
50
+ end
51
+
52
+ end
53
+
54
+ end
55
+
56
+ end
data/test/test_helper.rb CHANGED
@@ -72,6 +72,17 @@ module Test::Integration
72
72
  end
73
73
 
74
74
  def teardown
75
- ::RestClient.delete "#{URL}/articles-test" rescue nil
75
+ %w[
76
+ articles-test
77
+ active_record_articles
78
+ active_model_article_with_custom_as_serializations
79
+ active_record_class_with_tire_methods
80
+ mongoid_articles
81
+ mongoid_class_with_tire_methods
82
+ supermodel_articles
83
+ dynamic_index
84
+ model_with_nested_documents ].each do |index|
85
+ ::RestClient.delete "#{URL}/#{index}" rescue nil
86
+ end
76
87
  end
77
88
  end
@@ -14,6 +14,10 @@ module Tire
14
14
  assert_equal 'dummy', @index.name
15
15
  end
16
16
 
17
+ should "have an URL endpoint" do
18
+ assert_equal "#{Configuration.url}/#{@index.name}", @index.url
19
+ end
20
+
17
21
  should "return HTTP response" do
18
22
  assert_respond_to @index, :response
19
23
 
@@ -52,6 +56,47 @@ module Tire
52
56
  assert_nothing_raised { assert ! @index.delete }
53
57
  end
54
58
 
59
+ should "add an index alias" do
60
+ Configuration.client.expects(:post).with do |url, payload|
61
+ url =~ /_aliases/ &&
62
+ MultiJson.decode(payload)['actions'][0]['add'] == {'index' => 'dummy', 'alias' => 'foo'}
63
+ end.returns(mock_response('{"ok":true}'))
64
+
65
+ @index.add_alias 'foo'
66
+ end
67
+
68
+ should "add an index alias with configuration" do
69
+ Configuration.client.expects(:post).with do |url, payload|
70
+ url =~ /_aliases/ &&
71
+ MultiJson.decode(payload)['actions'][0]['add'] == {'index' => 'dummy', 'alias' => 'foo', 'routing' => 1 }
72
+ end.returns(mock_response('{"ok":true}'))
73
+
74
+ @index.add_alias 'foo', :routing => 1
75
+ end
76
+
77
+ should "delete an index alias" do
78
+ Configuration.client.expects(:post).with do |url, payload|
79
+ url =~ /_aliases/ &&
80
+ MultiJson.decode(payload)['actions'][0]['remove'] == {'index' => 'dummy', 'alias' => 'foo'}
81
+ end.returns(mock_response('{"ok":true}'))
82
+
83
+ @index.remove_alias 'foo'
84
+ end
85
+
86
+ should "list aliases for an index" do
87
+ json = {'dummy' => {'aliases' => {'foo' => {}}}}.to_json
88
+ Configuration.client.expects(:get).returns(mock_response(json))
89
+
90
+ assert_equal ['foo'], @index.aliases
91
+ end
92
+
93
+ should "return properties of an alias" do
94
+ json = {'dummy' => { 'aliases' => {'foo' => { 'filter' => { 'term' => { 'user' => 'john' } }}} }}.to_json
95
+ Configuration.client.expects(:get).returns(mock_response(json))
96
+
97
+ assert_equal( { 'filter' => { 'term' => {'user' => 'john'} } }, @index.aliases('foo') )
98
+ end
99
+
55
100
  should "refresh the index" do
56
101
  Configuration.client.expects(:post).returns(mock_response('{"ok":true,"_shards":{}}'))
57
102
  assert_nothing_raised { assert @index.refresh }
@@ -74,7 +119,7 @@ module Tire
74
119
 
75
120
  should "send text to the Analyze API" do
76
121
  Configuration.client.expects(:get).
77
- with("#{Configuration.url}/dummy/_analyze?pretty=true", "foo bar").
122
+ with("#{@index.url}/_analyze?pretty=true", "foo bar").
78
123
  returns(mock_response(@mock_analyze_response))
79
124
 
80
125
  response = @index.analyze("foo bar")
@@ -83,19 +128,19 @@ module Tire
83
128
 
84
129
  should "properly encode parameters" do
85
130
  Configuration.client.expects(:get).with do |url, payload|
86
- url == "#{Configuration.url}/dummy/_analyze?analyzer=whitespace&pretty=true"
131
+ url == "#{@index.url}/_analyze?analyzer=whitespace&pretty=true"
87
132
  end.returns(mock_response(@mock_analyze_response))
88
133
 
89
134
  @index.analyze("foo bar", :analyzer => 'whitespace')
90
135
 
91
136
  Configuration.client.expects(:get).with do |url, payload|
92
- url == "#{Configuration.url}/dummy/_analyze?field=title&pretty=true"
137
+ url == "#{@index.url}/_analyze?field=title&pretty=true"
93
138
  end.returns(mock_response(@mock_analyze_response))
94
139
 
95
140
  @index.analyze("foo bar", :field => 'title')
96
141
 
97
142
  Configuration.client.expects(:get).with do |url, payload|
98
- url == "#{Configuration.url}/dummy/_analyze?analyzer=keyword&format=text&pretty=true"
143
+ url == "#{@index.url}/_analyze?analyzer=keyword&format=text&pretty=true"
99
144
  end.returns(mock_response(@mock_analyze_response))
100
145
 
101
146
  @index.analyze("foo bar", :analyzer => 'keyword', :format => 'text')
@@ -141,25 +186,45 @@ module Tire
141
186
 
142
187
  end
143
188
 
189
+ context "settings" do
190
+
191
+ should "return index settings" do
192
+ json =<<-JSON
193
+ {
194
+ "dummy" : {
195
+ "settings" : {
196
+ "index.number_of_shards" : "20",
197
+ "index.number_of_replicas" : "0"
198
+ }
199
+ }
200
+ }
201
+ JSON
202
+ Configuration.client.stubs(:get).returns(mock_response(json))
203
+
204
+ assert_equal '20', @index.settings['index.number_of_shards']
205
+ end
206
+
207
+ end
208
+
144
209
  context "when storing" do
145
210
 
146
211
  should "set type from Hash :type property" do
147
212
  Configuration.client.expects(:post).with do |url,document|
148
- url == "#{Configuration.url}/dummy/article/"
213
+ url == "#{@index.url}/article/"
149
214
  end.returns(mock_response('{"ok":true,"_id":"test"}'))
150
215
  @index.store :type => 'article', :title => 'Test'
151
216
  end
152
217
 
153
218
  should "set type from Hash :_type property" do
154
219
  Configuration.client.expects(:post).with do |url,document|
155
- url == "#{Configuration.url}/dummy/article/"
220
+ url == "#{@index.url}/article/"
156
221
  end.returns(mock_response('{"ok":true,"_id":"test"}'))
157
222
  @index.store :_type => 'article', :title => 'Test'
158
223
  end
159
224
 
160
225
  should "set type from Object _type method" do
161
226
  Configuration.client.expects(:post).with do |url,document|
162
- url == "#{Configuration.url}/dummy/article/"
227
+ url == "#{@index.url}/article/"
163
228
  end.returns(mock_response('{"ok":true,"_id":"test"}'))
164
229
 
165
230
  article = Class.new do
@@ -171,7 +236,7 @@ module Tire
171
236
 
172
237
  should "set type from Object type method" do
173
238
  Configuration.client.expects(:post).with do |url,document|
174
- url == "#{Configuration.url}/dummy/article/"
239
+ url == "#{@index.url}/article/"
175
240
  end.returns(mock_response('{"ok":true,"_id":"test"}'))
176
241
 
177
242
  article = Class.new do
@@ -183,7 +248,7 @@ module Tire
183
248
 
184
249
  should "properly encode namespaced document types" do
185
250
  Configuration.client.expects(:post).with do |url,document|
186
- url == "#{Configuration.url}/dummy/my_namespace%2Fmy_model/"
251
+ url == "#{@index.url}/my_namespace%2Fmy_model/"
187
252
  end.returns(mock_response('{"ok":true,"_id":"123"}'))
188
253
 
189
254
  module MyNamespace
@@ -197,7 +262,7 @@ module Tire
197
262
  end
198
263
 
199
264
  should "set default type" do
200
- Configuration.client.expects(:post).with("#{Configuration.url}/dummy/document/", '{"title":"Test"}').returns(mock_response('{"ok":true,"_id":"test"}'))
265
+ Configuration.client.expects(:post).with("#{@index.url}/document/", '{"title":"Test"}').returns(mock_response('{"ok":true,"_id":"test"}'))
201
266
  @index.store :title => 'Test'
202
267
  end
203
268
 
@@ -221,14 +286,14 @@ module Tire
221
286
  context "document with ID" do
222
287
 
223
288
  should "store Hash it under its ID property" do
224
- Configuration.client.expects(:post).with("#{Configuration.url}/dummy/document/123",
289
+ Configuration.client.expects(:post).with("#{@index.url}/document/123",
225
290
  MultiJson.encode({:id => 123, :title => 'Test'})).
226
291
  returns(mock_response('{"ok":true,"_id":"123"}'))
227
292
  @index.store :id => 123, :title => 'Test'
228
293
  end
229
294
 
230
295
  should "store a custom class under its ID property" do
231
- Configuration.client.expects(:post).with("#{Configuration.url}/dummy/document/123",
296
+ Configuration.client.expects(:post).with("#{@index.url}/document/123",
232
297
  {:id => 123, :title => 'Test', :body => 'Lorem'}.to_json).
233
298
  returns(mock_response('{"ok":true,"_id":"123"}'))
234
299
  @index.store Article.new(:id => 123, :title => 'Test', :body => 'Lorem')
@@ -244,7 +309,7 @@ module Tire
244
309
  Configuration.reset :wrapper
245
310
 
246
311
  Configuration.client.stubs(:post).with do |url, payload|
247
- url == "#{Configuration.url}/dummy/article/" &&
312
+ url == "#{@index.url}/article/" &&
248
313
  payload =~ /"title":"Test"/
249
314
  end.
250
315
  returns(mock_response('{"ok":true,"_id":"id-1"}'))
@@ -252,7 +317,7 @@ module Tire
252
317
  end
253
318
 
254
319
  should "return document in default wrapper" do
255
- Configuration.client.expects(:get).with("#{Configuration.url}/dummy/article/id-1").
320
+ Configuration.client.expects(:get).with("#{@index.url}/article/id-1").
256
321
  returns(mock_response('{"_id":"id-1","_version":1, "_source" : {"title":"Test"}}'))
257
322
  article = @index.retrieve :article, 'id-1'
258
323
  assert_instance_of Results::Item, article
@@ -263,7 +328,7 @@ module Tire
263
328
  should "return document as a hash" do
264
329
  Configuration.wrapper Hash
265
330
 
266
- Configuration.client.expects(:get).with("#{Configuration.url}/dummy/article/id-1").
331
+ Configuration.client.expects(:get).with("#{@index.url}/article/id-1").
267
332
  returns(mock_response('{"_id":"id-1","_version":1, "_source" : {"title":"Test"}}'))
268
333
  article = @index.retrieve :article, 'id-1'
269
334
  assert_instance_of Hash, article
@@ -272,7 +337,7 @@ module Tire
272
337
  should "return document in custom wrapper" do
273
338
  Configuration.wrapper Article
274
339
 
275
- Configuration.client.expects(:get).with("#{Configuration.url}/dummy/article/id-1").
340
+ Configuration.client.expects(:get).with("#{@index.url}/article/id-1").
276
341
  returns(mock_response('{"_id":"id-1","_version":1, "_source" : {"title":"Test"}}'))
277
342
  article = @index.retrieve :article, 'id-1'
278
343
  assert_instance_of Article, article
@@ -280,7 +345,7 @@ module Tire
280
345
  end
281
346
 
282
347
  should "return nil for missing document" do
283
- Configuration.client.expects(:get).with("#{Configuration.url}/dummy/article/id-1").
348
+ Configuration.client.expects(:get).with("#{@index.url}/article/id-1").
284
349
  returns(mock_response('{"_id":"id-1","exists":false}'))
285
350
  article = @index.retrieve :article, 'id-1'
286
351
  assert_equal nil, article
@@ -293,7 +358,7 @@ module Tire
293
358
  end
294
359
 
295
360
  should "properly encode document type" do
296
- Configuration.client.expects(:get).with("#{Configuration.url}/dummy/my_namespace%2Fmy_model/id-1").
361
+ Configuration.client.expects(:get).with("#{@index.url}/my_namespace%2Fmy_model/id-1").
297
362
  returns(mock_response('{"_id":"id-1","_version":1, "_source" : {"title":"Test"}}'))
298
363
  article = @index.retrieve 'my_namespace/my_model', 'id-1'
299
364
  end
@@ -303,40 +368,40 @@ module Tire
303
368
  context "when removing" do
304
369
 
305
370
  should "get type from document" do
306
- Configuration.client.expects(:delete).with("#{Configuration.url}/dummy/article/1").
371
+ Configuration.client.expects(:delete).with("#{@index.url}/article/1").
307
372
  returns(mock_response('{"ok":true,"_id":"1"}')).twice
308
373
  @index.remove :id => 1, :type => 'article', :title => 'Test'
309
374
  @index.remove :id => 1, :type => 'article', :title => 'Test'
310
375
  end
311
376
 
312
377
  should "get namespaced type from document" do
313
- Configuration.client.expects(:delete).with("#{Configuration.url}/dummy/articles%2Farticle/1").
378
+ Configuration.client.expects(:delete).with("#{@index.url}/articles%2Farticle/1").
314
379
  returns(mock_response('{"ok":true,"_id":"1"}')).twice
315
380
  @index.remove :id => 1, :type => 'articles/article', :title => 'Test'
316
381
  @index.remove :id => 1, :type => 'articles/article', :title => 'Test'
317
382
  end
318
383
 
319
384
  should "set default type" do
320
- Configuration.client.expects(:delete).with("#{Configuration.url}/dummy/document/1").
385
+ Configuration.client.expects(:delete).with("#{@index.url}/document/1").
321
386
  returns(mock_response('{"ok":true,"_id":"1"}'))
322
387
  @index.remove :id => 1, :title => 'Test'
323
388
  end
324
389
 
325
390
  should "get ID from hash" do
326
- Configuration.client.expects(:delete).with("#{Configuration.url}/dummy/document/1").
391
+ Configuration.client.expects(:delete).with("#{@index.url}/document/1").
327
392
  returns(mock_response('{"ok":true,"_id":"1"}'))
328
393
  @index.remove :id => 1
329
394
  end
330
395
 
331
396
  should "get ID from method" do
332
397
  document = stub('document', :id => 1)
333
- Configuration.client.expects(:delete).with("#{Configuration.url}/dummy/document/1").
398
+ Configuration.client.expects(:delete).with("#{@index.url}/document/1").
334
399
  returns(mock_response('{"ok":true,"_id":"1"}'))
335
400
  @index.remove document
336
401
  end
337
402
 
338
403
  should "get type and ID from arguments" do
339
- Configuration.client.expects(:delete).with("#{Configuration.url}/dummy/article/1").
404
+ Configuration.client.expects(:delete).with("#{@index.url}/article/1").
340
405
  returns(mock_response('{"ok":true,"_id":"1"}'))
341
406
  @index.remove :article, 1
342
407
  end
@@ -348,7 +413,7 @@ module Tire
348
413
  end
349
414
 
350
415
  should "properly encode document type" do
351
- Configuration.client.expects(:delete).with("#{Configuration.url}/dummy/my_namespace%2Fmy_model/id-1").
416
+ Configuration.client.expects(:delete).with("#{@index.url}/my_namespace%2Fmy_model/id-1").
352
417
  returns(mock_response('{"_id":"id-1","_version":1, "_source" : {"title":"Test"}}'))
353
418
  article = @index.remove 'my_namespace/my_model', 'id-1'
354
419
  end
@@ -367,7 +432,7 @@ module Tire
367
432
 
368
433
  should "serialize Hashes" do
369
434
  Configuration.client.expects(:post).with do |url, json|
370
- url == "#{Configuration.url}/_bulk" &&
435
+ url == "#{@index.url}/_bulk" &&
371
436
  json =~ /"_index":"dummy"/ &&
372
437
  json =~ /"_type":"document"/ &&
373
438
  json =~ /"_id":"1"/ &&
@@ -379,12 +444,11 @@ module Tire
379
444
  end.returns(mock_response('{}'), 200)
380
445
 
381
446
  @index.bulk_store [ {:id => '1', :title => 'One'}, {:id => '2', :title => 'Two'} ]
382
-
383
447
  end
384
448
 
385
449
  should "serialize ActiveModel instances" do
386
450
  Configuration.client.expects(:post).with do |url, json|
387
- url == "#{Configuration.url}/_bulk" &&
451
+ url == "#{ActiveModelArticle.index.url}/_bulk" &&
388
452
  json =~ /"_index":"active_model_articles"/ &&
389
453
  json =~ /"_type":"active_model_article"/ &&
390
454
  json =~ /"_id":"1"/ &&
@@ -397,14 +461,13 @@ module Tire
397
461
  two = ActiveModelArticle.new 'title' => 'Two'; two.id = '2'
398
462
 
399
463
  ActiveModelArticle.index.bulk_store [ one, two ]
400
-
401
464
  end
402
465
 
403
466
  context "namespaced models" do
404
467
  should "not URL-escape the document_type" do
405
468
  Configuration.client.expects(:post).with do |url, json|
406
469
  puts url, json
407
- url == "#{Configuration.url}/_bulk" &&
470
+ url == "#{Configuration.url}/my_namespace_my_models/_bulk" &&
408
471
  json =~ %r|"_index":"my_namespace_my_models"| &&
409
472
  json =~ %r|"_type":"my_namespace/my_model"|
410
473
  end.returns(mock_response('{}', 200))
@@ -449,7 +512,10 @@ module Tire
449
512
  end
450
513
 
451
514
  should "display error message when collection item does not have ID" do
452
- Configuration.client.expects(:post).with{ |url, json| url == "#{Configuration.url}/_bulk" }.returns(mock_response('success', 200))
515
+ Configuration.client.expects(:post).with do |url, json|
516
+ url == "#{ActiveModelArticle.index.url}/_bulk"
517
+ end.returns(mock_response('success', 200))
518
+
453
519
  STDERR.expects(:puts).once
454
520
 
455
521
  documents = [ { :title => 'Bogus' }, { :title => 'Real', :id => 1 } ]
@@ -638,7 +704,7 @@ module Tire
638
704
  should "percolate document against all registered queries" do
639
705
  Configuration.client.expects(:get).with do |url,payload|
640
706
  payload = MultiJson.decode(payload)
641
- url == "#{Configuration.url}/dummy/document/_percolate" &&
707
+ url == "#{@index.url}/document/_percolate" &&
642
708
  payload['doc']['title'] == 'Test'
643
709
  end.
644
710
  returns(mock_response('{"ok":true,"_id":"test","matches":["alerts"]}'))
@@ -650,7 +716,7 @@ module Tire
650
716
  should "percolate a typed document against all registered queries" do
651
717
  Configuration.client.expects(:get).with do |url,payload|
652
718
  payload = MultiJson.decode(payload)
653
- url == "#{Configuration.url}/dummy/article/_percolate" &&
719
+ url == "#{@index.url}/article/_percolate" &&
654
720
  payload['doc']['title'] == 'Test'
655
721
  end.
656
722
  returns(mock_response('{"ok":true,"_id":"test","matches":["alerts"]}'))
@@ -663,7 +729,7 @@ module Tire
663
729
  Configuration.client.expects(:get).with do |url,payload|
664
730
  payload = MultiJson.decode(payload)
665
731
  # p [url, payload]
666
- url == "#{Configuration.url}/dummy/document/_percolate" &&
732
+ url == "#{@index.url}/document/_percolate" &&
667
733
  payload['doc']['title'] == 'Test' &&
668
734
  payload['query']['query_string']['query'] == 'tag:alerts'
669
735
  end.
@@ -678,7 +744,7 @@ module Tire
678
744
  should "percolate document against all registered queries" do
679
745
  Configuration.client.expects(:post).
680
746
  with do |url, payload|
681
- url == "#{Configuration.url}/dummy/article/?percolate=*" &&
747
+ url == "#{@index.url}/article/?percolate=*" &&
682
748
  payload =~ /"title":"Test"/
683
749
  end.
684
750
  returns(mock_response('{"ok":true,"_id":"test","matches":["alerts"]}'))
@@ -688,7 +754,7 @@ module Tire
688
754
  should "percolate document against specific queries" do
689
755
  Configuration.client.expects(:post).
690
756
  with do |url, payload|
691
- url == "#{Configuration.url}/dummy/article/?percolate=tag:alerts" &&
757
+ url == "#{@index.url}/article/?percolate=tag:alerts" &&
692
758
  payload =~ /"title":"Test"/
693
759
  end.
694
760
  returns(mock_response('{"ok":true,"_id":"test","matches":["alerts"]}'))
@@ -700,6 +766,53 @@ module Tire
700
766
 
701
767
  end
702
768
 
769
+ context "reindexing" do
770
+ setup do
771
+ @results = {
772
+ "_scroll_id" => "abc123",
773
+ "took" => 3,
774
+ "hits" => {
775
+ "total" => 10,
776
+ "hits" => [
777
+ { "_id" => "1", "_source" => { "title" => "Test" } }
778
+ ]
779
+ }
780
+ }
781
+ end
782
+
783
+ should "perform bulk store in the new index" do
784
+ Index.any_instance.stubs(:exists?).returns(true)
785
+ Search::Scan.any_instance.stubs(:__perform)
786
+ Search::Scan.any_instance.
787
+ expects(:results).
788
+ returns(Results::Collection.new(@results)).
789
+ then.
790
+ returns(Results::Collection.new(@results.merge('hits' => {'hits' => []}))).
791
+ at_least_once
792
+
793
+ Index.any_instance.expects(:bulk_store).once
794
+
795
+ @index.reindex 'whammy'
796
+ end
797
+
798
+ should "create the new index if it does not exist" do
799
+ options = { :settings => { :number_of_shards => 1 } }
800
+
801
+ Index.any_instance.stubs(:exists?).returns(false)
802
+ Search::Scan.any_instance.stubs(:__perform)
803
+ Search::Scan.any_instance.
804
+ expects(:results).
805
+ returns(Results::Collection.new(@results)).
806
+ then.
807
+ returns(Results::Collection.new(@results.merge('hits' => {'hits' => []}))).
808
+ at_least_once
809
+
810
+ Index.any_instance.expects(:create).with(options).once
811
+
812
+ @index.reindex 'whammy', options
813
+ end
814
+
815
+ end
703
816
  end
704
817
 
705
818
  end