search_flip 2.0.0.beta2 → 2.0.0.beta3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,185 @@
1
+
2
+ require File.expand_path("../spec_helper", __dir__)
3
+
4
+ RSpec.describe SearchFlip::Response do
5
+ describe "#total_count" do
6
+ it "returns the number of results" do
7
+ ProductIndex.import create_list(:product, 3)
8
+
9
+ expect(ProductIndex.total_count).to eq(3)
10
+ expect(ProductIndex.total_entries).to eq(3)
11
+ end
12
+ end
13
+
14
+ describe "#current_page" do
15
+ it "returns the current page number" do
16
+ expect(ProductIndex.match_all.current_page).to eq(1)
17
+
18
+ ProductIndex.import create_list(:product, 3)
19
+
20
+ expect(ProductIndex.paginate(page: 1, per_page: 2).current_page).to eq(1)
21
+ expect(ProductIndex.paginate(page: 2, per_page: 2).current_page).to eq(2)
22
+ expect(ProductIndex.paginate(page: 3, per_page: 2).current_page).to eq(3)
23
+ end
24
+ end
25
+
26
+ describe "#total_pages" do
27
+ it "returns the number of total pages" do
28
+ expect(ProductIndex.paginate(page: 1, per_page: 2).total_pages).to eq(1)
29
+
30
+ ProductIndex.import create_list(:product, 3)
31
+
32
+ expect(ProductIndex.paginate(page: 1, per_page: 2).total_pages).to eq(2)
33
+ end
34
+ end
35
+
36
+ describe "#previous_page" do
37
+ it "returns the previous page number" do
38
+ ProductIndex.import create_list(:product, 3)
39
+
40
+ expect(ProductIndex.paginate(page: 1, per_page: 2).previous_page).to be_nil
41
+ expect(ProductIndex.paginate(page: 2, per_page: 2).previous_page).to eq(1)
42
+ expect(ProductIndex.paginate(page: 3, per_page: 2).previous_page).to eq(2)
43
+ end
44
+ end
45
+
46
+ describe "#next_page" do
47
+ it "returns the next page number" do
48
+ ProductIndex.import create_list(:product, 3)
49
+
50
+ expect(ProductIndex.paginate(page: 1, per_page: 2).next_page).to eq(2)
51
+ expect(ProductIndex.paginate(page: 2, per_page: 2).next_page).to be_nil
52
+ end
53
+ end
54
+
55
+ describe "#first_page?" do
56
+ it "returns whether or not the current page is the first page" do
57
+ ProductIndex.import create(:product)
58
+
59
+ expect(ProductIndex.paginate(page: 1).first_page?).to eq(true)
60
+ expect(ProductIndex.paginate(page: 2).first_page?).to eq(false)
61
+ end
62
+ end
63
+
64
+ describe "#last_page?" do
65
+ it "returns whether or not the current page is the last page" do
66
+ ProductIndex.import create_list(:product, 31)
67
+
68
+ expect(ProductIndex.paginate(page: 2).last_page?).to eq(true)
69
+ expect(ProductIndex.paginate(page: 1).last_page?).to eq(false)
70
+ end
71
+ end
72
+
73
+ describe "#out_of_range?" do
74
+ it "returns whether or not the current page is out of range" do
75
+ ProductIndex.import create(:product)
76
+
77
+ expect(ProductIndex.paginate(page: 2).out_of_range?).to eq(true)
78
+ expect(ProductIndex.paginate(page: 1).out_of_range?).to eq(false)
79
+ end
80
+ end
81
+
82
+ describe "#results" do
83
+ it "returns the current results" do
84
+ products = create_list(:product, 3)
85
+
86
+ ProductIndex.import products
87
+
88
+ expect(ProductIndex.match_all.results.map(&:id).to_set).to eq(products.map(&:id).to_set)
89
+ end
90
+ end
91
+
92
+ describe "#hits" do
93
+ it "returns the current hits" do
94
+ ProductIndex.import create_list(:product, 3)
95
+
96
+ response = ProductIndex.match_all.response
97
+
98
+ expect(response.hits).to be_present
99
+ expect(response.hits).to eq(response.raw_response["hits"])
100
+ end
101
+ end
102
+
103
+ describe "#scroll_id" do
104
+ it "returns the current scroll id" do
105
+ ProductIndex.import create_list(:product, 3)
106
+
107
+ response = ProductIndex.scroll.response
108
+
109
+ expect(response.scroll_id).to be_present
110
+ expect(response.scroll_id).to eq(response.raw_response["_scroll_id"])
111
+ end
112
+ end
113
+
114
+ describe "#records" do
115
+ it "returns the records for the current hits" do
116
+ products = create_list(:product, 3)
117
+
118
+ ProductIndex.import products
119
+
120
+ expect(ProductIndex.match_all.records.to_set).to eq(products.to_set)
121
+ end
122
+ end
123
+
124
+ describe "#ids" do
125
+ it "returns the ids for the current hits" do
126
+ products = create_list(:product, 3)
127
+
128
+ ProductIndex.import products
129
+
130
+ response = ProductIndex.match_all.response
131
+
132
+ expect(response.ids.to_set).to eq(products.map(&:id).map(&:to_s).to_set)
133
+ expect(response.ids).to eq(response.raw_response["hits"]["hits"].map { |hit| hit["_id"] })
134
+ end
135
+ end
136
+
137
+ describe "#took" do
138
+ it "returns the took value" do
139
+ ProductIndex.import create_list(:product, 3)
140
+
141
+ response = ProductIndex.match_all.response
142
+
143
+ expect(response.took).to be_present
144
+ expect(response.took).to eq(response.raw_response["took"])
145
+ end
146
+ end
147
+
148
+ describe "#aggregations" do
149
+ it "returns a convenient version of the aggregations" do
150
+ product1 = create(:product, price: 10, category: "category1")
151
+ product2 = create(:product, price: 20, category: "category2")
152
+ product3 = create(:product, price: 30, category: "category1")
153
+
154
+ ProductIndex.import [product1, product2, product3]
155
+
156
+ query = ProductIndex.aggregate(:category) do |aggregation|
157
+ aggregation.aggregate(price_sum: { sum: { field: "price" } })
158
+ end
159
+
160
+ aggregations = query.aggregations(:category).each_with_object({}) { |(key, agg), hash| hash[key] = agg.doc_count }
161
+ expect(aggregations).to eq("category1" => 2, "category2" => 1)
162
+
163
+ expect(query.aggregations(:category)["category1"].price_sum.value).to eq(40)
164
+ expect(query.aggregations(:category)["category2"].price_sum.value).to eq(20)
165
+ end
166
+
167
+ it "returns the raw aggregations if no key is specified" do
168
+ product1 = create(:product, category: "category1")
169
+ product2 = create(:product, category: "category2")
170
+ product3 = create(:product, category: "category1")
171
+
172
+ ProductIndex.import [product1, product2, product3]
173
+
174
+ query = ProductIndex.aggregate(:category)
175
+
176
+ expected = [
177
+ { "doc_count" => 2, "key" => "category1" },
178
+ { "doc_count" => 1, "key" => "category2" }
179
+ ]
180
+
181
+ expect(query.aggregations["category"]["buckets"]).to eq(expected)
182
+ end
183
+ end
184
+ end
185
+
@@ -0,0 +1,29 @@
1
+
2
+ require File.expand_path("../spec_helper", __dir__)
3
+ require "search_flip/to_json"
4
+
5
+ RSpec.describe "to_json" do
6
+ it "uses the correct format for Time" do
7
+ Timecop.freeze Time.parse("2018-01-01 12:00:00 UTC") do
8
+ expect(Time.now.utc.to_json).to eq("\"2018-01-01T12:00:00.000000Z\"")
9
+ end
10
+ end
11
+
12
+ it "uses the correct format for Date" do
13
+ Timecop.freeze Time.parse("2018-01-01 12:00:00 UTC") do
14
+ expect(Date.today.to_json).to eq("\"2018-01-01\"")
15
+ end
16
+ end
17
+
18
+ it "uses the correct format for DateTime" do
19
+ Timecop.freeze Time.parse("2018-01-01 12:00:00 UTC") do
20
+ expect(Time.now.utc.to_json).to eq("\"2018-01-01T12:00:00.000000Z\"")
21
+ end
22
+ end
23
+
24
+ it "uses the correct format for TimeWithZone" do
25
+ Timecop.freeze Time.parse("2018-01-01 12:00:00 UTC") do
26
+ expect(Time.find_zone("UTC").now.to_json).to eq("\"2018-01-01T12:00:00.000000Z\"")
27
+ end
28
+ end
29
+ end
@@ -1,16 +1,25 @@
1
1
 
