aggs_test.rb
3.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
require_relative "test_helper"
class TestAggs < Minitest::Test
def setup
super
store [
{name: "Product Show", latitude: 37.7833, longitude: 12.4167, store_id: 1, in_stock: true, color: "blue", price: 21, created_at: 2.days.ago},
{name: "Product Hide", latitude: 29.4167, longitude: -98.5000, store_id: 2, in_stock: false, color: "green", price: 25, created_at: 2.days.from_now},
{name: "Product B", latitude: 43.9333, longitude: -122.4667, store_id: 2, in_stock: false, color: "red", price: 5},
{name: "Foo", latitude: 43.9333, longitude: 12.4667, store_id: 3, in_stock: false, color: "yellow", price: 15}
]
end
def test_basic
assert_equal ({1 => 1, 2 => 2}), store_agg(aggs: [:store_id])
end
def test_where
assert_equal ({1 => 1}), store_agg(aggs: {store_id: {where: {in_stock: true}}})
end
def test_field
assert_equal ({1 => 1, 2 => 2}), store_agg(aggs: {store_id: {}})
assert_equal ({1 => 1, 2 => 2}), store_agg(aggs: {store_id: {field: "store_id"}})
assert_equal ({1 => 1, 2 => 2}), store_agg({aggs: {store_id_new: {field: "store_id"}}}, "store_id_new")
end
def test_limit
agg = Product.search("Product", aggs: {store_id: {limit: 1}}).aggs["store_id"]
assert_equal 1, agg["buckets"].size
# assert_equal 3, agg["doc_count"]
assert_equal(1, agg["sum_other_doc_count"]) if Gem::Version.new(Searchkick.server_version) >= Gem::Version.new("1.4.0")
end
def test_where_no_smart_aggs
assert_equal ({2 => 2}), store_agg(where: {color: "red"}, aggs: {store_id: {where: {in_stock: false}}})
assert_equal ({2 => 2}), store_agg(where: {color: "blue"}, aggs: {store_id: {where: {in_stock: false}}})
end
def test_smart_aggs
assert_equal ({1 => 1}), store_agg(where: {in_stock: true}, aggs: [:store_id], smart_aggs: true)
end
def test_smart_aggs_where
assert_equal ({2 => 1}), store_agg(where: {color: "red"}, aggs: {store_id: {where: {in_stock: false}}}, smart_aggs: true)
end
def test_smart_aggs_where_override
assert_equal ({2 => 1}), store_agg(where: {color: "red"}, aggs: {store_id: {where: {in_stock: false, color: "blue"}}}, smart_aggs: true)
assert_equal ({}), store_agg(where: {color: "blue"}, aggs: {store_id: {where: {in_stock: false, color: "red"}}}, smart_aggs: true)
end
def test_smart_aggs_skip_agg
assert_equal ({1 => 1, 2 => 2}), store_agg(where: {store_id: 2}, aggs: [:store_id], smart_aggs: true)
end
def test_smart_aggs_skip_agg_complex
assert_equal ({1 => 1, 2 => 1}), store_agg(where: {store_id: 2, price: {gt: 5}}, aggs: [:store_id], smart_aggs: true)
end
def test_multiple_aggs
assert_equal ({"store_id" => {1 => 1, 2 => 2}, "color" => {"blue" => 1, "green" => 1, "red" => 1}}), store_multiple_aggs(aggs: [:store_id, :color])
end
protected
def buckets_as_hash(agg)
agg["buckets"].map { |v| [v["key"], v["doc_count"]] }.to_h
end
def store_agg(options, agg_key = "store_id")
buckets = Product.search("Product", options).aggs[agg_key]
buckets_as_hash(buckets)
end
def store_multiple_aggs(options)
Product.search("Product", options).aggs.map do |field, filtered_agg|
[field, buckets_as_hash(filtered_agg)]
end.to_h
end
end