chewy 0.5.2 → 0.6.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 +26 -4
- data/CHANGELOG.md +16 -0
- data/Gemfile +3 -1
- data/README.md +2 -10
- data/chewy.gemspec +0 -1
- data/gemfiles/Gemfile.rails-3.2.active_record +6 -0
- data/gemfiles/Gemfile.rails-3.2.active_record.kaminari +7 -0
- data/gemfiles/Gemfile.rails-3.2.active_record.will_paginate +7 -0
- data/gemfiles/Gemfile.rails-4.0.active_record +6 -0
- data/gemfiles/Gemfile.rails-4.0.active_record.kaminari +7 -0
- data/gemfiles/Gemfile.rails-4.0.active_record.will_paginate +7 -0
- data/gemfiles/Gemfile.rails-4.0.mongoid +6 -0
- data/gemfiles/Gemfile.rails-4.0.mongoid.kaminari +7 -0
- data/gemfiles/Gemfile.rails-4.0.mongoid.will_paginate +7 -0
- data/gemfiles/Gemfile.rails-4.1.active_record +6 -0
- data/gemfiles/Gemfile.rails-4.1.active_record.kaminari +7 -0
- data/gemfiles/Gemfile.rails-4.1.active_record.will_paginate +7 -0
- data/gemfiles/Gemfile.rails-4.1.mongoid +6 -0
- data/gemfiles/Gemfile.rails-4.1.mongoid.kaminari +7 -0
- data/gemfiles/Gemfile.rails-4.1.mongoid.will_paginate +7 -0
- data/gemfiles/Gemfile.rails-4.2.active_record +6 -0
- data/gemfiles/Gemfile.rails-4.2.active_record.kaminari +7 -0
- data/gemfiles/Gemfile.rails-4.2.active_record.will_paginate +7 -0
- data/gemfiles/Gemfile.rails-4.2.mongoid +6 -0
- data/gemfiles/Gemfile.rails-4.2.mongoid.kaminari +7 -0
- data/gemfiles/Gemfile.rails-4.2.mongoid.will_paginate +7 -0
- data/lib/chewy.rb +33 -5
- data/lib/chewy/config.rb +1 -0
- data/lib/chewy/index/search.rb +6 -3
- data/lib/chewy/query.rb +74 -1
- data/lib/chewy/query/compose.rb +4 -4
- data/lib/chewy/query/pagination.rb +5 -4
- data/lib/chewy/query/pagination/kaminari.rb +1 -1
- data/lib/chewy/query/pagination/will_paginate.rb +27 -0
- data/lib/chewy/type.rb +1 -0
- data/lib/chewy/type/adapter/active_record.rb +2 -2
- data/lib/chewy/type/adapter/mongoid.rb +147 -0
- data/lib/chewy/type/adapter/object.rb +1 -1
- data/lib/chewy/type/import.rb +1 -0
- data/lib/chewy/type/observe.rb +34 -6
- data/lib/chewy/version.rb +1 -1
- data/spec/chewy/config_spec.rb +17 -17
- data/spec/chewy/fields/base_spec.rb +62 -62
- data/spec/chewy/fields/root_spec.rb +5 -5
- data/spec/chewy/index/actions_spec.rb +127 -127
- data/spec/chewy/index/aliases_spec.rb +9 -9
- data/spec/chewy/index/search_spec.rb +4 -4
- data/spec/chewy/index/settings_spec.rb +33 -33
- data/spec/chewy/index_spec.rb +49 -49
- data/spec/chewy/query/criteria_spec.rb +173 -161
- data/spec/chewy/query/filters_spec.rb +76 -76
- data/spec/chewy/query/loading_spec.rb +54 -23
- data/spec/chewy/query/nodes/and_spec.rb +4 -4
- data/spec/chewy/query/nodes/bool_spec.rb +8 -8
- data/spec/chewy/query/nodes/equal_spec.rb +19 -19
- data/spec/chewy/query/nodes/exists_spec.rb +6 -6
- data/spec/chewy/query/nodes/has_child_spec.rb +25 -25
- data/spec/chewy/query/nodes/has_parent_spec.rb +25 -25
- data/spec/chewy/query/nodes/match_all_spec.rb +1 -1
- data/spec/chewy/query/nodes/missing_spec.rb +4 -4
- data/spec/chewy/query/nodes/not_spec.rb +4 -4
- data/spec/chewy/query/nodes/or_spec.rb +4 -4
- data/spec/chewy/query/nodes/prefix_spec.rb +5 -5
- data/spec/chewy/query/nodes/query_spec.rb +2 -2
- data/spec/chewy/query/nodes/range_spec.rb +18 -18
- data/spec/chewy/query/nodes/raw_spec.rb +1 -1
- data/spec/chewy/query/nodes/regexp_spec.rb +18 -18
- data/spec/chewy/query/nodes/script_spec.rb +4 -4
- data/spec/chewy/query/pagination/kaminari_spec.rb +41 -39
- data/spec/chewy/query/pagination/will_paginage_spec.rb +60 -0
- data/spec/chewy/query/pagination_spec.rb +8 -7
- data/spec/chewy/query_spec.rb +166 -167
- data/spec/chewy/rspec/update_index_spec.rb +1 -1
- data/spec/chewy/runtime/version_spec.rb +30 -30
- data/spec/chewy/runtime_spec.rb +3 -3
- data/spec/chewy/type/actions_spec.rb +3 -3
- data/spec/chewy/type/adapter/active_record_spec.rb +143 -143
- data/spec/chewy/type/adapter/mongoid_spec.rb +219 -0
- data/spec/chewy/type/adapter/object_spec.rb +39 -39
- data/spec/chewy/type/import_spec.rb +67 -37
- data/spec/chewy/type/mapping_spec.rb +12 -12
- data/spec/chewy/type/observe_spec.rb +5 -6
- data/spec/chewy/type/wrapper_spec.rb +12 -12
- data/spec/chewy_spec.rb +26 -28
- data/spec/spec_helper.rb +19 -31
- data/spec/support/active_record.rb +52 -0
- data/spec/support/class_helpers.rb +0 -4
- data/spec/support/mongoid.rb +87 -0
- metadata +33 -18
- data/gemfiles/Gemfile.rails-3.2 +0 -15
- data/gemfiles/Gemfile.rails-4.0 +0 -15
data/spec/chewy/query_spec.rb
CHANGED
@@ -1,9 +1,7 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
3
|
describe Chewy::Query do
|
4
|
-
before
|
5
|
-
Chewy.client.indices.delete index: '*'
|
6
|
-
end
|
4
|
+
before { Chewy.massacre }
|
7
5
|
|
8
6
|
before do
|
9
7
|
stub_index(:products) do
|
@@ -18,7 +16,7 @@ describe Chewy::Query do
|
|
18
16
|
subject { described_class.new(ProductsIndex) }
|
19
17
|
|
20
18
|
context 'unexistent index' do
|
21
|
-
specify { subject.to_a.
|
19
|
+
specify { expect(subject.to_a).to eq([]) }
|
22
20
|
end
|
23
21
|
|
24
22
|
context 'integration' do
|
@@ -31,114 +29,114 @@ describe Chewy::Query do
|
|
31
29
|
ProductsIndex::Country.import!(countries.map { |h| double(h) })
|
32
30
|
end
|
33
31
|
|
34
|
-
specify { subject.count.
|
35
|
-
specify { subject.first._data.
|
36
|
-
specify { subject.limit(6).count.
|
37
|
-
specify { subject.offset(6).count.
|
38
|
-
specify { subject.query(match: {name: 'name3'}).highlight(fields: {name: {}}).first.name.
|
39
|
-
specify { subject.query(match: {name: 'name3'}).highlight(fields: {name: {}}).first._data['_source']['name'].
|
40
|
-
specify { subject.types(:product).count.
|
41
|
-
specify { subject.types(:product, :country).count.
|
42
|
-
specify { subject.filter(term: {age: 10}).count.
|
43
|
-
specify { subject.query(term: {age: 10}).count.
|
32
|
+
specify { expect(subject.count).to eq(9) }
|
33
|
+
specify { expect(subject.first._data).to be_a Hash }
|
34
|
+
specify { expect(subject.limit(6).count).to eq(6) }
|
35
|
+
specify { expect(subject.offset(6).count).to eq(3) }
|
36
|
+
specify { expect(subject.query(match: {name: 'name3'}).highlight(fields: {name: {}}).first.name).to eq('<em>Name3</em>') }
|
37
|
+
specify { expect(subject.query(match: {name: 'name3'}).highlight(fields: {name: {}}).first._data['_source']['name']).to eq('Name3') }
|
38
|
+
specify { expect(subject.types(:product).count).to eq(3) }
|
39
|
+
specify { expect(subject.types(:product, :country).count).to eq(6) }
|
40
|
+
specify { expect(subject.filter(term: {age: 10}).count).to eq(1) }
|
41
|
+
specify { expect(subject.query(term: {age: 10}).count).to eq(1) }
|
44
42
|
end
|
45
43
|
|
46
44
|
describe '#==' do
|
47
45
|
let(:data) { 3.times.map { |i| {id: i.next.to_s, name: "Name#{i.next}", age: 10 * i.next}.stringify_keys! } }
|
48
46
|
before { ProductsIndex::Product.import!(data.map { |h| double(h) }) }
|
49
47
|
|
50
|
-
specify { subject.query(match: 'hello').
|
51
|
-
specify { subject.query(match: 'hello').
|
52
|
-
specify { subject.limit(10).
|
53
|
-
specify { subject.limit(10).
|
54
|
-
specify { subject.limit(2).
|
48
|
+
specify { expect(subject.query(match: 'hello')).to eq(subject.query(match: 'hello')) }
|
49
|
+
specify { expect(subject.query(match: 'hello')).not_to eq(subject.query(match: 'world')) }
|
50
|
+
specify { expect(subject.limit(10)).to eq(subject.limit(10)) }
|
51
|
+
specify { expect(subject.limit(10)).not_to eq(subject.limit(11)) }
|
52
|
+
specify { expect(subject.limit(2)).to eq(subject.limit(2).to_a) }
|
55
53
|
end
|
56
54
|
|
57
55
|
describe '#query_mode' do
|
58
|
-
specify { subject.query_mode(:should).
|
59
|
-
specify { subject.query_mode(:should).
|
60
|
-
specify { subject.query_mode(:should).criteria.options.
|
56
|
+
specify { expect(subject.query_mode(:should)).to be_a described_class }
|
57
|
+
specify { expect(subject.query_mode(:should)).not_to eq(subject) }
|
58
|
+
specify { expect(subject.query_mode(:should).criteria.options).to include(query_mode: :should) }
|
61
59
|
specify { expect { subject.query_mode(:should) }.not_to change { subject.criteria.options } }
|
62
60
|
end
|
63
61
|
|
64
62
|
describe '#filter_mode' do
|
65
|
-
specify { subject.filter_mode(:or).
|
66
|
-
specify { subject.filter_mode(:or).
|
67
|
-
specify { subject.filter_mode(:or).criteria.options.
|
63
|
+
specify { expect(subject.filter_mode(:or)).to be_a described_class }
|
64
|
+
specify { expect(subject.filter_mode(:or)).not_to eq(subject) }
|
65
|
+
specify { expect(subject.filter_mode(:or).criteria.options).to include(filter_mode: :or) }
|
68
66
|
specify { expect { subject.filter_mode(:or) }.not_to change { subject.criteria.options } }
|
69
67
|
end
|
70
68
|
|
71
69
|
describe '#post_filter_mode' do
|
72
|
-
specify { subject.post_filter_mode(:or).
|
73
|
-
specify { subject.post_filter_mode(:or).
|
74
|
-
specify { subject.post_filter_mode(:or).criteria.options.
|
70
|
+
specify { expect(subject.post_filter_mode(:or)).to be_a described_class }
|
71
|
+
specify { expect(subject.post_filter_mode(:or)).not_to eq(subject) }
|
72
|
+
specify { expect(subject.post_filter_mode(:or).criteria.options).to include(post_filter_mode: :or) }
|
75
73
|
specify { expect { subject.post_filter_mode(:or) }.not_to change { subject.criteria.options } }
|
76
74
|
end
|
77
75
|
|
78
76
|
describe '#boost_mode' do
|
79
|
-
specify { subject.boost_mode(:replace).
|
80
|
-
specify { subject.boost_mode(:replace).
|
81
|
-
specify { subject.boost_mode(:replace).criteria.options.
|
77
|
+
specify { expect(subject.boost_mode(:replace)).to be_a described_class }
|
78
|
+
specify { expect(subject.boost_mode(:replace)).not_to eq(subject) }
|
79
|
+
specify { expect(subject.boost_mode(:replace).criteria.options).to include(boost_mode: :replace) }
|
82
80
|
specify { expect { subject.boost_mode(:replace) }.not_to change { subject.criteria.options } }
|
83
81
|
end
|
84
82
|
|
85
83
|
describe '#score_mode' do
|
86
|
-
specify { subject.score_mode(:first).
|
87
|
-
specify { subject.score_mode(:first).
|
88
|
-
specify { subject.score_mode(:first).criteria.options.
|
84
|
+
specify { expect(subject.score_mode(:first)).to be_a described_class }
|
85
|
+
specify { expect(subject.score_mode(:first)).not_to eq(subject) }
|
86
|
+
specify { expect(subject.score_mode(:first).criteria.options).to include(score_mode: :first) }
|
89
87
|
specify { expect { subject.score_mode(:first) }.not_to change { subject.criteria.options } }
|
90
88
|
end
|
91
89
|
|
92
90
|
describe '#limit' do
|
93
|
-
specify { subject.limit(10).
|
94
|
-
specify { subject.limit(10).
|
95
|
-
specify { subject.limit(10).criteria.request_options.
|
91
|
+
specify { expect(subject.limit(10)).to be_a described_class }
|
92
|
+
specify { expect(subject.limit(10)).not_to eq(subject) }
|
93
|
+
specify { expect(subject.limit(10).criteria.request_options).to include(size: 10) }
|
96
94
|
specify { expect { subject.limit(10) }.not_to change { subject.criteria.request_options } }
|
97
95
|
end
|
98
96
|
|
99
97
|
describe '#offset' do
|
100
|
-
specify { subject.offset(10).
|
101
|
-
specify { subject.offset(10).
|
102
|
-
specify { subject.offset(10).criteria.request_options.
|
98
|
+
specify { expect(subject.offset(10)).to be_a described_class }
|
99
|
+
specify { expect(subject.offset(10)).not_to eq(subject) }
|
100
|
+
specify { expect(subject.offset(10).criteria.request_options).to include(from: 10) }
|
103
101
|
specify { expect { subject.offset(10) }.not_to change { subject.criteria.request_options } }
|
104
102
|
end
|
105
103
|
|
106
104
|
describe '#script_score' do
|
107
|
-
specify { subject.script_score('23').
|
108
|
-
specify { subject.script_score('23').
|
109
|
-
specify { subject.script_score('23').criteria.scores.
|
105
|
+
specify { expect(subject.script_score('23')).to be_a described_class }
|
106
|
+
specify { expect(subject.script_score('23')).not_to eq(subject) }
|
107
|
+
specify { expect(subject.script_score('23').criteria.scores).to eq([ { script_score: { script: '23' } } ]) }
|
110
108
|
specify { expect { subject.script_score('23') }.not_to change { subject.criteria.scores } }
|
111
|
-
specify { subject.script_score('23', filter: { foo: :bar}).criteria.scores.
|
109
|
+
specify { expect(subject.script_score('23', filter: { foo: :bar}).criteria.scores).to eq([{ script_score: { script: '23' }, filter: { foo: :bar } }]) }
|
112
110
|
end
|
113
111
|
|
114
112
|
describe '#boost_factor' do
|
115
|
-
specify { subject.boost_factor('23').
|
116
|
-
specify { subject.boost_factor('23').
|
117
|
-
specify { subject.boost_factor('23').criteria.scores.
|
113
|
+
specify { expect(subject.boost_factor('23')).to be_a described_class }
|
114
|
+
specify { expect(subject.boost_factor('23')).not_to eq(subject) }
|
115
|
+
specify { expect(subject.boost_factor('23').criteria.scores).to eq([ { boost_factor: 23 } ]) }
|
118
116
|
specify { expect { subject.boost_factor('23') }.not_to change { subject.criteria.scores } }
|
119
|
-
specify { subject.boost_factor('23', filter: { foo: :bar}).criteria.scores.
|
117
|
+
specify { expect(subject.boost_factor('23', filter: { foo: :bar}).criteria.scores).to eq([{ boost_factor: 23, filter: { foo: :bar } }]) }
|
120
118
|
end
|
121
119
|
|
122
120
|
describe '#random_score' do
|
123
|
-
specify { subject.random_score('23').
|
124
|
-
specify { subject.random_score('23').
|
125
|
-
specify { subject.random_score('23').criteria.scores.
|
121
|
+
specify { expect(subject.random_score('23')).to be_a described_class }
|
122
|
+
specify { expect(subject.random_score('23')).not_to eq(subject) }
|
123
|
+
specify { expect(subject.random_score('23').criteria.scores).to eq([ { random_score: { seed: 23 } } ]) }
|
126
124
|
specify { expect { subject.random_score('23') }.not_to change { subject.criteria.scores } }
|
127
|
-
specify { subject.random_score('23', filter: { foo: :bar}).criteria.scores.
|
125
|
+
specify { expect(subject.random_score('23', filter: { foo: :bar}).criteria.scores).to eq([{ random_score: { seed: 23 }, filter: { foo: :bar } }]) }
|
128
126
|
end
|
129
127
|
|
130
128
|
describe '#field_value_score' do
|
131
|
-
specify { subject.field_value_factor(field: :boost).
|
132
|
-
specify { subject.field_value_factor(field: :boost).
|
133
|
-
specify { subject.field_value_factor(field: :boost).criteria.scores.
|
129
|
+
specify { expect(subject.field_value_factor(field: :boost)).to be_a described_class }
|
130
|
+
specify { expect(subject.field_value_factor(field: :boost)).not_to eq(subject) }
|
131
|
+
specify { expect(subject.field_value_factor(field: :boost).criteria.scores).to eq([ { field_value_factor: { field: :boost } } ]) }
|
134
132
|
specify { expect { subject.field_value_factor(field: :boost) }.not_to change { subject.criteria.scores } }
|
135
|
-
specify { subject.field_value_factor({ field: :boost }, filter: { foo: :bar}).criteria.scores.
|
133
|
+
specify { expect(subject.field_value_factor({ field: :boost }, filter: { foo: :bar}).criteria.scores).to eq([{ field_value_factor: { field: :boost }, filter: { foo: :bar } }]) }
|
136
134
|
end
|
137
135
|
|
138
136
|
describe '#decay' do
|
139
|
-
specify { subject.decay(:gauss, :field).
|
140
|
-
specify { subject.decay(:gauss, :field).
|
141
|
-
specify { subject.decay(:gauss, :field).criteria.scores.
|
137
|
+
specify { expect(subject.decay(:gauss, :field)).to be_a described_class }
|
138
|
+
specify { expect(subject.decay(:gauss, :field)).not_to eq(subject) }
|
139
|
+
specify { expect(subject.decay(:gauss, :field).criteria.scores).to eq([ {
|
142
140
|
gauss: {
|
143
141
|
field: {
|
144
142
|
origin: 0,
|
@@ -147,15 +145,15 @@ describe Chewy::Query do
|
|
147
145
|
decay: 0.1
|
148
146
|
}
|
149
147
|
}
|
150
|
-
}] }
|
148
|
+
}]) }
|
151
149
|
specify { expect { subject.decay(:gauss, :field) }.not_to change { subject.criteria.scores } }
|
152
150
|
specify {
|
153
|
-
subject.decay(:gauss, :field,
|
151
|
+
expect(subject.decay(:gauss, :field,
|
154
152
|
origin: '11, 12',
|
155
153
|
scale: '2km',
|
156
154
|
offset: '5km',
|
157
155
|
decay: 0.4,
|
158
|
-
filter: { foo: :bar }).criteria.scores.
|
156
|
+
filter: { foo: :bar }).criteria.scores).to eq([
|
159
157
|
{
|
160
158
|
gauss: {
|
161
159
|
field: {
|
@@ -167,19 +165,19 @@ describe Chewy::Query do
|
|
167
165
|
},
|
168
166
|
filter: { foo: :bar }
|
169
167
|
}
|
170
|
-
]
|
168
|
+
])
|
171
169
|
}
|
172
170
|
end
|
173
171
|
|
174
172
|
describe '#facets' do
|
175
|
-
specify { subject.facets(term: {field: 'hello'}).
|
176
|
-
specify { subject.facets(term: {field: 'hello'}).
|
177
|
-
specify { subject.facets(term: {field: 'hello'}).criteria.facets.
|
173
|
+
specify { expect(subject.facets(term: {field: 'hello'})).to be_a described_class }
|
174
|
+
specify { expect(subject.facets(term: {field: 'hello'})).not_to eq(subject) }
|
175
|
+
specify { expect(subject.facets(term: {field: 'hello'}).criteria.facets).to include(term: {field: 'hello'}) }
|
178
176
|
specify { expect { subject.facets(term: {field: 'hello'}) }.not_to change { subject.criteria.facets } }
|
179
177
|
|
180
|
-
context 'results' do
|
178
|
+
context 'results', :orm do
|
181
179
|
before { stub_model(:city) }
|
182
|
-
let(:cities) { 10.times.map { |i| City.create! name: "name#{i}", rating: i % 3 } }
|
180
|
+
let(:cities) { 10.times.map { |i| City.create! id: i + 1, name: "name#{i}", rating: i % 3 } }
|
183
181
|
|
184
182
|
before do
|
185
183
|
stub_index(:cities) do
|
@@ -191,8 +189,8 @@ describe Chewy::Query do
|
|
191
189
|
|
192
190
|
before { CitiesIndex::City.import! cities }
|
193
191
|
|
194
|
-
specify { CitiesIndex.facets.
|
195
|
-
specify { CitiesIndex.facets(ratings: {terms: {field: 'rating'}}).facets.
|
192
|
+
specify { expect(CitiesIndex.facets).to eq({}) }
|
193
|
+
specify { expect(CitiesIndex.facets(ratings: {terms: {field: 'rating'}}).facets).to eq({
|
196
194
|
'ratings' => {
|
197
195
|
'_type' => 'terms', 'missing' => 0, 'total' => 10, 'other' => 0,
|
198
196
|
'terms' => [
|
@@ -201,19 +199,19 @@ describe Chewy::Query do
|
|
201
199
|
{'term' => 1, 'count' => 3}
|
202
200
|
]
|
203
201
|
}
|
204
|
-
} }
|
202
|
+
}) }
|
205
203
|
end
|
206
204
|
end
|
207
205
|
|
208
206
|
describe '#aggregations' do
|
209
|
-
specify { subject.aggregations(aggregation1: {field: 'hello'}).
|
210
|
-
specify { subject.aggregations(aggregation1: {field: 'hello'}).
|
211
|
-
specify { subject.aggregations(aggregation1: {field: 'hello'}).criteria.aggregations.
|
207
|
+
specify { expect(subject.aggregations(aggregation1: {field: 'hello'})).to be_a described_class }
|
208
|
+
specify { expect(subject.aggregations(aggregation1: {field: 'hello'})).not_to eq(subject) }
|
209
|
+
specify { expect(subject.aggregations(aggregation1: {field: 'hello'}).criteria.aggregations).to include(aggregation1: {field: 'hello'}) }
|
212
210
|
specify { expect { subject.aggregations(aggregation1: {field: 'hello'}) }.not_to change { subject.criteria.aggregations } }
|
213
211
|
|
214
|
-
context 'results' do
|
212
|
+
context 'results', :orm do
|
215
213
|
before { stub_model(:city) }
|
216
|
-
let(:cities) { 10.times.map { |i| City.create! name: "name#{i}", rating: i % 3 } }
|
214
|
+
let(:cities) { 10.times.map { |i| City.create! id: i + 1, name: "name#{i}", rating: i % 3 } }
|
217
215
|
|
218
216
|
context do
|
219
217
|
before do
|
@@ -226,26 +224,26 @@ describe Chewy::Query do
|
|
226
224
|
|
227
225
|
before { CitiesIndex::City.import! cities }
|
228
226
|
|
229
|
-
specify { CitiesIndex.aggregations.
|
230
|
-
specify { CitiesIndex.aggregations(ratings: {terms: {field: 'rating'}})
|
231
|
-
.aggregations['ratings']['buckets'].map { |h| h.slice('key', 'doc_count') }.
|
227
|
+
specify { expect(CitiesIndex.aggregations).to eq({}) }
|
228
|
+
specify { expect(CitiesIndex.aggregations(ratings: {terms: {field: 'rating'}})
|
229
|
+
.aggregations['ratings']['buckets'].map { |h| h.slice('key', 'doc_count') }).to eq([
|
232
230
|
{ 'key' => 0, 'doc_count' => 4 },
|
233
231
|
{ 'key' => 1, 'doc_count' => 3 },
|
234
232
|
{ 'key' => 2, 'doc_count' => 3 }
|
235
|
-
] }
|
233
|
+
]) }
|
236
234
|
end
|
237
235
|
end
|
238
236
|
end
|
239
237
|
|
240
238
|
describe '#suggest' do
|
241
239
|
specify { subject.suggest(name1: {text: 'hello', term: {field: 'name'}}) }
|
242
|
-
specify { subject.suggest(name1: {text: 'hello'}).
|
240
|
+
specify { expect(subject.suggest(name1: {text: 'hello'})).not_to eq(subject) }
|
243
241
|
specify { expect(subject.suggest(name1: {text: 'hello'}).criteria.suggest).to include(name1: {text: 'hello'}) }
|
244
242
|
specify { expect { subject.suggest(name1: {text: 'hello'}) }.not_to change { subject.criteria.suggest } }
|
245
243
|
|
246
|
-
context 'results' do
|
244
|
+
context 'results', :orm do
|
247
245
|
before { stub_model(:city) }
|
248
|
-
let(:cities) { 10.times.map { |i| City.create! name: "name#{i}" } }
|
246
|
+
let(:cities) { 10.times.map { |i| City.create! id: i + 1, name: "name#{i}" } }
|
249
247
|
|
250
248
|
context do
|
251
249
|
before do
|
@@ -258,8 +256,8 @@ describe Chewy::Query do
|
|
258
256
|
|
259
257
|
before { CitiesIndex::City.import! cities }
|
260
258
|
|
261
|
-
specify { CitiesIndex.suggest.
|
262
|
-
specify { CitiesIndex.suggest(name: {text: 'name', term: {field: 'name'}}).suggest.
|
259
|
+
specify { expect(CitiesIndex.suggest).to eq({}) }
|
260
|
+
specify { expect(CitiesIndex.suggest(name: {text: 'name', term: {field: 'name'}}).suggest).to eq({
|
263
261
|
'name' => [
|
264
262
|
{'text' => 'name', 'offset' => 0, 'length' => 4, 'options' => [
|
265
263
|
{'text' => 'name0', 'score' => 0.75, 'freq' => 1},
|
@@ -269,7 +267,7 @@ describe Chewy::Query do
|
|
269
267
|
{'text' => 'name4', 'score' => 0.75, 'freq' => 1}
|
270
268
|
]
|
271
269
|
}
|
272
|
-
] }
|
270
|
+
] })
|
273
271
|
}
|
274
272
|
end
|
275
273
|
end
|
@@ -286,140 +284,140 @@ describe Chewy::Query do
|
|
286
284
|
ProductsIndex::Country.import!(countries.map { |h| double(h) })
|
287
285
|
end
|
288
286
|
|
289
|
-
specify { expect { subject.query(match: {name: 'name3'}).delete_all }.to change { ProductsIndex.
|
287
|
+
specify { expect { subject.query(match: {name: 'name3'}).delete_all }.to change { ProductsIndex.total }.from(9).to(8) }
|
290
288
|
specify { expect { subject.filter { age == [10, 20] }.delete_all }.to change { ProductsIndex.total_count }.from(9).to(7) }
|
291
|
-
specify { expect { subject.types(:product).delete_all }.to change { ProductsIndex::Product.
|
292
|
-
specify { expect { ProductsIndex.delete_all }.to change { ProductsIndex.
|
293
|
-
specify { expect { ProductsIndex::City.delete_all }.to change { ProductsIndex.
|
289
|
+
specify { expect { subject.types(:product).delete_all }.to change { ProductsIndex::Product.total_entries }.from(3).to(0) }
|
290
|
+
specify { expect { ProductsIndex.delete_all }.to change { ProductsIndex.total }.from(9).to(0) }
|
291
|
+
specify { expect { ProductsIndex::City.delete_all }.to change { ProductsIndex.total }.from(9).to(6) }
|
294
292
|
end
|
295
293
|
|
296
294
|
describe '#none' do
|
297
|
-
specify { subject.none.
|
298
|
-
specify { subject.none.
|
299
|
-
specify { subject.none.criteria.
|
295
|
+
specify { expect(subject.none).to be_a described_class }
|
296
|
+
specify { expect(subject.none).not_to eq(subject) }
|
297
|
+
specify { expect(subject.none.criteria).to be_none }
|
300
298
|
|
301
299
|
context do
|
302
300
|
before { expect_any_instance_of(described_class).not_to receive(:_response) }
|
303
301
|
|
304
|
-
specify { subject.none.to_a.
|
305
|
-
specify { subject.query(match: 'hello').none.to_a.
|
306
|
-
specify { subject.none.query(match: 'hello').to_a.
|
302
|
+
specify { expect(subject.none.to_a).to eq([]) }
|
303
|
+
specify { expect(subject.query(match: 'hello').none.to_a).to eq([]) }
|
304
|
+
specify { expect(subject.none.query(match: 'hello').to_a).to eq([]) }
|
307
305
|
end
|
308
306
|
end
|
309
307
|
|
310
308
|
describe '#strategy' do
|
311
|
-
specify { subject.strategy('query_first').
|
312
|
-
specify { subject.strategy('query_first').
|
313
|
-
specify { subject.strategy('query_first').criteria.options.
|
309
|
+
specify { expect(subject.strategy('query_first')).to be_a described_class }
|
310
|
+
specify { expect(subject.strategy('query_first')).not_to eq(subject) }
|
311
|
+
specify { expect(subject.strategy('query_first').criteria.options).to include(strategy: 'query_first') }
|
314
312
|
specify { expect { subject.strategy('query_first') }.not_to change { subject.criteria.options } }
|
315
313
|
end
|
316
314
|
|
317
315
|
describe '#query' do
|
318
|
-
specify { subject.query(match: 'hello').
|
319
|
-
specify { subject.query(match: 'hello').
|
320
|
-
specify { subject.query(match: 'hello').criteria.queries.
|
316
|
+
specify { expect(subject.query(match: 'hello')).to be_a described_class }
|
317
|
+
specify { expect(subject.query(match: 'hello')).not_to eq(subject) }
|
318
|
+
specify { expect(subject.query(match: 'hello').criteria.queries).to include(match: 'hello') }
|
321
319
|
specify { expect { subject.query(match: 'hello') }.not_to change { subject.criteria.queries } }
|
322
320
|
end
|
323
321
|
|
324
322
|
describe '#filter' do
|
325
|
-
specify { subject.filter(term: {field: 'hello'}).
|
326
|
-
specify { subject.filter(term: {field: 'hello'}).
|
323
|
+
specify { expect(subject.filter(term: {field: 'hello'})).to be_a described_class }
|
324
|
+
specify { expect(subject.filter(term: {field: 'hello'})).not_to eq(subject) }
|
327
325
|
specify { expect { subject.filter(term: {field: 'hello'}) }.not_to change { subject.criteria.filters } }
|
328
|
-
specify { subject.filter([{term: {field: 'hello'}}, {term: {field: 'world'}}]).criteria.filters
|
329
|
-
.
|
326
|
+
specify { expect(subject.filter([{term: {field: 'hello'}}, {term: {field: 'world'}}]).criteria.filters)
|
327
|
+
.to eq([{term: {field: 'hello'}}, {term: {field: 'world'}}]) }
|
330
328
|
|
331
329
|
specify { expect { subject.filter{ name == 'John' } }.not_to change { subject.criteria.filters } }
|
332
|
-
specify { subject.filter{ name == 'John' }.criteria.filters.
|
330
|
+
specify { expect(subject.filter{ name == 'John' }.criteria.filters).to eq([{term: {'name' => 'John'}}]) }
|
333
331
|
end
|
334
332
|
|
335
333
|
describe '#post_filter' do
|
336
|
-
specify { subject.post_filter(term: {field: 'hello'}).
|
337
|
-
specify { subject.post_filter(term: {field: 'hello'}).
|
334
|
+
specify { expect(subject.post_filter(term: {field: 'hello'})).to be_a described_class }
|
335
|
+
specify { expect(subject.post_filter(term: {field: 'hello'})).not_to eq(subject) }
|
338
336
|
specify { expect { subject.post_filter(term: {field: 'hello'}) }.not_to change { subject.criteria.post_filters } }
|
339
|
-
specify { subject.post_filter([{term: {field: 'hello'}}, {term: {field: 'world'}}]).criteria.post_filters
|
340
|
-
.
|
337
|
+
specify { expect(subject.post_filter([{term: {field: 'hello'}}, {term: {field: 'world'}}]).criteria.post_filters)
|
338
|
+
.to eq([{term: {field: 'hello'}}, {term: {field: 'world'}}]) }
|
341
339
|
|
342
340
|
specify { expect { subject.post_filter{ name == 'John' } }.not_to change { subject.criteria.post_filters } }
|
343
|
-
specify { subject.post_filter{ name == 'John' }.criteria.post_filters.
|
341
|
+
specify { expect(subject.post_filter{ name == 'John' }.criteria.post_filters).to eq([{term: {'name' => 'John'}}]) }
|
344
342
|
end
|
345
343
|
|
346
344
|
describe '#order' do
|
347
|
-
specify { subject.order(field: 'hello').
|
348
|
-
specify { subject.order(field: 'hello').
|
345
|
+
specify { expect(subject.order(field: 'hello')).to be_a described_class }
|
346
|
+
specify { expect(subject.order(field: 'hello')).not_to eq(subject) }
|
349
347
|
specify { expect { subject.order(field: 'hello') }.not_to change { subject.criteria.sort } }
|
350
348
|
|
351
|
-
specify { subject.order(:field).criteria.sort.
|
352
|
-
specify { subject.order([:field1, :field2]).criteria.sort.
|
353
|
-
specify { subject.order(field: :asc).criteria.sort.
|
354
|
-
specify { subject.order({field1: {order: :asc}, field2: :desc}).order([:field3], :field4).criteria.sort.
|
349
|
+
specify { expect(subject.order(:field).criteria.sort).to eq([:field]) }
|
350
|
+
specify { expect(subject.order([:field1, :field2]).criteria.sort).to eq([:field1, :field2]) }
|
351
|
+
specify { expect(subject.order(field: :asc).criteria.sort).to eq([{field: :asc}]) }
|
352
|
+
specify { expect(subject.order({field1: {order: :asc}, field2: :desc}).order([:field3], :field4).criteria.sort).to eq([{field1: {order: :asc}}, {field2: :desc}, :field3, :field4]) }
|
355
353
|
end
|
356
354
|
|
357
355
|
describe '#reorder' do
|
358
|
-
specify { subject.reorder(field: 'hello').
|
359
|
-
specify { subject.reorder(field: 'hello').
|
356
|
+
specify { expect(subject.reorder(field: 'hello')).to be_a described_class }
|
357
|
+
specify { expect(subject.reorder(field: 'hello')).not_to eq(subject) }
|
360
358
|
specify { expect { subject.reorder(field: 'hello') }.not_to change { subject.criteria.sort } }
|
361
359
|
|
362
|
-
specify { subject.order(:field1).reorder(:field2).criteria.sort.
|
363
|
-
specify { subject.order(:field1).reorder(:field2).order(:field3).criteria.sort.
|
364
|
-
specify { subject.order(:field1).reorder(:field2).reorder(:field3).criteria.sort.
|
360
|
+
specify { expect(subject.order(:field1).reorder(:field2).criteria.sort).to eq([:field2]) }
|
361
|
+
specify { expect(subject.order(:field1).reorder(:field2).order(:field3).criteria.sort).to eq([:field2, :field3]) }
|
362
|
+
specify { expect(subject.order(:field1).reorder(:field2).reorder(:field3).criteria.sort).to eq([:field3]) }
|
365
363
|
end
|
366
364
|
|
367
365
|
describe '#only' do
|
368
|
-
specify { subject.only(:field).
|
369
|
-
specify { subject.only(:field).
|
366
|
+
specify { expect(subject.only(:field)).to be_a described_class }
|
367
|
+
specify { expect(subject.only(:field)).not_to eq(subject) }
|
370
368
|
specify { expect { subject.only(:field) }.not_to change { subject.criteria.fields } }
|
371
369
|
|
372
|
-
specify { subject.only(:field1, :field2).criteria.fields.
|
373
|
-
specify { subject.only([:field1, :field2]).only(:field3).criteria.fields.
|
370
|
+
specify { expect(subject.only(:field1, :field2).criteria.fields).to match_array(['field1', 'field2']) }
|
371
|
+
specify { expect(subject.only([:field1, :field2]).only(:field3).criteria.fields).to match_array(['field1', 'field2', 'field3']) }
|
374
372
|
end
|
375
373
|
|
376
374
|
describe '#only!' do
|
377
|
-
specify { subject.only!(:field).
|
378
|
-
specify { subject.only!(:field).
|
375
|
+
specify { expect(subject.only!(:field)).to be_a described_class }
|
376
|
+
specify { expect(subject.only!(:field)).not_to eq(subject) }
|
379
377
|
specify { expect { subject.only!(:field) }.not_to change { subject.criteria.fields } }
|
380
378
|
|
381
|
-
specify { subject.only!(:field1, :field2).criteria.fields.
|
382
|
-
specify { subject.only!([:field1, :field2]).only!(:field3).criteria.fields.
|
383
|
-
specify { subject.only([:field1, :field2]).only!(:field3).criteria.fields.
|
379
|
+
specify { expect(subject.only!(:field1, :field2).criteria.fields).to match_array(['field1', 'field2']) }
|
380
|
+
specify { expect(subject.only!([:field1, :field2]).only!(:field3).criteria.fields).to match_array(['field3']) }
|
381
|
+
specify { expect(subject.only([:field1, :field2]).only!(:field3).criteria.fields).to match_array(['field3']) }
|
384
382
|
end
|
385
383
|
|
386
384
|
describe '#types' do
|
387
|
-
specify { subject.types(:product).
|
388
|
-
specify { subject.types(:product).
|
385
|
+
specify { expect(subject.types(:product)).to be_a described_class }
|
386
|
+
specify { expect(subject.types(:product)).not_to eq(subject) }
|
389
387
|
specify { expect { subject.types(:product) }.not_to change { subject.criteria.types } }
|
390
388
|
|
391
|
-
specify { subject.types(:user).criteria.types.
|
392
|
-
specify { subject.types(:product, :city).criteria.types.
|
393
|
-
specify { subject.types([:product, :city]).types(:country).criteria.types.
|
389
|
+
specify { expect(subject.types(:user).criteria.types).to eq(['user']) }
|
390
|
+
specify { expect(subject.types(:product, :city).criteria.types).to match_array(['product', 'city']) }
|
391
|
+
specify { expect(subject.types([:product, :city]).types(:country).criteria.types).to match_array(['product', 'city', 'country']) }
|
394
392
|
end
|
395
393
|
|
396
394
|
describe '#types!' do
|
397
|
-
specify { subject.types!(:product).
|
398
|
-
specify { subject.types!(:product).
|
395
|
+
specify { expect(subject.types!(:product)).to be_a described_class }
|
396
|
+
specify { expect(subject.types!(:product)).not_to eq(subject) }
|
399
397
|
specify { expect { subject.types!(:product) }.not_to change { subject.criteria.types } }
|
400
398
|
|
401
|
-
specify { subject.types!(:user).criteria.types.
|
402
|
-
specify { subject.types!(:product, :city).criteria.types.
|
403
|
-
specify { subject.types!([:product, :city]).types!(:country).criteria.types.
|
404
|
-
specify { subject.types([:product, :city]).types!(:country).criteria.types.
|
399
|
+
specify { expect(subject.types!(:user).criteria.types).to eq(['user']) }
|
400
|
+
specify { expect(subject.types!(:product, :city).criteria.types).to match_array(['product', 'city']) }
|
401
|
+
specify { expect(subject.types!([:product, :city]).types!(:country).criteria.types).to match_array(['country']) }
|
402
|
+
specify { expect(subject.types([:product, :city]).types!(:country).criteria.types).to match_array(['country']) }
|
405
403
|
end
|
406
404
|
|
407
405
|
describe '#aggregations' do
|
408
|
-
specify { subject.aggregations(attribute: {terms: {field: 'attribute'}}).
|
409
|
-
specify { subject.aggregations(attribute: {terms: {field: 'attribute'}}).
|
410
|
-
specify { subject.aggregations(attribute: {terms: {field: 'attribute'}}).criteria.request_body[:body].
|
406
|
+
specify { expect(subject.aggregations(attribute: {terms: {field: 'attribute'}})).to be_a described_class }
|
407
|
+
specify { expect(subject.aggregations(attribute: {terms: {field: 'attribute'}})).not_to eq(subject) }
|
408
|
+
specify { expect(subject.aggregations(attribute: {terms: {field: 'attribute'}}).criteria.request_body[:body]).to include(aggregations: {attribute: {terms: {field: 'attribute'}}}) }
|
411
409
|
end
|
412
410
|
|
413
411
|
describe '#merge' do
|
414
412
|
let(:query) { described_class.new(ProductsIndex) }
|
415
413
|
|
416
|
-
specify { subject.filter { name == 'name' }.merge(query.filter { age == 42 }).criteria.filters
|
417
|
-
.
|
414
|
+
specify { expect(subject.filter { name == 'name' }.merge(query.filter { age == 42 }).criteria.filters)
|
415
|
+
.to eq([{term: {'name' => 'name'}}, {term: {'age' => 42}}]) }
|
418
416
|
end
|
419
417
|
|
420
|
-
describe '#to_a' do
|
418
|
+
describe '#to_a', :orm do
|
421
419
|
before { stub_model(:city) }
|
422
|
-
let(:cities) { 3.times.map { |i| City.create! name: "name#{i}", rating: i } }
|
420
|
+
let(:cities) { 3.times.map { |i| City.create! id: i + 1, name: "name#{i}", rating: i } }
|
423
421
|
|
424
422
|
context do
|
425
423
|
before do
|
@@ -434,22 +432,23 @@ describe Chewy::Query do
|
|
434
432
|
|
435
433
|
before { CitiesIndex::City.import! cities }
|
436
434
|
|
437
|
-
specify { CitiesIndex.order(:rating).first.
|
438
|
-
specify { CitiesIndex.order(:rating).first.name.
|
439
|
-
specify { CitiesIndex.order(:rating).first.rating.
|
440
|
-
specify { CitiesIndex.order(:rating).first.nested.
|
441
|
-
specify { CitiesIndex.order(:rating).first.id.
|
435
|
+
specify { expect(CitiesIndex.order(:rating).first).to be_a CitiesIndex::City }
|
436
|
+
specify { expect(CitiesIndex.order(:rating).first.name).to eq('name0') }
|
437
|
+
specify { expect(CitiesIndex.order(:rating).first.rating).to eq(0) }
|
438
|
+
specify { expect(CitiesIndex.order(:rating).first.nested).to eq({'name' => 'name0'}) }
|
439
|
+
specify { expect(CitiesIndex.order(:rating).first.id).to eq(cities.first.id.to_s) }
|
442
440
|
|
443
|
-
specify { CitiesIndex.order(:rating).only(:name).first.name.
|
444
|
-
specify { CitiesIndex.order(:rating).only(:name).first.rating.
|
445
|
-
specify { CitiesIndex.order(:rating).only(:nested).first.nested.
|
441
|
+
specify { expect(CitiesIndex.order(:rating).only(:name).first.name).to eq('name0') }
|
442
|
+
specify { expect(CitiesIndex.order(:rating).only(:name).first.rating).to be_nil }
|
443
|
+
specify { expect(CitiesIndex.order(:rating).only(:nested).first.nested).to eq({'name' => 'name0'}) }
|
446
444
|
|
447
|
-
specify { CitiesIndex.order(:rating).first._score.
|
448
|
-
specify { CitiesIndex.all.first._score.
|
449
|
-
specify { CitiesIndex.query(match: {name: 'name0'}).first._score.
|
445
|
+
specify { expect(CitiesIndex.order(:rating).first._score).to be_nil }
|
446
|
+
specify { expect(CitiesIndex.all.first._score).to be > 0 }
|
447
|
+
specify { expect(CitiesIndex.query(match: {name: 'name0'}).first._score).to be > 0 }
|
448
|
+
specify { expect(CitiesIndex.query(match: {name: 'name0'}).took).to be >= 0 }
|
450
449
|
|
451
|
-
specify { CitiesIndex.order(:rating).first.
|
452
|
-
specify { CitiesIndex.order(:rating).explain.first.
|
450
|
+
specify { expect(CitiesIndex.order(:rating).first._explanation).to be_nil }
|
451
|
+
specify { expect(CitiesIndex.order(:rating).explain.first._explanation).to be_present }
|
453
452
|
end
|
454
453
|
|
455
454
|
context 'sourceless' do
|
@@ -466,10 +465,10 @@ describe Chewy::Query do
|
|
466
465
|
end
|
467
466
|
before { CitiesIndex::City.import! cities }
|
468
467
|
|
469
|
-
specify { CitiesIndex.order(:rating).first.
|
470
|
-
specify { CitiesIndex.order(:rating).first.name.
|
471
|
-
specify { CitiesIndex.order(:rating).first.rating.
|
472
|
-
specify { CitiesIndex.order(:rating).first.nested.
|
468
|
+
specify { expect(CitiesIndex.order(:rating).first).to be_a CitiesIndex::City }
|
469
|
+
specify { expect(CitiesIndex.order(:rating).first.name).to be_nil }
|
470
|
+
specify { expect(CitiesIndex.order(:rating).first.rating).to be_nil }
|
471
|
+
specify { expect(CitiesIndex.order(:rating).first.nested).to be_nil }
|
473
472
|
end
|
474
473
|
end
|
475
474
|
end
|