2
- require "minitest"
3
- require "minitest/autorun"
4
- require "webmock/minitest"
5
- require "mocha/minitest"
6
2
  require "search_flip"
3
+ require "webmock/rspec"
7
4
  require "active_record"
8
5
  require "factory_bot"
9
6
  require "timecop"
10
7
  require "yaml"
11
8
 
9
+ require File.expand_path("delegate_matcher", __dir__)
10
+
12
11
  WebMock.allow_net_connect!
13
12
 
13
+ RSpec.configure do |config|
14
+ config.include FactoryBot::Syntax::Methods
15
+
16
+ config.before do
17
+ TestIndex.delete_index if TestIndex.index_exists?
18
+ ProductIndex.match_all.delete
19
+ Product.delete_all
20
+ end
21
+ end
22
+
14
23
  ActiveRecord::Base.establish_connection(adapter: "sqlite3", database: ":memory:")
15
24
 
16
25
  SearchFlip::Config[:auto_refresh] = true
@@ -169,75 +178,3 @@ end
169
178
 
170
179
  TestIndex.delete_index if TestIndex.index_exists?
171
180
 
172
- class SearchFlip::TestCase < MiniTest::Test
173
- include FactoryBot::Syntax::Methods
174
-
175
- def self.should_delegate_method(method, to:, subject:, as: method)
176
- define_method :"test_delegate_#{method}_to_#{to}" do
177
- assert subject.respond_to?(method), "subject doesn't respond to #{method}"
178
-
179
- target = subject.send(to)
180
-
181
- assert target.respond_to?(as), "#{to} doesn't respond to #{as}"
182
-
183
- params = Array.new(subject.method(method).arity.abs) { |i| "param-#{i}" }
184
-
185
- mock_target = mock
186
- mock_target.expects(as).with(*params)
187
-
188
- subject.stubs(to).returns(mock_target)
189
-
190
- subject.send(method, *params)
191
- end
192
- end
193
-
194
- def self.should_delegate_methods(*methods, to:, subject:)
195
- methods.each do |method|
196
- should_delegate_method method, to: to, subject: subject
197
- end
198
- end
199
-
200
- def assert_difference(expressions, difference = 1, &block)
201
- callables = Array(expressions).map { |e| -> { eval(e, block.binding) } }
202
-
203
- before = callables.map(&:call)
204
-
205
- res = yield
206
-
207
- Array(expressions).zip(callables).each_with_index do |(code, callable), i|
208
- assert_equal before[i] + difference, callable.call, "#{code.inspect} didn't change by #{difference}"
209
- end
210
-
211
- res
212
- end
213
-
214
- def assert_no_difference(expressions, &block)
215
- assert_difference(expressions, 0, &block)
216
- end
217
-
218
- def assert_not_nil(object)
219
- assert !object.nil?, "shouldn't be nil"
220
- end
221
-
222
- def assert_present(object)
223
- assert object.present?, "should be present"
224
- end
225
-
226
- def assert_blank(object)
227
- assert object.blank?, "should be blank"
228
- end
229
-
230
- def refute_present(object)
231
- refute object.present?, "shouldn't be present"
232
- end
233
-
234
- def refute_blank(object)
235
- refute object.blank?, "shouldn't be blank"
236
- end
237
-
238
- def setup
239
- ProductIndex.match_all.delete
240
- Product.delete_all
241
- TestIndex.delete_index if TestIndex.index_exists?
242
- end
243
- end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: search_flip
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.0.0.beta2
4
+ version: 2.0.0.beta3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Benjamin Vetter
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-02-27 00:00:00.000000000 Z
11
+ date: 2019-03-07 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord
@@ -53,21 +53,7 @@ dependencies:
53
53
  - !ruby/object:Gem::Version
