elasticsearch-model 5.1.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 +18 -11
- data/Rakefile +27 -36
- data/elasticsearch-model.gemspec +5 -5
- data/examples/activerecord_custom_analyzer.rb +135 -0
- data/examples/activerecord_mapping_completion.rb +3 -18
- data/examples/datamapper_article.rb +11 -1
- data/gemfiles/3.0.gemfile +6 -1
- data/gemfiles/4.0.gemfile +7 -1
- data/gemfiles/5.0.gemfile +7 -1
- data/lib/elasticsearch/model/adapters/active_record.rb +7 -23
- data/lib/elasticsearch/model/adapters/mongoid.rb +10 -3
- data/lib/elasticsearch/model/importing.rb +1 -1
- data/lib/elasticsearch/model/indexing.rb +6 -4
- data/lib/elasticsearch/model/naming.rb +9 -2
- data/lib/elasticsearch/model/response/aggregations.rb +1 -1
- data/lib/elasticsearch/model/response/base.rb +3 -2
- 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/pagination.rb +2 -192
- data/lib/elasticsearch/model/response/result.rb +1 -1
- data/lib/elasticsearch/model/response/suggestions.rb +1 -1
- data/lib/elasticsearch/model/response.rb +11 -10
- data/lib/elasticsearch/model/version.rb +1 -1
- data/lib/elasticsearch/model.rb +15 -8
- 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/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/app.rb +21 -0
- data/spec/support/model.json +1 -0
- metadata +134 -90
- data/test/integration/active_record_associations_parent_child_test.rb +0 -147
- data/test/integration/active_record_associations_test.rb +0 -339
- data/test/integration/active_record_basic_test.rb +0 -251
- data/test/integration/active_record_custom_serialization_test.rb +0 -67
- data/test/integration/active_record_import_test.rb +0 -115
- data/test/integration/active_record_namespaced_model_test.rb +0 -54
- 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 -176
- 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 -104
- 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 -203
- data/test/unit/indexing_test.rb +0 -687
- 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 -31
- 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 → spec}/support/model.yml +0 -0
|
@@ -0,0 +1,214 @@
|
|
|
1
|
+
require 'spec_helper'
|
|
2
|
+
|
|
3
|
+
describe Elasticsearch::Model::Importing do
|
|
4
|
+
|
|
5
|
+
before(:all) do
|
|
6
|
+
class DummyImportingModel
|
|
7
|
+
end
|
|
8
|
+
|
|
9
|
+
module DummyImportingAdapter
|
|
10
|
+
module ImportingMixin
|
|
11
|
+
def __find_in_batches(options={}, &block)
|
|
12
|
+
yield if block_given?
|
|
13
|
+
end
|
|
14
|
+
def __transform
|
|
15
|
+
lambda {|a|}
|
|
16
|
+
end
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
def importing_mixin
|
|
20
|
+
ImportingMixin
|
|
21
|
+
end; module_function :importing_mixin
|
|
22
|
+
end
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
after(:all) do
|
|
26
|
+
remove_classes(DummyImportingModel, DummyImportingAdapter)
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
before do
|
|
30
|
+
allow(Elasticsearch::Model::Adapter).to receive(:from_class).with(DummyImportingModel).and_return(DummyImportingAdapter)
|
|
31
|
+
DummyImportingModel.__send__ :include, Elasticsearch::Model::Importing
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
context 'when a model includes the Importing module' do
|
|
35
|
+
|
|
36
|
+
it 'provides importing methods' do
|
|
37
|
+
expect(DummyImportingModel.respond_to?(:import)).to be(true)
|
|
38
|
+
expect(DummyImportingModel.respond_to?(:__find_in_batches)).to be(true)
|
|
39
|
+
end
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
describe '#import' do
|
|
43
|
+
|
|
44
|
+
before do
|
|
45
|
+
allow(DummyImportingModel).to receive(:index_name).and_return('foo')
|
|
46
|
+
allow(DummyImportingModel).to receive(:document_type).and_return('foo')
|
|
47
|
+
allow(DummyImportingModel).to receive(:index_exists?).and_return(true)
|
|
48
|
+
allow(DummyImportingModel).to receive(:__batch_to_bulk)
|
|
49
|
+
allow(client).to receive(:bulk).and_return(response)
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
let(:client) do
|
|
53
|
+
double('client')
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
let(:response) do
|
|
57
|
+
{ 'items' => [] }
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
context 'when no options are provided' do
|
|
61
|
+
|
|
62
|
+
before do
|
|
63
|
+
expect(DummyImportingModel).to receive(:client).and_return(client)
|
|
64
|
+
allow(DummyImportingModel).to receive(:index_exists?).and_return(true)
|
|
65
|
+
end
|
|
66
|
+
|
|
67
|
+
it 'uses the client to import documents' do
|
|
68
|
+
expect(DummyImportingModel.import).to eq(0)
|
|
69
|
+
end
|
|
70
|
+
end
|
|
71
|
+
|
|
72
|
+
context 'when there is an error' do
|
|
73
|
+
|
|
74
|
+
before do
|
|
75
|
+
expect(DummyImportingModel).to receive(:client).and_return(client)
|
|
76
|
+
allow(DummyImportingModel).to receive(:index_exists?).and_return(true)
|
|
77
|
+
end
|
|
78
|
+
|
|
79
|
+
let(:response) do
|
|
80
|
+
{ 'items' => [{ 'index' => { } }, { 'index' => { 'error' => 'FAILED' } }] }
|
|
81
|
+
end
|
|
82
|
+
|
|
83
|
+
it 'returns the number of errors' do
|
|
84
|
+
expect(DummyImportingModel.import).to eq(1)
|
|
85
|
+
end
|
|
86
|
+
|
|
87
|
+
context 'when the method is called with the option to return the errors' do
|
|
88
|
+
|
|
89
|
+
it 'returns the errors' do
|
|
90
|
+
expect(DummyImportingModel.import(return: 'errors')).to eq([{ 'index' => { 'error' => 'FAILED' } }])
|
|
91
|
+
end
|
|
92
|
+
end
|
|
93
|
+
|
|
94
|
+
context 'when the method is called with a block' do
|
|
95
|
+
|
|
96
|
+
it 'yields the response to the block' do
|
|
97
|
+
DummyImportingModel.import do |response|
|
|
98
|
+
expect(response['items'].size).to eq(2)
|
|
99
|
+
end
|
|
100
|
+
end
|
|
101
|
+
end
|
|
102
|
+
end
|
|
103
|
+
|
|
104
|
+
context 'when the index does not exist' do
|
|
105
|
+
|
|
106
|
+
before do
|
|
107
|
+
allow(DummyImportingModel).to receive(:index_exists?).and_return(false)
|
|
108
|
+
end
|
|
109
|
+
|
|
110
|
+
it 'raises an exception' do
|
|
111
|
+
expect {
|
|
112
|
+
DummyImportingModel.import
|
|
113
|
+
}.to raise_exception(ArgumentError)
|
|
114
|
+
end
|
|
115
|
+
end
|
|
116
|
+
|
|
117
|
+
context 'when the method is called with the force option' do
|
|
118
|
+
|
|
119
|
+
before do
|
|
120
|
+
expect(DummyImportingModel).to receive(:create_index!).with(force: true, index: 'foo').and_return(true)
|
|
121
|
+
expect(DummyImportingModel).to receive(:__find_in_batches).with(foo: 'bar').and_return(true)
|
|
122
|
+
end
|
|
123
|
+
|
|
124
|
+
it 'deletes and creates the index' do
|
|
125
|
+
expect(DummyImportingModel.import(force: true, foo: 'bar')).to eq(0)
|
|
126
|
+
end
|
|
127
|
+
end
|
|
128
|
+
|
|
129
|
+
context 'when the method is called with the refresh option' do
|
|
130
|
+
|
|
131
|
+
before do
|
|
132
|
+
expect(DummyImportingModel).to receive(:refresh_index!).with(index: 'foo').and_return(true)
|
|
133
|
+
expect(DummyImportingModel).to receive(:__find_in_batches).with(foo: 'bar').and_return(true)
|
|
134
|
+
end
|
|
135
|
+
|
|
136
|
+
it 'refreshes the index' do
|
|
137
|
+
expect(DummyImportingModel.import(refresh: true, foo: 'bar')).to eq(0)
|
|
138
|
+
end
|
|
139
|
+
end
|
|
140
|
+
|
|
141
|
+
context 'when a different index name is provided' do
|
|
142
|
+
|
|
143
|
+
before do
|
|
144
|
+
expect(DummyImportingModel).to receive(:client).and_return(client)
|
|
145
|
+
expect(client).to receive(:bulk).with(body: nil, index: 'my-new-index', type: 'foo').and_return(response)
|
|
146
|
+
end
|
|
147
|
+
|
|
148
|
+
it 'uses the alternate index name' do
|
|
149
|
+
expect(DummyImportingModel.import(index: 'my-new-index')).to eq(0)
|
|
150
|
+
end
|
|
151
|
+
end
|
|
152
|
+
|
|
153
|
+
context 'when a different document type is provided' do
|
|
154
|
+
|
|
155
|
+
before do
|
|
156
|
+
expect(DummyImportingModel).to receive(:client).and_return(client)
|
|
157
|
+
expect(client).to receive(:bulk).with(body: nil, index: 'foo', type: 'my-new-type').and_return(response)
|
|
158
|
+
end
|
|
159
|
+
|
|
160
|
+
it 'uses the alternate index name' do
|
|
161
|
+
expect(DummyImportingModel.import(type: 'my-new-type')).to eq(0)
|
|
162
|
+
end
|
|
163
|
+
end
|
|
164
|
+
|
|
165
|
+
context 'the transform method' do
|
|
166
|
+
|
|
167
|
+
before do
|
|
168
|
+
expect(DummyImportingModel).to receive(:client).and_return(client)
|
|
169
|
+
expect(DummyImportingModel).to receive(:__transform).and_return(transform)
|
|
170
|
+
expect(DummyImportingModel).to receive(:__batch_to_bulk).with(anything, transform)
|
|
171
|
+
end
|
|
172
|
+
|
|
173
|
+
let(:transform) do
|
|
174
|
+
lambda {|a|}
|
|
175
|
+
end
|
|
176
|
+
|
|
177
|
+
it 'applies the transform method to the results' do
|
|
178
|
+
expect(DummyImportingModel.import).to eq(0)
|
|
179
|
+
end
|
|
180
|
+
end
|
|
181
|
+
|
|
182
|
+
context 'when a transform is provided as an option' do
|
|
183
|
+
|
|
184
|
+
context 'when the transform option is not a lambda' do
|
|
185
|
+
|
|
186
|
+
let(:transform) do
|
|
187
|
+
'not_callable'
|
|
188
|
+
end
|
|
189
|
+
|
|
190
|
+
it 'raises an error' do
|
|
191
|
+
expect {
|
|
192
|
+
DummyImportingModel.import(transform: transform)
|
|
193
|
+
}.to raise_exception(ArgumentError)
|
|
194
|
+
end
|
|
195
|
+
end
|
|
196
|
+
|
|
197
|
+
context 'when the transform option is a lambda' do
|
|
198
|
+
|
|
199
|
+
before do
|
|
200
|
+
expect(DummyImportingModel).to receive(:client).and_return(client)
|
|
201
|
+
expect(DummyImportingModel).to receive(:__batch_to_bulk).with(anything, transform)
|
|
202
|
+
end
|
|
203
|
+
|
|
204
|
+
let(:transform) do
|
|
205
|
+
lambda {|a|}
|
|
206
|
+
end
|
|
207
|
+
|
|
208
|
+
it 'applies the transform lambda to the results' do
|
|
209
|
+
expect(DummyImportingModel.import(transform: transform)).to eq(0)
|
|
210
|
+
end
|
|
211
|
+
end
|
|
212
|
+
end
|
|
213
|
+
end
|
|
214
|
+
end
|