search.rb
1018 Bytes
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
require "bundler/setup"
Bundler.require(:default)
require "active_record"
require "benchmark/ips"
ActiveRecord::Base.default_timezone = :utc
ActiveRecord::Base.time_zone_aware_attributes = true
ActiveRecord::Base.establish_connection adapter: "sqlite3", database: "/tmp/searchkick"
class Product < ActiveRecord::Base
searchkick batch_size: 1000
def search_data
{
name: name,
color: color,
store_id: store_id
}
end
end
# total_docs = 1000000
# ActiveRecord::Migration.create_table :products, force: :cascade do |t|
# t.string :name
# t.string :color
# t.integer :store_id
# end
# Product.import ["name", "color", "store_id"], total_docs.times.map { |i| ["Product #{i}", ["red", "blue"].sample, rand(10)] }
# puts "Imported"
# Product.reindex
query = Product.search("product", fields: [:name], where: {color: "red", store_id: 5}, limit: 10000, load: false, execute: false)
require "pp"
pp query.body.as_json
puts
Benchmark.ips do |x|
x.report { query.dup.execute }
end