elasticsearch-model 6.0.0 → 6.1.0
Sign up to get free protection for your applications and to get access to all the features.
- 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
@@ -1,255 +0,0 @@
|
|
1
|
-
require 'test_helper'
|
2
|
-
require 'active_record'
|
3
|
-
|
4
|
-
puts "ActiveRecord #{ActiveRecord::VERSION::STRING}", '-'*80
|
5
|
-
|
6
|
-
# Needed for ActiveRecord 3.x ?
|
7
|
-
ActiveRecord::Base.establish_connection( :adapter => 'sqlite3', :database => ":memory:" ) unless ActiveRecord::Base.connected?
|
8
|
-
|
9
|
-
::ActiveRecord::Base.raise_in_transactional_callbacks = true if ::ActiveRecord::Base.respond_to?(:raise_in_transactional_callbacks) && ::ActiveRecord::VERSION::MAJOR.to_s < '5'
|
10
|
-
|
11
|
-
module Elasticsearch
|
12
|
-
module Model
|
13
|
-
class ActiveRecordBasicIntegrationTest < Elasticsearch::Test::IntegrationTestCase
|
14
|
-
|
15
|
-
class ::Article < ActiveRecord::Base
|
16
|
-
include Elasticsearch::Model
|
17
|
-
include Elasticsearch::Model::Callbacks
|
18
|
-
|
19
|
-
document_type 'article'
|
20
|
-
|
21
|
-
settings index: { number_of_shards: 1, number_of_replicas: 0 } do
|
22
|
-
mapping do
|
23
|
-
indexes :title, type: 'text', analyzer: 'snowball'
|
24
|
-
indexes :body, type: 'text'
|
25
|
-
indexes :clicks, type: 'integer'
|
26
|
-
indexes :created_at, type: 'date'
|
27
|
-
end
|
28
|
-
end
|
29
|
-
|
30
|
-
def as_indexed_json(options = {})
|
31
|
-
attributes
|
32
|
-
.symbolize_keys
|
33
|
-
.slice(:title, :body, :clicks, :created_at)
|
34
|
-
.merge(suggest_title: title)
|
35
|
-
end
|
36
|
-
end
|
37
|
-
|
38
|
-
context "ActiveRecord basic integration" do
|
39
|
-
setup do
|
40
|
-
ActiveRecord::Schema.define(:version => 1) do
|
41
|
-
create_table :articles do |t|
|
42
|
-
t.string :title
|
43
|
-
t.string :body
|
44
|
-
t.integer :clicks, :default => 0
|
45
|
-
t.datetime :created_at, :default => 'NOW()'
|
46
|
-
end
|
47
|
-
end
|
48
|
-
|
49
|
-
Article.delete_all
|
50
|
-
Article.__elasticsearch__.create_index! force: true
|
51
|
-
|
52
|
-
::Article.create! title: 'Test', body: '', clicks: 1
|
53
|
-
::Article.create! title: 'Testing Coding', body: '', clicks: 2
|
54
|
-
::Article.create! title: 'Coding', body: '', clicks: 3
|
55
|
-
|
56
|
-
Article.__elasticsearch__.refresh_index!
|
57
|
-
end
|
58
|
-
|
59
|
-
should "index and find a document" do
|
60
|
-
response = Article.search('title:test')
|
61
|
-
|
62
|
-
assert response.any?, "Response should not be empty: #{response.to_a.inspect}"
|
63
|
-
|
64
|
-
assert_equal 2, response.results.size
|
65
|
-
assert_equal 2, response.records.size
|
66
|
-
|
67
|
-
assert_instance_of Elasticsearch::Model::Response::Result, response.results.first
|
68
|
-
assert_instance_of Article, response.records.first
|
69
|
-
|
70
|
-
assert_equal 'Test', response.results.first.title
|
71
|
-
assert_equal 'Test', response.records.first.title
|
72
|
-
end
|
73
|
-
|
74
|
-
should "provide access to result" do
|
75
|
-
response = Article.search query: { match: { title: 'test' } }, highlight: { fields: { title: {} } }
|
76
|
-
|
77
|
-
assert_equal 'Test', response.results.first.title
|
78
|
-
|
79
|
-
assert_equal true, response.results.first.title?
|
80
|
-
assert_equal false, response.results.first.boo?
|
81
|
-
|
82
|
-
assert_equal true, response.results.first.highlight?
|
83
|
-
assert_equal true, response.results.first.highlight.title?
|
84
|
-
assert_equal false, response.results.first.highlight.boo?
|
85
|
-
end
|
86
|
-
|
87
|
-
should "iterate over results" do
|
88
|
-
response = Article.search('title:test')
|
89
|
-
|
90
|
-
assert_equal ['1', '2'], response.results.map(&:_id)
|
91
|
-
assert_equal [1, 2], response.records.map(&:id)
|
92
|
-
end
|
93
|
-
|
94
|
-
should "return _id and _type as #id and #type" do
|
95
|
-
response = Article.search('title:test')
|
96
|
-
|
97
|
-
assert_equal '1', response.results.first.id
|
98
|
-
assert_equal 'article', response.results.first.type
|
99
|
-
end
|
100
|
-
|
101
|
-
should "access results from records" do
|
102
|
-
response = Article.search('title:test')
|
103
|
-
|
104
|
-
response.records.each_with_hit do |r, h|
|
105
|
-
assert_not_nil h._score
|
106
|
-
assert_not_nil h._source.title
|
107
|
-
end
|
108
|
-
end
|
109
|
-
|
110
|
-
should "preserve the search results order for records" do
|
111
|
-
response = Article.search query: { match: { title: 'code' }}, sort: { clicks: :desc }
|
112
|
-
|
113
|
-
assert_equal response.records[0].clicks, 3
|
114
|
-
assert_equal response.records[1].clicks, 2
|
115
|
-
|
116
|
-
response.records.each_with_hit do |r, h|
|
117
|
-
assert_equal h._id, r.id.to_s
|
118
|
-
end
|
119
|
-
|
120
|
-
response.records.map_with_hit do |r, h|
|
121
|
-
assert_equal h._id, r.id.to_s
|
122
|
-
end
|
123
|
-
end
|
124
|
-
|
125
|
-
should "remove document from index on destroy" do
|
126
|
-
article = Article.first
|
127
|
-
|
128
|
-
article.destroy
|
129
|
-
assert_equal 2, Article.count
|
130
|
-
|
131
|
-
Article.__elasticsearch__.refresh_index!
|
132
|
-
|
133
|
-
response = Article.search 'title:test'
|
134
|
-
|
135
|
-
assert_equal 1, response.results.size
|
136
|
-
assert_equal 1, response.records.size
|
137
|
-
end
|
138
|
-
|
139
|
-
should "index updates to the document" do
|
140
|
-
article = Article.first
|
141
|
-
|
142
|
-
article.title = 'Writing'
|
143
|
-
article.save
|
144
|
-
|
145
|
-
Article.__elasticsearch__.refresh_index!
|
146
|
-
|
147
|
-
response = Article.search 'title:write'
|
148
|
-
|
149
|
-
assert_equal 1, response.results.size
|
150
|
-
assert_equal 1, response.records.size
|
151
|
-
end
|
152
|
-
|
153
|
-
should "update specific attributes" do
|
154
|
-
article = Article.first
|
155
|
-
|
156
|
-
response = Article.search 'title:special'
|
157
|
-
|
158
|
-
assert_equal 0, response.results.size
|
159
|
-
assert_equal 0, response.records.size
|
160
|
-
|
161
|
-
article.__elasticsearch__.update_document_attributes title: 'special'
|
162
|
-
|
163
|
-
Article.__elasticsearch__.refresh_index!
|
164
|
-
|
165
|
-
response = Article.search 'title:special'
|
166
|
-
|
167
|
-
assert_equal 1, response.results.size
|
168
|
-
assert_equal 1, response.records.size
|
169
|
-
end
|
170
|
-
|
171
|
-
should "update document when save is called multiple times in a transaction" do
|
172
|
-
article = Article.first
|
173
|
-
response = Article.search 'body:dummy'
|
174
|
-
|
175
|
-
assert_equal 0, response.results.size
|
176
|
-
assert_equal 0, response.records.size
|
177
|
-
|
178
|
-
ActiveRecord::Base.transaction do
|
179
|
-
article.body = 'dummy'
|
180
|
-
article.save
|
181
|
-
|
182
|
-
article.title = 'special'
|
183
|
-
article.save
|
184
|
-
end
|
185
|
-
|
186
|
-
article.__elasticsearch__.update_document
|
187
|
-
Article.__elasticsearch__.refresh_index!
|
188
|
-
|
189
|
-
response = Article.search 'body:dummy'
|
190
|
-
assert_equal 1, response.results.size
|
191
|
-
assert_equal 1, response.records.size
|
192
|
-
end
|
193
|
-
|
194
|
-
should "return results for a DSL search" do
|
195
|
-
response = Article.search query: { match: { title: { query: 'test' } } }
|
196
|
-
|
197
|
-
assert_equal 2, response.results.size
|
198
|
-
assert_equal 2, response.records.size
|
199
|
-
end
|
200
|
-
|
201
|
-
should "return a paged collection" do
|
202
|
-
response = Article.search query: { match: { title: { query: 'test' } } },
|
203
|
-
size: 2,
|
204
|
-
from: 1
|
205
|
-
|
206
|
-
assert_equal 1, response.results.size
|
207
|
-
assert_equal 1, response.records.size
|
208
|
-
|
209
|
-
assert_equal 'Testing Coding', response.results.first.title
|
210
|
-
assert_equal 'Testing Coding', response.records.first.title
|
211
|
-
end
|
212
|
-
|
213
|
-
should "allow chaining SQL commands on response.records" do
|
214
|
-
response = Article.search query: { match: { title: { query: 'test' } } }
|
215
|
-
|
216
|
-
assert_equal 2, response.records.size
|
217
|
-
assert_equal 1, response.records.where(title: 'Test').size
|
218
|
-
assert_equal 'Test', response.records.where(title: 'Test').first.title
|
219
|
-
end
|
220
|
-
|
221
|
-
should "allow ordering response.records in SQL" do
|
222
|
-
response = Article.search query: { match: { title: { query: 'test' } } }
|
223
|
-
|
224
|
-
if defined?(::ActiveRecord) && ::ActiveRecord::VERSION::MAJOR >= 4
|
225
|
-
assert_equal 'Testing Coding', response.records.order(title: :desc).first.title
|
226
|
-
assert_equal 'Testing Coding', response.records.order(title: :desc)[0].title
|
227
|
-
else
|
228
|
-
assert_equal 'Testing Coding', response.records.order('title DESC').first.title
|
229
|
-
assert_equal 'Testing Coding', response.records.order('title DESC')[0].title
|
230
|
-
end
|
231
|
-
end
|
232
|
-
|
233
|
-
should "allow dot access to response" do
|
234
|
-
response = Article.search query: { match: { title: { query: 'test' } } },
|
235
|
-
aggregations: {
|
236
|
-
dates: { date_histogram: { field: 'created_at', interval: 'hour' } },
|
237
|
-
clicks: { global: {}, aggregations: { min: { min: { field: 'clicks' } } } }
|
238
|
-
},
|
239
|
-
suggest: { text: 'tezt', title: { term: { field: 'title', suggest_mode: 'always' } } }
|
240
|
-
|
241
|
-
response.response.respond_to?(:aggregations)
|
242
|
-
assert_equal 2, response.aggregations.dates.buckets.first.doc_count
|
243
|
-
assert_equal 3, response.aggregations.clicks.doc_count
|
244
|
-
assert_equal 1.0, response.aggregations.clicks.min.value
|
245
|
-
assert_nil response.aggregations.clicks.max
|
246
|
-
|
247
|
-
response.response.respond_to?(:suggest)
|
248
|
-
assert_equal 1, response.suggestions.title.first.options.size
|
249
|
-
assert_equal ['test'], response.suggestions.terms
|
250
|
-
end
|
251
|
-
end
|
252
|
-
|
253
|
-
end
|
254
|
-
end
|
255
|
-
end
|
@@ -1,67 +0,0 @@
|
|
1
|
-
require 'test_helper'
|
2
|
-
require 'active_record'
|
3
|
-
|
4
|
-
# Needed for ActiveRecord 3.x ?
|
5
|
-
ActiveRecord::Base.establish_connection( :adapter => 'sqlite3', :database => ":memory:" ) unless ActiveRecord::Base.connected?
|
6
|
-
|
7
|
-
::ActiveRecord::Base.raise_in_transactional_callbacks = true if ::ActiveRecord::Base.respond_to?(:raise_in_transactional_callbacks) && ::ActiveRecord::VERSION::MAJOR.to_s < '5'
|
8
|
-
|
9
|
-
module Elasticsearch
|
10
|
-
module Model
|
11
|
-
class ActiveRecordCustomSerializationTest < Elasticsearch::Test::IntegrationTestCase
|
12
|
-
context "ActiveRecord model with custom JSON serialization" do
|
13
|
-
setup do
|
14
|
-
class ::ArticleWithCustomSerialization < ActiveRecord::Base
|
15
|
-
include Elasticsearch::Model
|
16
|
-
include Elasticsearch::Model::Callbacks
|
17
|
-
|
18
|
-
mapping do
|
19
|
-
indexes :title
|
20
|
-
end
|
21
|
-
|
22
|
-
def as_indexed_json(options={})
|
23
|
-
# as_json(options.merge root: false).slice('title')
|
24
|
-
{ title: self.title }
|
25
|
-
end
|
26
|
-
end
|
27
|
-
|
28
|
-
ActiveRecord::Schema.define(:version => 1) do
|
29
|
-
create_table ArticleWithCustomSerialization.table_name do |t|
|
30
|
-
t.string :title
|
31
|
-
t.string :status
|
32
|
-
end
|
33
|
-
end
|
34
|
-
|
35
|
-
ArticleWithCustomSerialization.delete_all
|
36
|
-
ArticleWithCustomSerialization.__elasticsearch__.create_index! force: true
|
37
|
-
end
|
38
|
-
|
39
|
-
should "index only the title attribute when creating" do
|
40
|
-
ArticleWithCustomSerialization.create! title: 'Test', status: 'green'
|
41
|
-
|
42
|
-
a = ArticleWithCustomSerialization.__elasticsearch__.client.get \
|
43
|
-
index: 'article_with_custom_serializations',
|
44
|
-
type: '_doc',
|
45
|
-
id: '1'
|
46
|
-
|
47
|
-
assert_equal( { 'title' => 'Test' }, a['_source'] )
|
48
|
-
end
|
49
|
-
|
50
|
-
should "index only the title attribute when updating" do
|
51
|
-
ArticleWithCustomSerialization.create! title: 'Test', status: 'green'
|
52
|
-
|
53
|
-
article = ArticleWithCustomSerialization.first
|
54
|
-
article.update_attributes title: 'UPDATED', status: 'red'
|
55
|
-
|
56
|
-
a = ArticleWithCustomSerialization.__elasticsearch__.client.get \
|
57
|
-
index: 'article_with_custom_serializations',
|
58
|
-
type: '_doc',
|
59
|
-
id: '1'
|
60
|
-
|
61
|
-
assert_equal( { 'title' => 'UPDATED' }, a['_source'] )
|
62
|
-
end
|
63
|
-
end
|
64
|
-
|
65
|
-
end
|
66
|
-
end
|
67
|
-
end
|
@@ -1,168 +0,0 @@
|
|
1
|
-
require 'test_helper'
|
2
|
-
require 'active_record'
|
3
|
-
|
4
|
-
# Needed for ActiveRecord 3.x ?
|
5
|
-
ActiveRecord::Base.establish_connection( :adapter => 'sqlite3', :database => ":memory:" ) unless ActiveRecord::Base.connected?
|
6
|
-
|
7
|
-
::ActiveRecord::Base.raise_in_transactional_callbacks = true if ::ActiveRecord::Base.respond_to?(:raise_in_transactional_callbacks) && ::ActiveRecord::VERSION::MAJOR.to_s < '5'
|
8
|
-
|
9
|
-
module Elasticsearch
|
10
|
-
module Model
|
11
|
-
class ActiveRecordImportIntegrationTest < Elasticsearch::Test::IntegrationTestCase
|
12
|
-
|
13
|
-
context "ActiveRecord importing" do
|
14
|
-
setup do
|
15
|
-
Object.send(:remove_const, :ImportArticle) if defined?(ImportArticle)
|
16
|
-
class ::ImportArticle < ActiveRecord::Base
|
17
|
-
include Elasticsearch::Model
|
18
|
-
|
19
|
-
scope :popular, -> { where('views >= 50') }
|
20
|
-
|
21
|
-
mapping do
|
22
|
-
indexes :title, type: 'text'
|
23
|
-
indexes :views, type: 'integer'
|
24
|
-
indexes :numeric, type: 'integer'
|
25
|
-
indexes :created_at, type: 'date'
|
26
|
-
end
|
27
|
-
end
|
28
|
-
|
29
|
-
ActiveRecord::Schema.define(:version => 1) do
|
30
|
-
create_table :import_articles do |t|
|
31
|
-
t.string :title
|
32
|
-
t.integer :views
|
33
|
-
t.string :numeric # For the sake of invalid data sent to Elasticsearch
|
34
|
-
t.datetime :created_at, :default => 'NOW()'
|
35
|
-
end
|
36
|
-
end
|
37
|
-
|
38
|
-
ImportArticle.delete_all
|
39
|
-
ImportArticle.__elasticsearch__.create_index! force: true
|
40
|
-
ImportArticle.__elasticsearch__.client.cluster.health wait_for_status: 'yellow'
|
41
|
-
|
42
|
-
100.times { |i| ImportArticle.create! title: "Test #{i}", views: i }
|
43
|
-
end
|
44
|
-
|
45
|
-
should "import all the documents" do
|
46
|
-
assert_equal 100, ImportArticle.count
|
47
|
-
|
48
|
-
ImportArticle.__elasticsearch__.refresh_index!
|
49
|
-
assert_equal 0, ImportArticle.search('*').results.total
|
50
|
-
|
51
|
-
batches = 0
|
52
|
-
errors = ImportArticle.import(batch_size: 10) do |response|
|
53
|
-
batches += 1
|
54
|
-
end
|
55
|
-
|
56
|
-
assert_equal 0, errors
|
57
|
-
assert_equal 10, batches
|
58
|
-
|
59
|
-
ImportArticle.__elasticsearch__.refresh_index!
|
60
|
-
assert_equal 100, ImportArticle.search('*').results.total
|
61
|
-
end
|
62
|
-
|
63
|
-
should "import only documents from a specific scope" do
|
64
|
-
assert_equal 100, ImportArticle.count
|
65
|
-
|
66
|
-
assert_equal 0, ImportArticle.import(scope: 'popular')
|
67
|
-
|
68
|
-
ImportArticle.__elasticsearch__.refresh_index!
|
69
|
-
assert_equal 50, ImportArticle.search('*').results.total
|
70
|
-
end
|
71
|
-
|
72
|
-
should "import only documents from a specific query" do
|
73
|
-
assert_equal 100, ImportArticle.count
|
74
|
-
|
75
|
-
assert_equal 0, ImportArticle.import(query: -> { where('views >= 30') })
|
76
|
-
|
77
|
-
ImportArticle.__elasticsearch__.refresh_index!
|
78
|
-
assert_equal 70, ImportArticle.search('*').results.total
|
79
|
-
end
|
80
|
-
|
81
|
-
should "report and not store/index invalid documents" do
|
82
|
-
ImportArticle.create! title: "Test INVALID", numeric: "INVALID"
|
83
|
-
|
84
|
-
assert_equal 101, ImportArticle.count
|
85
|
-
|
86
|
-
ImportArticle.__elasticsearch__.refresh_index!
|
87
|
-
assert_equal 0, ImportArticle.search('*').results.total
|
88
|
-
|
89
|
-
batches = 0
|
90
|
-
errors = ImportArticle.__elasticsearch__.import(batch_size: 10) do |response|
|
91
|
-
batches += 1
|
92
|
-
end
|
93
|
-
|
94
|
-
assert_equal 1, errors
|
95
|
-
assert_equal 11, batches
|
96
|
-
|
97
|
-
ImportArticle.__elasticsearch__.refresh_index!
|
98
|
-
assert_equal 100, ImportArticle.search('*').results.total
|
99
|
-
end
|
100
|
-
|
101
|
-
should "transform documents with the option" do
|
102
|
-
assert_equal 100, ImportArticle.count
|
103
|
-
|
104
|
-
assert_equal 0, ImportArticle.import( transform: ->(a) {{ index: { data: { name: a.title, foo: 'BAR' } }}} )
|
105
|
-
|
106
|
-
ImportArticle.__elasticsearch__.refresh_index!
|
107
|
-
assert_contains ImportArticle.search('*').results.first._source.keys, 'name'
|
108
|
-
assert_contains ImportArticle.search('*').results.first._source.keys, 'foo'
|
109
|
-
assert_equal 100, ImportArticle.search('test').results.total
|
110
|
-
assert_equal 100, ImportArticle.search('bar').results.total
|
111
|
-
end
|
112
|
-
end
|
113
|
-
|
114
|
-
context "ActiveRecord importing when the model has a default scope" do
|
115
|
-
|
116
|
-
setup do
|
117
|
-
Object.send(:remove_const, :ImportArticle) if defined?(ImportArticle)
|
118
|
-
class ::ImportArticle < ActiveRecord::Base
|
119
|
-
include Elasticsearch::Model
|
120
|
-
|
121
|
-
default_scope { where('views >= 8') }
|
122
|
-
|
123
|
-
mapping do
|
124
|
-
indexes :title, type: 'text'
|
125
|
-
indexes :views, type: 'integer'
|
126
|
-
indexes :numeric, type: 'integer'
|
127
|
-
indexes :created_at, type: 'date'
|
128
|
-
end
|
129
|
-
end
|
130
|
-
|
131
|
-
ActiveRecord::Schema.define(:version => 1) do
|
132
|
-
create_table :import_articles do |t|
|
133
|
-
t.string :title
|
134
|
-
t.integer :views
|
135
|
-
t.string :numeric # For the sake of invalid data sent to Elasticsearch
|
136
|
-
t.datetime :created_at, :default => 'NOW()'
|
137
|
-
end
|
138
|
-
end
|
139
|
-
|
140
|
-
ImportArticle.delete_all
|
141
|
-
ImportArticle.__elasticsearch__.delete_index! force: true
|
142
|
-
ImportArticle.__elasticsearch__.create_index! force: true
|
143
|
-
ImportArticle.__elasticsearch__.client.cluster.health wait_for_status: 'yellow'
|
144
|
-
|
145
|
-
10.times { |i| ImportArticle.create! title: "Test #{i}", views: i }
|
146
|
-
end
|
147
|
-
|
148
|
-
should "import only documents from the default scope" do
|
149
|
-
assert_equal 2, ImportArticle.count
|
150
|
-
|
151
|
-
assert_equal 0, ImportArticle.import
|
152
|
-
|
153
|
-
ImportArticle.__elasticsearch__.refresh_index!
|
154
|
-
assert_equal 2, ImportArticle.search('*').results.total
|
155
|
-
end
|
156
|
-
|
157
|
-
should "import only documents from a specific query combined with the default scope" do
|
158
|
-
assert_equal 2, ImportArticle.count
|
159
|
-
|
160
|
-
assert_equal 0, ImportArticle.import(query: -> { where("title = 'Test 9'") })
|
161
|
-
|
162
|
-
ImportArticle.__elasticsearch__.refresh_index!
|
163
|
-
assert_equal 1, ImportArticle.search('*').results.total
|
164
|
-
end
|
165
|
-
end
|
166
|
-
end
|
167
|
-
end
|
168
|
-
end
|