elasticsearch-model 6.0.0 → 6.1.0
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/Gemfile +5 -0
- data/README.md +14 -7
- data/Rakefile +27 -36
- data/elasticsearch-model.gemspec +1 -1
- data/examples/activerecord_mapping_completion.rb +2 -15
- data/gemfiles/3.0.gemfile +6 -1
- data/gemfiles/4.0.gemfile +7 -1
- data/gemfiles/5.0.gemfile +6 -0
- data/lib/elasticsearch/model.rb +15 -8
- data/lib/elasticsearch/model/adapters/active_record.rb +7 -26
- data/lib/elasticsearch/model/indexing.rb +5 -3
- data/lib/elasticsearch/model/naming.rb +6 -1
- data/lib/elasticsearch/model/response.rb +2 -2
- data/lib/elasticsearch/model/response/pagination.rb +2 -192
- data/lib/elasticsearch/model/response/pagination/kaminari.rb +109 -0
- data/lib/elasticsearch/model/response/pagination/will_paginate.rb +95 -0
- data/lib/elasticsearch/model/response/result.rb +1 -1
- data/lib/elasticsearch/model/version.rb +1 -1
- data/spec/elasticsearch/model/adapter_spec.rb +119 -0
- data/spec/elasticsearch/model/adapters/active_record/associations_spec.rb +334 -0
- data/spec/elasticsearch/model/adapters/active_record/basic_spec.rb +340 -0
- data/spec/elasticsearch/model/adapters/active_record/dynamic_index_name_spec.rb +18 -0
- data/spec/elasticsearch/model/adapters/active_record/import_spec.rb +187 -0
- data/spec/elasticsearch/model/adapters/active_record/multi_model_spec.rb +110 -0
- data/spec/elasticsearch/model/adapters/active_record/namespaced_model_spec.rb +38 -0
- data/spec/elasticsearch/model/adapters/active_record/pagination_spec.rb +315 -0
- data/spec/elasticsearch/model/adapters/active_record/parent_child_spec.rb +75 -0
- data/spec/elasticsearch/model/adapters/active_record/serialization_spec.rb +61 -0
- data/spec/elasticsearch/model/adapters/active_record_spec.rb +207 -0
- data/spec/elasticsearch/model/adapters/default_spec.rb +41 -0
- data/spec/elasticsearch/model/adapters/mongoid/basic_spec.rb +267 -0
- data/spec/elasticsearch/model/adapters/mongoid/multi_model_spec.rb +66 -0
- data/spec/elasticsearch/model/adapters/mongoid_spec.rb +235 -0
- data/spec/elasticsearch/model/adapters/multiple_spec.rb +125 -0
- data/spec/elasticsearch/model/callbacks_spec.rb +33 -0
- data/spec/elasticsearch/model/client_spec.rb +66 -0
- data/spec/elasticsearch/model/hash_wrapper_spec.rb +12 -0
- data/spec/elasticsearch/model/importing_spec.rb +214 -0
- data/spec/elasticsearch/model/indexing_spec.rb +918 -0
- data/spec/elasticsearch/model/module_spec.rb +101 -0
- data/spec/elasticsearch/model/multimodel_spec.rb +55 -0
- data/spec/elasticsearch/model/naming_inheritance_spec.rb +184 -0
- data/spec/elasticsearch/model/naming_spec.rb +186 -0
- data/spec/elasticsearch/model/proxy_spec.rb +107 -0
- data/spec/elasticsearch/model/response/aggregations_spec.rb +66 -0
- data/spec/elasticsearch/model/response/base_spec.rb +90 -0
- data/spec/elasticsearch/model/response/pagination/kaminari_spec.rb +410 -0
- data/spec/elasticsearch/model/response/pagination/will_paginate_spec.rb +262 -0
- data/spec/elasticsearch/model/response/records_spec.rb +118 -0
- data/spec/elasticsearch/model/response/response_spec.rb +131 -0
- data/spec/elasticsearch/model/response/result_spec.rb +122 -0
- data/spec/elasticsearch/model/response/results_spec.rb +56 -0
- data/spec/elasticsearch/model/searching_search_request_spec.rb +112 -0
- data/spec/elasticsearch/model/searching_spec.rb +49 -0
- data/spec/elasticsearch/model/serializing_spec.rb +22 -0
- data/spec/spec_helper.rb +161 -0
- data/spec/support/app.rb +21 -0
- data/spec/support/app/answer.rb +33 -0
- data/spec/support/app/article.rb +22 -0
- data/spec/support/app/article_for_pagination.rb +12 -0
- data/spec/support/app/article_with_custom_serialization.rb +13 -0
- data/spec/support/app/article_with_dynamic_index_name.rb +15 -0
- data/spec/support/app/author.rb +9 -0
- data/spec/support/app/authorship.rb +4 -0
- data/spec/support/app/category.rb +3 -0
- data/spec/support/app/comment.rb +3 -0
- data/spec/support/app/episode.rb +11 -0
- data/spec/support/app/image.rb +19 -0
- data/spec/support/app/import_article.rb +12 -0
- data/spec/support/app/mongoid_article.rb +21 -0
- data/spec/support/app/namespaced_book.rb +10 -0
- data/spec/support/app/parent_and_child_searchable.rb +24 -0
- data/spec/support/app/post.rb +14 -0
- data/spec/support/app/question.rb +27 -0
- data/spec/support/app/searchable.rb +48 -0
- data/spec/support/app/series.rb +11 -0
- data/spec/support/model.json +1 -0
- data/{test → spec}/support/model.yml +0 -0
- metadata +129 -86
- data/test/integration/active_record_associations_parent_child_test.rb +0 -188
- data/test/integration/active_record_associations_test.rb +0 -339
- data/test/integration/active_record_basic_test.rb +0 -255
- data/test/integration/active_record_custom_serialization_test.rb +0 -67
- data/test/integration/active_record_import_test.rb +0 -168
- data/test/integration/active_record_namespaced_model_test.rb +0 -56
- data/test/integration/active_record_pagination_test.rb +0 -149
- data/test/integration/dynamic_index_name_test.rb +0 -52
- data/test/integration/mongoid_basic_test.rb +0 -240
- data/test/integration/multiple_models_test.rb +0 -176
- data/test/support/model.json +0 -1
- data/test/test_helper.rb +0 -92
- data/test/unit/adapter_active_record_test.rb +0 -157
- data/test/unit/adapter_default_test.rb +0 -41
- data/test/unit/adapter_mongoid_test.rb +0 -161
- data/test/unit/adapter_multiple_test.rb +0 -106
- data/test/unit/adapter_test.rb +0 -69
- data/test/unit/callbacks_test.rb +0 -31
- data/test/unit/client_test.rb +0 -27
- data/test/unit/hash_wrapper_test.rb +0 -13
- data/test/unit/importing_test.rb +0 -224
- data/test/unit/indexing_test.rb +0 -720
- data/test/unit/module_test.rb +0 -68
- data/test/unit/multimodel_test.rb +0 -38
- data/test/unit/naming_inheritance_test.rb +0 -94
- data/test/unit/naming_test.rb +0 -103
- data/test/unit/proxy_test.rb +0 -98
- data/test/unit/response_aggregations_test.rb +0 -46
- data/test/unit/response_base_test.rb +0 -40
- data/test/unit/response_pagination_kaminari_test.rb +0 -433
- data/test/unit/response_pagination_will_paginate_test.rb +0 -398
- data/test/unit/response_records_test.rb +0 -91
- data/test/unit/response_result_test.rb +0 -90
- data/test/unit/response_results_test.rb +0 -34
- data/test/unit/response_test.rb +0 -104
- data/test/unit/searching_search_request_test.rb +0 -78
- data/test/unit/searching_test.rb +0 -41
- data/test/unit/serializing_test.rb +0 -17
data/test/unit/indexing_test.rb
DELETED
@@ -1,720 +0,0 @@
|
|
1
|
-
require 'test_helper'
|
2
|
-
|
3
|
-
class Elasticsearch::Model::IndexingTest < Test::Unit::TestCase
|
4
|
-
context "Indexing module: " do
|
5
|
-
class ::DummyIndexingModel
|
6
|
-
extend ActiveModel::Naming
|
7
|
-
extend Elasticsearch::Model::Naming::ClassMethods
|
8
|
-
extend Elasticsearch::Model::Indexing::ClassMethods
|
9
|
-
|
10
|
-
def self.foo
|
11
|
-
'bar'
|
12
|
-
end
|
13
|
-
end
|
14
|
-
|
15
|
-
class NotFound < Exception; end
|
16
|
-
|
17
|
-
context "Settings class" do
|
18
|
-
should "be convertible to hash" do
|
19
|
-
hash = { foo: 'bar' }
|
20
|
-
settings = Elasticsearch::Model::Indexing::Settings.new hash
|
21
|
-
assert_equal hash, settings.to_hash
|
22
|
-
assert_equal settings.to_hash, settings.as_json
|
23
|
-
end
|
24
|
-
end
|
25
|
-
|
26
|
-
context "Settings method" do
|
27
|
-
should "initialize the index settings" do
|
28
|
-
assert_instance_of Elasticsearch::Model::Indexing::Settings, DummyIndexingModel.settings
|
29
|
-
end
|
30
|
-
|
31
|
-
should "update and return the index settings from a hash" do
|
32
|
-
DummyIndexingModel.settings foo: 'boo'
|
33
|
-
DummyIndexingModel.settings bar: 'bam'
|
34
|
-
|
35
|
-
assert_equal( {foo: 'boo', bar: 'bam'}, DummyIndexingModel.settings.to_hash)
|
36
|
-
end
|
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
|
-
|
52
|
-
should "evaluate the block" do
|
53
|
-
DummyIndexingModel.expects(:foo)
|
54
|
-
|
55
|
-
DummyIndexingModel.settings do
|
56
|
-
foo
|
57
|
-
end
|
58
|
-
end
|
59
|
-
end
|
60
|
-
|
61
|
-
context "Mappings class" do
|
62
|
-
should "initialize the index mappings" do
|
63
|
-
assert_instance_of Elasticsearch::Model::Indexing::Mappings, DummyIndexingModel.mappings
|
64
|
-
end
|
65
|
-
|
66
|
-
should "raise an exception when not passed type" do
|
67
|
-
assert_raise ArgumentError do
|
68
|
-
Elasticsearch::Model::Indexing::Mappings.new
|
69
|
-
end
|
70
|
-
end
|
71
|
-
|
72
|
-
should "be convertible to hash" do
|
73
|
-
mappings = Elasticsearch::Model::Indexing::Mappings.new :mytype, { foo: 'bar' }
|
74
|
-
assert_equal( { :mytype => { foo: 'bar', :properties => {} } }, mappings.to_hash )
|
75
|
-
assert_equal mappings.to_hash, mappings.as_json
|
76
|
-
end
|
77
|
-
|
78
|
-
should "define properties" do
|
79
|
-
mappings = Elasticsearch::Model::Indexing::Mappings.new :mytype
|
80
|
-
assert_respond_to mappings, :indexes
|
81
|
-
|
82
|
-
mappings.indexes :foo, { type: 'boolean', include_in_all: false }
|
83
|
-
assert_equal 'boolean', mappings.to_hash[:mytype][:properties][:foo][:type]
|
84
|
-
end
|
85
|
-
|
86
|
-
should "define type as 'text' by default" do
|
87
|
-
mappings = Elasticsearch::Model::Indexing::Mappings.new :mytype
|
88
|
-
|
89
|
-
mappings.indexes :bar
|
90
|
-
assert_equal 'text', mappings.to_hash[:mytype][:properties][:bar][:type]
|
91
|
-
end
|
92
|
-
|
93
|
-
should "define multiple fields" do
|
94
|
-
mappings = Elasticsearch::Model::Indexing::Mappings.new :mytype
|
95
|
-
|
96
|
-
mappings.indexes :my_field, type: 'text' do
|
97
|
-
indexes :raw, type: 'keyword'
|
98
|
-
end
|
99
|
-
|
100
|
-
assert_equal 'text', mappings.to_hash[:mytype][:properties][:my_field][:type]
|
101
|
-
assert_equal 'keyword', mappings.to_hash[:mytype][:properties][:my_field][:fields][:raw][:type]
|
102
|
-
assert_nil mappings.to_hash[:mytype][:properties][:my_field][:properties]
|
103
|
-
end
|
104
|
-
|
105
|
-
should "define embedded properties" do
|
106
|
-
mappings = Elasticsearch::Model::Indexing::Mappings.new :mytype
|
107
|
-
|
108
|
-
mappings.indexes :foo do
|
109
|
-
indexes :bar
|
110
|
-
end
|
111
|
-
|
112
|
-
mappings.indexes :foo_object, type: 'object' do
|
113
|
-
indexes :bar
|
114
|
-
end
|
115
|
-
|
116
|
-
mappings.indexes :foo_nested, type: 'nested' do
|
117
|
-
indexes :bar
|
118
|
-
end
|
119
|
-
|
120
|
-
mappings.indexes :foo_nested_as_symbol, type: :nested do
|
121
|
-
indexes :bar
|
122
|
-
end
|
123
|
-
|
124
|
-
# Object is the default when `type` is missing and there's a block passed
|
125
|
-
#
|
126
|
-
assert_equal 'object', mappings.to_hash[:mytype][:properties][:foo][:type]
|
127
|
-
assert_equal 'text', mappings.to_hash[:mytype][:properties][:foo][:properties][:bar][:type]
|
128
|
-
assert_nil mappings.to_hash[:mytype][:properties][:foo][:fields]
|
129
|
-
|
130
|
-
assert_equal 'object', mappings.to_hash[:mytype][:properties][:foo_object][:type]
|
131
|
-
assert_equal 'text', mappings.to_hash[:mytype][:properties][:foo_object][:properties][:bar][:type]
|
132
|
-
assert_nil mappings.to_hash[:mytype][:properties][:foo_object][:fields]
|
133
|
-
|
134
|
-
assert_equal 'nested', mappings.to_hash[:mytype][:properties][:foo_nested][:type]
|
135
|
-
assert_equal 'text', mappings.to_hash[:mytype][:properties][:foo_nested][:properties][:bar][:type]
|
136
|
-
assert_nil mappings.to_hash[:mytype][:properties][:foo_nested][:fields]
|
137
|
-
|
138
|
-
assert_equal :nested, mappings.to_hash[:mytype][:properties][:foo_nested_as_symbol][:type]
|
139
|
-
assert_not_nil mappings.to_hash[:mytype][:properties][:foo_nested_as_symbol][:properties]
|
140
|
-
assert_nil mappings.to_hash[:mytype][:properties][:foo_nested_as_symbol][:fields]
|
141
|
-
end
|
142
|
-
end
|
143
|
-
|
144
|
-
context "Mappings method" do
|
145
|
-
should "initialize the index mappings" do
|
146
|
-
assert_instance_of Elasticsearch::Model::Indexing::Mappings, DummyIndexingModel.mappings
|
147
|
-
end
|
148
|
-
|
149
|
-
should "update and return the index mappings" do
|
150
|
-
DummyIndexingModel.mappings foo: 'boo'
|
151
|
-
DummyIndexingModel.mappings bar: 'bam'
|
152
|
-
assert_equal( { _doc: { foo: "boo", bar: "bam", properties: {} } },
|
153
|
-
DummyIndexingModel.mappings.to_hash )
|
154
|
-
end
|
155
|
-
|
156
|
-
should "evaluate the block" do
|
157
|
-
DummyIndexingModel.mappings.expects(:indexes).with(:foo).returns(true)
|
158
|
-
|
159
|
-
DummyIndexingModel.mappings do
|
160
|
-
indexes :foo
|
161
|
-
end
|
162
|
-
end
|
163
|
-
end
|
164
|
-
|
165
|
-
context "Instance methods" do
|
166
|
-
class ::DummyIndexingModelWithCallbacks
|
167
|
-
extend Elasticsearch::Model::Indexing::ClassMethods
|
168
|
-
include Elasticsearch::Model::Indexing::InstanceMethods
|
169
|
-
|
170
|
-
def self.before_save(&block)
|
171
|
-
(@callbacks ||= {})[block.hash] = block
|
172
|
-
end
|
173
|
-
|
174
|
-
def changes_to_save
|
175
|
-
{:foo => ['One', 'Two']}
|
176
|
-
end
|
177
|
-
end
|
178
|
-
|
179
|
-
class ::DummyIndexingModelWithNoChanges
|
180
|
-
extend Elasticsearch::Model::Indexing::ClassMethods
|
181
|
-
include Elasticsearch::Model::Indexing::InstanceMethods
|
182
|
-
|
183
|
-
def self.before_save(&block)
|
184
|
-
(@callbacks ||= {})[block.hash] = block
|
185
|
-
end
|
186
|
-
|
187
|
-
def changes_to_save
|
188
|
-
{}
|
189
|
-
end
|
190
|
-
end
|
191
|
-
|
192
|
-
class ::DummyIndexingModelWithCallbacksAndCustomAsIndexedJson
|
193
|
-
extend Elasticsearch::Model::Indexing::ClassMethods
|
194
|
-
include Elasticsearch::Model::Indexing::InstanceMethods
|
195
|
-
|
196
|
-
def self.before_save(&block)
|
197
|
-
(@callbacks ||= {})[block.hash] = block
|
198
|
-
end
|
199
|
-
|
200
|
-
def changes_to_save
|
201
|
-
{:foo => ['A', 'B'], :bar => ['C', 'D']}
|
202
|
-
end
|
203
|
-
|
204
|
-
def as_indexed_json(options={})
|
205
|
-
{ :foo => 'B' }
|
206
|
-
end
|
207
|
-
end
|
208
|
-
|
209
|
-
class ::DummyIndexingModelWithOldDirty
|
210
|
-
extend Elasticsearch::Model::Indexing::ClassMethods
|
211
|
-
include Elasticsearch::Model::Indexing::InstanceMethods
|
212
|
-
|
213
|
-
def self.before_save(&block)
|
214
|
-
(@callbacks ||= {})[block.hash] = block
|
215
|
-
end
|
216
|
-
|
217
|
-
def changes
|
218
|
-
{:foo => ['One', 'Two']}
|
219
|
-
end
|
220
|
-
end
|
221
|
-
|
222
|
-
should "register before_save callback when included" do
|
223
|
-
::DummyIndexingModelWithCallbacks.expects(:before_save).returns(true)
|
224
|
-
::DummyIndexingModelWithCallbacks.__send__ :include, Elasticsearch::Model::Indexing::InstanceMethods
|
225
|
-
end
|
226
|
-
|
227
|
-
should "set the @__changed_model_attributes variable before save" do
|
228
|
-
instance = ::DummyIndexingModelWithCallbacks.new
|
229
|
-
instance.expects(:instance_variable_set).with do |name, value|
|
230
|
-
assert_equal :@__changed_model_attributes, name
|
231
|
-
assert_equal({foo: 'Two'}, value)
|
232
|
-
true
|
233
|
-
end
|
234
|
-
|
235
|
-
::DummyIndexingModelWithCallbacks.__send__ :include, Elasticsearch::Model::Indexing::InstanceMethods
|
236
|
-
|
237
|
-
::DummyIndexingModelWithCallbacks.instance_variable_get(:@callbacks).each do |n,b|
|
238
|
-
instance.instance_eval(&b)
|
239
|
-
end
|
240
|
-
end
|
241
|
-
|
242
|
-
# https://github.com/elastic/elasticsearch-rails/issues/714
|
243
|
-
# https://github.com/rails/rails/pull/25337#issuecomment-225166796
|
244
|
-
should "set the @__changed_model_attributes variable before save for old ActiveModel::Dirty" do
|
245
|
-
instance = ::DummyIndexingModelWithOldDirty.new
|
246
|
-
instance.expects(:instance_variable_set).with do |name, value|
|
247
|
-
assert_equal :@__changed_model_attributes, name
|
248
|
-
assert_equal({foo: 'Two'}, value)
|
249
|
-
true
|
250
|
-
end
|
251
|
-
|
252
|
-
::DummyIndexingModelWithOldDirty.__send__ :include, Elasticsearch::Model::Indexing::InstanceMethods
|
253
|
-
|
254
|
-
::DummyIndexingModelWithOldDirty.instance_variable_get(:@callbacks).each do |n,b|
|
255
|
-
instance.instance_eval(&b)
|
256
|
-
end
|
257
|
-
end
|
258
|
-
|
259
|
-
should "have the index_document method" do
|
260
|
-
client = mock('client')
|
261
|
-
instance = ::DummyIndexingModelWithCallbacks.new
|
262
|
-
|
263
|
-
client.expects(:index).with do |payload|
|
264
|
-
assert_equal 'foo', payload[:index]
|
265
|
-
assert_equal 'bar', payload[:type]
|
266
|
-
assert_equal '1', payload[:id]
|
267
|
-
assert_equal 'JSON', payload[:body]
|
268
|
-
true
|
269
|
-
end
|
270
|
-
|
271
|
-
instance.expects(:client).returns(client)
|
272
|
-
instance.expects(:as_indexed_json).returns('JSON')
|
273
|
-
instance.expects(:index_name).returns('foo')
|
274
|
-
instance.expects(:document_type).returns('bar')
|
275
|
-
instance.expects(:id).returns('1')
|
276
|
-
|
277
|
-
instance.index_document
|
278
|
-
end
|
279
|
-
|
280
|
-
should "pass extra options to the index_document method to client.index" do
|
281
|
-
client = mock('client')
|
282
|
-
instance = ::DummyIndexingModelWithCallbacks.new
|
283
|
-
|
284
|
-
client.expects(:index).with do |payload|
|
285
|
-
assert_equal 'A', payload[:parent]
|
286
|
-
true
|
287
|
-
end
|
288
|
-
|
289
|
-
instance.expects(:client).returns(client)
|
290
|
-
instance.expects(:as_indexed_json).returns('JSON')
|
291
|
-
instance.expects(:index_name).returns('foo')
|
292
|
-
instance.expects(:document_type).returns('bar')
|
293
|
-
instance.expects(:id).returns('1')
|
294
|
-
|
295
|
-
instance.index_document(parent: 'A')
|
296
|
-
end
|
297
|
-
|
298
|
-
should "have the delete_document method" do
|
299
|
-
client = mock('client')
|
300
|
-
instance = ::DummyIndexingModelWithCallbacks.new
|
301
|
-
|
302
|
-
client.expects(:delete).with do |payload|
|
303
|
-
assert_equal 'foo', payload[:index]
|
304
|
-
assert_equal 'bar', payload[:type]
|
305
|
-
assert_equal '1', payload[:id]
|
306
|
-
true
|
307
|
-
end
|
308
|
-
|
309
|
-
instance.expects(:client).returns(client)
|
310
|
-
instance.expects(:index_name).returns('foo')
|
311
|
-
instance.expects(:document_type).returns('bar')
|
312
|
-
instance.expects(:id).returns('1')
|
313
|
-
|
314
|
-
instance.delete_document()
|
315
|
-
end
|
316
|
-
|
317
|
-
should "pass extra options to the delete_document method to client.delete" do
|
318
|
-
client = mock('client')
|
319
|
-
instance = ::DummyIndexingModelWithCallbacks.new
|
320
|
-
|
321
|
-
client.expects(:delete).with do |payload|
|
322
|
-
assert_equal 'A', payload[:parent]
|
323
|
-
true
|
324
|
-
end
|
325
|
-
|
326
|
-
instance.expects(:client).returns(client)
|
327
|
-
instance.expects(:id).returns('1')
|
328
|
-
instance.expects(:index_name).returns('foo')
|
329
|
-
instance.expects(:document_type).returns('bar')
|
330
|
-
|
331
|
-
instance.delete_document(parent: 'A')
|
332
|
-
end
|
333
|
-
|
334
|
-
should "update the document by re-indexing when no changes are present" do
|
335
|
-
client = mock('client')
|
336
|
-
instance = ::DummyIndexingModelWithCallbacks.new
|
337
|
-
|
338
|
-
# Reset the fake `changes`
|
339
|
-
instance.instance_variable_set(:@__changed_model_attributes, nil)
|
340
|
-
|
341
|
-
instance.expects(:index_document)
|
342
|
-
instance.update_document
|
343
|
-
end
|
344
|
-
|
345
|
-
should "update the document by partial update when changes are present" do
|
346
|
-
client = mock('client')
|
347
|
-
instance = ::DummyIndexingModelWithCallbacks.new
|
348
|
-
|
349
|
-
# Set the fake `changes` hash
|
350
|
-
instance.instance_variable_set(:@__changed_model_attributes, {foo: 'bar'})
|
351
|
-
|
352
|
-
client.expects(:update).with do |payload|
|
353
|
-
assert_equal 'foo', payload[:index]
|
354
|
-
assert_equal 'bar', payload[:type]
|
355
|
-
assert_equal '1', payload[:id]
|
356
|
-
assert_equal({foo: 'bar'}, payload[:body][:doc])
|
357
|
-
true
|
358
|
-
end
|
359
|
-
|
360
|
-
instance.expects(:client).returns(client)
|
361
|
-
instance.expects(:index_name).returns('foo')
|
362
|
-
instance.expects(:document_type).returns('bar')
|
363
|
-
instance.expects(:id).returns('1')
|
364
|
-
|
365
|
-
instance.update_document
|
366
|
-
end
|
367
|
-
|
368
|
-
should "exclude attributes not contained in custom as_indexed_json during partial update" do
|
369
|
-
client = mock('client')
|
370
|
-
instance = ::DummyIndexingModelWithCallbacksAndCustomAsIndexedJson.new
|
371
|
-
|
372
|
-
# Set the fake `changes` hash
|
373
|
-
instance.instance_variable_set(:@__changed_model_attributes, {'foo' => 'B', 'bar' => 'D' })
|
374
|
-
|
375
|
-
client.expects(:update).with do |payload|
|
376
|
-
assert_equal({:foo => 'B'}, payload[:body][:doc])
|
377
|
-
true
|
378
|
-
end
|
379
|
-
|
380
|
-
instance.expects(:client).returns(client)
|
381
|
-
instance.expects(:index_name).returns('foo')
|
382
|
-
instance.expects(:document_type).returns('bar')
|
383
|
-
instance.expects(:id).returns('1')
|
384
|
-
|
385
|
-
instance.update_document
|
386
|
-
end
|
387
|
-
|
388
|
-
should "get attributes from as_indexed_json during partial update" do
|
389
|
-
client = mock('client')
|
390
|
-
instance = ::DummyIndexingModelWithCallbacksAndCustomAsIndexedJson.new
|
391
|
-
|
392
|
-
instance.instance_variable_set(:@__changed_model_attributes, { 'foo' => { 'bar' => 'BAR'} })
|
393
|
-
# Overload as_indexed_json
|
394
|
-
instance.expects(:as_indexed_json).returns({ 'foo' => 'BAR' })
|
395
|
-
|
396
|
-
client.expects(:update).with do |payload|
|
397
|
-
assert_equal({'foo' => 'BAR'}, payload[:body][:doc])
|
398
|
-
true
|
399
|
-
end
|
400
|
-
|
401
|
-
instance.expects(:client).returns(client)
|
402
|
-
instance.expects(:index_name).returns('foo')
|
403
|
-
instance.expects(:document_type).returns('bar')
|
404
|
-
instance.expects(:id).returns('1')
|
405
|
-
|
406
|
-
instance.update_document
|
407
|
-
end
|
408
|
-
|
409
|
-
should "index instead of update when nothing was changed" do
|
410
|
-
client = mock('client')
|
411
|
-
instance = ::DummyIndexingModelWithNoChanges.new
|
412
|
-
|
413
|
-
# Set the fake `changes` hash
|
414
|
-
instance.instance_variable_set(:@__changed_model_attributes, {})
|
415
|
-
# Overload as_indexed_json for running index
|
416
|
-
instance.expects(:as_indexed_json).returns({ 'foo' => 'BAR' })
|
417
|
-
|
418
|
-
client.expects(:index)
|
419
|
-
client.expects(:update).never
|
420
|
-
|
421
|
-
instance.expects(:client).returns(client)
|
422
|
-
instance.expects(:index_name).returns('foo')
|
423
|
-
instance.expects(:document_type).returns('bar')
|
424
|
-
instance.expects(:id).returns('1')
|
425
|
-
|
426
|
-
instance.update_document({})
|
427
|
-
end
|
428
|
-
|
429
|
-
should "update only the specific attributes" do
|
430
|
-
client = mock('client')
|
431
|
-
instance = ::DummyIndexingModelWithCallbacks.new
|
432
|
-
|
433
|
-
# Set the fake `changes` hash
|
434
|
-
instance.instance_variable_set(:@__changed_model_attributes, {author: 'john'})
|
435
|
-
|
436
|
-
client.expects(:update).with do |payload|
|
437
|
-
assert_equal 'foo', payload[:index]
|
438
|
-
assert_equal 'bar', payload[:type]
|
439
|
-
assert_equal '1', payload[:id]
|
440
|
-
assert_equal({title: 'green'}, payload[:body][:doc])
|
441
|
-
true
|
442
|
-
end
|
443
|
-
|
444
|
-
instance.expects(:client).returns(client)
|
445
|
-
instance.expects(:index_name).returns('foo')
|
446
|
-
instance.expects(:document_type).returns('bar')
|
447
|
-
instance.expects(:id).returns('1')
|
448
|
-
|
449
|
-
instance.update_document_attributes title: "green"
|
450
|
-
end
|
451
|
-
|
452
|
-
should "pass options to the update_document_attributes method" do
|
453
|
-
client = mock('client')
|
454
|
-
instance = ::DummyIndexingModelWithCallbacks.new
|
455
|
-
|
456
|
-
client.expects(:update).with do |payload|
|
457
|
-
assert_equal 'foo', payload[:index]
|
458
|
-
assert_equal 'bar', payload[:type]
|
459
|
-
assert_equal '1', payload[:id]
|
460
|
-
assert_equal({title: 'green'}, payload[:body][:doc])
|
461
|
-
assert_equal true, payload[:refresh]
|
462
|
-
true
|
463
|
-
end
|
464
|
-
|
465
|
-
instance.expects(:client).returns(client)
|
466
|
-
instance.expects(:index_name).returns('foo')
|
467
|
-
instance.expects(:document_type).returns('bar')
|
468
|
-
instance.expects(:id).returns('1')
|
469
|
-
|
470
|
-
instance.update_document_attributes( { title: "green" }, { refresh: true } )
|
471
|
-
end
|
472
|
-
end
|
473
|
-
|
474
|
-
context "Checking for index existence" do
|
475
|
-
context "when the index exists" do
|
476
|
-
should "return true" do
|
477
|
-
indices = mock('indices', exists: true)
|
478
|
-
client = stub('client', indices: indices)
|
479
|
-
|
480
|
-
DummyIndexingModelForRecreate.expects(:client).returns(client).at_least_once
|
481
|
-
|
482
|
-
assert_equal true, DummyIndexingModelForRecreate.index_exists?
|
483
|
-
end
|
484
|
-
end
|
485
|
-
|
486
|
-
context "when the index does not exists" do
|
487
|
-
should "return false" do
|
488
|
-
indices = mock('indices', exists: false)
|
489
|
-
client = stub('client', indices: indices)
|
490
|
-
|
491
|
-
DummyIndexingModelForRecreate.expects(:client).returns(client).at_least_once
|
492
|
-
|
493
|
-
assert_equal false, DummyIndexingModelForRecreate.index_exists?
|
494
|
-
end
|
495
|
-
end
|
496
|
-
|
497
|
-
context "when the indices API raises an error" do
|
498
|
-
should "return false" do
|
499
|
-
client = stub('client')
|
500
|
-
client.expects(:indices).raises(StandardError)
|
501
|
-
|
502
|
-
DummyIndexingModelForRecreate.expects(:client).returns(client).at_least_once
|
503
|
-
|
504
|
-
assert_equal false, DummyIndexingModelForRecreate.index_exists?
|
505
|
-
end
|
506
|
-
end
|
507
|
-
|
508
|
-
context "the indices.exists API raises an error" do
|
509
|
-
should "return false" do
|
510
|
-
indices = stub('indices')
|
511
|
-
client = stub('client')
|
512
|
-
client.expects(:indices).returns(indices)
|
513
|
-
|
514
|
-
indices.expects(:exists).raises(StandardError)
|
515
|
-
|
516
|
-
DummyIndexingModelForRecreate.expects(:client).returns(client).at_least_once
|
517
|
-
|
518
|
-
assert_equal false, DummyIndexingModelForRecreate.index_exists?
|
519
|
-
end
|
520
|
-
end
|
521
|
-
end
|
522
|
-
|
523
|
-
context "Re-creating the index" do
|
524
|
-
class ::DummyIndexingModelForRecreate
|
525
|
-
extend ActiveModel::Naming
|
526
|
-
extend Elasticsearch::Model::Naming::ClassMethods
|
527
|
-
extend Elasticsearch::Model::Indexing::ClassMethods
|
528
|
-
|
529
|
-
settings index: { number_of_shards: 1 } do
|
530
|
-
mappings do
|
531
|
-
indexes :foo, analyzer: 'keyword'
|
532
|
-
end
|
533
|
-
end
|
534
|
-
end
|
535
|
-
|
536
|
-
should "delete the index without raising exception when the index is not found" do
|
537
|
-
client = stub('client')
|
538
|
-
indices = stub('indices')
|
539
|
-
client.stubs(:indices).returns(indices)
|
540
|
-
|
541
|
-
indices.expects(:delete).returns({}).then.raises(NotFound).at_least_once
|
542
|
-
|
543
|
-
DummyIndexingModelForRecreate.expects(:client).returns(client).at_least_once
|
544
|
-
|
545
|
-
assert_nothing_raised { DummyIndexingModelForRecreate.delete_index! force: true }
|
546
|
-
end
|
547
|
-
|
548
|
-
should "raise an exception without the force option" do
|
549
|
-
client = stub('client')
|
550
|
-
indices = stub('indices')
|
551
|
-
client.stubs(:indices).returns(indices)
|
552
|
-
|
553
|
-
indices.expects(:delete).raises(NotFound)
|
554
|
-
|
555
|
-
DummyIndexingModelForRecreate.expects(:client).returns(client)
|
556
|
-
|
557
|
-
assert_raise(NotFound) { DummyIndexingModelForRecreate.delete_index! }
|
558
|
-
end
|
559
|
-
|
560
|
-
should "raise a regular exception when deleting the index" do
|
561
|
-
client = stub('client')
|
562
|
-
|
563
|
-
indices = stub('indices')
|
564
|
-
indices.expects(:delete).raises(Exception)
|
565
|
-
client.stubs(:indices).returns(indices)
|
566
|
-
|
567
|
-
DummyIndexingModelForRecreate.expects(:client).returns(client)
|
568
|
-
|
569
|
-
assert_raise(Exception) { DummyIndexingModelForRecreate.delete_index! force: true }
|
570
|
-
end
|
571
|
-
|
572
|
-
should "create the index with correct settings and mappings when it doesn't exist" do
|
573
|
-
client = stub('client')
|
574
|
-
indices = stub('indices')
|
575
|
-
client.stubs(:indices).returns(indices)
|
576
|
-
|
577
|
-
indices.expects(:create).with do |payload|
|
578
|
-
assert_equal 'dummy_indexing_model_for_recreates', payload[:index]
|
579
|
-
assert_equal 1, payload[:body][:settings][:index][:number_of_shards]
|
580
|
-
assert_equal 'keyword', payload[:body][:mappings][:_doc][:properties][:foo][:analyzer]
|
581
|
-
true
|
582
|
-
end.returns({})
|
583
|
-
|
584
|
-
DummyIndexingModelForRecreate.expects(:index_exists?).returns(false)
|
585
|
-
DummyIndexingModelForRecreate.expects(:client).returns(client).at_least_once
|
586
|
-
|
587
|
-
assert_nothing_raised { DummyIndexingModelForRecreate.create_index! }
|
588
|
-
end
|
589
|
-
|
590
|
-
should "get the index settings and mappings from options" do
|
591
|
-
client = stub('client')
|
592
|
-
indices = stub('indices')
|
593
|
-
client.stubs(:indices).returns(indices)
|
594
|
-
|
595
|
-
indices.expects(:create).with do |payload|
|
596
|
-
assert_equal 'foobar', payload[:index]
|
597
|
-
assert_equal 3, payload[:body][:settings][:index][:number_of_shards]
|
598
|
-
assert_equal 'bar', payload[:body][:mappings][:foobar][:properties][:foo][:analyzer]
|
599
|
-
true
|
600
|
-
end.returns({})
|
601
|
-
|
602
|
-
DummyIndexingModelForRecreate.expects(:index_exists?).returns(false)
|
603
|
-
DummyIndexingModelForRecreate.expects(:client).returns(client).at_least_once
|
604
|
-
|
605
|
-
DummyIndexingModelForRecreate.create_index! \
|
606
|
-
index: 'foobar',
|
607
|
-
settings: { index: { number_of_shards: 3 } },
|
608
|
-
mappings: { foobar: { properties: { foo: { analyzer: 'bar' } } } }
|
609
|
-
end
|
610
|
-
|
611
|
-
should "not create the index when it exists" do
|
612
|
-
client = stub('client')
|
613
|
-
indices = stub('indices')
|
614
|
-
client.stubs(:indices).returns(indices)
|
615
|
-
|
616
|
-
indices.expects(:create).never
|
617
|
-
|
618
|
-
DummyIndexingModelForRecreate.expects(:index_exists?).returns(true)
|
619
|
-
DummyIndexingModelForRecreate.expects(:client).returns(client).never
|
620
|
-
|
621
|
-
assert_nothing_raised { DummyIndexingModelForRecreate.create_index! }
|
622
|
-
end
|
623
|
-
|
624
|
-
should "raise exception during index creation" do
|
625
|
-
client = stub('client')
|
626
|
-
indices = stub('indices')
|
627
|
-
client.stubs(:indices).returns(indices)
|
628
|
-
|
629
|
-
indices.expects(:delete).returns({})
|
630
|
-
indices.expects(:create).raises(Exception).at_least_once
|
631
|
-
|
632
|
-
DummyIndexingModelForRecreate.expects(:index_exists?).returns(false)
|
633
|
-
DummyIndexingModelForRecreate.expects(:client).returns(client).at_least_once
|
634
|
-
|
635
|
-
assert_raise(Exception) { DummyIndexingModelForRecreate.create_index! force: true }
|
636
|
-
end
|
637
|
-
|
638
|
-
should "delete the index first with the force option" do
|
639
|
-
client = stub('client')
|
640
|
-
indices = stub('indices')
|
641
|
-
client.stubs(:indices).returns(indices)
|
642
|
-
|
643
|
-
indices.expects(:delete).returns({})
|
644
|
-
indices.expects(:create).returns({}).at_least_once
|
645
|
-
|
646
|
-
DummyIndexingModelForRecreate.expects(:index_exists?).returns(false)
|
647
|
-
DummyIndexingModelForRecreate.expects(:client).returns(client).at_least_once
|
648
|
-
|
649
|
-
assert_nothing_raised do
|
650
|
-
DummyIndexingModelForRecreate.create_index! force: true
|
651
|
-
end
|
652
|
-
end
|
653
|
-
|
654
|
-
should "refresh the index without raising exception with the force option" do
|
655
|
-
client = stub('client')
|
656
|
-
indices = stub('indices')
|
657
|
-
client.stubs(:indices).returns(indices)
|
658
|
-
|
659
|
-
indices.expects(:refresh).returns({}).then.raises(NotFound).at_least_once
|
660
|
-
|
661
|
-
DummyIndexingModelForRecreate.expects(:client).returns(client).at_least_once
|
662
|
-
|
663
|
-
assert_nothing_raised { DummyIndexingModelForRecreate.refresh_index! force: true }
|
664
|
-
end
|
665
|
-
|
666
|
-
should "raise a regular exception when refreshing the index" do
|
667
|
-
client = stub('client')
|
668
|
-
indices = stub('indices')
|
669
|
-
client.stubs(:indices).returns(indices)
|
670
|
-
|
671
|
-
indices.expects(:refresh).returns({}).then.raises(Exception).at_least_once
|
672
|
-
|
673
|
-
DummyIndexingModelForRecreate.expects(:client).returns(client).at_least_once
|
674
|
-
|
675
|
-
assert_nothing_raised { DummyIndexingModelForRecreate.refresh_index! force: true }
|
676
|
-
end
|
677
|
-
|
678
|
-
context "with a custom index name" do
|
679
|
-
setup do
|
680
|
-
@client = stub('client')
|
681
|
-
@indices = stub('indices')
|
682
|
-
@client.stubs(:indices).returns(@indices)
|
683
|
-
DummyIndexingModelForRecreate.expects(:client).returns(@client).at_least_once
|
684
|
-
end
|
685
|
-
|
686
|
-
should "create the custom index" do
|
687
|
-
@indices.expects(:create).with do |arguments|
|
688
|
-
assert_equal 'custom-foo', arguments[:index]
|
689
|
-
true
|
690
|
-
end
|
691
|
-
DummyIndexingModelForRecreate.expects(:index_exists?).with do |arguments|
|
692
|
-
assert_equal 'custom-foo', arguments[:index]
|
693
|
-
true
|
694
|
-
end
|
695
|
-
|
696
|
-
DummyIndexingModelForRecreate.create_index! index: 'custom-foo'
|
697
|
-
end
|
698
|
-
|
699
|
-
should "delete the custom index" do
|
700
|
-
@indices.expects(:delete).with do |arguments|
|
701
|
-
assert_equal 'custom-foo', arguments[:index]
|
702
|
-
true
|
703
|
-
end
|
704
|
-
|
705
|
-
DummyIndexingModelForRecreate.delete_index! index: 'custom-foo'
|
706
|
-
end
|
707
|
-
|
708
|
-
should "refresh the custom index" do
|
709
|
-
@indices.expects(:refresh).with do |arguments|
|
710
|
-
assert_equal 'custom-foo', arguments[:index]
|
711
|
-
true
|
712
|
-
end
|
713
|
-
|
714
|
-
DummyIndexingModelForRecreate.refresh_index! index: 'custom-foo'
|
715
|
-
end
|
716
|
-
end
|
717
|
-
end
|
718
|
-
|
719
|
-
end
|
720
|
-
end
|