54
54
  version: '0'
55
55
  - !ruby/object:Gem::Dependency
56
- name: minitest
57
- requirement: !ruby/object:Gem::Requirement
58
- requirements:
59
- - - ">="
60
- - !ruby/object:Gem::Version
61
- version: '0'
62
- type: :development
63
- prerelease: false
64
- version_requirements: !ruby/object:Gem::Requirement
65
- requirements:
66
- - - ">="
67
- - !ruby/object:Gem::Version
68
- version: '0'
69
- - !ruby/object:Gem::Dependency
70
- name: mocha
56
+ name: rake
71
57
  requirement: !ruby/object:Gem::Requirement
72
58
  requirements:
73
59
  - - ">="
@@ -81,7 +67,7 @@ dependencies:
81
67
  - !ruby/object:Gem::Version
82
68
  version: '0'
83
69
  - !ruby/object:Gem::Dependency
84
- name: rake
70
+ name: rspec
85
71
  requirement: !ruby/object:Gem::Requirement
86
72
  requirements:
87
73
  - - ">="
@@ -214,21 +200,25 @@ files:
214
200
  - lib/search_flip/to_json.rb
215
201
  - lib/search_flip/version.rb
216
202
  - search_flip.gemspec
217
- - test/search_flip/aggregation_test.rb
218
- - test/search_flip/bulk_test.rb
219
- - test/search_flip/connection_test.rb
220
- - test/search_flip/criteria_test.rb
221
- - test/search_flip/http_client_test.rb
222
- - test/search_flip/index_test.rb
223
- - test/search_flip/model_test.rb
224
- - test/search_flip/response_test.rb
225
- - test/search_flip/to_json_test.rb
226
- - test/test_helper.rb
203
+ - spec/delegate_matcher.rb
204
+ - spec/search_flip/aggregation_spec.rb
205
+ - spec/search_flip/bulk_spec.rb
206
+ - spec/search_flip/connection_spec.rb
207
+ - spec/search_flip/criteria_spec.rb
208
+ - spec/search_flip/http_client_spec.rb
209
+ - spec/search_flip/index_spec.rb
210
+ - spec/search_flip/model_spec.rb
211
+ - spec/search_flip/response_spec.rb
212
+ - spec/search_flip/to_json_spec.rb
213
+ - spec/spec_helper.rb
227
214
  homepage: https://github.com/mrkamel/search_flip
