elasticsearch-model 0.1.9 → 7.1.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/.gitignore +3 -2
- data/Gemfile +22 -0
- data/LICENSE.txt +199 -10
- data/README.md +96 -43
- data/Rakefile +49 -35
- data/elasticsearch-model.gemspec +53 -41
- data/examples/activerecord_article.rb +18 -1
- data/examples/activerecord_associations.rb +86 -17
- data/examples/activerecord_custom_analyzer.rb +152 -0
- data/examples/activerecord_mapping_completion.rb +33 -16
- data/examples/activerecord_mapping_edge_ngram.rb +118 -0
- data/examples/couchbase_article.rb +17 -0
- data/examples/datamapper_article.rb +28 -1
- data/examples/mongoid_article.rb +17 -0
- data/examples/ohm_article.rb +17 -0
- data/examples/riak_article.rb +17 -0
- data/gemfiles/3.0.gemfile +23 -1
- data/gemfiles/4.0.gemfile +25 -1
- data/gemfiles/5.0.gemfile +35 -0
- data/gemfiles/6.0.gemfile +36 -0
- data/lib/elasticsearch/model/adapter.rb +17 -0
- data/lib/elasticsearch/model/adapters/active_record.rb +31 -27
- data/lib/elasticsearch/model/adapters/default.rb +17 -0
- data/lib/elasticsearch/model/adapters/mongoid.rb +27 -3
- data/lib/elasticsearch/model/adapters/multiple.rb +29 -4
- data/lib/elasticsearch/model/callbacks.rb +17 -0
- data/lib/elasticsearch/model/client.rb +17 -0
- data/lib/elasticsearch/model/ext/active_record.rb +17 -0
- data/lib/elasticsearch/model/hash_wrapper.rb +32 -0
- data/lib/elasticsearch/model/importing.rb +61 -17
- data/lib/elasticsearch/model/indexing.rb +87 -49
- data/lib/elasticsearch/model/multimodel.rb +17 -0
- data/lib/elasticsearch/model/naming.rb +33 -2
- data/lib/elasticsearch/model/proxy.rb +61 -18
- data/lib/elasticsearch/model/response/aggregations.rb +55 -0
- data/lib/elasticsearch/model/response/base.rb +25 -3
- data/lib/elasticsearch/model/response/pagination/kaminari.rb +126 -0
- data/lib/elasticsearch/model/response/pagination/will_paginate.rb +112 -0
- data/lib/elasticsearch/model/response/pagination.rb +19 -192
- data/lib/elasticsearch/model/response/records.rb +17 -1
- data/lib/elasticsearch/model/response/result.rb +19 -2
- data/lib/elasticsearch/model/response/results.rb +17 -0
- data/lib/elasticsearch/model/response/suggestions.rb +20 -1
- data/lib/elasticsearch/model/response.rb +28 -10
- data/lib/elasticsearch/model/searching.rb +17 -0
- data/lib/elasticsearch/model/serializing.rb +17 -0
- data/lib/elasticsearch/model/version.rb +18 -1
- data/lib/elasticsearch/model.rb +36 -39
- data/spec/elasticsearch/model/adapter_spec.rb +136 -0
- data/spec/elasticsearch/model/adapters/active_record/associations_spec.rb +351 -0
- data/spec/elasticsearch/model/adapters/active_record/basic_spec.rb +395 -0
- data/spec/elasticsearch/model/adapters/active_record/dynamic_index_name_spec.rb +35 -0
- data/spec/elasticsearch/model/adapters/active_record/import_spec.rb +193 -0
- data/spec/elasticsearch/model/adapters/active_record/multi_model_spec.rb +127 -0
- data/spec/elasticsearch/model/adapters/active_record/namespaced_model_spec.rb +55 -0
- data/spec/elasticsearch/model/adapters/active_record/pagination_spec.rb +332 -0
- data/spec/elasticsearch/model/adapters/active_record/parent_child_spec.rb +92 -0
- data/spec/elasticsearch/model/adapters/active_record/serialization_spec.rb +78 -0
- data/spec/elasticsearch/model/adapters/active_record_spec.rb +224 -0
- data/spec/elasticsearch/model/adapters/default_spec.rb +58 -0
- data/spec/elasticsearch/model/adapters/mongoid/basic_spec.rb +284 -0
- data/spec/elasticsearch/model/adapters/mongoid/multi_model_spec.rb +83 -0
- data/spec/elasticsearch/model/adapters/mongoid_spec.rb +252 -0
- data/spec/elasticsearch/model/adapters/multiple_spec.rb +142 -0
- data/spec/elasticsearch/model/callbacks_spec.rb +50 -0
- data/spec/elasticsearch/model/client_spec.rb +83 -0
- data/spec/elasticsearch/model/hash_wrapper_spec.rb +29 -0
- data/spec/elasticsearch/model/importing_spec.rb +243 -0
- data/spec/elasticsearch/model/indexing_spec.rb +1014 -0
- data/spec/elasticsearch/model/module_spec.rb +94 -0
- data/spec/elasticsearch/model/multimodel_spec.rb +72 -0
- data/spec/elasticsearch/model/naming_spec.rb +203 -0
- data/spec/elasticsearch/model/proxy_spec.rb +124 -0
- data/spec/elasticsearch/model/response/aggregations_spec.rb +83 -0
- data/spec/elasticsearch/model/response/base_spec.rb +107 -0
- data/spec/elasticsearch/model/response/pagination/kaminari_spec.rb +472 -0
- data/spec/elasticsearch/model/response/pagination/will_paginate_spec.rb +279 -0
- data/spec/elasticsearch/model/response/records_spec.rb +135 -0
- data/spec/elasticsearch/model/response/response_spec.rb +148 -0
- data/spec/elasticsearch/model/response/result_spec.rb +139 -0
- data/spec/elasticsearch/model/response/results_spec.rb +73 -0
- data/spec/elasticsearch/model/searching_search_request_spec.rb +129 -0
- data/spec/elasticsearch/model/searching_spec.rb +66 -0
- data/spec/elasticsearch/model/serializing_spec.rb +39 -0
- data/spec/spec_helper.rb +193 -0
- data/spec/support/app/answer.rb +50 -0
- data/spec/support/app/article.rb +39 -0
- data/spec/support/app/article_for_pagination.rb +29 -0
- data/spec/support/app/article_no_type.rb +37 -0
- data/spec/support/app/article_with_custom_serialization.rb +30 -0
- data/spec/support/app/article_with_dynamic_index_name.rb +32 -0
- data/spec/support/app/author.rb +26 -0
- data/spec/support/app/authorship.rb +21 -0
- data/spec/support/app/category.rb +20 -0
- data/spec/support/app/comment.rb +20 -0
- data/spec/support/app/episode.rb +28 -0
- data/spec/support/app/image.rb +36 -0
- data/spec/support/app/import_article.rb +29 -0
- data/spec/support/app/mongoid_article.rb +38 -0
- data/spec/support/app/namespaced_book.rb +27 -0
- data/spec/support/app/parent_and_child_searchable.rb +41 -0
- data/spec/support/app/post.rb +31 -0
- data/spec/support/app/question.rb +44 -0
- data/spec/support/app/searchable.rb +65 -0
- data/spec/support/app/series.rb +28 -0
- data/spec/support/app.rb +46 -0
- data/spec/support/model.json +1 -0
- data/{test → spec}/support/model.yml +0 -0
- metadata +175 -121
- data/test/integration/active_record_associations_parent_child.rb +0 -139
- data/test/integration/active_record_associations_test.rb +0 -326
- data/test/integration/active_record_basic_test.rb +0 -234
- data/test/integration/active_record_custom_serialization_test.rb +0 -62
- data/test/integration/active_record_import_test.rb +0 -109
- data/test/integration/active_record_namespaced_model_test.rb +0 -49
- data/test/integration/active_record_pagination_test.rb +0 -145
- data/test/integration/dynamic_index_name_test.rb +0 -47
- data/test/integration/mongoid_basic_test.rb +0 -177
- data/test/integration/multiple_models_test.rb +0 -172
- data/test/support/model.json +0 -1
- data/test/test_helper.rb +0 -93
- 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/importing_test.rb +0 -203
- data/test/unit/indexing_test.rb +0 -650
- data/test/unit/module_test.rb +0 -57
- data/test/unit/multimodel_test.rb +0 -38
- data/test/unit/naming_test.rb +0 -103
- data/test/unit/proxy_test.rb +0 -100
- 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
@@ -0,0 +1,243 @@
|
|
1
|
+
# Licensed to Elasticsearch B.V. under one or more contributor
|
2
|
+
# license agreements. See the NOTICE file distributed with
|
3
|
+
# this work for additional information regarding copyright
|
4
|
+
# ownership. Elasticsearch B.V. licenses this file to you under
|
5
|
+
# the Apache License, Version 2.0 (the "License"); you may
|
6
|
+
# not use this file except in compliance with the License.
|
7
|
+
# You may obtain a copy of the License at
|
8
|
+
#
|
9
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
10
|
+
#
|
11
|
+
# Unless required by applicable law or agreed to in writing,
|
12
|
+
# software distributed under the License is distributed on an
|
13
|
+
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
14
|
+
# KIND, either express or implied. See the License for the
|
15
|
+
# specific language governing permissions and limitations
|
16
|
+
# under the License.
|
17
|
+
|
18
|
+
require 'spec_helper'
|
19
|
+
|
20
|
+
describe Elasticsearch::Model::Importing do
|
21
|
+
|
22
|
+
before(:all) do
|
23
|
+
class DummyImportingModel
|
24
|
+
end
|
25
|
+
|
26
|
+
module DummyImportingAdapter
|
27
|
+
module ImportingMixin
|
28
|
+
def __find_in_batches(options={}, &block)
|
29
|
+
yield if block_given?
|
30
|
+
end
|
31
|
+
def __transform
|
32
|
+
lambda {|a|}
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
def importing_mixin
|
37
|
+
ImportingMixin
|
38
|
+
end; module_function :importing_mixin
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
after(:all) do
|
43
|
+
remove_classes(DummyImportingModel, DummyImportingAdapter)
|
44
|
+
end
|
45
|
+
|
46
|
+
before do
|
47
|
+
allow(Elasticsearch::Model::Adapter).to receive(:from_class).with(DummyImportingModel).and_return(DummyImportingAdapter)
|
48
|
+
DummyImportingModel.__send__ :include, Elasticsearch::Model::Importing
|
49
|
+
end
|
50
|
+
|
51
|
+
context 'when a model includes the Importing module' do
|
52
|
+
|
53
|
+
it 'provides importing methods' do
|
54
|
+
expect(DummyImportingModel.respond_to?(:import)).to be(true)
|
55
|
+
expect(DummyImportingModel.respond_to?(:__find_in_batches)).to be(true)
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
describe '#import' do
|
60
|
+
|
61
|
+
before do
|
62
|
+
allow(DummyImportingModel).to receive(:index_name).and_return('foo')
|
63
|
+
allow(DummyImportingModel).to receive(:document_type).and_return('foo')
|
64
|
+
allow(DummyImportingModel).to receive(:index_exists?).and_return(true)
|
65
|
+
allow(DummyImportingModel).to receive(:__batch_to_bulk)
|
66
|
+
allow(client).to receive(:bulk).and_return(response)
|
67
|
+
end
|
68
|
+
|
69
|
+
let(:client) do
|
70
|
+
double('client')
|
71
|
+
end
|
72
|
+
|
73
|
+
let(:response) do
|
74
|
+
{ 'items' => [] }
|
75
|
+
end
|
76
|
+
|
77
|
+
context 'when no options are provided' do
|
78
|
+
|
79
|
+
before do
|
80
|
+
expect(DummyImportingModel).to receive(:client).and_return(client)
|
81
|
+
allow(DummyImportingModel).to receive(:index_exists?).and_return(true)
|
82
|
+
end
|
83
|
+
|
84
|
+
it 'uses the client to import documents' do
|
85
|
+
expect(DummyImportingModel.import).to eq(0)
|
86
|
+
end
|
87
|
+
end
|
88
|
+
|
89
|
+
context 'when there is an error' do
|
90
|
+
|
91
|
+
before do
|
92
|
+
expect(DummyImportingModel).to receive(:client).and_return(client)
|
93
|
+
allow(DummyImportingModel).to receive(:index_exists?).and_return(true)
|
94
|
+
end
|
95
|
+
|
96
|
+
let(:response) do
|
97
|
+
{ 'items' => [{ 'index' => { } }, { 'index' => { 'error' => 'FAILED' } }] }
|
98
|
+
end
|
99
|
+
|
100
|
+
it 'returns the number of errors' do
|
101
|
+
expect(DummyImportingModel.import).to eq(1)
|
102
|
+
end
|
103
|
+
|
104
|
+
context 'when the method is called with the option to return the errors' do
|
105
|
+
|
106
|
+
it 'returns the errors' do
|
107
|
+
expect(DummyImportingModel.import(return: 'errors')).to eq([{ 'index' => { 'error' => 'FAILED' } }])
|
108
|
+
end
|
109
|
+
end
|
110
|
+
|
111
|
+
context 'when the method is called with a block' do
|
112
|
+
|
113
|
+
it 'yields the response to the block' do
|
114
|
+
DummyImportingModel.import do |response|
|
115
|
+
expect(response['items'].size).to eq(2)
|
116
|
+
end
|
117
|
+
end
|
118
|
+
end
|
119
|
+
end
|
120
|
+
|
121
|
+
context 'when the index does not exist' do
|
122
|
+
|
123
|
+
before do
|
124
|
+
allow(DummyImportingModel).to receive(:index_exists?).and_return(false)
|
125
|
+
end
|
126
|
+
|
127
|
+
it 'raises an exception' do
|
128
|
+
expect {
|
129
|
+
DummyImportingModel.import
|
130
|
+
}.to raise_exception(ArgumentError)
|
131
|
+
end
|
132
|
+
end
|
133
|
+
|
134
|
+
context 'when the method is called with the force option' do
|
135
|
+
|
136
|
+
before do
|
137
|
+
expect(DummyImportingModel).to receive(:create_index!).with(force: true, index: 'foo').and_return(true)
|
138
|
+
expect(DummyImportingModel).to receive(:__find_in_batches).with(foo: 'bar').and_return(true)
|
139
|
+
end
|
140
|
+
|
141
|
+
it 'deletes and creates the index' do
|
142
|
+
expect(DummyImportingModel.import(force: true, foo: 'bar')).to eq(0)
|
143
|
+
end
|
144
|
+
end
|
145
|
+
|
146
|
+
context 'when the method is called with the refresh option' do
|
147
|
+
|
148
|
+
before do
|
149
|
+
expect(DummyImportingModel).to receive(:refresh_index!).with(index: 'foo').and_return(true)
|
150
|
+
expect(DummyImportingModel).to receive(:__find_in_batches).with(foo: 'bar').and_return(true)
|
151
|
+
end
|
152
|
+
|
153
|
+
it 'refreshes the index' do
|
154
|
+
expect(DummyImportingModel.import(refresh: true, foo: 'bar')).to eq(0)
|
155
|
+
end
|
156
|
+
end
|
157
|
+
|
158
|
+
context 'when a different index name is provided' do
|
159
|
+
|
160
|
+
before do
|
161
|
+
expect(DummyImportingModel).to receive(:client).and_return(client)
|
162
|
+
expect(client).to receive(:bulk).with(body: nil, index: 'my-new-index', type: 'foo').and_return(response)
|
163
|
+
end
|
164
|
+
|
165
|
+
it 'uses the alternate index name' do
|
166
|
+
expect(DummyImportingModel.import(index: 'my-new-index')).to eq(0)
|
167
|
+
end
|
168
|
+
end
|
169
|
+
|
170
|
+
context 'when a different document type is provided' do
|
171
|
+
|
172
|
+
before do
|
173
|
+
expect(DummyImportingModel).to receive(:client).and_return(client)
|
174
|
+
expect(client).to receive(:bulk).with(body: nil, index: 'foo', type: 'my-new-type').and_return(response)
|
175
|
+
end
|
176
|
+
|
177
|
+
it 'uses the alternate index name' do
|
178
|
+
expect(DummyImportingModel.import(type: 'my-new-type')).to eq(0)
|
179
|
+
end
|
180
|
+
end
|
181
|
+
|
182
|
+
context 'the transform method' do
|
183
|
+
|
184
|
+
before do
|
185
|
+
expect(DummyImportingModel).to receive(:client).and_return(client)
|
186
|
+
expect(DummyImportingModel).to receive(:__transform).and_return(transform)
|
187
|
+
expect(DummyImportingModel).to receive(:__batch_to_bulk).with(anything, transform)
|
188
|
+
end
|
189
|
+
|
190
|
+
let(:transform) do
|
191
|
+
lambda {|a|}
|
192
|
+
end
|
193
|
+
|
194
|
+
it 'applies the transform method to the results' do
|
195
|
+
expect(DummyImportingModel.import).to eq(0)
|
196
|
+
end
|
197
|
+
end
|
198
|
+
|
199
|
+
context 'when a transform is provided as an option' do
|
200
|
+
|
201
|
+
context 'when the transform option is not a lambda' do
|
202
|
+
|
203
|
+
let(:transform) do
|
204
|
+
'not_callable'
|
205
|
+
end
|
206
|
+
|
207
|
+
it 'raises an error' do
|
208
|
+
expect {
|
209
|
+
DummyImportingModel.import(transform: transform)
|
210
|
+
}.to raise_exception(ArgumentError)
|
211
|
+
end
|
212
|
+
end
|
213
|
+
|
214
|
+
context 'when the transform option is a lambda' do
|
215
|
+
|
216
|
+
before do
|
217
|
+
expect(DummyImportingModel).to receive(:client).and_return(client)
|
218
|
+
expect(DummyImportingModel).to receive(:__batch_to_bulk).with(anything, transform)
|
219
|
+
end
|
220
|
+
|
221
|
+
let(:transform) do
|
222
|
+
lambda {|a|}
|
223
|
+
end
|
224
|
+
|
225
|
+
it 'applies the transform lambda to the results' do
|
226
|
+
expect(DummyImportingModel.import(transform: transform)).to eq(0)
|
227
|
+
end
|
228
|
+
end
|
229
|
+
end
|
230
|
+
|
231
|
+
context 'when a pipeline is provided as an options' do
|
232
|
+
|
233
|
+
before do
|
234
|
+
expect(DummyImportingModel).to receive(:client).and_return(client)
|
235
|
+
expect(client).to receive(:bulk).with(body: nil, index: 'foo', type: 'foo', pipeline: 'my-pipeline').and_return(response)
|
236
|
+
end
|
237
|
+
|
238
|
+
it 'uses the pipeline option' do
|
239
|
+
expect(DummyImportingModel.import(pipeline: 'my-pipeline')).to eq(0)
|
240
|
+
end
|
241
|
+
end
|
242
|
+
end
|
243
|
+
end
|