chewy 0.4.1 → 0.5.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.travis.yml +3 -3
- data/CHANGELOG.md +43 -1
- data/Gemfile +3 -0
- data/README.md +49 -11
- data/chewy.gemspec +1 -2
- data/gemfiles/Gemfile.rails-3.2 +1 -0
- data/gemfiles/Gemfile.rails-4.0 +1 -0
- data/lib/chewy.rb +8 -2
- data/lib/chewy/backports/deep_dup.rb +46 -0
- data/lib/chewy/backports/duplicable.rb +90 -0
- data/lib/chewy/config.rb +33 -6
- data/lib/chewy/errors.rb +1 -1
- data/lib/chewy/fields/base.rb +19 -7
- data/lib/chewy/fields/root.rb +13 -0
- data/lib/chewy/index/actions.rb +14 -6
- data/lib/chewy/index/search.rb +3 -2
- data/lib/chewy/query.rb +131 -17
- data/lib/chewy/query/compose.rb +27 -17
- data/lib/chewy/query/criteria.rb +34 -22
- data/lib/chewy/query/loading.rb +94 -10
- data/lib/chewy/query/nodes/exists.rb +1 -1
- data/lib/chewy/query/nodes/has_relation.rb +1 -1
- data/lib/chewy/query/nodes/missing.rb +1 -1
- data/lib/chewy/query/pagination.rb +8 -38
- data/lib/chewy/query/pagination/kaminari.rb +37 -0
- data/lib/chewy/runtime.rb +9 -0
- data/lib/chewy/runtime/version.rb +25 -0
- data/lib/chewy/type/adapter/active_record.rb +21 -7
- data/lib/chewy/type/adapter/base.rb +1 -1
- data/lib/chewy/type/adapter/object.rb +9 -6
- data/lib/chewy/type/import.rb +7 -4
- data/lib/chewy/type/mapping.rb +9 -9
- data/lib/chewy/type/wrapper.rb +1 -1
- data/lib/chewy/version.rb +1 -1
- data/lib/tasks/chewy.rake +40 -21
- data/spec/chewy/config_spec.rb +1 -1
- data/spec/chewy/fields/base_spec.rb +273 -8
- data/spec/chewy/index/actions_spec.rb +1 -2
- data/spec/chewy/index/aliases_spec.rb +0 -1
- data/spec/chewy/index/search_spec.rb +0 -8
- data/spec/chewy/index/settings_spec.rb +0 -2
- data/spec/chewy/index_spec.rb +0 -2
- data/spec/chewy/query/criteria_spec.rb +85 -18
- data/spec/chewy/query/loading_spec.rb +26 -9
- data/spec/chewy/query/nodes/and_spec.rb +2 -2
- data/spec/chewy/query/nodes/exists_spec.rb +6 -6
- data/spec/chewy/query/nodes/missing_spec.rb +4 -4
- data/spec/chewy/query/nodes/or_spec.rb +2 -2
- data/spec/chewy/query/pagination/kaminari_spec.rb +55 -0
- data/spec/chewy/query/pagination_spec.rb +15 -22
- data/spec/chewy/query_spec.rb +121 -52
- data/spec/chewy/rspec/update_index_spec.rb +0 -1
- data/spec/chewy/runtime/version_spec.rb +48 -0
- data/spec/chewy/runtime_spec.rb +9 -0
- data/spec/chewy/type/adapter/active_record_spec.rb +52 -0
- data/spec/chewy/type/adapter/object_spec.rb +33 -0
- data/spec/chewy/type/import_spec.rb +1 -3
- data/spec/chewy/type/mapping_spec.rb +4 -6
- data/spec/chewy/type/observe_spec.rb +0 -2
- data/spec/chewy/type/wrapper_spec.rb +0 -2
- data/spec/chewy/type_spec.rb +26 -5
- data/spec/chewy_spec.rb +0 -2
- data/spec/spec_helper.rb +2 -2
- metadata +15 -21
- data/lib/chewy/fields/default.rb +0 -10
- data/spec/chewy/fields/default_spec.rb +0 -6
@@ -1,8 +1,6 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
3
|
describe Chewy::Index::Search do
|
4
|
-
include ClassHelpers
|
5
|
-
|
6
4
|
before do
|
7
5
|
stub_index(:products) do
|
8
6
|
define_type :product
|
@@ -12,12 +10,6 @@ describe Chewy::Index::Search do
|
|
12
10
|
|
13
11
|
let(:product) { ProductsIndex::Product }
|
14
12
|
|
15
|
-
describe '.all' do
|
16
|
-
specify { product.all.should be_a Chewy::Query }
|
17
|
-
specify { product.all.object_id.should_not == product.all.object_id }
|
18
|
-
specify { product.all.should == product.all }
|
19
|
-
end
|
20
|
-
|
21
13
|
describe '.search_string' do
|
22
14
|
specify do
|
23
15
|
expect(ProductsIndex.client).to receive(:search).with(hash_including(q: 'hello')).twice
|
data/spec/chewy/index_spec.rb
CHANGED
@@ -1,32 +1,39 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
3
|
describe Chewy::Query::Criteria do
|
4
|
-
include ClassHelpers
|
5
|
-
|
6
4
|
subject { described_class.new }
|
7
5
|
|
8
6
|
its(:options) { should be_a Hash }
|
7
|
+
its(:request_options) { should be_a Hash }
|
9
8
|
its(:facets) { should == {} }
|
10
9
|
its(:aggregations) { should == {} }
|
11
10
|
its(:queries) { should == [] }
|
12
11
|
its(:filters) { should == [] }
|
12
|
+
its(:post_filters) { should == [] }
|
13
13
|
its(:sort) { should == [] }
|
14
14
|
its(:fields) { should == [] }
|
15
15
|
its(:types) { should == [] }
|
16
16
|
|
17
|
-
its(:
|
17
|
+
its(:request_options?) { should be_false }
|
18
18
|
its(:facets?) { should be_false }
|
19
19
|
its(:aggregations?) { should be_false }
|
20
20
|
its(:queries?) { should be_false }
|
21
21
|
its(:filters?) { should be_false }
|
22
|
+
its(:post_filters?) { should be_false }
|
22
23
|
its(:sort?) { should be_false }
|
23
24
|
its(:fields?) { should be_false }
|
24
25
|
its(:types?) { should be_false }
|
25
26
|
|
27
|
+
its(:none?){ should be_false }
|
28
|
+
|
26
29
|
describe '#update_options' do
|
27
30
|
specify { expect { subject.update_options(field: 'hello') }.to change { subject.options }.to(hash_including(field: 'hello')) }
|
28
31
|
end
|
29
32
|
|
33
|
+
describe '#update_request_options' do
|
34
|
+
specify { expect { subject.update_request_options(field: 'hello') }.to change { subject.request_options }.to(hash_including(field: 'hello')) }
|
35
|
+
end
|
36
|
+
|
30
37
|
describe '#update_facets' do
|
31
38
|
specify { expect { subject.update_facets(field: 'hello') }.to change { subject.facets? }.to(true) }
|
32
39
|
specify { expect { subject.update_facets(field: 'hello') }.to change { subject.facets }.to(field: 'hello') }
|
@@ -55,6 +62,15 @@ describe Chewy::Query::Criteria do
|
|
55
62
|
.to change { subject.filters }.to([{field: 'hello'}, {field: 'world'}]) }
|
56
63
|
end
|
57
64
|
|
65
|
+
describe '#update_post_filters' do
|
66
|
+
specify { expect { subject.update_post_filters(field: 'hello') }.to change { subject.post_filters? }.to(true) }
|
67
|
+
specify { expect { subject.update_post_filters(field: 'hello') }.to change { subject.post_filters }.to([{field: 'hello'}]) }
|
68
|
+
specify { expect { subject.update_post_filters(field: 'hello'); subject.update_post_filters(field: 'world') }
|
69
|
+
.to change { subject.post_filters }.to([{field: 'hello'}, {field: 'world'}]) }
|
70
|
+
specify { expect { subject.update_post_filters([{field: 'hello'}, {field: 'world'}, nil]) }
|
71
|
+
.to change { subject.post_filters }.to([{field: 'hello'}, {field: 'world'}]) }
|
72
|
+
end
|
73
|
+
|
58
74
|
describe '#update_sort' do
|
59
75
|
specify { expect { subject.update_sort(:field) }.to change { subject.sort? }.to(true) }
|
60
76
|
|
@@ -102,6 +118,8 @@ describe Chewy::Query::Criteria do
|
|
102
118
|
|
103
119
|
specify { subject.tap { |c| c.update_options(opt1: 'hello') }
|
104
120
|
.merge(criteria.tap { |c| c.update_options(opt2: 'hello') }).options.should include(opt1: 'hello', opt2: 'hello') }
|
121
|
+
specify { subject.tap { |c| c.update_request_options(opt1: 'hello') }
|
122
|
+
.merge(criteria.tap { |c| c.update_request_options(opt2: 'hello') }).request_options.should include(opt1: 'hello', opt2: 'hello') }
|
105
123
|
specify { subject.tap { |c| c.update_facets(field1: 'hello') }
|
106
124
|
.merge(criteria.tap { |c| c.update_facets(field1: 'hello') }).facets.should == {field1: 'hello', field1: 'hello'} }
|
107
125
|
specify { subject.tap { |c| c.update_aggregations(field1: 'hello') }
|
@@ -110,6 +128,8 @@ describe Chewy::Query::Criteria do
|
|
110
128
|
.merge(criteria.tap { |c| c.update_queries(field2: 'hello') }).queries.should == [{field1: 'hello'}, {field2: 'hello'}] }
|
111
129
|
specify { subject.tap { |c| c.update_filters(field1: 'hello') }
|
112
130
|
.merge(criteria.tap { |c| c.update_filters(field2: 'hello') }).filters.should == [{field1: 'hello'}, {field2: 'hello'}] }
|
131
|
+
specify { subject.tap { |c| c.update_post_filters(field1: 'hello') }
|
132
|
+
.merge(criteria.tap { |c| c.update_post_filters(field2: 'hello') }).post_filters.should == [{field1: 'hello'}, {field2: 'hello'}] }
|
113
133
|
specify { subject.tap { |c| c.update_sort(:field1) }
|
114
134
|
.merge(criteria.tap { |c| c.update_sort(:field2) }).sort.should == [:field1, :field2] }
|
115
135
|
specify { subject.tap { |c| c.update_fields(:field1) }
|
@@ -126,6 +146,8 @@ describe Chewy::Query::Criteria do
|
|
126
146
|
|
127
147
|
specify { subject.tap { |c| c.update_options(opt1: 'hello') }
|
128
148
|
.merge!(criteria.tap { |c| c.update_options(opt2: 'hello') }).options.should include(opt1: 'hello', opt2: 'hello') }
|
149
|
+
specify { subject.tap { |c| c.update_request_options(opt1: 'hello') }
|
150
|
+
.merge!(criteria.tap { |c| c.update_request_options(opt2: 'hello') }).request_options.should include(opt1: 'hello', opt2: 'hello') }
|
129
151
|
specify { subject.tap { |c| c.update_facets(field1: 'hello') }
|
130
152
|
.merge!(criteria.tap { |c| c.update_facets(field1: 'hello') }).facets.should == {field1: 'hello', field1: 'hello'} }
|
131
153
|
specify { subject.tap { |c| c.update_aggregations(field1: 'hello') }
|
@@ -134,6 +156,8 @@ describe Chewy::Query::Criteria do
|
|
134
156
|
.merge!(criteria.tap { |c| c.update_queries(field2: 'hello') }).queries.should == [{field1: 'hello'}, {field2: 'hello'}] }
|
135
157
|
specify { subject.tap { |c| c.update_filters(field1: 'hello') }
|
136
158
|
.merge!(criteria.tap { |c| c.update_filters(field2: 'hello') }).filters.should == [{field1: 'hello'}, {field2: 'hello'}] }
|
159
|
+
specify { subject.tap { |c| c.update_post_filters(field1: 'hello') }
|
160
|
+
.merge!(criteria.tap { |c| c.update_post_filters(field2: 'hello') }).post_filters.should == [{field1: 'hello'}, {field2: 'hello'}] }
|
137
161
|
specify { subject.tap { |c| c.update_sort(:field1) }
|
138
162
|
.merge!(criteria.tap { |c| c.update_sort(:field2) }).sort.should == [:field1, :field2] }
|
139
163
|
specify { subject.tap { |c| c.update_fields(:field1) }
|
@@ -149,39 +173,57 @@ describe Chewy::Query::Criteria do
|
|
149
173
|
end
|
150
174
|
|
151
175
|
specify { request_body.should == {body: {}} }
|
152
|
-
specify { request_body {
|
153
|
-
specify { request_body {
|
154
|
-
specify { request_body {
|
176
|
+
specify { request_body { update_request_options(size: 10) }.should == {body: {size: 10}} }
|
177
|
+
specify { request_body { update_request_options(from: 10) }.should == {body: {from: 10}} }
|
178
|
+
specify { request_body { update_request_options(explain: true) }.should == {body: {explain: true}} }
|
155
179
|
specify { request_body { update_queries(:query) }.should == {body: {query: :query}} }
|
156
180
|
specify { request_body {
|
157
|
-
|
181
|
+
update_request_options(from: 10); update_sort(:field); update_fields(:field); update_queries(:query)
|
158
182
|
}.should == {body: {query: :query, from: 10, sort: [:field], _source: ['field']}} }
|
183
|
+
|
184
|
+
specify { request_body {
|
185
|
+
update_queries(:query); update_filters(:filters);
|
186
|
+
}.should == {body: {query: {filtered: {query: :query, filter: :filters}}}} }
|
187
|
+
specify { request_body {
|
188
|
+
update_queries(:query); update_post_filters(:post_filter);
|
189
|
+
}.should == {body: {query: :query, post_filter: :post_filter}} }
|
159
190
|
end
|
160
191
|
|
161
|
-
describe '#
|
162
|
-
def
|
192
|
+
describe '#_filtered_query' do
|
193
|
+
def _filtered_query options = {}, &block
|
163
194
|
subject.instance_exec(&block) if block
|
164
|
-
subject.send(:
|
195
|
+
subject.send(:_filtered_query, subject.send(:_request_query), subject.send(:_request_filter), options)
|
165
196
|
end
|
166
197
|
|
167
|
-
specify {
|
168
|
-
specify {
|
169
|
-
specify {
|
198
|
+
specify { _filtered_query.should == {} }
|
199
|
+
specify { _filtered_query { update_queries(:query) }.should == {query: :query} }
|
200
|
+
specify { _filtered_query(strategy: 'query_first') { update_queries(:query) }.should == {query: :query} }
|
201
|
+
specify { _filtered_query { update_queries([:query1, :query2]) }
|
170
202
|
.should == {query: {bool: {must: [:query1, :query2]}}} }
|
171
|
-
specify {
|
203
|
+
specify { _filtered_query { update_options(query_mode: :should); update_queries([:query1, :query2]) }
|
172
204
|
.should == {query: {bool: {should: [:query1, :query2]}}} }
|
173
|
-
specify {
|
205
|
+
specify { _filtered_query { update_options(query_mode: :dis_max); update_queries([:query1, :query2]) }
|
174
206
|
.should == {query: {dis_max: {queries: [:query1, :query2]}}} }
|
175
207
|
|
176
|
-
specify {
|
208
|
+
specify { _filtered_query(strategy: 'query_first') { update_filters([:filter1, :filter2]) }
|
209
|
+
.should == {query: {filtered: {query: {match_all: {}}, filter: {and: [:filter1, :filter2]}, strategy: 'query_first'}}} }
|
210
|
+
specify { _filtered_query { update_filters([:filter1, :filter2]) }
|
177
211
|
.should == {query: {filtered: {query: {match_all: {}}, filter: {and: [:filter1, :filter2]}}}} }
|
178
|
-
|
212
|
+
|
213
|
+
specify { _filtered_query { update_filters([:filter1, :filter2]); update_queries([:query1, :query2]) }
|
179
214
|
.should == {query: {filtered: {
|
180
215
|
query: {bool: {must: [:query1, :query2]}},
|
181
216
|
filter: {and: [:filter1, :filter2]}
|
182
217
|
}}}
|
183
218
|
}
|
184
|
-
specify {
|
219
|
+
specify { _filtered_query(strategy: 'query_first') { update_filters([:filter1, :filter2]); update_queries([:query1, :query2]) }
|
220
|
+
.should == {query: {filtered: {
|
221
|
+
query: {bool: {must: [:query1, :query2]}},
|
222
|
+
filter: {and: [:filter1, :filter2]},
|
223
|
+
strategy: 'query_first'
|
224
|
+
}}}
|
225
|
+
}
|
226
|
+
specify { _filtered_query {
|
185
227
|
update_options(query_mode: :should); update_options(filter_mode: :or);
|
186
228
|
update_filters([:filter1, :filter2]); update_queries([:query1, :query2])
|
187
229
|
}.should == {query: {filtered: {
|
@@ -222,6 +264,31 @@ describe Chewy::Query::Criteria do
|
|
222
264
|
.should == {and: [{or: [{type: {value: 'type1'}}, {type: {value: 'type2'}}]}, {bool: {should: [:filter1, :filter2]}}]} }
|
223
265
|
end
|
224
266
|
|
267
|
+
describe '#_request_post_filter' do
|
268
|
+
def _request_post_filter &block
|
269
|
+
subject.instance_exec(&block) if block
|
270
|
+
subject.send(:_request_post_filter)
|
271
|
+
end
|
272
|
+
|
273
|
+
specify { _request_post_filter.should be_nil }
|
274
|
+
|
275
|
+
specify { _request_post_filter { update_post_filters([:post_filter1, :post_filter2]) }
|
276
|
+
.should == {and: [:post_filter1, :post_filter2]} }
|
277
|
+
specify { _request_post_filter { update_options(post_filter_mode: :or); update_post_filters([:post_filter1, :post_filter2]) }
|
278
|
+
.should == {or: [:post_filter1, :post_filter2]} }
|
279
|
+
specify { _request_post_filter { update_options(post_filter_mode: :must); update_post_filters([:post_filter1, :post_filter2]) }
|
280
|
+
.should == {bool: {must: [:post_filter1, :post_filter2]}} }
|
281
|
+
specify { _request_post_filter { update_options(post_filter_mode: :should); update_post_filters([:post_filter1, :post_filter2]) }
|
282
|
+
.should == {bool: {should: [:post_filter1, :post_filter2]}} }
|
283
|
+
|
284
|
+
context do
|
285
|
+
before { Chewy.stub(filter_mode: :or) }
|
286
|
+
|
287
|
+
specify { _request_post_filter { update_post_filters([:post_filter1, :post_filter2]) }
|
288
|
+
.should == {or: [:post_filter1, :post_filter2]} }
|
289
|
+
end
|
290
|
+
end
|
291
|
+
|
225
292
|
describe '#_request_types' do
|
226
293
|
def _request_types &block
|
227
294
|
subject.instance_exec(&block) if block
|
@@ -1,7 +1,6 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
3
|
describe Chewy::Query::Loading do
|
4
|
-
include ClassHelpers
|
5
4
|
before { Chewy.client.indices.delete index: '*' }
|
6
5
|
|
7
6
|
before do
|
@@ -26,13 +25,31 @@ describe Chewy::Query::Loading do
|
|
26
25
|
|
27
26
|
before { PlacesIndex.import!(cities: cities, countries: countries) }
|
28
27
|
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
28
|
+
describe '#load' do
|
29
|
+
specify { PlacesIndex.order(:rating).limit(6).load.total_count.should == 12 }
|
30
|
+
specify { PlacesIndex.order(:rating).limit(6).load.should =~ cities.first(3) + countries.first(3) }
|
31
|
+
|
32
|
+
specify { PlacesIndex.order(:rating).limit(6).load(city: { scope: ->{ where('rating < 2') } })
|
33
|
+
.should =~ cities.first(2) + countries.first(3) + [nil] }
|
34
|
+
specify { PlacesIndex.limit(6).load(city: { scope: ->{ where('rating < 2') } }).order(:rating)
|
35
|
+
.should =~ cities.first(2) + countries.first(3) + [nil] }
|
36
|
+
specify { PlacesIndex.order(:rating).limit(6).load(scope: ->{ where('rating < 2') })
|
37
|
+
.should =~ cities.first(2) + countries.first(2) + [nil] * 2 }
|
38
|
+
specify { PlacesIndex.order(:rating).limit(6).load(city: { scope: City.where('rating < 2') })
|
39
|
+
.should =~ cities.first(2) + countries.first(3) + [nil] }
|
40
|
+
end
|
41
|
+
|
42
|
+
describe '#preload' do
|
43
|
+
specify { PlacesIndex.order(:rating).limit(6).preload(scope: ->{ where('rating < 2') })
|
44
|
+
.map(&:_object).should =~ cities.first(2) + countries.first(2) + [nil] * 2 }
|
45
|
+
specify { PlacesIndex.limit(6).preload(scope: ->{ where('rating < 2') }).order(:rating)
|
46
|
+
.map(&:_object).should =~ cities.first(2) + countries.first(2) + [nil] * 2 }
|
47
|
+
specify { PlacesIndex.order(:rating).limit(6).preload(only: :city, scope: ->{ where('rating < 2') })
|
48
|
+
.map(&:_object).should =~ cities.first(2) + [nil] * 4 }
|
49
|
+
specify { PlacesIndex.order(:rating).limit(6).preload(except: [:city], scope: ->{ where('rating < 2') })
|
50
|
+
.map(&:_object).should =~ countries.first(2) + [nil] * 4 }
|
51
|
+
specify { PlacesIndex.order(:rating).limit(6).preload(only: [:city], except: :city, scope: ->{ where('rating < 2') })
|
52
|
+
.map(&:_object).should =~ [nil] * 6 }
|
53
|
+
end
|
37
54
|
end
|
38
55
|
end
|
@@ -7,10 +7,10 @@ describe Chewy::Query::Nodes::And do
|
|
7
7
|
end
|
8
8
|
|
9
9
|
specify { render { name? & (email == 'email') }.should == {
|
10
|
-
and: [{exists: {
|
10
|
+
and: [{exists: {field: 'name'}}, {term: {'email' => 'email'}}]
|
11
11
|
} }
|
12
12
|
specify { render { ~(name? & (email == 'email')) }.should == {
|
13
|
-
and: {filters: [{exists: {
|
13
|
+
and: {filters: [{exists: {field: 'name'}}, {term: {'email' => 'email'}}], _cache: true}
|
14
14
|
} }
|
15
15
|
end
|
16
16
|
end
|
@@ -6,13 +6,13 @@ describe Chewy::Query::Nodes::Exists do
|
|
6
6
|
Chewy::Query::Filters.new(&block).__render__
|
7
7
|
end
|
8
8
|
|
9
|
-
specify { render { name? }.should == {exists: {
|
9
|
+
specify { render { name? }.should == {exists: {field: 'name'}} }
|
10
10
|
|
11
|
-
specify { render { !!name? }.should == {exists: {
|
12
|
-
specify { render { !!name }.should == {exists: {
|
13
|
-
specify { render { name != nil }.should == {exists: {
|
14
|
-
specify { render { !(name == nil) }.should == {exists: {
|
11
|
+
specify { render { !!name? }.should == {exists: {field: 'name'}} }
|
12
|
+
specify { render { !!name }.should == {exists: {field: 'name'}} }
|
13
|
+
specify { render { name != nil }.should == {exists: {field: 'name'}} }
|
14
|
+
specify { render { !(name == nil) }.should == {exists: {field: 'name'}} }
|
15
15
|
|
16
|
-
specify { render { ~name? }.should == {exists: {
|
16
|
+
specify { render { ~name? }.should == {exists: {field: 'name'}} }
|
17
17
|
end
|
18
18
|
end
|
@@ -6,10 +6,10 @@ describe Chewy::Query::Nodes::Missing do
|
|
6
6
|
Chewy::Query::Filters.new(&block).__render__
|
7
7
|
end
|
8
8
|
|
9
|
-
specify { render { !name }.should == {missing: {
|
10
|
-
specify { render { !name? }.should == {missing: {
|
11
|
-
specify { render { name == nil }.should == {missing: {
|
9
|
+
specify { render { !name }.should == {missing: {field: 'name', existence: true, null_value: false}} }
|
10
|
+
specify { render { !name? }.should == {missing: {field: 'name', existence: true, null_value: true}} }
|
11
|
+
specify { render { name == nil }.should == {missing: {field: 'name', existence: false, null_value: true}} }
|
12
12
|
|
13
|
-
specify { render { ~!name }.should == {missing: {
|
13
|
+
specify { render { ~!name }.should == {missing: {field: 'name', existence: true, null_value: false}} }
|
14
14
|
end
|
15
15
|
end
|
@@ -7,10 +7,10 @@ describe Chewy::Query::Nodes::Or do
|
|
7
7
|
end
|
8
8
|
|
9
9
|
specify { render { name? | (email == 'email') }.should == {
|
10
|
-
or: [{exists: {
|
10
|
+
or: [{exists: {field: 'name'}}, {term: {'email' => 'email'}}]
|
11
11
|
} }
|
12
12
|
specify { render { ~(name? | (email == 'email')) }.should == {
|
13
|
-
or: {filters: [{exists: {
|
13
|
+
or: {filters: [{exists: {field: 'name'}}, {term: {'email' => 'email'}}], _cache: true}
|
14
14
|
} }
|
15
15
|
end
|
16
16
|
end
|
@@ -0,0 +1,55 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Chewy::Query::Pagination::Kaminari do
|
4
|
+
before { Chewy.client.indices.delete index: '*' }
|
5
|
+
|
6
|
+
before do
|
7
|
+
stub_index(:products) do
|
8
|
+
define_type(:product) do
|
9
|
+
field :name
|
10
|
+
field :age, type: 'integer'
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
let(:search) { ProductsIndex.order(:age) }
|
16
|
+
|
17
|
+
specify { search.total_pages.should == 0 }
|
18
|
+
|
19
|
+
context do
|
20
|
+
let(:data) { 10.times.map { |i| {id: i.next.to_s, name: "Name#{i.next}", age: 10 * i.next}.stringify_keys! } }
|
21
|
+
|
22
|
+
before { ProductsIndex::Product.import!(data.map { |h| double(h) }) }
|
23
|
+
before { ::Kaminari.config.stub(default_per_page: 3) }
|
24
|
+
|
25
|
+
describe '#per, #page' do
|
26
|
+
specify { search.map { |e| e.attributes.except('_score', '_explanation') }.should =~ data }
|
27
|
+
specify { search.page(1).map { |e| e.attributes.except('_score', '_explanation') }.should == data[0..2] }
|
28
|
+
specify { search.page(2).map { |e| e.attributes.except('_score', '_explanation') }.should == data[3..5] }
|
29
|
+
specify { search.page(2).per(4).map { |e| e.attributes.except('_score', '_explanation') }.should == data[4..7] }
|
30
|
+
specify { search.per(2).page(3).map { |e| e.attributes.except('_score', '_explanation') }.should == data[4..5] }
|
31
|
+
specify { search.per(5).page.map { |e| e.attributes.except('_score', '_explanation') }.should == data[0..4] }
|
32
|
+
specify { search.page.per(4).map { |e| e.attributes.except('_score', '_explanation') }.should == data[0..3] }
|
33
|
+
end
|
34
|
+
|
35
|
+
describe '#total_pages' do
|
36
|
+
specify { search.total_pages.should == 4 }
|
37
|
+
specify { search.per(5).page(2).total_pages.should == 2 }
|
38
|
+
specify { search.per(2).page(3).total_pages.should == 5 }
|
39
|
+
end
|
40
|
+
|
41
|
+
describe '#total_count' do
|
42
|
+
specify { search.per(4).page(1).total_count.should == 10 }
|
43
|
+
specify { search.filter(numeric_range: {age: {gt: 20}}).limit(3).total_count.should == 8 }
|
44
|
+
end
|
45
|
+
|
46
|
+
describe '#load' do
|
47
|
+
specify { search.per(2).page(1).load.first.age.should == 10 }
|
48
|
+
specify { search.per(2).page(3).load.first.age.should == 50 }
|
49
|
+
specify { search.per(2).page(3).load.page(2).load.first.age.should == 30 }
|
50
|
+
|
51
|
+
specify { search.per(4).page(1).load.total_count.should == 10 }
|
52
|
+
specify { search.per(2).page(3).load.total_pages.should == 5 }
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
@@ -1,7 +1,6 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
3
|
describe Chewy::Query::Pagination do
|
4
|
-
include ClassHelpers
|
5
4
|
before { Chewy.client.indices.delete index: '*' }
|
6
5
|
|
7
6
|
before do
|
@@ -12,31 +11,25 @@ describe Chewy::Query::Pagination do
|
|
12
11
|
end
|
13
12
|
end
|
14
13
|
end
|
15
|
-
let(:data) { 10.times.map { |i| {id: i.next.to_s, name: "Name#{i.next}", age: 10 * i.next}.stringify_keys! } }
|
16
|
-
|
17
|
-
before { ProductsIndex::Product.import!(data.map { |h| double(h) }) }
|
18
|
-
before { Kaminari.config.stub(default_per_page: 3) }
|
19
14
|
|
20
15
|
let(:search) { ProductsIndex.order(:age) }
|
21
16
|
|
22
|
-
|
23
|
-
specify { search.map { |e| e.attributes.except('_score', '_explanation') }.should =~ data }
|
24
|
-
specify { search.page(1).map { |e| e.attributes.except('_score', '_explanation') }.should == data[0..2] }
|
25
|
-
specify { search.page(2).map { |e| e.attributes.except('_score', '_explanation') }.should == data[3..5] }
|
26
|
-
specify { search.page(2).per(4).map { |e| e.attributes.except('_score', '_explanation') }.should == data[4..7] }
|
27
|
-
specify { search.per(2).page(3).map { |e| e.attributes.except('_score', '_explanation') }.should == data[4..5] }
|
28
|
-
specify { search.per(5).page.map { |e| e.attributes.except('_score', '_explanation') }.should == data[0..4] }
|
29
|
-
specify { search.page.per(4).map { |e| e.attributes.except('_score', '_explanation') }.should == data[0..3] }
|
30
|
-
end
|
17
|
+
specify { search.total_count.should == 0 }
|
31
18
|
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
end
|
19
|
+
context do
|
20
|
+
let(:data) { 10.times.map { |i| {id: i.next.to_s, name: "Name#{i.next}", age: 10 * i.next}.stringify_keys! } }
|
21
|
+
|
22
|
+
before { ProductsIndex::Product.import!(data.map { |h| double(h) }) }
|
37
23
|
|
38
|
-
|
39
|
-
|
40
|
-
|
24
|
+
describe '#total_count' do
|
25
|
+
specify { search.total_count.should == 10 }
|
26
|
+
specify { search.limit(5).total_count.should == 10 }
|
27
|
+
specify { search.filter(numeric_range: {age: {gt: 20}}).limit(3).total_count.should == 8 }
|
28
|
+
end
|
29
|
+
|
30
|
+
describe '#load' do
|
31
|
+
specify { search.load.total_count.should == 10 }
|
32
|
+
specify { search.limit(5).load.total_count.should == 10 }
|
33
|
+
end
|
41
34
|
end
|
42
35
|
end
|