searchkick.rb
1.78 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
require "active_model"
require "elasticsearch"
require "hashie"
require "searchkick/version"
require "searchkick/index"
require "searchkick/results"
require "searchkick/query"
require "searchkick/reindex_job"
require "searchkick/model"
require "searchkick/tasks"
require "searchkick/logging" if defined?(Rails)
# background jobs
begin
require "active_job"
rescue LoadError
# do nothing
end
require "searchkick/reindex_v2_job" if defined?(ActiveJob)
module Searchkick
class MissingIndexError < StandardError; end
class UnsupportedVersionError < StandardError; end
class InvalidQueryError < Elasticsearch::Transport::Transport::Errors::BadRequest; end
class << self
attr_accessor :search_method_name
attr_accessor :wordnet_path
attr_accessor :timeout
attr_accessor :models
end
self.search_method_name = :search
self.wordnet_path = "/var/lib/wn_s.pl"
self.timeout = 10
self.models = []
def self.client
@client ||=
Elasticsearch::Client.new(
url: ENV["ELASTICSEARCH_URL"],
transport_options: {request: {timeout: timeout}}
)
end
class << self
attr_writer :client
end
def self.server_version
@server_version ||= client.info["version"]["number"]
end
def self.enable_callbacks
Thread.current[:searchkick_callbacks_enabled] = true
end
def self.disable_callbacks
Thread.current[:searchkick_callbacks_enabled] = false
end
def self.callbacks?
Thread.current[:searchkick_callbacks_enabled].nil? || Thread.current[:searchkick_callbacks_enabled]
end
def self.env
@env ||= ENV["RAILS_ENV"] || ENV["RACK_ENV"] || "development"
end
end
# TODO find better ActiveModel hook
ActiveModel::Callbacks.send(:include, Searchkick::Model)
ActiveRecord::Base.send(:extend, Searchkick::Model) if defined?(ActiveRecord)