elasticsearch-model 0.1.7 → 0.1.8
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.
- checksums.yaml +4 -4
- data/CHANGELOG.md +26 -1
- data/README.md +1 -1
- data/examples/activerecord_mapping_completion.rb +69 -0
- data/examples/mongoid_article.rb +2 -2
- data/lib/elasticsearch/model.rb +1 -1
- data/lib/elasticsearch/model/adapters/mongoid.rb +2 -12
- data/lib/elasticsearch/model/adapters/multiple.rb +8 -6
- data/lib/elasticsearch/model/importing.rb +3 -0
- data/lib/elasticsearch/model/indexing.rb +62 -8
- data/lib/elasticsearch/model/proxy.rb +2 -1
- data/lib/elasticsearch/model/response.rb +6 -0
- data/lib/elasticsearch/model/response/pagination.rb +25 -6
- data/lib/elasticsearch/model/response/results.rb +1 -1
- data/lib/elasticsearch/model/version.rb +1 -1
- data/test/integration/active_record_basic_test.rb +29 -4
- data/test/integration/multiple_models_test.rb +44 -26
- data/test/support/model.json +1 -0
- data/test/support/model.yml +2 -0
- data/test/unit/adapter_mongoid_test.rb +3 -1
- data/test/unit/importing_test.rb +39 -12
- data/test/unit/indexing_test.rb +111 -22
- data/test/unit/response_pagination_kaminari_test.rb +208 -8
- data/test/unit/response_pagination_will_paginate_test.rb +204 -14
- data/test/unit/response_test.rb +11 -1
- metadata +7 -2
@@ -11,6 +11,7 @@ module Elasticsearch
|
|
11
11
|
ActiveRecord::Schema.define(:version => 1) do
|
12
12
|
create_table :articles do |t|
|
13
13
|
t.string :title
|
14
|
+
t.string :body
|
14
15
|
t.datetime :created_at, :default => 'NOW()'
|
15
16
|
end
|
16
17
|
end
|
@@ -22,6 +23,7 @@ module Elasticsearch
|
|
22
23
|
settings index: { number_of_shards: 1, number_of_replicas: 0 } do
|
23
24
|
mapping do
|
24
25
|
indexes :title, type: 'string', analyzer: 'snowball'
|
26
|
+
indexes :body, type: 'string'
|
25
27
|
indexes :created_at, type: 'date'
|
26
28
|
end
|
27
29
|
end
|
@@ -30,9 +32,9 @@ module Elasticsearch
|
|
30
32
|
Article.delete_all
|
31
33
|
Article.__elasticsearch__.create_index! force: true
|
32
34
|
|
33
|
-
::Article.create! title: 'Test'
|
34
|
-
::Article.create! title: 'Testing Coding'
|
35
|
-
::Article.create! title: 'Coding'
|
35
|
+
::Article.create! title: 'Test', body: ''
|
36
|
+
::Article.create! title: 'Testing Coding', body: ''
|
37
|
+
::Article.create! title: 'Coding', body: ''
|
36
38
|
|
37
39
|
Article.__elasticsearch__.refresh_index!
|
38
40
|
end
|
@@ -128,7 +130,7 @@ module Elasticsearch
|
|
128
130
|
assert_equal 1, response.records.size
|
129
131
|
end
|
130
132
|
|
131
|
-
|
133
|
+
should "update specific attributes" do
|
132
134
|
article = Article.first
|
133
135
|
|
134
136
|
response = Article.search 'title:special'
|
@@ -146,6 +148,29 @@ module Elasticsearch
|
|
146
148
|
assert_equal 1, response.records.size
|
147
149
|
end
|
148
150
|
|
151
|
+
should "update document when save is called multiple times in a transaction" do
|
152
|
+
article = Article.first
|
153
|
+
response = Article.search 'body:dummy'
|
154
|
+
|
155
|
+
assert_equal 0, response.results.size
|
156
|
+
assert_equal 0, response.records.size
|
157
|
+
|
158
|
+
ActiveRecord::Base.transaction do
|
159
|
+
article.body = 'dummy'
|
160
|
+
article.save
|
161
|
+
|
162
|
+
article.title = 'special'
|
163
|
+
article.save
|
164
|
+
end
|
165
|
+
|
166
|
+
article.__elasticsearch__.update_document
|
167
|
+
Article.__elasticsearch__.refresh_index!
|
168
|
+
|
169
|
+
response = Article.search 'body:dummy'
|
170
|
+
assert_equal 1, response.results.size
|
171
|
+
assert_equal 1, response.records.size
|
172
|
+
end
|
173
|
+
|
149
174
|
should "return results for a DSL search" do
|
150
175
|
response = Article.search query: { match: { title: { query: 'test' } } }
|
151
176
|
|
@@ -20,28 +20,28 @@ module Elasticsearch
|
|
20
20
|
end
|
21
21
|
end
|
22
22
|
|
23
|
-
|
24
|
-
|
25
|
-
|
23
|
+
module ::NameSearch
|
24
|
+
extend ActiveSupport::Concern
|
25
|
+
|
26
|
+
included do
|
27
|
+
include Elasticsearch::Model
|
28
|
+
include Elasticsearch::Model::Callbacks
|
26
29
|
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
30
|
+
settings index: {number_of_shards: 1, number_of_replicas: 0} do
|
31
|
+
mapping do
|
32
|
+
indexes :name, type: 'string', analyzer: 'snowball'
|
33
|
+
indexes :created_at, type: 'date'
|
34
|
+
end
|
31
35
|
end
|
32
36
|
end
|
33
37
|
end
|
34
38
|
|
35
|
-
class ::
|
36
|
-
include
|
37
|
-
|
39
|
+
class ::Episode < ActiveRecord::Base
|
40
|
+
include NameSearch
|
41
|
+
end
|
38
42
|
|
39
|
-
|
40
|
-
|
41
|
-
indexes :name, type: 'string', analyzer: 'snowball'
|
42
|
-
indexes :created_at, type: 'date'
|
43
|
-
end
|
44
|
-
end
|
43
|
+
class ::Series < ActiveRecord::Base
|
44
|
+
include NameSearch
|
45
45
|
end
|
46
46
|
|
47
47
|
[::Episode, ::Series].each do |model|
|
@@ -56,7 +56,7 @@ module Elasticsearch
|
|
56
56
|
end
|
57
57
|
|
58
58
|
should "find matching documents across multiple models" do
|
59
|
-
response = Elasticsearch::Model.search("greatest"
|
59
|
+
response = Elasticsearch::Model.search(%q<"The greatest Episode"^2 OR "The greatest Series">, [Series, Episode])
|
60
60
|
|
61
61
|
assert response.any?, "Response should not be empty: #{response.to_a.inspect}"
|
62
62
|
|
@@ -75,22 +75,40 @@ module Elasticsearch
|
|
75
75
|
end
|
76
76
|
|
77
77
|
should "provide access to results" do
|
78
|
-
|
79
|
-
response = Elasticsearch::Model.search(q, [Series, Episode])
|
78
|
+
response = Elasticsearch::Model.search(%q<"A great Episode"^2 OR "A great Series">, [Series, Episode])
|
80
79
|
|
81
80
|
assert_equal 'A great Episode', response.results[0].name
|
82
81
|
assert_equal true, response.results[0].name?
|
83
82
|
assert_equal false, response.results[0].boo?
|
84
|
-
assert_equal true, response.results[0].highlight?
|
85
|
-
assert_equal true, response.results[0].highlight.name?
|
86
|
-
assert_equal false, response.results[0].highlight.boo?
|
87
83
|
|
88
84
|
assert_equal 'A great Series', response.results[1].name
|
89
85
|
assert_equal true, response.results[1].name?
|
90
86
|
assert_equal false, response.results[1].boo?
|
91
|
-
|
92
|
-
|
93
|
-
|
87
|
+
end
|
88
|
+
|
89
|
+
should "only retrieve records for existing results" do
|
90
|
+
::Series.find_by_name("The greatest Series").delete
|
91
|
+
::Series.__elasticsearch__.refresh_index!
|
92
|
+
response = Elasticsearch::Model.search(%q<"The greatest Episode"^2 OR "The greatest Series">, [Series, Episode])
|
93
|
+
|
94
|
+
assert response.any?, "Response should not be empty: #{response.to_a.inspect}"
|
95
|
+
|
96
|
+
assert_equal 2, response.results.size
|
97
|
+
assert_equal 1, response.records.size
|
98
|
+
|
99
|
+
assert_instance_of Elasticsearch::Model::Response::Result, response.results.first
|
100
|
+
assert_instance_of Episode, response.records.first
|
101
|
+
|
102
|
+
assert_equal 'The greatest Episode', response.results[0].name
|
103
|
+
assert_equal 'The greatest Episode', response.records[0].name
|
104
|
+
end
|
105
|
+
|
106
|
+
should "paginate the results" do
|
107
|
+
response = Elasticsearch::Model.search('series OR episode', [Series, Episode])
|
108
|
+
|
109
|
+
assert_equal 3, response.page(1).per(3).results.size
|
110
|
+
assert_equal 3, response.page(2).per(3).results.size
|
111
|
+
assert_equal 0, response.page(3).per(3).results.size
|
94
112
|
end
|
95
113
|
|
96
114
|
if Mongo.available?
|
@@ -128,7 +146,7 @@ module Elasticsearch
|
|
128
146
|
end
|
129
147
|
|
130
148
|
should "find matching documents across multiple models" do
|
131
|
-
response = Elasticsearch::Model.search("greatest"
|
149
|
+
response = Elasticsearch::Model.search(%q<"greatest Episode" OR "greatest Image"^2>, [Episode, Image])
|
132
150
|
|
133
151
|
assert response.any?, "Response should not be empty: #{response.to_a.inspect}"
|
134
152
|
|
@@ -0,0 +1 @@
|
|
1
|
+
{ "baz": "qux" }
|
@@ -76,7 +76,9 @@ class Elasticsearch::Model::AdapterMongoidTest < Test::Unit::TestCase
|
|
76
76
|
|
77
77
|
context "Importing" do
|
78
78
|
should "implement the __find_in_batches method" do
|
79
|
-
|
79
|
+
relation = mock()
|
80
|
+
relation.stubs(:no_timeout).returns([])
|
81
|
+
DummyClassForMongoid.expects(:all).returns(relation)
|
80
82
|
|
81
83
|
DummyClassForMongoid.__send__ :extend, Elasticsearch::Model::Adapter::Mongoid::Importing
|
82
84
|
DummyClassForMongoid.__find_in_batches do; end
|
data/test/unit/importing_test.rb
CHANGED
@@ -44,6 +44,7 @@ class Elasticsearch::Model::ImportingTest < Test::Unit::TestCase
|
|
44
44
|
DummyImportingModel.expects(:client).returns(client)
|
45
45
|
DummyImportingModel.expects(:index_name).returns('foo')
|
46
46
|
DummyImportingModel.expects(:document_type).returns('foo')
|
47
|
+
DummyImportingModel.stubs(:index_exists?).returns(true)
|
47
48
|
DummyImportingModel.stubs(:__batch_to_bulk)
|
48
49
|
assert_equal 0, DummyImportingModel.import
|
49
50
|
end
|
@@ -61,6 +62,7 @@ class Elasticsearch::Model::ImportingTest < Test::Unit::TestCase
|
|
61
62
|
DummyImportingModel.stubs(:client).returns(client)
|
62
63
|
DummyImportingModel.stubs(:index_name).returns('foo')
|
63
64
|
DummyImportingModel.stubs(:document_type).returns('foo')
|
65
|
+
DummyImportingModel.stubs(:index_exists?).returns(true)
|
64
66
|
DummyImportingModel.stubs(:__batch_to_bulk)
|
65
67
|
|
66
68
|
assert_equal 1, DummyImportingModel.import
|
@@ -79,6 +81,7 @@ class Elasticsearch::Model::ImportingTest < Test::Unit::TestCase
|
|
79
81
|
DummyImportingModel.stubs(:client).returns(client)
|
80
82
|
DummyImportingModel.stubs(:index_name).returns('foo')
|
81
83
|
DummyImportingModel.stubs(:document_type).returns('foo')
|
84
|
+
DummyImportingModel.stubs(:index_exists?).returns(true)
|
82
85
|
DummyImportingModel.stubs(:__batch_to_bulk)
|
83
86
|
|
84
87
|
assert_equal [{'index' => {'error' => 'FAILED'}}], DummyImportingModel.import(return: 'errors')
|
@@ -97,6 +100,7 @@ class Elasticsearch::Model::ImportingTest < Test::Unit::TestCase
|
|
97
100
|
DummyImportingModel.stubs(:client).returns(client)
|
98
101
|
DummyImportingModel.stubs(:index_name).returns('foo')
|
99
102
|
DummyImportingModel.stubs(:document_type).returns('foo')
|
103
|
+
DummyImportingModel.stubs(:index_exists?).returns(true)
|
100
104
|
DummyImportingModel.stubs(:__batch_to_bulk)
|
101
105
|
|
102
106
|
DummyImportingModel.import do |response|
|
@@ -104,22 +108,42 @@ class Elasticsearch::Model::ImportingTest < Test::Unit::TestCase
|
|
104
108
|
end
|
105
109
|
end
|
106
110
|
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
111
|
+
context "when the index does not exist" do
|
112
|
+
should "raise an exception" do
|
113
|
+
Elasticsearch::Model::Adapter.expects(:from_class)
|
114
|
+
.with(DummyImportingModel)
|
115
|
+
.returns(DummyImportingAdapter)
|
116
|
+
|
117
|
+
DummyImportingModel.__send__ :include, Elasticsearch::Model::Importing
|
118
|
+
|
119
|
+
DummyImportingModel.expects(:index_name).returns('foo')
|
120
|
+
DummyImportingModel.expects(:document_type).returns('foo')
|
121
|
+
DummyImportingModel.expects(:index_exists?).returns(false)
|
113
122
|
|
114
|
-
|
115
|
-
|
116
|
-
|
123
|
+
assert_raise ArgumentError do
|
124
|
+
DummyImportingModel.import
|
125
|
+
end
|
117
126
|
end
|
127
|
+
end
|
118
128
|
|
119
|
-
|
120
|
-
|
129
|
+
context "with the force option" do
|
130
|
+
should "delete and create the index" do
|
131
|
+
DummyImportingModel.expects(:__find_in_batches).with do |options|
|
132
|
+
assert_equal 'bar', options[:foo]
|
133
|
+
assert_nil options[:force]
|
134
|
+
true
|
135
|
+
end
|
136
|
+
|
137
|
+
DummyImportingModel.expects(:create_index!).with do |options|
|
138
|
+
assert_equal true, options[:force]
|
139
|
+
true
|
140
|
+
end
|
141
|
+
|
142
|
+
DummyImportingModel.expects(:index_name).returns('foo')
|
143
|
+
DummyImportingModel.expects(:document_type).returns('foo')
|
121
144
|
|
122
|
-
|
145
|
+
DummyImportingModel.import force: true, foo: 'bar'
|
146
|
+
end
|
123
147
|
end
|
124
148
|
|
125
149
|
should "allow passing a different index / type" do
|
@@ -141,6 +165,7 @@ class Elasticsearch::Model::ImportingTest < Test::Unit::TestCase
|
|
141
165
|
.returns({'items' => [ {'index' => {} }]})
|
142
166
|
|
143
167
|
DummyImportingModel.stubs(:client).returns(client)
|
168
|
+
DummyImportingModel.stubs(:index_exists?).returns(true)
|
144
169
|
DummyImportingModel.stubs(:__batch_to_bulk)
|
145
170
|
|
146
171
|
DummyImportingModel.import index: 'my-new-index', type: 'my-other-type'
|
@@ -151,6 +176,7 @@ class Elasticsearch::Model::ImportingTest < Test::Unit::TestCase
|
|
151
176
|
transform = lambda {|a|}
|
152
177
|
|
153
178
|
DummyImportingModel.stubs(:client).returns(client)
|
179
|
+
DummyImportingModel.stubs(:index_exists?).returns(true)
|
154
180
|
DummyImportingModel.expects(:__transform).returns(transform)
|
155
181
|
DummyImportingModel.expects(:__batch_to_bulk).with(anything, transform)
|
156
182
|
|
@@ -162,6 +188,7 @@ class Elasticsearch::Model::ImportingTest < Test::Unit::TestCase
|
|
162
188
|
transform = lambda {|a|}
|
163
189
|
|
164
190
|
DummyImportingModel.stubs(:client).returns(client)
|
191
|
+
DummyImportingModel.stubs(:index_exists?).returns(true)
|
165
192
|
DummyImportingModel.expects(:__batch_to_bulk).with(anything, transform)
|
166
193
|
|
167
194
|
DummyImportingModel.import index: 'foo', type: 'bar', transform: transform
|
data/test/unit/indexing_test.rb
CHANGED
@@ -28,13 +28,27 @@ class Elasticsearch::Model::IndexingTest < Test::Unit::TestCase
|
|
28
28
|
assert_instance_of Elasticsearch::Model::Indexing::Settings, DummyIndexingModel.settings
|
29
29
|
end
|
30
30
|
|
31
|
-
should "update and return the index settings" do
|
31
|
+
should "update and return the index settings from a hash" do
|
32
32
|
DummyIndexingModel.settings foo: 'boo'
|
33
33
|
DummyIndexingModel.settings bar: 'bam'
|
34
34
|
|
35
35
|
assert_equal( {foo: 'boo', bar: 'bam'}, DummyIndexingModel.settings.to_hash)
|
36
36
|
end
|
37
37
|
|
38
|
+
should "update and return the index settings from a yml file" do
|
39
|
+
DummyIndexingModel.settings File.open("test/support/model.yml")
|
40
|
+
DummyIndexingModel.settings bar: 'bam'
|
41
|
+
|
42
|
+
assert_equal( {foo: 'boo', bar: 'bam', 'baz' => 'qux'}, DummyIndexingModel.settings.to_hash)
|
43
|
+
end
|
44
|
+
|
45
|
+
should "update and return the index settings from a json file" do
|
46
|
+
DummyIndexingModel.settings File.open("test/support/model.json")
|
47
|
+
DummyIndexingModel.settings bar: 'bam'
|
48
|
+
|
49
|
+
assert_equal( {foo: 'boo', bar: 'bam', 'baz' => 'qux'}, DummyIndexingModel.settings.to_hash)
|
50
|
+
end
|
51
|
+
|
38
52
|
should "evaluate the block" do
|
39
53
|
DummyIndexingModel.expects(:foo)
|
40
54
|
|
@@ -76,6 +90,28 @@ class Elasticsearch::Model::IndexingTest < Test::Unit::TestCase
|
|
76
90
|
assert_equal 'string', mappings.to_hash[:mytype][:properties][:bar][:type]
|
77
91
|
end
|
78
92
|
|
93
|
+
should "define multiple fields" do
|
94
|
+
mappings = Elasticsearch::Model::Indexing::Mappings.new :mytype
|
95
|
+
|
96
|
+
mappings.indexes :foo_1, type: 'string' do
|
97
|
+
indexes :raw, analyzer: 'keyword'
|
98
|
+
end
|
99
|
+
|
100
|
+
mappings.indexes :foo_2, type: 'multi_field' do
|
101
|
+
indexes :raw, analyzer: 'keyword'
|
102
|
+
end
|
103
|
+
|
104
|
+
assert_equal 'string', mappings.to_hash[:mytype][:properties][:foo_1][:type]
|
105
|
+
assert_equal 'string', mappings.to_hash[:mytype][:properties][:foo_1][:fields][:raw][:type]
|
106
|
+
assert_equal 'keyword', mappings.to_hash[:mytype][:properties][:foo_1][:fields][:raw][:analyzer]
|
107
|
+
assert_nil mappings.to_hash[:mytype][:properties][:foo_1][:properties]
|
108
|
+
|
109
|
+
assert_equal 'multi_field', mappings.to_hash[:mytype][:properties][:foo_2][:type]
|
110
|
+
assert_equal 'string', mappings.to_hash[:mytype][:properties][:foo_2][:fields][:raw][:type]
|
111
|
+
assert_equal 'keyword', mappings.to_hash[:mytype][:properties][:foo_2][:fields][:raw][:analyzer]
|
112
|
+
assert_nil mappings.to_hash[:mytype][:properties][:foo_2][:properties]
|
113
|
+
end
|
114
|
+
|
79
115
|
should "define embedded properties" do
|
80
116
|
mappings = Elasticsearch::Model::Indexing::Mappings.new :mytype
|
81
117
|
|
@@ -83,20 +119,27 @@ class Elasticsearch::Model::IndexingTest < Test::Unit::TestCase
|
|
83
119
|
indexes :bar
|
84
120
|
end
|
85
121
|
|
86
|
-
mappings.indexes :
|
87
|
-
indexes :
|
88
|
-
|
122
|
+
mappings.indexes :foo_object, type: 'object' do
|
123
|
+
indexes :bar
|
124
|
+
end
|
125
|
+
|
126
|
+
mappings.indexes :foo_nested, type: 'nested' do
|
127
|
+
indexes :bar
|
89
128
|
end
|
90
129
|
|
130
|
+
# Object is the default when `type` is missing and there's a block passed
|
131
|
+
#
|
91
132
|
assert_equal 'object', mappings.to_hash[:mytype][:properties][:foo][:type]
|
92
133
|
assert_equal 'string', mappings.to_hash[:mytype][:properties][:foo][:properties][:bar][:type]
|
134
|
+
assert_nil mappings.to_hash[:mytype][:properties][:foo][:fields]
|
93
135
|
|
94
|
-
assert_equal '
|
95
|
-
assert_equal '
|
96
|
-
|
97
|
-
end
|
136
|
+
assert_equal 'object', mappings.to_hash[:mytype][:properties][:foo_object][:type]
|
137
|
+
assert_equal 'string', mappings.to_hash[:mytype][:properties][:foo_object][:properties][:bar][:type]
|
138
|
+
assert_nil mappings.to_hash[:mytype][:properties][:foo_object][:fields]
|
98
139
|
|
99
|
-
|
140
|
+
assert_equal 'nested', mappings.to_hash[:mytype][:properties][:foo_nested][:type]
|
141
|
+
assert_equal 'string', mappings.to_hash[:mytype][:properties][:foo_nested][:properties][:bar][:type]
|
142
|
+
assert_nil mappings.to_hash[:mytype][:properties][:foo_nested][:fields]
|
100
143
|
end
|
101
144
|
end
|
102
145
|
|
@@ -106,8 +149,8 @@ class Elasticsearch::Model::IndexingTest < Test::Unit::TestCase
|
|
106
149
|
end
|
107
150
|
|
108
151
|
should "update and return the index mappings" do
|
109
|
-
DummyIndexingModel.mappings foo: 'boo'
|
110
|
-
DummyIndexingModel.mappings bar: 'bam'
|
152
|
+
DummyIndexingModel.mappings foo: 'boo'
|
153
|
+
DummyIndexingModel.mappings bar: 'bam'
|
111
154
|
assert_equal( { dummy_indexing_model: { foo: "boo", bar: "bam", properties: {} } },
|
112
155
|
DummyIndexingModel.mappings.to_hash )
|
113
156
|
end
|
@@ -371,6 +414,55 @@ class Elasticsearch::Model::IndexingTest < Test::Unit::TestCase
|
|
371
414
|
end
|
372
415
|
end
|
373
416
|
|
417
|
+
context "Checking for index existence" do
|
418
|
+
context "the index exists" do
|
419
|
+
should "return true" do
|
420
|
+
indices = mock('indices', exists: true)
|
421
|
+
client = stub('client', indices: indices)
|
422
|
+
|
423
|
+
DummyIndexingModelForRecreate.expects(:client).returns(client).at_least_once
|
424
|
+
|
425
|
+
assert_equal true, DummyIndexingModelForRecreate.index_exists?
|
426
|
+
end
|
427
|
+
end
|
428
|
+
|
429
|
+
context "the index does not exists" do
|
430
|
+
should "return false" do
|
431
|
+
indices = mock('indices', exists: false)
|
432
|
+
client = stub('client', indices: indices)
|
433
|
+
|
434
|
+
DummyIndexingModelForRecreate.expects(:client).returns(client).at_least_once
|
435
|
+
|
436
|
+
assert_equal false, DummyIndexingModelForRecreate.index_exists?
|
437
|
+
end
|
438
|
+
end
|
439
|
+
|
440
|
+
context "the indices raises" do
|
441
|
+
should "return false" do
|
442
|
+
client = stub('client')
|
443
|
+
client.expects(:indices).raises(StandardError)
|
444
|
+
|
445
|
+
DummyIndexingModelForRecreate.expects(:client).returns(client).at_least_once
|
446
|
+
|
447
|
+
assert_equal false, DummyIndexingModelForRecreate.index_exists?
|
448
|
+
end
|
449
|
+
end
|
450
|
+
|
451
|
+
context "the indices raises" do
|
452
|
+
should "return false" do
|
453
|
+
indices = stub('indices')
|
454
|
+
client = stub('client')
|
455
|
+
client.expects(:indices).returns(indices)
|
456
|
+
|
457
|
+
indices.expects(:exists).raises(StandardError)
|
458
|
+
|
459
|
+
DummyIndexingModelForRecreate.expects(:client).returns(client).at_least_once
|
460
|
+
|
461
|
+
assert_equal false, DummyIndexingModelForRecreate.index_exists?
|
462
|
+
end
|
463
|
+
end
|
464
|
+
end
|
465
|
+
|
374
466
|
context "Re-creating the index" do
|
375
467
|
class ::DummyIndexingModelForRecreate
|
376
468
|
extend ActiveModel::Naming
|
@@ -425,8 +517,6 @@ class Elasticsearch::Model::IndexingTest < Test::Unit::TestCase
|
|
425
517
|
indices = stub('indices')
|
426
518
|
client.stubs(:indices).returns(indices)
|
427
519
|
|
428
|
-
indices.expects(:exists).returns(false)
|
429
|
-
|
430
520
|
indices.expects(:create).with do |payload|
|
431
521
|
assert_equal 'dummy_indexing_model_for_recreates', payload[:index]
|
432
522
|
assert_equal 1, payload[:body][:settings][:index][:number_of_shards]
|
@@ -434,6 +524,7 @@ class Elasticsearch::Model::IndexingTest < Test::Unit::TestCase
|
|
434
524
|
true
|
435
525
|
end.returns({})
|
436
526
|
|
527
|
+
DummyIndexingModelForRecreate.expects(:index_exists?).returns(false)
|
437
528
|
DummyIndexingModelForRecreate.expects(:client).returns(client).at_least_once
|
438
529
|
|
439
530
|
assert_nothing_raised { DummyIndexingModelForRecreate.create_index! }
|
@@ -444,11 +535,10 @@ class Elasticsearch::Model::IndexingTest < Test::Unit::TestCase
|
|
444
535
|
indices = stub('indices')
|
445
536
|
client.stubs(:indices).returns(indices)
|
446
537
|
|
447
|
-
indices.expects(:exists).returns(true)
|
448
|
-
|
449
538
|
indices.expects(:create).never
|
450
539
|
|
451
|
-
DummyIndexingModelForRecreate.expects(:
|
540
|
+
DummyIndexingModelForRecreate.expects(:index_exists?).returns(true)
|
541
|
+
DummyIndexingModelForRecreate.expects(:client).returns(client).never
|
452
542
|
|
453
543
|
assert_nothing_raised { DummyIndexingModelForRecreate.create_index! }
|
454
544
|
end
|
@@ -459,9 +549,9 @@ class Elasticsearch::Model::IndexingTest < Test::Unit::TestCase
|
|
459
549
|
client.stubs(:indices).returns(indices)
|
460
550
|
|
461
551
|
indices.expects(:delete).returns({})
|
462
|
-
indices.expects(:
|
463
|
-
indices.expects(:create).raises(Exception)
|
552
|
+
indices.expects(:create).raises(Exception).at_least_once
|
464
553
|
|
554
|
+
DummyIndexingModelForRecreate.expects(:index_exists?).returns(false)
|
465
555
|
DummyIndexingModelForRecreate.expects(:client).returns(client).at_least_once
|
466
556
|
|
467
557
|
assert_raise(Exception) { DummyIndexingModelForRecreate.create_index! force: true }
|
@@ -473,9 +563,9 @@ class Elasticsearch::Model::IndexingTest < Test::Unit::TestCase
|
|
473
563
|
client.stubs(:indices).returns(indices)
|
474
564
|
|
475
565
|
indices.expects(:delete).returns({})
|
476
|
-
indices.expects(:exists).returns(false)
|
477
566
|
indices.expects(:create).returns({}).at_least_once
|
478
567
|
|
568
|
+
DummyIndexingModelForRecreate.expects(:index_exists?).returns(false)
|
479
569
|
DummyIndexingModelForRecreate.expects(:client).returns(client).at_least_once
|
480
570
|
|
481
571
|
assert_nothing_raised do
|
@@ -516,12 +606,11 @@ class Elasticsearch::Model::IndexingTest < Test::Unit::TestCase
|
|
516
606
|
end
|
517
607
|
|
518
608
|
should "create the custom index" do
|
519
|
-
@indices.expects(:
|
609
|
+
@indices.expects(:create).with do |arguments|
|
520
610
|
assert_equal 'custom-foo', arguments[:index]
|
521
611
|
true
|
522
612
|
end
|
523
|
-
|
524
|
-
@indices.expects(:create).with do |arguments|
|
613
|
+
DummyIndexingModelForRecreate.expects(:index_exists?).with do |arguments|
|
525
614
|
assert_equal 'custom-foo', arguments[:index]
|
526
615
|
true
|
527
616
|
end
|