-
-
Save cesarizu/53ff8e99d9882c5382ef3213708d0262 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require "bundler/inline" | |
gemfile do | |
source "https://rubygems.org" | |
gem "activerecord", require: "active_record" | |
gem "activejob", require: "active_job" | |
gem "sqlite3" | |
gem "searchkick", git: "https://github.com/ankane/searchkick.git" | |
end | |
puts "Searchkick version: #{Searchkick::VERSION}" | |
puts "Elasticsearch version: #{Searchkick.server_version}" | |
ActiveRecord::Base.establish_connection adapter: "sqlite3", database: ":memory:" | |
ActiveJob::Base.queue_adapter = :inline | |
ActiveRecord::Migration.create_table :products do |t| | |
t.string :name | |
end | |
class Product < ActiveRecord::Base | |
searchkick | |
end | |
TOTAL_PRODUCTS = 10000 | |
BATCH_SIZE = 100 | |
full_reindex= false | |
TOTAL_PRODUCTS.times do |i| | |
Product.create!(name: "Test #{i}") | |
end | |
if full_reindex | |
Product.reindex | |
else | |
Product.reindex(import: false) | |
(TOTAL_PRODUCTS / BATCH_SIZE).times do |n| | |
Searchkick.callbacks(:bulk) { | |
Product.order(id: :asc).limit(BATCH_SIZE).offset(n * BATCH_SIZE) | |
} | |
end | |
end | |
p Product.search("test", fields: [:name]).response |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment