index_test.rb
5.45 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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
require_relative "test_helper"
class IndexTest < Minitest::Test
def setup
super
Region.destroy_all
end
def test_clean_indices
suffix = Searchkick.index_suffix ? "_#{Searchkick.index_suffix}" : ""
old_index = Searchkick::Index.new("products_test#{suffix}_20130801000000000")
different_index = Searchkick::Index.new("items_test#{suffix}_20130801000000000")
old_index.delete if old_index.exists?
different_index.delete if different_index.exists?
# create indexes
old_index.create
different_index.create
Product.searchkick_index.clean_indices
assert Product.searchkick_index.exists?
assert different_index.exists?
assert !old_index.exists?
end
def test_clean_indices_old_format
suffix = Searchkick.index_suffix ? "_#{Searchkick.index_suffix}" : ""
old_index = Searchkick::Index.new("products_test#{suffix}_20130801000000")
old_index.create
Product.searchkick_index.clean_indices
assert !old_index.exists?
end
def test_retain
Product.reindex
assert_equal 1, Product.searchkick_index.all_indices.size
Product.reindex(retain: true)
assert_equal 2, Product.searchkick_index.all_indices.size
end
def test_total_docs
store_names ["Product A"]
assert_equal 1, Product.searchkick_index.total_docs
end
def test_mappings
store_names ["Dollar Tree"], Store
assert_equal ["Dollar Tree"], Store.search(body: {query: {match: {name: "dollar"}}}).map(&:name)
mapping = Store.search_index.mapping.values.first["mappings"]
mapping = mapping["store"] if Searchkick.server_below?("7.0.0")
assert_equal "text", mapping["properties"]["name"]["type"]
end
def test_body
store_names ["Dollar Tree"], Store
assert_equal ["Dollar Tree"], Store.search(body: {query: {match: {name: "dollar"}}}, load: false).map(&:name)
assert_equal ["Dollar Tree"], Store.search.body(query: {match: {name: "dollar"}}).load(false).map(&:name)
end
def test_body_incompatible_options
assert_raises(ArgumentError) do
Store.search(body: {query: {match: {name: "dollar"}}}, where: {id: 1}).to_a
end
assert_raises(ArgumentError) do
Store.search.body(query: {match: {name: "dollar"}}).where(id: 1).to_a
end
end
def test_block
store_names ["Dollar Tree"]
products =
Product.search "boom" do |body|
body[:query] = {match_all: {}}
end
assert_equal ["Dollar Tree"], products.map(&:name)
end
def test_tokens
assert_equal ["dollar", "dollartre", "tree"], Product.searchkick_index.tokens("Dollar Tree", analyzer: "searchkick_index")
end
def test_tokens_analyzer
assert_equal ["dollar", "tree"], Product.searchkick_index.tokens("Dollar Tree", analyzer: "searchkick_search2")
end
def test_record_not_found
store_names ["Product A", "Product B"]
Product.where(name: "Product A").delete_all
assert_output nil, /\[searchkick\] WARNING: Records in search index do not exist in database/ do
assert_search "product", ["Product B"]
end
ensure
Product.reindex
end
def test_bad_mapping
Product.searchkick_index.delete
store_names ["Product A"]
error = assert_raises(Searchkick::InvalidQueryError) { Product.search("test").to_a }
assert_equal "Bad mapping - run Product.reindex", error.message
ensure
Product.reindex
end
def test_remove_blank_id
store_names ["Product A"]
Product.searchkick_index.remove(Product.new)
assert_search "product", ["Product A"]
ensure
Product.reindex
end
def test_missing_index
assert_raises(Searchkick::MissingIndexError) { Product.search("test", index_name: "not_found").to_a }
end
def test_unsupported_version
raises_exception = ->(_) { raise Elasticsearch::Transport::Transport::Error, "[500] No query registered for [multi_match]" }
Searchkick.client.stub :search, raises_exception do
assert_raises(Searchkick::UnsupportedVersionError) { Product.search("test").to_a }
end
end
def test_invalid_body
assert_raises(Searchkick::InvalidQueryError) { Product.search(body: {boom: true}).to_a }
end
def test_transaction
skip unless defined?(ActiveRecord)
Product.transaction do
store_names ["Product A"]
raise ActiveRecord::Rollback
end
assert_search "*", []
end
def test_filterable
# skip for 5.0 since it throws
# Cannot search on field [alt_description] since it is not indexed.
store [{name: "Product A", alt_description: "Hello"}]
assert_raises(Searchkick::InvalidQueryError) do
assert_search "*", [], where: {alt_description: "Hello"}
end
end
def test_filterable_non_string
store [{name: "Product A", store_id: 1}]
assert_search "*", ["Product A"], where: {store_id: 1}
end
def test_large_value
skip if nobrainer?
large_value = 1000.times.map { "hello" }.join(" ")
store [{name: "Product A", text: large_value}], Region
assert_search "product", ["Product A"], {}, Region
assert_search "hello", ["Product A"], {fields: [:name, :text]}, Region
assert_search "hello", ["Product A"], {}, Region
end
def test_very_large_value
skip if nobrainer?
large_value = 10000.times.map { "hello" }.join(" ")
store [{name: "Product A", text: large_value}], Region
assert_search "product", ["Product A"], {}, Region
assert_search "hello", ["Product A"], {fields: [:name, :text]}, Region
# values that exceed ignore_above are not included in _all field :(
# assert_search "hello", ["Product A"], {}, Region
end
end