Class: ThinkingSphinx::Configuration
- Inherits:
-
Object
- Object
- ThinkingSphinx::Configuration
- Includes:
- Singleton
- Defined in:
- lib/thinking_sphinx/configuration.rb
Overview
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 file |
config/#Configuration.environment.sphinx.conf |
searchd log file |
log/searchd.log |
query log file |
log/searchd.query.log |
pid file |
log/searchd.#Configuration.environment.pid |
searchd files |
db/sphinx/#Configuration.environment/ |
address |
127.0.0.1 |
port |
9312 |
allow star |
false |
stop timeout |
5 |
min prefix length |
1 |
min infix length |
1 |
mem limit |
64M |
max matches |
1000 |
morphology |
nil |
charset type |
utf-8 |
charset table |
nil |
ignore chars |
nil |
html strip |
false |
html remove elements |
'' |
searchd_binary_name |
searchd |
indexer_binary_name |
indexer |
hard_retry_count |
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.
Constant Summary
- SourceOptions =
Riddle::Configuration::SQLSource.settings.map { |setting| setting.to_s } - %w( type sql_query_pre sql_query sql_joined_field sql_file_field sql_query_range sql_attr_uint sql_attr_bool sql_attr_bigint sql_query_info sql_attr_timestamp sql_attr_str2ordinal sql_attr_float sql_attr_multi sql_attr_string sql_attr_str2wordcount sql_column_buffers sql_field_string sql_field_str2wordcount )
- IndexOptions =
Riddle::Configuration::Index.settings.map { |setting| setting.to_s } - %w( source prefix_fields infix_fields )
- CustomOptions =
%w( disable_range use_64_bit )
- @@environment =
nil
Instance Attribute Summary (collapse)
-
- (Object) allow_star
Returns the value of attribute allow_star.
-
- (Object) app_root
Returns the value of attribute app_root.
-
- (Object) configuration
readonly
Returns the value of attribute configuration.
-
- (Object) controller
readonly
Returns the value of attribute controller.
-
- (Object) delayed_job_priority
Returns the value of attribute delayed_job_priority.
-
- (Object) hard_retry_count
Returns the value of attribute hard_retry_count.
-
- (Object) index_options
Returns the value of attribute index_options.
-
- (Object) indexed_models
Returns the value of attribute indexed_models.
-
- (Object) model_directories
Returns the value of attribute model_directories.
-
- (Object) searchd_file_path
Returns the value of attribute searchd_file_path.
-
- (Object) shuffle
Returns the value of attribute shuffle.
-
- (Object) source_options
Returns the value of attribute source_options.
-
- (Object) stop_timeout
Returns the value of attribute stop_timeout.
-
- (Object) timeout
Returns the value of attribute timeout.
-
- (Object) touched_reindex_file
Returns the value of attribute touched_reindex_file.
-
- (Object) use_64_bit
Returns the value of attribute use_64_bit.
-
- (Object) version
Returns the value of attribute version.
Class Method Summary (collapse)
Instance Method Summary (collapse)
- - (Object) address
- - (Object) address=(address)
- - (Object) bin_path
- - (Object) bin_path=(path)
-
- (Object) build(file_path = nil)
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.
- - (Object) client
- - (Object) config_file
- - (Object) config_file=(file)
- - (Object) environment
- - (Object) generate
- - (Object) indexer_binary_name
- - (Object) indexer_binary_name=(name)
-
- (Configuration) initialize(app_root = Dir.pwd)
constructor
Load in the configuration settings - this will look for config/sphinx.yml and parse it according to the current environment.
- - (Object) models_by_crc
- - (Object) pid_file
- - (Object) pid_file=(pid_file)
- - (Object) port
- - (Object) port=(port)
- - (Object) query_log_file
- - (Object) query_log_file=(file)
- - (Object) reset(custom_app_root = nil)
- - (Object) searchd_binary_name
- - (Object) searchd_binary_name=(name)
- - (Object) searchd_log_file
- - (Object) searchd_log_file=(file)
- - (Object) touch_reindex_file(output)
- - (Object) use_socket=(use_socket)
Constructor Details
- (Configuration) initialize(app_root = Dir.pwd)
Load in the configuration settings - this will look for config/sphinx.yml and parse it according to the current environment.
80 81 82 |
# File 'lib/thinking_sphinx/configuration.rb', line 80 def initialize(app_root = Dir.pwd) self.reset end |
Instance Attribute Details
- (Object) allow_star
Returns the value of attribute allow_star
66 67 68 |
# File 'lib/thinking_sphinx/configuration.rb', line 66 def allow_star @allow_star end |
- (Object) app_root
Returns the value of attribute app_root
66 67 68 |
# File 'lib/thinking_sphinx/configuration.rb', line 66 def app_root @app_root end |
- (Object) configuration (readonly)
Returns the value of attribute configuration
73 74 75 |
# File 'lib/thinking_sphinx/configuration.rb', line 73 def configuration @configuration end |
- (Object) controller (readonly)
Returns the value of attribute controller
73 74 75 |
# File 'lib/thinking_sphinx/configuration.rb', line 73 def controller @controller end |
- (Object) delayed_job_priority
Returns the value of attribute delayed_job_priority
66 67 68 |
# File 'lib/thinking_sphinx/configuration.rb', line 66 def delayed_job_priority @delayed_job_priority end |
- (Object) hard_retry_count
Returns the value of attribute hard_retry_count
66 67 68 |
# File 'lib/thinking_sphinx/configuration.rb', line 66 def hard_retry_count @hard_retry_count end |
- (Object) index_options
Returns the value of attribute index_options
71 72 73 |
# File 'lib/thinking_sphinx/configuration.rb', line 71 def @index_options end |
- (Object) indexed_models
Returns the value of attribute indexed_models
66 67 68 |
# File 'lib/thinking_sphinx/configuration.rb', line 66 def indexed_models @indexed_models end |
- (Object) model_directories
Returns the value of attribute model_directories
66 67 68 |
# File 'lib/thinking_sphinx/configuration.rb', line 66 def model_directories @model_directories end |
- (Object) searchd_file_path
Returns the value of attribute searchd_file_path
66 67 68 |
# File 'lib/thinking_sphinx/configuration.rb', line 66 def searchd_file_path @searchd_file_path end |
- (Object) shuffle
Returns the value of attribute shuffle
66 67 68 |
# File 'lib/thinking_sphinx/configuration.rb', line 66 def shuffle @shuffle end |
- (Object) source_options
Returns the value of attribute source_options
71 72 73 |
# File 'lib/thinking_sphinx/configuration.rb', line 71 def @source_options end |
- (Object) stop_timeout
Returns the value of attribute stop_timeout
66 67 68 |
# File 'lib/thinking_sphinx/configuration.rb', line 66 def stop_timeout @stop_timeout end |
- (Object) timeout
Returns the value of attribute timeout
264 265 266 |
# File 'lib/thinking_sphinx/configuration.rb', line 264 def timeout @timeout end |
- (Object) touched_reindex_file
Returns the value of attribute touched_reindex_file
66 67 68 |
# File 'lib/thinking_sphinx/configuration.rb', line 66 def touched_reindex_file @touched_reindex_file end |
- (Object) use_64_bit
Returns the value of attribute use_64_bit
66 67 68 |
# File 'lib/thinking_sphinx/configuration.rb', line 66 def use_64_bit @use_64_bit end |
- (Object) version
Returns the value of attribute version
66 67 68 |
# File 'lib/thinking_sphinx/configuration.rb', line 66 def version @version end |
Class Method Details
+ (Object) configure {|instance| ... }
84 85 86 87 |
# File 'lib/thinking_sphinx/configuration.rb', line 84 def self.configure(&block) yield instance instance.reset(instance.app_root) end |
+ (Object) environment
134 135 136 137 138 139 140 141 142 143 144 |
# 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 |
+ (Object) reset_environment
146 147 148 149 150 |
# File 'lib/thinking_sphinx/configuration.rb', line 146 def self.reset_environment ThinkingSphinx.mutex.synchronize do @@environment = nil end end |
Instance Method Details
- (Object) address
182 183 184 |
# File 'lib/thinking_sphinx/configuration.rb', line 182 def address @address end |
- (Object) address=(address)
186 187 188 189 |
# File 'lib/thinking_sphinx/configuration.rb', line 186 def address=(address) @address = address @configuration.searchd.address = address end |
- (Object) bin_path
240 241 242 |
# File 'lib/thinking_sphinx/configuration.rb', line 240 def bin_path @controller.bin_path end |
- (Object) bin_path=(path)
244 245 246 |
# File 'lib/thinking_sphinx/configuration.rb', line 244 def bin_path=(path) @controller.bin_path = path end |
- (Object) build(file_path = nil)
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.
172 173 174 175 176 177 178 179 180 |
# 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 |
- (Object) client
266 267 268 269 270 271 272 |
# 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 |
- (Object) config_file
232 233 234 |
# File 'lib/thinking_sphinx/configuration.rb', line 232 def config_file @controller.path end |
- (Object) config_file=(file)
236 237 238 |
# File 'lib/thinking_sphinx/configuration.rb', line 236 def config_file=(file) @controller.path = file end |
- (Object) environment
152 153 154 |
# File 'lib/thinking_sphinx/configuration.rb', line 152 def environment self.class.environment end |
- (Object) generate
156 157 158 159 160 161 162 163 164 165 166 |
# 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 |
- (Object) indexer_binary_name
256 257 258 |
# File 'lib/thinking_sphinx/configuration.rb', line 256 def indexer_binary_name @controller.indexer_binary_name end |
- (Object) indexer_binary_name=(name)
260 261 262 |
# File 'lib/thinking_sphinx/configuration.rb', line 260 def indexer_binary_name=(name) @controller.indexer_binary_name = name end |
- (Object) models_by_crc
274 275 276 277 278 279 280 281 282 283 284 |
# 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 |
- (Object) pid_file
208 209 210 |
# File 'lib/thinking_sphinx/configuration.rb', line 208 def pid_file @configuration.searchd.pid_file end |
- (Object) pid_file=(pid_file)
212 213 214 |
# File 'lib/thinking_sphinx/configuration.rb', line 212 def pid_file=(pid_file) @configuration.searchd.pid_file = pid_file end |
- (Object) port
191 192 193 |
# File 'lib/thinking_sphinx/configuration.rb', line 191 def port @port end |
- (Object) port=(port)
195 196 197 198 |
# File 'lib/thinking_sphinx/configuration.rb', line 195 def port=(port) @port = port @configuration.searchd.port = port end |
- (Object) query_log_file
224 225 226 |
# File 'lib/thinking_sphinx/configuration.rb', line 224 def query_log_file @configuration.searchd.query_log end |
- (Object) query_log_file=(file)
228 229 230 |
# File 'lib/thinking_sphinx/configuration.rb', line 228 def query_log_file=(file) @configuration.searchd.query_log = file end |
- (Object) reset(custom_app_root = nil)
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 |
# 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. = {} self. = { :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 |
- (Object) searchd_binary_name
248 249 250 |
# File 'lib/thinking_sphinx/configuration.rb', line 248 def searchd_binary_name @controller.searchd_binary_name end |
- (Object) searchd_binary_name=(name)
252 253 254 |
# File 'lib/thinking_sphinx/configuration.rb', line 252 def searchd_binary_name=(name) @controller.searchd_binary_name = name end |
- (Object) searchd_log_file
216 217 218 |
# File 'lib/thinking_sphinx/configuration.rb', line 216 def searchd_log_file @configuration.searchd.log end |
- (Object) searchd_log_file=(file)
220 221 222 |
# File 'lib/thinking_sphinx/configuration.rb', line 220 def searchd_log_file=(file) @configuration.searchd.log = file end |
- (Object) touch_reindex_file(output)
286 287 288 289 |
# 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 =~ /succesfully sent SIGHUP to searchd/ false end |
- (Object) use_socket=(use_socket)
200 201 202 203 204 205 206 |
# 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 |