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
@@ -0,0 +1,127 @@
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::Adapter::ActiveRecord MultiModel' do
21
+
22
+ before(:all) do
23
+ ActiveRecord::Schema.define do
24
+ create_table Episode.table_name do |t|
25
+ t.string :name
26
+ t.datetime :created_at, :default => 'NOW()'
27
+ end
28
+
29
+ create_table Series.table_name do |t|
30
+ t.string :name
31
+ t.datetime :created_at, :default => 'NOW()'
32
+ end
33
+ end
34
+ end
35
+
36
+ before do
37
+ models = [ Episode, Series ]
38
+ clear_tables(models)
39
+ models.each do |model|
40
+ model.__elasticsearch__.create_index! force: true
41
+ model.create name: "The #{model.name}"
42
+ model.create name: "A great #{model.name}"
43
+ model.create name: "The greatest #{model.name}"
44
+ model.__elasticsearch__.refresh_index!
45
+ end
46
+ end
47
+
48
+ after do
49
+ clear_indices(Episode, Series)
50
+ clear_tables(Episode, Series)
51
+ end
52
+
53
+ context 'when the search is across multimodels' do
54
+
55
+ let(:search_result) do
56
+ Elasticsearch::Model.search(%q<"The greatest Episode"^2 OR "The greatest Series">, [Series, Episode])
57
+ end
58
+
59
+ it 'executes the search across models' do
60
+ expect(search_result.results.size).to eq(2)
61
+ expect(search_result.records.size).to eq(2)
62
+ end
63
+
64
+ describe '#results' do
65
+
66
+ it 'returns an instance of Elasticsearch::Model::Response::Result' do
67
+ expect(search_result.results[0]).to be_a(Elasticsearch::Model::Response::Result)
68
+ expect(search_result.results[1]).to be_a(Elasticsearch::Model::Response::Result)
69
+ end
70
+
71
+ it 'returns the correct model instance' do
72
+ expect(search_result.results[0].name).to eq('The greatest Episode')
73
+ expect(search_result.results[1].name).to eq('The greatest Series')
74
+ end
75
+
76
+ it 'provides access to the results' do
77
+ expect(search_result.results[0].name).to eq('The greatest Episode')
78
+ expect(search_result.results[0].name?).to be(true)
79
+ expect(search_result.results[0].boo?).to be(false)
80
+
81
+ expect(search_result.results[1].name).to eq('The greatest Series')
82
+ expect(search_result.results[1].name?).to be(true)
83
+ expect(search_result.results[1].boo?).to be(false)
84
+ end
85
+ end
86
+
87
+ describe '#records' do
88
+
89
+ it 'returns an instance of Elasticsearch::Model::Response::Result' do
90
+ expect(search_result.records[0]).to be_a(Episode)
91
+ expect(search_result.records[1]).to be_a(Series)
92
+ end
93
+
94
+ it 'returns the correct model instance' do
95
+ expect(search_result.records[0].name).to eq('The greatest Episode')
96
+ expect(search_result.records[1].name).to eq('The greatest Series')
97
+ end
98
+
99
+ context 'when the data store is changed' do
100
+
101
+ before do
102
+ Series.find_by_name("The greatest Series").delete
103
+ Series.__elasticsearch__.refresh_index!
104
+ end
105
+
106
+ it 'only returns matching records' do
107
+ expect(search_result.results.size).to eq(2)
108
+ expect(search_result.records.size).to eq(1 )
109
+ expect(search_result.records[0].name).to eq('The greatest Episode')
110
+ end
111
+ end
112
+ end
113
+
114
+ describe 'pagination' do
115
+
116
+ let(:search_result) do
117
+ Elasticsearch::Model.search('series OR episode', [Series, Episode])
118
+ end
119
+
120
+ it 'properly paginates the results' do
121
+ expect(search_result.page(1).per(3).results.size).to eq(3)
122
+ expect(search_result.page(2).per(3).results.size).to eq(3)
123
+ expect(search_result.page(3).per(3).results.size).to eq(0)
124
+ end
125
+ end
126
+ end
127
+ end
@@ -0,0 +1,55 @@
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::Adapter::ActiveRecord Namespaced Model' do
21
+
22
+ before(:all) do
23
+ ActiveRecord::Schema.define(:version => 1) do
24
+ create_table :books do |t|
25
+ t.string :title
26
+ end
27
+ end
28
+
29
+ MyNamespace::Book.delete_all
30
+ MyNamespace::Book.__elasticsearch__.create_index!(force: true, include_type_name: true)
31
+ MyNamespace::Book.create!(title: 'Test')
32
+ MyNamespace::Book.__elasticsearch__.refresh_index!
33
+ end
34
+
35
+ after do
36
+ clear_indices(MyNamespace::Book)
37
+ clear_tables(MyNamespace::Book)
38
+ end
39
+
40
+ context 'when the model is namespaced' do
41
+
42
+ it 'has the proper index name' do
43
+ expect(MyNamespace::Book.index_name).to eq('my_namespace-books')
44
+ end
45
+
46
+ it 'has the proper document type' do
47
+ expect(MyNamespace::Book.document_type).to eq('book')
48
+ end
49
+
50
+ it 'saves the document into the index' do
51
+ expect(MyNamespace::Book.search('title:test').results.size).to eq(1)
52
+ expect(MyNamespace::Book.search('title:test').results.first.title).to eq('Test')
53
+ end
54
+ end
55
+ end
@@ -0,0 +1,332 @@
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::Adapter::ActiveRecord Pagination' do
21
+
22
+ before(:all) do
23
+ ActiveRecord::Schema.define(:version => 1) do
24
+ create_table ArticleForPagination.table_name do |t|
25
+ t.string :title
26
+ t.datetime :created_at, :default => 'NOW()'
27
+ t.boolean :published
28
+ end
29
+ end
30
+
31
+ Kaminari::Hooks.init if defined?(Kaminari::Hooks)
32
+
33
+ ArticleForPagination.__elasticsearch__.create_index! force: true
34
+
35
+ 68.times do |i|
36
+ ArticleForPagination.create! title: "Test #{i}", published: (i % 2 == 0)
37
+ end
38
+
39
+ ArticleForPagination.import
40
+ ArticleForPagination.__elasticsearch__.refresh_index!
41
+ end
42
+
43
+ context 'when no other page is specified' do
44
+
45
+ let(:records) do
46
+ ArticleForPagination.search('title:test').page(1).records
47
+ end
48
+
49
+ describe '#size' do
50
+
51
+ it 'returns the correct size' do
52
+ expect(records.size).to eq(25)
53
+ end
54
+ end
55
+
56
+ describe '#current_page' do
57
+
58
+ it 'returns the correct current page' do
59
+ expect(records.current_page).to eq(1)
60
+ end
61
+ end
62
+
63
+ describe '#prev_page' do
64
+
65
+ it 'returns the correct previous page' do
66
+ expect(records.prev_page).to be_nil
67
+ end
68
+ end
69
+
70
+ describe '#next_page' do
71
+
72
+ it 'returns the correct next page' do
73
+ expect(records.next_page).to eq(2)
74
+ end
75
+ end
76
+
77
+ describe '#total_pages' do
78
+
79
+ it 'returns the correct total pages' do
80
+ expect(records.total_pages).to eq(3)
81
+ end
82
+ end
83
+
84
+ describe '#first_page?' do
85
+
86
+ it 'returns the correct first page' do
87
+ expect(records.first_page?).to be(true)
88
+ end
89
+ end
90
+
91
+ describe '#last_page?' do
92
+
93
+ it 'returns the correct last page' do
94
+ expect(records.last_page?).to be(false)
95
+ end
96
+ end
97
+
98
+ describe '#out_of_range?' do
99
+
100
+ it 'returns whether the pagination is out of range' do
101
+ expect(records.out_of_range?).to be(false)
102
+ end
103
+ end
104
+ end
105
+
106
+ context 'when a specific page is specified' do
107
+
108
+ let(:records) do
109
+ ArticleForPagination.search('title:test').page(2).records
110
+ end
111
+
112
+ describe '#size' do
113
+
114
+ it 'returns the correct size' do
115
+ expect(records.size).to eq(25)
116
+ end
117
+ end
118
+
119
+ describe '#current_page' do
120
+
121
+ it 'returns the correct current page' do
122
+ expect(records.current_page).to eq(2)
123
+ end
124
+ end
125
+
126
+ describe '#prev_page' do
127
+
128
+ it 'returns the correct previous page' do
129
+ expect(records.prev_page).to eq(1)
130
+ end
131
+ end
132
+
133
+ describe '#next_page' do
134
+
135
+ it 'returns the correct next page' do
136
+ expect(records.next_page).to eq(3)
137
+ end
138
+ end
139
+
140
+ describe '#total_pages' do
141
+
142
+ it 'returns the correct total pages' do
143
+ expect(records.total_pages).to eq(3)
144
+ end
145
+ end
146
+
147
+ describe '#first_page?' do
148
+
149
+ it 'returns the correct first page' do
150
+ expect(records.first_page?).to be(false)
151
+ end
152
+ end
153
+
154
+ describe '#last_page?' do
155
+
156
+ it 'returns the correct last page' do
157
+ expect(records.last_page?).to be(false)
158
+ end
159
+ end
160
+
161
+ describe '#out_of_range?' do
162
+
163
+ it 'returns whether the pagination is out of range' do
164
+ expect(records.out_of_range?).to be(false)
165
+ end
166
+ end
167
+ end
168
+
169
+ context 'when a the last page is specified' do
170
+
171
+ let(:records) do
172
+ ArticleForPagination.search('title:test').page(3).records
173
+ end
174
+
175
+ describe '#size' do
176
+
177
+ it 'returns the correct size' do
178
+ expect(records.size).to eq(18)
179
+ end
180
+ end
181
+
182
+ describe '#current_page' do
183
+
184
+ it 'returns the correct current page' do
185
+ expect(records.current_page).to eq(3)
186
+ end
187
+ end
188
+
189
+ describe '#prev_page' do
190
+
191
+ it 'returns the correct previous page' do
192
+ expect(records.prev_page).to eq(2)
193
+ end
194
+ end
195
+
196
+ describe '#next_page' do
197
+
198
+ it 'returns the correct next page' do
199
+ expect(records.next_page).to be_nil
200
+ end
201
+ end
202
+
203
+ describe '#total_pages' do
204
+
205
+ it 'returns the correct total pages' do
206
+ expect(records.total_pages).to eq(3)
207
+ end
208
+ end
209
+
210
+ describe '#first_page?' do
211
+
212
+ it 'returns the correct first page' do
213
+ expect(records.first_page?).to be(false)
214
+ end
215
+ end
216
+
217
+ describe '#last_page?' do
218
+
219
+ it 'returns the correct last page' do
220
+ expect(records.last_page?).to be(true)
221
+ end
222
+ end
223
+
224
+ describe '#out_of_range?' do
225
+
226
+ it 'returns whether the pagination is out of range' do
227
+ expect(records.out_of_range?).to be(false)
228
+ end
229
+ end
230
+ end
231
+
232
+ context 'when an invalid page is specified' do
233
+
234
+ let(:records) do
235
+ ArticleForPagination.search('title:test').page(6).records
236
+ end
237
+
238
+ describe '#size' do
239
+
240
+ it 'returns the correct size' do
241
+ expect(records.size).to eq(0)
242
+ end
243
+ end
244
+
245
+ describe '#current_page' do
246
+
247
+ it 'returns the correct current page' do
248
+ expect(records.current_page).to eq(6)
249
+ end
250
+ end
251
+
252
+ describe '#next_page' do
253
+
254
+ it 'returns the correct next page' do
255
+ expect(records.next_page).to be_nil
256
+ end
257
+ end
258
+
259
+ describe '#total_pages' do
260
+
261
+ it 'returns the correct total pages' do
262
+ expect(records.total_pages).to eq(3)
263
+ end
264
+ end
265
+
266
+ describe '#first_page?' do
267
+
268
+ it 'returns the correct first page' do
269
+ expect(records.first_page?).to be(false)
270
+ end
271
+ end
272
+
273
+ describe '#last_page?' do
274
+
275
+ it 'returns whether it is the last page', if: !(Kaminari::VERSION < '1') do
276
+ expect(records.last_page?).to be(false)
277
+ end
278
+
279
+ it 'returns whether it is the last page', if: Kaminari::VERSION < '1' do
280
+ expect(records.last_page?).to be(true) # Kaminari returns current_page >= total_pages in version < 1.0
281
+ end
282
+ end
283
+
284
+ describe '#out_of_range?' do
285
+
286
+ it 'returns whether the pagination is out of range' do
287
+ expect(records.out_of_range?).to be(true)
288
+ end
289
+ end
290
+ end
291
+
292
+ context 'when a scope is also specified' do
293
+
294
+ let(:records) do
295
+ ArticleForPagination.search('title:test').page(2).records.published
296
+ end
297
+
298
+ describe '#size' do
299
+
300
+ it 'returns the correct size' do
301
+ expect(records.size).to eq(12)
302
+ end
303
+ end
304
+ end
305
+
306
+ context 'when a sorting is specified' do
307
+
308
+ let(:search) do
309
+ ArticleForPagination.search({ query: { match: { title: 'test' } }, sort: [ { id: 'desc' } ] })
310
+ end
311
+
312
+ it 'applies the sort' do
313
+ expect(search.page(2).records.first.id).to eq(43)
314
+ expect(search.page(3).records.first.id).to eq(18)
315
+ expect(search.page(2).per(5).records.first.id).to eq(63)
316
+ end
317
+ end
318
+
319
+ context 'when the model has a specific default per page set' do
320
+
321
+ around do |example|
322
+ original_default = ArticleForPagination.instance_variable_get(:@_default_per_page)
323
+ ArticleForPagination.paginates_per 50
324
+ example.run
325
+ ArticleForPagination.paginates_per original_default
326
+ end
327
+
328
+ it 'uses the default per page setting' do
329
+ expect(ArticleForPagination.search('*').page(1).records.size).to eq(50)
330
+ end
331
+ end
332
+ end
@@ -0,0 +1,92 @@
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::Adapter::ActiveRecord Parent-Child' do
21
+
22
+ before(:all) do
23
+ ActiveRecord::Schema.define(version: 1) do
24
+ create_table :questions do |t|
25
+ t.string :title
26
+ t.text :text
27
+ t.string :author
28
+ t.timestamps null: false
29
+ end
30
+
31
+ create_table :answers do |t|
32
+ t.text :text
33
+ t.string :author
34
+ t.references :question
35
+ t.timestamps null: false
36
+ end
37
+
38
+ add_index(:answers, :question_id) unless index_exists?(:answers, :question_id)
39
+
40
+ clear_tables(Question)
41
+ ParentChildSearchable.create_index!(force: true, include_type_name: true)
42
+
43
+ q_1 = Question.create!(title: 'First Question', author: 'John')
44
+ q_2 = Question.create!(title: 'Second Question', author: 'Jody')
45
+
46
+ q_1.answers.create!(text: 'Lorem Ipsum', author: 'Adam')
47
+ q_1.answers.create!(text: 'Dolor Sit', author: 'Ryan')
48
+
49
+ q_2.answers.create!(text: 'Amet Et', author: 'John')
50
+
51
+ Question.__elasticsearch__.refresh_index!
52
+ end
53
+ end
54
+
55
+ describe 'has_child search' do
56
+
57
+ let(:search_result) do
58
+ Question.search(query: { has_child: { type: 'answer', query: { match: { author: 'john' } } } })
59
+ end
60
+
61
+ it 'finds parents by matching on child search criteria' do
62
+ expect(search_result.records.first.title).to eq('Second Question')
63
+ end
64
+ end
65
+
66
+ describe 'hash_parent search' do
67
+
68
+ let(:search_result) do
69
+ Answer.search(query: { has_parent: { parent_type: 'question', query: { match: { author: 'john' } } } })
70
+ end
71
+
72
+ it 'finds children by matching in parent criteria' do
73
+ expect(search_result.records.map(&:author)).to match(['Adam', 'Ryan'])
74
+ end
75
+ end
76
+
77
+ context 'when a parent is deleted' do
78
+
79
+ before do
80
+ Question.where(title: 'First Question').each(&:destroy)
81
+ Question.__elasticsearch__.refresh_index!
82
+ end
83
+
84
+ let(:search_result) do
85
+ Answer.search(query: { has_parent: { parent_type: 'question', query: { match_all: {} } } })
86
+ end
87
+
88
+ it 'deletes the children' do
89
+ expect(search_result.results.total).to eq(1)
90
+ end
91
+ end
92
+ end
@@ -0,0 +1,78 @@
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::Adapter::ActiveRecord Serialization' do
21
+
22
+ before(:all) do
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
+ context 'when the model has a custom serialization defined' do
35
+
36
+ before do
37
+ ArticleWithCustomSerialization.create!(title: 'Test', status: 'green')
38
+ ArticleWithCustomSerialization.__elasticsearch__.refresh_index!
39
+ end
40
+
41
+ context 'when a document is indexed' do
42
+
43
+ let(:search_result) do
44
+ ArticleWithCustomSerialization.__elasticsearch__.client.get(index: 'article_with_custom_serializations',
45
+ type: '_doc',
46
+ id: '1')
47
+ end
48
+
49
+ it 'applies the serialization when indexing' do
50
+ expect(search_result['_source']).to eq('title' => 'Test')
51
+ end
52
+ end
53
+
54
+ context 'when a document is updated' do
55
+
56
+ before do
57
+ article.update_attributes(title: 'UPDATED', status: 'yellow')
58
+ ArticleWithCustomSerialization.__elasticsearch__.refresh_index!
59
+ end
60
+
61
+ let!(:article) do
62
+ art = ArticleWithCustomSerialization.create!(title: 'Test', status: 'red')
63
+ ArticleWithCustomSerialization.__elasticsearch__.refresh_index!
64
+ art
65
+ end
66
+
67
+ let(:search_result) do
68
+ ArticleWithCustomSerialization.__elasticsearch__.client.get(index: 'article_with_custom_serializations',
69
+ type: '_doc',
70
+ id: article.id)
71
+ end
72
+
73
+ it 'applies the serialization when updating' do
74
+ expect(search_result['_source']).to eq('title' => 'UPDATED')
75
+ end
76
+ end
77
+ end
78
+ end