This class both keeps track of the configuration settings for Sphinx and also generates the resulting file for Sphinx to use.
Here are the default settings, relative to Rails.root where relevant:
config/#{environment}.sphinx.conf
log/searchd.log
log/searchd.query.log
log/searchd.#{environment}.pid
db/sphinx/#{environment}/
127.0.0.1
9312
false
5
1
1
64M
1000
nil
utf-8
nil
nil
false
”
searchd
indexer
0
If you want to change these settings, create a YAML file at config/sphinx.yml with settings for each environment, in a similar fashion to database.yml - using the following keys: #config_file, #searchd_log_file, #query_log_file, #pid_file, #searchd_file_path, port, #allow_star, enable_star, min_prefix_len, min_infix_len, mem_limit, max_matches, morphology, charset_type, charset_table, ignore_chars, html_strip, html_remove_elements, #delayed_job_priority, #searchd_binary_name, indexer_binary_name.
I think you’ve got the idea.
Each setting in the YAML file is optional - so only put in the ones you want to change.
Keep in mind, if for some particular reason you’re using a version of Sphinx older than 0.9.8 r871 (that’s prior to the proper 0.9.8 release), don’t set #allow_star to true.
# File lib/thinking_sphinx/configuration.rb, line 84 def self.configure(&block) yield instance instance.reset(instance.app_root) end
# File lib/thinking_sphinx/configuration.rb, line 134 def self.environment @@environment ||= if defined?(Merb) Merb.environment elsif defined?(Rails) Rails.env elsif defined?(Sinatra) Sinatra::Application.environment.to_s else ENV['RAILS_ENV'] || 'development' end end
Load in the configuration settings - this will look for config/sphinx.yml and parse it according to the current environment.
# File lib/thinking_sphinx/configuration.rb, line 80 def initialize(app_root = Dir.pwd) self.reset end
# File lib/thinking_sphinx/configuration.rb, line 146 def self.reset_environment ThinkingSphinx.mutex.synchronize do @@environment = nil end end
# File lib/thinking_sphinx/configuration.rb, line 182 def address @address end
# File lib/thinking_sphinx/configuration.rb, line 186 def address=(address) @address = address @configuration.searchd.address = address end
# File lib/thinking_sphinx/configuration.rb, line 240 def bin_path @controller.bin_path end
# File lib/thinking_sphinx/configuration.rb, line 244 def bin_path=(path) @controller.bin_path = path end
Generate the config file for Sphinx by using all the settings defined and looping through all the models with indexes to build the relevant indexer and searchd configuration, and sources and indexes details.
# File lib/thinking_sphinx/configuration.rb, line 172 def build(file_path=nil) file_path ||= "#{self.config_file}" generate open(file_path, "w") do |file| file.write @configuration.render end end
# File lib/thinking_sphinx/configuration.rb, line 266 def client client = Riddle::Client.new shuffled_addresses, port, configuration.searchd.client_key client.max_matches = configuration.searchd.max_matches || 1000 client.timeout = timeout || 0 client end
# File lib/thinking_sphinx/configuration.rb, line 232 def config_file @controller.path end
# File lib/thinking_sphinx/configuration.rb, line 236 def config_file=(file) @controller.path = file end
# File lib/thinking_sphinx/configuration.rb, line 152 def environment self.class.environment end
# File lib/thinking_sphinx/configuration.rb, line 156 def generate @configuration.indices.clear ThinkingSphinx.context.indexed_models.each do |model| model = model.constantize model.define_indexes @configuration.indices.concat model.to_riddle enforce_common_attribute_types end end
# File lib/thinking_sphinx/configuration.rb, line 256 def indexer_binary_name @controller.indexer_binary_name end
# File lib/thinking_sphinx/configuration.rb, line 260 def indexer_binary_name=(name) @controller.indexer_binary_name = name end
# File lib/thinking_sphinx/configuration.rb, line 274 def models_by_crc @models_by_crc ||= begin ThinkingSphinx.context.indexed_models.inject({}) do |hash, model| hash[model.constantize.to_crc32] = model model.constantize.descendants.each { |subclass| hash[subclass.to_crc32] = subclass.name } hash end end end
# File lib/thinking_sphinx/configuration.rb, line 208 def pid_file @configuration.searchd.pid_file end
# File lib/thinking_sphinx/configuration.rb, line 212 def pid_file=(pid_file) @configuration.searchd.pid_file = pid_file end
# File lib/thinking_sphinx/configuration.rb, line 191 def port @port end
# File lib/thinking_sphinx/configuration.rb, line 195 def port=(port) @port = port @configuration.searchd.port = port end
# File lib/thinking_sphinx/configuration.rb, line 224 def query_log_file @configuration.searchd.query_log end
# File lib/thinking_sphinx/configuration.rb, line 228 def query_log_file=(file) @configuration.searchd.query_log = file end
# File lib/thinking_sphinx/configuration.rb, line 89 def reset(custom_app_root=nil) if custom_app_root self.app_root = custom_app_root else self.app_root = Merb.root if defined?(Merb) self.app_root = Sinatra::Application.root if defined?(Sinatra) self.app_root = Rails.root if defined?(Rails) self.app_root ||= app_root end @configuration = Riddle::Configuration.new @configuration.searchd.pid_file = "#{self.app_root}/log/searchd.#{environment}.pid" @configuration.searchd.log = "#{self.app_root}/log/searchd.log" @configuration.searchd.query_log = "#{self.app_root}/log/searchd.query.log" @controller = Riddle::Controller.new @configuration, "#{self.app_root}/config/#{environment}.sphinx.conf" self.address = "127.0.0.1" self.port = 9312 self.searchd_file_path = "#{self.app_root}/db/sphinx/#{environment}" self.allow_star = false self.stop_timeout = 5 self.model_directories = initial_model_directories self.delayed_job_priority = 0 self.indexed_models = [] self.shuffle = false self.hard_retry_count = 0 self.source_options = {} self.index_options = { :charset_type => "utf-8" } self.version = nil parse_config self.version ||= @controller.sphinx_version ThinkingSphinx::Attribute::SphinxTypeMappings.merge!( :string => :sql_attr_string ) if Riddle.loaded_version.to_i > 1 self end
# File lib/thinking_sphinx/configuration.rb, line 248 def searchd_binary_name @controller.searchd_binary_name end
# File lib/thinking_sphinx/configuration.rb, line 252 def searchd_binary_name=(name) @controller.searchd_binary_name = name end
# File lib/thinking_sphinx/configuration.rb, line 216 def searchd_log_file @configuration.searchd.log end
# File lib/thinking_sphinx/configuration.rb, line 220 def searchd_log_file=(file) @configuration.searchd.log = file end
# File lib/thinking_sphinx/configuration.rb, line 286 def touch_reindex_file(output) return FileUtils.touch(@touched_reindex_file) if @touched_reindex_file and output =~ %rsuccesfully sent SIGHUP to searchd/ false end
# File lib/thinking_sphinx/configuration.rb, line 200 def use_socket=(use_socket) if use_socket socket = "#{app_root}/tmp/sockets/searchd.#{self.environment}.sock" @configuration.searchd.listen = socket self.address = socket end end