lib/searchkick.rb in searchkick-2.5.0 vs lib/searchkick.rb in searchkick-3.0.0
- old
+ new
@@ -1,33 +1,50 @@
require "active_model"
+require "active_support/core_ext/hash/deep_merge"
require "elasticsearch"
require "hashie"
-require "searchkick/version"
-require "searchkick/index_options"
+
+require "searchkick/bulk_indexer"
require "searchkick/index"
require "searchkick/indexer"
-require "searchkick/reindex_queue"
require "searchkick/hash_wrapper"
-require "searchkick/results"
-require "searchkick/query"
-require "searchkick/multi_search"
-require "searchkick/model"
-require "searchkick/tasks"
require "searchkick/middleware"
+require "searchkick/model"
+require "searchkick/multi_search"
+require "searchkick/query"
+require "searchkick/reindex_queue"
+require "searchkick/record_data"
+require "searchkick/record_indexer"
+require "searchkick/results"
+require "searchkick/version"
+
require "searchkick/logging" if defined?(ActiveSupport::Notifications)
-require "active_support/core_ext/hash/deep_merge"
+begin
+ require "rake"
+rescue LoadError
+ # do nothing
+end
+require "searchkick/tasks" if defined?(Rake)
+
+begin
+ require "rake"
+rescue LoadError
+ # do nothing
+end
+require "searchkick/tasks" if defined?(Rake)
+
# background jobs
begin
require "active_job"
rescue LoadError
# do nothing
end
if defined?(ActiveJob)
require "searchkick/bulk_reindex_job"
- require "searchkick/process_queue_job"
require "searchkick/process_batch_job"
+ require "searchkick/process_queue_job"
require "searchkick/reindex_v2_job"
end
module Searchkick
class Error < StandardError; end
@@ -36,20 +53,21 @@
class InvalidQueryError < Elasticsearch::Transport::Transport::Errors::BadRequest; end
class DangerousOperation < Error; end
class ImportError < Error; end
class << self
- attr_accessor :search_method_name, :wordnet_path, :timeout, :models, :client_options, :redis, :index_prefix, :index_suffix, :queue_name
+ attr_accessor :search_method_name, :wordnet_path, :timeout, :models, :client_options, :redis, :index_prefix, :index_suffix, :queue_name, :model_options
attr_writer :client, :env, :search_timeout
attr_reader :aws_credentials
end
self.search_method_name = :search
self.wordnet_path = "/var/lib/wn_s.pl"
self.timeout = 10
self.models = []
self.client_options = {}
self.queue_name = :searchkick
+ self.model_options = {}
def self.client
@client ||= begin
require "typhoeus/adapters/faraday" if defined?(Typhoeus)
@@ -78,32 +96,32 @@
def self.server_below?(version)
Gem::Version.new(server_version.sub("-", ".")) < Gem::Version.new(version.sub("-", "."))
end
- def self.search(term = "*", **options, &block)
- klass = options[:model]
+ def self.search(term = "*", model: nil, **options, &block)
+ klass = model
- # TODO add in next major version
- # if !klass
- # index_name = Array(options[:index_name])
- # if index_name.size == 1 && index_name.first.respond_to?(:searchkick_index)
- # klass = index_name.first
- # end
- # end
+ # make Searchkick.search(index_name: [Product]) and Product.search equivalent
+ unless klass
+ index_name = Array(options[:index_name])
+ if index_name.size == 1 && index_name.first.respond_to?(:searchkick_index)
+ klass = index_name.first
+ end
+ end
- query = Searchkick::Query.new(klass, term, options.except(:model))
+ query = Searchkick::Query.new(klass, term, options)
block.call(query.body) if block
if options[:execute] == false
query
else
query.execute
end
end
- def self.multi_search(queries, retry_misspellings: false)
- Searchkick::MultiSearch.new(queries, retry_misspellings: retry_misspellings).perform
+ def self.multi_search(queries)
+ Searchkick::MultiSearch.new(queries).perform
end
# callbacks
def self.enable_callbacks
@@ -112,21 +130,26 @@
def self.disable_callbacks
self.callbacks_value = false
end
- def self.callbacks?
- Thread.current[:searchkick_callbacks_enabled].nil? || Thread.current[:searchkick_callbacks_enabled]
+ def self.callbacks?(default: true)
+ if callbacks_value.nil?
+ default
+ else
+ callbacks_value != false
+ end
end
def self.callbacks(value)
if block_given?
previous_value = callbacks_value
begin
self.callbacks_value = value
- yield
+ result = yield
indexer.perform if callbacks_value == :bulk
+ result
ensure
self.callbacks_value = previous_value
end
else
self.callbacks_value = value
@@ -221,10 +244,10 @@
end
end
end
# TODO find better ActiveModel hook
-ActiveModel::Callbacks.send(:include, Searchkick::Model)
+ActiveModel::Callbacks.include(Searchkick::Model)
ActiveSupport.on_load(:active_record) do
extend Searchkick::Model
end