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.
Files changed (145) hide show
  1. checksums.yaml +5 -5
  2. data/.gitignore +3 -2
  3. data/Gemfile +22 -0
  4. data/LICENSE.txt +199 -10
  5. data/README.md +96 -43
  6. data/Rakefile +49 -35
  7. data/elasticsearch-model.gemspec +53 -41
  8. data/examples/activerecord_article.rb +18 -1
  9. data/examples/activerecord_associations.rb +86 -17
  10. data/examples/activerecord_custom_analyzer.rb +152 -0
  11. data/examples/activerecord_mapping_completion.rb +33 -16
  12. data/examples/activerecord_mapping_edge_ngram.rb +118 -0
  13. data/examples/couchbase_article.rb +17 -0
  14. data/examples/datamapper_article.rb +28 -1
  15. data/examples/mongoid_article.rb +17 -0
  16. data/examples/ohm_article.rb +17 -0
  17. data/examples/riak_article.rb +17 -0
  18. data/gemfiles/3.0.gemfile +23 -1
  19. data/gemfiles/4.0.gemfile +25 -1
  20. data/gemfiles/5.0.gemfile +35 -0
  21. data/gemfiles/6.0.gemfile +36 -0
  22. data/lib/elasticsearch/model/adapter.rb +17 -0
  23. data/lib/elasticsearch/model/adapters/active_record.rb +31 -27
  24. data/lib/elasticsearch/model/adapters/default.rb +17 -0
  25. data/lib/elasticsearch/model/adapters/mongoid.rb +27 -3
  26. data/lib/elasticsearch/model/adapters/multiple.rb +29 -4
  27. data/lib/elasticsearch/model/callbacks.rb +17 -0
  28. data/lib/elasticsearch/model/client.rb +17 -0
  29. data/lib/elasticsearch/model/ext/active_record.rb +17 -0
  30. data/lib/elasticsearch/model/hash_wrapper.rb +32 -0
  31. data/lib/elasticsearch/model/importing.rb +61 -17
  32. data/lib/elasticsearch/model/indexing.rb +87 -49
  33. data/lib/elasticsearch/model/multimodel.rb +17 -0
  34. data/lib/elasticsearch/model/naming.rb +33 -2
  35. data/lib/elasticsearch/model/proxy.rb +61 -18
  36. data/lib/elasticsearch/model/response/aggregations.rb +55 -0
  37. data/lib/elasticsearch/model/response/base.rb +25 -3
  38. data/lib/elasticsearch/model/response/pagination/kaminari.rb +126 -0
  39. data/lib/elasticsearch/model/response/pagination/will_paginate.rb +112 -0
  40. data/lib/elasticsearch/model/response/pagination.rb +19 -192
  41. data/lib/elasticsearch/model/response/records.rb +17 -1
  42. data/lib/elasticsearch/model/response/result.rb +19 -2
  43. data/lib/elasticsearch/model/response/results.rb +17 -0
  44. data/lib/elasticsearch/model/response/suggestions.rb +20 -1
  45. data/lib/elasticsearch/model/response.rb +28 -10
  46. data/lib/elasticsearch/model/searching.rb +17 -0
  47. data/lib/elasticsearch/model/serializing.rb +17 -0
  48. data/lib/elasticsearch/model/version.rb +18 -1
  49. data/lib/elasticsearch/model.rb +36 -39
  50. data/spec/elasticsearch/model/adapter_spec.rb +136 -0
  51. data/spec/elasticsearch/model/adapters/active_record/associations_spec.rb +351 -0
  52. data/spec/elasticsearch/model/adapters/active_record/basic_spec.rb +395 -0
  53. data/spec/elasticsearch/model/adapters/active_record/dynamic_index_name_spec.rb +35 -0
  54. data/spec/elasticsearch/model/adapters/active_record/import_spec.rb +193 -0
  55. data/spec/elasticsearch/model/adapters/active_record/multi_model_spec.rb +127 -0
  56. data/spec/elasticsearch/model/adapters/active_record/namespaced_model_spec.rb +55 -0
  57. data/spec/elasticsearch/model/adapters/active_record/pagination_spec.rb +332 -0
  58. data/spec/elasticsearch/model/adapters/active_record/parent_child_spec.rb +92 -0
  59. data/spec/elasticsearch/model/adapters/active_record/serialization_spec.rb +78 -0
  60. data/spec/elasticsearch/model/adapters/active_record_spec.rb +224 -0
  61. data/spec/elasticsearch/model/adapters/default_spec.rb +58 -0
  62. data/spec/elasticsearch/model/adapters/mongoid/basic_spec.rb +284 -0
  63. data/spec/elasticsearch/model/adapters/mongoid/multi_model_spec.rb +83 -0
  64. data/spec/elasticsearch/model/adapters/mongoid_spec.rb +252 -0
  65. data/spec/elasticsearch/model/adapters/multiple_spec.rb +142 -0
  66. data/spec/elasticsearch/model/callbacks_spec.rb +50 -0
  67. data/spec/elasticsearch/model/client_spec.rb +83 -0
  68. data/spec/elasticsearch/model/hash_wrapper_spec.rb +29 -0
  69. data/spec/elasticsearch/model/importing_spec.rb +243 -0
  70. data/spec/elasticsearch/model/indexing_spec.rb +1014 -0
  71. data/spec/elasticsearch/model/module_spec.rb +94 -0
  72. data/spec/elasticsearch/model/multimodel_spec.rb +72 -0
  73. data/spec/elasticsearch/model/naming_spec.rb +203 -0
  74. data/spec/elasticsearch/model/proxy_spec.rb +124 -0
  75. data/spec/elasticsearch/model/response/aggregations_spec.rb +83 -0
  76. data/spec/elasticsearch/model/response/base_spec.rb +107 -0
  77. data/spec/elasticsearch/model/response/pagination/kaminari_spec.rb +472 -0
  78. data/spec/elasticsearch/model/response/pagination/will_paginate_spec.rb +279 -0
  79. data/spec/elasticsearch/model/response/records_spec.rb +135 -0
  80. data/spec/elasticsearch/model/response/response_spec.rb +148 -0
  81. data/spec/elasticsearch/model/response/result_spec.rb +139 -0
  82. data/spec/elasticsearch/model/response/results_spec.rb +73 -0
  83. data/spec/elasticsearch/model/searching_search_request_spec.rb +129 -0
  84. data/spec/elasticsearch/model/searching_spec.rb +66 -0
  85. data/spec/elasticsearch/model/serializing_spec.rb +39 -0
  86. data/spec/spec_helper.rb +193 -0
  87. data/spec/support/app/answer.rb +50 -0
  88. data/spec/support/app/article.rb +39 -0
  89. data/spec/support/app/article_for_pagination.rb +29 -0
  90. data/spec/support/app/article_no_type.rb +37 -0
  91. data/spec/support/app/article_with_custom_serialization.rb +30 -0
  92. data/spec/support/app/article_with_dynamic_index_name.rb +32 -0
  93. data/spec/support/app/author.rb +26 -0
  94. data/spec/support/app/authorship.rb +21 -0
  95. data/spec/support/app/category.rb +20 -0
  96. data/spec/support/app/comment.rb +20 -0
  97. data/spec/support/app/episode.rb +28 -0
  98. data/spec/support/app/image.rb +36 -0
  99. data/spec/support/app/import_article.rb +29 -0
  100. data/spec/support/app/mongoid_article.rb +38 -0
  101. data/spec/support/app/namespaced_book.rb +27 -0
  102. data/spec/support/app/parent_and_child_searchable.rb +41 -0
  103. data/spec/support/app/post.rb +31 -0
  104. data/spec/support/app/question.rb +44 -0
  105. data/spec/support/app/searchable.rb +65 -0
  106. data/spec/support/app/series.rb +28 -0
  107. data/spec/support/app.rb +46 -0
  108. data/spec/support/model.json +1 -0
  109. data/{test → spec}/support/model.yml +0 -0
  110. metadata +175 -121
  111. data/test/integration/active_record_associations_parent_child.rb +0 -139
  112. data/test/integration/active_record_associations_test.rb +0 -326
  113. data/test/integration/active_record_basic_test.rb +0 -234
  114. data/test/integration/active_record_custom_serialization_test.rb +0 -62
  115. data/test/integration/active_record_import_test.rb +0 -109
  116. data/test/integration/active_record_namespaced_model_test.rb +0 -49
  117. data/test/integration/active_record_pagination_test.rb +0 -145
  118. data/test/integration/dynamic_index_name_test.rb +0 -47
  119. data/test/integration/mongoid_basic_test.rb +0 -177
  120. data/test/integration/multiple_models_test.rb +0 -172
  121. data/test/support/model.json +0 -1
  122. data/test/test_helper.rb +0 -93
  123. data/test/unit/adapter_active_record_test.rb +0 -157
  124. data/test/unit/adapter_default_test.rb +0 -41
  125. data/test/unit/adapter_mongoid_test.rb +0 -104
  126. data/test/unit/adapter_multiple_test.rb +0 -106
  127. data/test/unit/adapter_test.rb +0 -69
  128. data/test/unit/callbacks_test.rb +0 -31
  129. data/test/unit/client_test.rb +0 -27
  130. data/test/unit/importing_test.rb +0 -203
  131. data/test/unit/indexing_test.rb +0 -650
  132. data/test/unit/module_test.rb +0 -57
  133. data/test/unit/multimodel_test.rb +0 -38
  134. data/test/unit/naming_test.rb +0 -103
  135. data/test/unit/proxy_test.rb +0 -100
  136. data/test/unit/response_base_test.rb +0 -40
  137. data/test/unit/response_pagination_kaminari_test.rb +0 -433
  138. data/test/unit/response_pagination_will_paginate_test.rb +0 -398
  139. data/test/unit/response_records_test.rb +0 -91
  140. data/test/unit/response_result_test.rb +0 -90
  141. data/test/unit/response_results_test.rb +0 -31
  142. data/test/unit/response_test.rb +0 -104
  143. data/test/unit/searching_search_request_test.rb +0 -78
  144. data/test/unit/searching_test.rb +0 -41
  145. data/test/unit/serializing_test.rb +0 -17