228
215
  licenses:
229
216
  - MIT
230
217
  metadata: {}
231
- post_install_message:
218
+ post_install_message: |
219
+ Thanks for using search_flip!
220
+ When upgrading from 1.x to 2.x, please check out
221
+ https://github.com/mrkamel/search_flip/blob/master/UPDATING.md
232
222
  rdoc_options: []
233
223
  require_paths:
234
224
  - lib
@@ -249,13 +239,14 @@ signing_key:
249
239
  specification_version: 4
250
240
  summary: Powerful ElasticSearch client library to easily build complex queries
251
241
  test_files:
252
- - test/search_flip/aggregation_test.rb
253
- - test/search_flip/bulk_test.rb
254
- - test/search_flip/connection_test.rb
255
- - test/search_flip/criteria_test.rb
256
- - test/search_flip/http_client_test.rb
257
- - test/search_flip/index_test.rb
258
- - test/search_flip/model_test.rb
259
- - test/search_flip/response_test.rb
260
- - test/search_flip/to_json_test.rb
261
- - test/test_helper.rb
242
+ - spec/delegate_matcher.rb
243
+ - spec/search_flip/aggregation_spec.rb
244
+ - spec/search_flip/bulk_spec.rb
245
+ - spec/search_flip/connection_spec.rb
246
+ - spec/search_flip/criteria_spec.rb
247
+ - spec/search_flip/http_client_spec.rb
248
+ - spec/search_flip/index_spec.rb
249
+ - spec/search_flip/model_spec.rb
250
+ - spec/search_flip/response_spec.rb
251
+ - spec/search_flip/to_json_spec.rb
252
+ - spec/spec_helper.rb
@@ -1,230 +0,0 @@
1
-
2
- require File.expand_path("../test_helper", __dir__)
3
-
4
- class SearchFlip::AggregationTest < SearchFlip::TestCase
5
- def test_where
6
- product1 = create(:product, category: "category1", title: "title", description: "description")
7
- product2 = create(:product, category: "category2", title: "title", description: "description")
8
- product3 = create(:product, category: "category1", title: "title", description: "description")
9
- product4 = create(:product, category: "category2", title: "title", description: "other")
10
- product5 = create(:product, category: "category1", title: "other", description: "description")
11
-
12
- ProductIndex.import [product1, product2, product3, product4, product5]
13
-
14
- query = ProductIndex.aggregate(category: {}) do |aggregation|
15
- aggregation.where(title: "title").where(description: "description").aggregate(:category)
16
- end
17
-
18
- assert_equal Hash["category1" => 2, "category2" => 1],
19
- query.aggregations(:category).category.buckets.each_with_object({}) { |bucket, hash| hash[bucket[:key]] = bucket.doc_count }
20
- end
21
-
22
- def test_where_with_array
23
- product1 = create(:product, category: "category1", title: "title1", description: "description1")
24
- product2 = create(:product, category: "category2", title: "title2", description: "description2")
25
- product3 = create(:product, category: "category1", title: "title3", description: "description3")
26
- product4 = create(:product, category: "category2", title: "title4", description: "other")
27
- product5 = create(:product, category: "category1", title: "other", description: "description")
28
-
29
- ProductIndex.import [product1, product2, product3, product4, product5]
30
-
31
- query = ProductIndex.aggregate(category: {}) do |aggregation|
32
- aggregation
33
- .where(title: ["title1", "title2", "title3", "title4"])
34
- .where(description: ["description1", "description2", "description3"])
35
- .aggregate(:category)
36
- end
37
-
38
- assert_equal Hash["category1" => 2, "category2" => 1],
39
- query.aggregations(:category).category.buckets.each_with_object({}) { |bucket, hash| hash[bucket[:key]] = bucket.doc_count }
40
- end
41
-
42
- def test_where_with_range
43
- product1 = create(:product, category: "category1", title: "title1", price: 100)
44
- product2 = create(:product, category: "category2", title: "title2", price: 150)
45
- product3 = create(:product, category: "category1", title: "title3", price: 200)
46
- product4 = create(:product, category: "category2", title: "title4", price: 250)
47
- product5 = create(:product, category: "category1", title: "other", price: 200)
48
-
49
- ProductIndex.import [product1, product2, product3, product4, product5]
50
-
51
- query = ProductIndex.aggregate(category: {}) do |aggregation|
52
- aggregation.where(title: "title1".."title3").where(price: 100..200).aggregate(:category)
53
- end
54
-
55
- assert_equal Hash["category1" => 2, "category2" => 1],
56
- query.aggregations(:category).category.buckets.each_with_object({}) { |bucket, hash| hash[bucket[:key]] = bucket.doc_count }
57
- end
58
-
59
- def test_where_not
60
- product1 = create(:product, category: "category1", title: "title1")
61
- product2 = create(:product, category: "category2", title: "title2")
62
- product3 = create(:product, category: "category1", title: "title3")
63
- product4 = create(:product, category: "category2", title: "title4")
64
- product5 = create(:product, category: "category1", title: "title5")
65
-
66
- ProductIndex.import [product1, product2, product3, product4, product5]
67
-
68
- query = ProductIndex.aggregate(category: {}) do |aggregation|
69
- aggregation.where_not(title: "title4").where_not(title: "title5").aggregate(:category)
70
- end
71
-
72
- assert_equal Hash["category1" => 2, "category2" => 1],
73
- query.aggregations(:category).category.buckets.each_with_object({}) { |bucket, hash| hash[bucket[:key]] = bucket.doc_count }
74
- end
75
-
76
- def test_where_not_with_array
77
- product1 = create(:product, category: "category1", title: "title1")
78
- product2 = create(:product, category: "category2", title: "title2")
79
- product3 = create(:product, category: "category1", title: "title3")
80
- product4 = create(:product, category: "category2", title: "title4")
81
- product5 = create(:product, category: "category1", title: "title5")
82
- product6 = create(:product, category: "category2", title: "title6")
83
- product7 = create(:product, category: "category1", title: "title7")
84
-
85
- ProductIndex.import [product1, product2, product3, product4, product5, product6, product7]
86
-
87
- query = ProductIndex.aggregate(category: {}) do |aggregation|
88
- aggregation.where_not(title: ["title1", "title2"]).where_not(title: ["title6", "title7"]).aggregate(:category)
89
- end
90
-
91
- assert_equal Hash["category1" => 2, "category2" => 1],
92
- query.aggregations(:category).category.buckets.each_with_object({}) { |bucket, hash| hash[bucket[:key]] = bucket.doc_count }
93
- end
94
-
95
- def test_where_not_with_range
96
- product1 = create(:product, category: "category1", title: "title1", price: 100)
97
- product2 = create(:product, category: "category2", title: "title2", price: 150)
98
- product3 = create(:product, category: "category1", title: "title3", price: 200)
99
- product4 = create(:product, category: "category2", title: "title4", price: 250)
100
- product5 = create(:product, category: "category1", title: "title5", price: 300)
101
- product6 = create(:product, category: "category2", title: "title6", price: 350)
102
- product7 = create(:product, category: "category1", title: "title7", price: 400)
103
-
104
- ProductIndex.import [product1, product2, product3, product4, product5, product6, product7]
105
-
106
- query = ProductIndex.aggregate(category: {}) do |aggregation|
107
- aggregation.where_not(price: 100..150).where_not(title: "title6".."title7").aggregate(:category)
108
- end
109
-
110
- assert_equal Hash["category1" => 2, "category2" => 1],
111
- query.aggregations(:category).category.buckets.each_with_object({}) { |bucket, hash| hash[bucket[:key]] = bucket.doc_count }
112
- end
113
-
114
- def test_filter
115
- product1 = create(:product, category: "category1", title: "title", price: 100)
116
- product2 = create(:product, category: "category2", title: "title", price: 150)
117
- product3 = create(:product, category: "category1", title: "title", price: 200)
118
- product4 = create(:product, category: "category2", title: "other", price: 200)
119
- product5 = create(:product, category: "category1", title: "title", price: 250)
120
-
121
- ProductIndex.import [product1, product2, product3, product4, product5]
122
-
123
- query = ProductIndex.aggregate(category: {}) do |aggregation|
124
- aggregation.filter(range: { price: { gte: 100, lte: 200 } }).filter(term: { title: "title" }).aggregate(:category)
125
- end
126
-
127
- assert_equal Hash["category1" => 2, "category2" => 1],
128
- query.aggregations(:category).category.buckets.each_with_object({}) { |bucket, hash| hash[bucket[:key]] = bucket.doc_count }
129
- end
130
-
131
- def test_range
132
- product1 = create(:product, category: "category1", title: "title1", price: 100)
133
- product2 = create(:product, category: "category2", title: "title2", price: 150)
134
- product3 = create(:product, category: "category1", title: "title3", price: 200)
135
- product4 = create(:product, category: "category2", title: "title4", price: 250)
136
- product5 = create(:product, category: "category1", title: "title5", price: 300)
137
- product6 = create(:product, category: "category2", title: "title6", price: 350)
138
- product7 = create(:product, category: "category1", title: "title7", price: 400)
139
-
140
- ProductIndex.import [product1, product2, product3, product4, product5, product6, product7]
141
-
142
- query = ProductIndex.aggregate(category: {}) do |aggregation|
143
- aggregation.range(:price, gte: 100, lte: 200).range(:title, gte: "title1", lte: "title3").aggregate(:category)
144
- end
145
-
146
- assert_equal Hash["category1" => 2, "category2" => 1],
147
- query.aggregations(:category).category.buckets.each_with_object({}) { |bucket, hash| hash[bucket[:key]] = bucket.doc_count }
148
- end
149
-
150
- def test_match_all
151
- product1 = create(:product, category: "category1")
152
- product2 = create(:product, category: "category2")
153
- product3 = create(:product, category: "category1")
154
-
155
- ProductIndex.import [product1, product2, product3]
156
-
157
- query = ProductIndex.aggregate(category: {}) do |aggregation|
158
- aggregation.match_all.aggregate(:category)
159
- end
160
-
161
- assert_equal Hash["category1" => 2, "category2" => 1],
162
- query.aggregations(:category).category.buckets.each_with_object({}) { |bucket, hash| hash[bucket[:key]] = bucket.doc_count }
163
- end
164
-
165
- def test_exists
166
- product1 = create(:product, category: "category1", title: "title1", price: 10)
167
- product2 = create(:product, category: "category2", title: "title2")
168
- product3 = create(:product, category: "category1", title: "title3", price: 20)
169
- product4 = create(:product, category: "category2", title: "title4", price: 30)
170
- product5 = create(:product, category: "category1", price: 40)
171
-
172
- ProductIndex.import [product1, product2, product3, product4, product5]
173
-
174
- query = ProductIndex.aggregate(category: {}) do |aggregation|
175
- aggregation.exists(:title).exists(:price).aggregate(:category)
176
- end
177
-
178
- assert_equal Hash["category1" => 2, "category2" => 1],
179
- query.aggregations(:category).category.buckets.each_with_object({}) { |bucket, hash| hash[bucket[:key]] = bucket.doc_count }
180
- end
181
-
182
- def test_exists_not
183
- product1 = create(:product, category: "category1")
184
- product2 = create(:product, category: "category2", title: "title2")
185
- product3 = create(:product, category: "category1")
186
- product4 = create(:product, category: "category2")
187
- product5 = create(:product, category: "category1", price: 40)
188
-
189
- ProductIndex.import [product1, product2, product3, product4, product5]
190
-
191
- query = ProductIndex.aggregate(category: {}) do |aggregation|
192
- aggregation.exists_not(:title).exists_not(:price).aggregate(:category)
193
- end
194
-
195
- assert_equal Hash["category1" => 2, "category2" => 1],
196
- query.aggregations(:category).category.buckets.each_with_object({}) { |bucket, hash| hash[bucket[:key]] = bucket.doc_count }
197
- end
198
-
199
- def test_aggregate
200
- product1 = create(:product, category: "category1", title: "title1", price: 10)
201
- product2 = create(:product, category: "category1", title: "title2", price: 15)
202
- product3 = create(:product, category: "category1", title: "title1", price: 20)
203
- product4 = create(:product, category: "category2", title: "title2", price: 25)
204
- product5 = create(:product, category: "category2", title: "title1", price: 30)
205
- product6 = create(:product, category: "category2", title: "title2", price: 35)
206
-
207
- ProductIndex.import [product1, product2, product3, product4, product5, product6]
208
-
209
- query = ProductIndex.aggregate(:category) do |aggregation|
210
- aggregation.aggregate(:title) do |aggregation2|
211
- aggregation2.aggregate(price: { sum: { field: "price" } })
212
- end
213
- end
214
-
215
- assert_equal Hash["category1" => 3, "category2" => 3],
216
- query.aggregations(:category).each_with_object({}) { |(key, agg), hash| hash[key] = agg.doc_count }
217
-
218
- assert_equal Hash["title1" => 2, "title2" => 1],
219
- query.aggregations(:category)["category1"].title.buckets.each_with_object({}) { |bucket, hash| hash[bucket[:key]] = bucket.doc_count }
220
-
221
- assert_equal Hash["title1" => 1, "title2" => 2],
222
- query.aggregations(:category)["category2"].title.buckets.each_with_object({}) { |bucket, hash| hash[bucket[:key]] = bucket.doc_count }
223
-
224
- assert_equal 30, query.aggregations(:category)["category1"].title.buckets.detect { |bucket| bucket[:key] == "title1" }.price.value
225
- assert_equal 15, query.aggregations(:category)["category1"].title.buckets.detect { |bucket| bucket[:key] == "title2" }.price.value
226
- assert_equal 30, query.aggregations(:category)["category2"].title.buckets.detect { |bucket| bucket[:key] == "title1" }.price.value
227
- assert_equal 60, query.aggregations(:category)["category2"].title.buckets.detect { |bucket| bucket[:key] == "title2" }.price.value
228
- end
229
- end
230
-