@@ -1,326 +0,0 @@
1
- require 'test_helper'
2
- require 'active_record'
3
-
4
- module Elasticsearch
5
- module Model
6
- class ActiveRecordAssociationsIntegrationTest < Elasticsearch::Test::IntegrationTestCase
7
-
8
- context "ActiveRecord associations" do
9
- setup do
10
-
11
- # ----- Schema definition ---------------------------------------------------------------
12
-
13
- ActiveRecord::Schema.define(version: 1) do
14
- create_table :categories do |t|
15
- t.string :title
16
- t.timestamps
17
- end
18
-
19
- create_table :categories_posts, id: false do |t|
20
- t.references :post, :category
21
- end
22
-
23
- create_table :authors do |t|
24
- t.string :first_name, :last_name
25
- t.timestamps
26
- end
27
-
28
- create_table :authorships do |t|
29
- t.string :first_name, :last_name
30
- t.references :post
31
- t.references :author
32
- t.timestamps
33
- end
34
-
35
- create_table :comments do |t|
36
- t.string :text
37
- t.string :author
38
- t.references :post
39
- t.timestamps
40
- end and add_index(:comments, :post_id)
41
-
42
- create_table :posts do |t|
43
- t.string :title
44
- t.text :text
45
- t.boolean :published
46
- t.timestamps
47
- end
48
- end
49
-
50
- # ----- Models definition -------------------------------------------------------------------------
51
-
52
- class Category < ActiveRecord::Base
53
- has_and_belongs_to_many :posts
54
- end
55
-
56
- class Author < ActiveRecord::Base
57
- has_many :authorships
58
-
59
- def full_name
60
- [first_name, last_name].compact.join(' ')
61
- end
62
- end
63
-
64
- class Authorship < ActiveRecord::Base
65
- belongs_to :author
66
- belongs_to :post, touch: true
67
- end
68
-
69
- class Comment < ActiveRecord::Base
70
- belongs_to :post, touch: true
71
- end
72
-
73
- class Post < ActiveRecord::Base
74
- has_and_belongs_to_many :categories, after_add: [ lambda { |a,c| a.__elasticsearch__.index_document } ],
75
- after_remove: [ lambda { |a,c| a.__elasticsearch__.index_document } ]
76
- has_many :authorships
77
- has_many :authors, through: :authorships
78
- has_many :comments
79
- end
80
-
81
- # ----- Search integration via Concern module -----------------------------------------------------
82
-
83
- module Searchable
84
- extend ActiveSupport::Concern
85
-
86
- included do
87
- include Elasticsearch::Model
88
- include Elasticsearch::Model::Callbacks
89
-
90
- # Set up the mapping
91
- #
92
- settings index: { number_of_shards: 1, number_of_replicas: 0 } do
93
- mapping do
94
- indexes :title, analyzer: 'snowball'
95
- indexes :created_at, type: 'date'
96
-
97
- indexes :authors do
98
- indexes :first_name
99
- indexes :last_name
100
- indexes :full_name, type: 'multi_field' do
101
- indexes :full_name
102
- indexes :raw, analyzer: 'keyword'
103
- end
104
- end
105
-
106
- indexes :categories, analyzer: 'keyword'
107
-
108
- indexes :comments, type: 'nested' do
109
- indexes :text
110
- indexes :author
111
- end
112
- end
113
- end
114
-
115
- # Customize the JSON serialization for Elasticsearch
116
- #
117
- def as_indexed_json(options={})
118
- {
119
- title: title,
120
- text: text,
121
- categories: categories.map(&:title),
122
- authors: authors.as_json(methods: [:full_name], only: [:full_name, :first_name, :last_name]),
123
- comments: comments.as_json(only: [:text, :author])
124
- }
125
- end
126
-
127
- # Update document in the index after touch
128
- #
129
- after_touch() { __elasticsearch__.index_document }
130
- end
131
- end
132
-
133
- # Include the search integration
134
- #
135
- Post.__send__ :include, Searchable
136
- Comment.__send__ :include, Elasticsearch::Model
137
- Comment.__send__ :include, Elasticsearch::Model::Callbacks
138
-
139
- # ----- Reset the indices -----------------------------------------------------------------
140
-
141
- Post.delete_all
142
- Post.__elasticsearch__.create_index! force: true
143
-
144
- Comment.delete_all
145
- Comment.__elasticsearch__.create_index! force: true
146
- end
147
-
148
- should "index and find a document" do
149
- Post.create! title: 'Test'
150
- Post.create! title: 'Testing Coding'
151
- Post.create! title: 'Coding'
152
- Post.__elasticsearch__.refresh_index!
153
-
154
- response = Post.search('title:test')
155
-
156
- assert_equal 2, response.results.size
157
- assert_equal 2, response.records.size
158
-
159
- assert_equal 'Test', response.results.first.title
160
- assert_equal 'Test', response.records.first.title
161
- end
162
-
163
- should "reindex a document after categories are changed" do
164
- # Create categories
165
- category_a = Category.where(title: "One").first_or_create!
166
- category_b = Category.where(title: "Two").first_or_create!
167
-
168
- # Create post
169
- post = Post.create! title: "First Post", text: "This is the first post..."
170
-
171
- # Assign categories
172
- post.categories = [category_a, category_b]
173
-
174
- Post.__elasticsearch__.refresh_index!
175
-
176
- query = { query: {
177
- filtered: {
178
- query: {
179
- multi_match: {
180
- fields: ['title'],
181
- query: 'first'
182
- }
183
- },
184
- filter: {
185
- terms: {
186
- categories: ['One']
187
- }
188
- }
189
- }
190
- }
191
- }
192
-
193
- response = Post.search query
194
-
195
- assert_equal 1, response.results.size
196
- assert_equal 1, response.records.size
197
-
198
- # Remove category "One"
199
- post.categories = [category_b]
200
-
201
- Post.__elasticsearch__.refresh_index!
202
- response = Post.search query
203
-
204
- assert_equal 0, response.results.size
205
- assert_equal 0, response.records.size
206
- end
207
-
208
- should "reindex a document after authors are changed" do
209
- # Create authors
210
- author_a = Author.where(first_name: "John", last_name: "Smith").first_or_create!
211
- author_b = Author.where(first_name: "Mary", last_name: "Smith").first_or_create!
212
- author_c = Author.where(first_name: "Kobe", last_name: "Griss").first_or_create!
213
-
214
- # Create posts
215
- post_1 = Post.create! title: "First Post", text: "This is the first post..."
216
- post_2 = Post.create! title: "Second Post", text: "This is the second post..."
217
- post_3 = Post.create! title: "Third Post", text: "This is the third post..."
218
-
219
- # Assign authors
220
- post_1.authors = [author_a, author_b]
221
- post_2.authors = [author_a]
222
- post_3.authors = [author_c]
223
-
224
- Post.__elasticsearch__.refresh_index!
225
-
226
- response = Post.search 'authors.full_name:john'
227
-
228
- assert_equal 2, response.results.size
229
- assert_equal 2, response.records.size
230
-
231
- post_3.authors << author_a
232
-
233
- Post.__elasticsearch__.refresh_index!
234
-
235
- response = Post.search 'authors.full_name:john'
236
-
237
- assert_equal 3, response.results.size
238
- assert_equal 3, response.records.size
239
- end if defined?(::ActiveRecord) && ::ActiveRecord::VERSION::MAJOR >= 4
240
-
241
- should "reindex a document after comments are added" do
242
- # Create posts
243
- post_1 = Post.create! title: "First Post", text: "This is the first post..."
244
- post_2 = Post.create! title: "Second Post", text: "This is the second post..."
245
-
246
- # Add comments
247
- post_1.comments.create! author: 'John', text: 'Excellent'
248
- post_1.comments.create! author: 'Abby', text: 'Good'
249
-
250
- post_2.comments.create! author: 'John', text: 'Terrible'
251
-
252
- Post.__elasticsearch__.refresh_index!
253
-
254
- response = Post.search 'comments.author:john AND comments.text:good'
255
- assert_equal 0, response.results.size
256
-
257
- # Add comment
258
- post_1.comments.create! author: 'John', text: 'Or rather just good...'
259
-
260
- Post.__elasticsearch__.refresh_index!
261
-
262
- response = Post.search 'comments.author:john AND comments.text:good'
263
- assert_equal 0, response.results.size
264
-
265
- response = Post.search \
266
- query: {
267
- nested: {
268
- path: 'comments',
269
- query: {
270
- bool: {
271
- must: [
272
- { match: { 'comments.author' => 'john' } },
273
- { match: { 'comments.text' => 'good' } }
274
- ]
275
- }
276
- }
277
- }
278
- }
279
-
280
- assert_equal 1, response.results.size
281
- end if defined?(::ActiveRecord) && ::ActiveRecord::VERSION::MAJOR >= 4
282
-
283
- should "reindex a document after Post#touch" do
284
- # Create categories
285
- category_a = Category.where(title: "One").first_or_create!
286
-
287
- # Create post
288
- post = Post.create! title: "First Post", text: "This is the first post..."
289
-
290
- # Assign category
291
- post.categories << category_a
292
-
293
- Post.__elasticsearch__.refresh_index!
294
-
295
- assert_equal 1, Post.search('categories:One').size
296
-
297
- # Update category
298
- category_a.update_attribute :title, "Updated"
299
-
300
- # Trigger touch on posts in category
301
- category_a.posts.each { |p| p.touch }
302
-
303
- Post.__elasticsearch__.refresh_index!
304
-
305
- assert_equal 0, Post.search('categories:One').size
306
- assert_equal 1, Post.search('categories:Updated').size
307
- end
308
-
309
- should "eagerly load associated records" do
310
- post_1 = Post.create(title: 'One')
311
- post_2 = Post.create(title: 'Two')
312
- post_1.comments.create text: 'First comment'
313
- post_1.comments.create text: 'Second comment'
314
-
315
- Comment.__elasticsearch__.refresh_index!
316
-
317
- records = Comment.search('first').records(includes: :post)
318
-
319
- assert records.first.association(:post).loaded?, "The associated Post should be eagerly loaded"
320
- assert_equal 'One', records.first.post.title
321
- end
322
- end
323
-
324
- end
325
- end
326
- end
@@ -1,234 +0,0 @@
1
- require 'test_helper'
2
- require 'active_record'
3
-
4
- puts "ActiveRecord #{ActiveRecord::VERSION::STRING}", '-'*80
5
-
6
- module Elasticsearch
7
- module Model
8
- class ActiveRecordBasicIntegrationTest < Elasticsearch::Test::IntegrationTestCase
9
- context "ActiveRecord basic integration" do
10
- setup do
11
- ActiveRecord::Schema.define(:version => 1) do
12
- create_table :articles do |t|
13
- t.string :title
14
- t.string :body
15
- t.datetime :created_at, :default => 'NOW()'
16
- end
17
- end
18
-
19
- class ::Article < ActiveRecord::Base
20
- include Elasticsearch::Model
21
- include Elasticsearch::Model::Callbacks
22
-
23
- settings index: { number_of_shards: 1, number_of_replicas: 0 } do
24
- mapping do
25
- indexes :title, type: 'string', analyzer: 'snowball'
26
- indexes :body, type: 'string'
27
- indexes :created_at, type: 'date'
28
- end
29
- end
30
-
31
- def as_indexed_json(options = {})
32
- attributes
33
- .symbolize_keys
34
- .slice(:title, :body, :created_at)
35
- .merge(suggest_title: title)
36
- end
37
- end
38
-
39
- Article.delete_all
40
- Article.__elasticsearch__.create_index! force: true
41
-
42
- ::Article.create! title: 'Test', body: ''
43
- ::Article.create! title: 'Testing Coding', body: ''
44
- ::Article.create! title: 'Coding', body: ''
45
-
46
- Article.__elasticsearch__.refresh_index!
47
- end
48
-
49
- should "index and find a document" do
50
- response = Article.search('title:test')
51
-
52
- assert response.any?, "Response should not be empty: #{response.to_a.inspect}"
53
-
54
- assert_equal 2, response.results.size
55
- assert_equal 2, response.records.size
56
-
57
- assert_instance_of Elasticsearch::Model::Response::Result, response.results.first
58
- assert_instance_of Article, response.records.first
59
-
60
- assert_equal 'Test', response.results.first.title
61
- assert_equal 'Test', response.records.first.title
62
- end
63
-
64
- should "provide access to result" do
65
- response = Article.search query: { match: { title: 'test' } }, highlight: { fields: { title: {} } }
66
-
67
- assert_equal 'Test', response.results.first.title
68
-
69
- assert_equal true, response.results.first.title?
70
- assert_equal false, response.results.first.boo?
71
-
72
- assert_equal true, response.results.first.highlight?
73
- assert_equal true, response.results.first.highlight.title?
74
- assert_equal false, response.results.first.highlight.boo?
75
- end
76
-
77
- should "iterate over results" do
78
- response = Article.search('title:test')
79
-
80
- assert_equal ['1', '2'], response.results.map(&:_id)
81
- assert_equal [1, 2], response.records.map(&:id)
82
- end
83
-
84
- should "return _id and _type as #id and #type" do
85
- response = Article.search('title:test')
86
-
87
- assert_equal '1', response.results.first.id
88
- assert_equal 'article', response.results.first.type
89
- end
90
-
91
- should "access results from records" do
92
- response = Article.search('title:test')
93
-
94
- response.records.each_with_hit do |r, h|
95
- assert_not_nil h._score
96
- assert_not_nil h._source.title
97
- end
98
- end
99
-
100
- should "preserve the search results order for records" do
101
- response = Article.search('title:code')
102
-
103
- response.records.each_with_hit do |r, h|
104
- assert_equal h._id, r.id.to_s
105
- end
106
-
107
- response.records.map_with_hit do |r, h|
108
- assert_equal h._id, r.id.to_s
109
- end
110
- end
111
-
112
- should "remove document from index on destroy" do
113
- article = Article.first
114
-
115
- article.destroy
116
- assert_equal 2, Article.count
117
-
118
- Article.__elasticsearch__.refresh_index!
119
-
120
- response = Article.search 'title:test'
121
-
122
- assert_equal 1, response.results.size
123
- assert_equal 1, response.records.size
124
- end
125
-
126
- should "index updates to the document" do
127
- article = Article.first
128
-
129
- article.title = 'Writing'
130
- article.save
131
-
132
- Article.__elasticsearch__.refresh_index!
133
-
134
- response = Article.search 'title:write'
135
-
136
- assert_equal 1, response.results.size
137
- assert_equal 1, response.records.size
138
- end
139
-
140
- should "update specific attributes" do
141
- article = Article.first
142
-
143
- response = Article.search 'title:special'
144
-
145
- assert_equal 0, response.results.size
146
- assert_equal 0, response.records.size
147
-
148
- article.__elasticsearch__.update_document_attributes title: 'special'
149
-
150
- Article.__elasticsearch__.refresh_index!
151
-
152
- response = Article.search 'title:special'
153
-
154
- assert_equal 1, response.results.size
155
- assert_equal 1, response.records.size
156
- end
157
-
158
- should "update document when save is called multiple times in a transaction" do
159
- article = Article.first
160
- response = Article.search 'body:dummy'
161
-
162
- assert_equal 0, response.results.size
163
- assert_equal 0, response.records.size
164
-
165
- ActiveRecord::Base.transaction do
166
- article.body = 'dummy'
167
- article.save
168
-
169
- article.title = 'special'
170
- article.save
171
- end
172
-
173
- article.__elasticsearch__.update_document
174
- Article.__elasticsearch__.refresh_index!
175
-
176
- response = Article.search 'body:dummy'
177
- assert_equal 1, response.results.size
178
- assert_equal 1, response.records.size
179
- end
180
-
181
- should "return results for a DSL search" do
182
- response = Article.search query: { match: { title: { query: 'test' } } }
183
-
184
- assert_equal 2, response.results.size
185
- assert_equal 2, response.records.size
186
- end
187
-
188
- should "return a paged collection" do
189
- response = Article.search query: { match: { title: { query: 'test' } } },
190
- size: 2,
191
- from: 1
192
-
193
- assert_equal 1, response.results.size
194
- assert_equal 1, response.records.size
195
-
196
- assert_equal 'Testing Coding', response.results.first.title
197
- assert_equal 'Testing Coding', response.records.first.title
198
- end
199
-
200
- should "allow chaining SQL commands on response.records" do
201
- response = Article.search query: { match: { title: { query: 'test' } } }
202
-
203
- assert_equal 2, response.records.size
204
- assert_equal 1, response.records.where(title: 'Test').size
205
- assert_equal 'Test', response.records.where(title: 'Test').first.title
206
- end
207
-
208
- should "allow ordering response.records in SQL" do
209
- response = Article.search query: { match: { title: { query: 'test' } } }
210
-
211
- if defined?(::ActiveRecord) && ::ActiveRecord::VERSION::MAJOR >= 4
212
- assert_equal 'Testing Coding', response.records.order(title: :desc).first.title
213
- else
214
- assert_equal 'Testing Coding', response.records.order('title DESC').first.title
215
- end
216
- end
217
-
218
- should "allow dot access to response" do
219
- response = Article.search query: { match: { title: { query: 'test' } } },
220
- aggregations: { dates: { date_histogram: { field: 'created_at', interval: 'hour' } } },
221
- suggest: { text: 'tezt', title: { term: { field: 'title', suggest_mode: 'always' } } }
222
-
223
- response.response.respond_to?(:aggregations)
224
- assert_equal 2, response.aggregations.dates.buckets.first.doc_count
225
-
226
- response.response.respond_to?(:suggest)
227
- assert_equal 1, response.suggestions.title.first.options.size
228
- assert_equal ['test'], response.suggestions.terms
229
- end
230
- end
231
-
232
- end
233
- end
234
- end
@@ -1,62 +0,0 @@
1
- require 'test_helper'
2
- require 'active_record'
3
-
4
- module Elasticsearch
5
- module Model
6
- class ActiveRecordCustomSerializationTest < Elasticsearch::Test::IntegrationTestCase
7
- context "ActiveRecord model with custom JSON serialization" do
8
- setup do
9
- class ::ArticleWithCustomSerialization < ActiveRecord::Base
10
- include Elasticsearch::Model
11
- include Elasticsearch::Model::Callbacks
12
-
13
- mapping do
14
- indexes :title
15
- end
16
-
17
- def as_indexed_json(options={})
18
- # as_json(options.merge root: false).slice('title')
19
- { title: self.title }
20
- end
21
- end
22
-
23
- ActiveRecord::Schema.define(:version => 1) do
24
- create_table ArticleWithCustomSerialization.table_name do |t|
25
- t.string :title
26
- t.string :status
27
- end
28
- end
29
-
30
- ArticleWithCustomSerialization.delete_all
31
- ArticleWithCustomSerialization.__elasticsearch__.create_index! force: true
32
- end
33
-
34
- should "index only the title attribute when creating" do
35
- ArticleWithCustomSerialization.create! title: 'Test', status: 'green'
36
-
37
- a = ArticleWithCustomSerialization.__elasticsearch__.client.get \
38
- index: 'article_with_custom_serializations',
39
- type: 'article_with_custom_serialization',
40
- id: '1'
41
-
42
- assert_equal( { 'title' => 'Test' }, a['_source'] )
43
- end
44
-
45
- should "index only the title attribute when updating" do
46
- ArticleWithCustomSerialization.create! title: 'Test', status: 'green'
47
-
48
- article = ArticleWithCustomSerialization.first
49
- article.update_attributes title: 'UPDATED', status: 'red'
50
-
51
- a = ArticleWithCustomSerialization.__elasticsearch__.client.get \
52
- index: 'article_with_custom_serializations',
53
- type: 'article_with_custom_serialization',
54
- id: '1'
55
-
56
- assert_equal( { 'title' => 'UPDATED' }, a['_source'] )
57
- end
58
- end
59
-
60
- end
61
- end
62
- end