Class: ThinkingSphinx::Configuration
- Inherits:
-
Object
- Object
- ThinkingSphinx::Configuration
- 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
-
3312
- allow star
-
false
- min prefix length
-
1
- min infix length
-
1
- mem limit
-
64M
- max matches
-
1000
- morphology
-
stem_en
- charset type
-
utf-8
- charset table
-
nil
- ignore chars
-
nil
- html strip
-
false
- html remove elements
-
”
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. 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.
Instance Attribute Summary collapse
-
#address ⇒ Object
Returns the value of attribute address.
-
#allow_star ⇒ Object
Returns the value of attribute allow_star.
-
#app_root ⇒ Object
Returns the value of attribute app_root.
-
#charset_table ⇒ Object
Returns the value of attribute charset_table.
-
#charset_type ⇒ Object
Returns the value of attribute charset_type.
-
#config_file ⇒ Object
Returns the value of attribute config_file.
-
#enable_star ⇒ Object
Returns the value of attribute enable_star.
-
#environment ⇒ Object
readonly
Returns the value of attribute environment.
-
#html_remove_elements ⇒ Object
Returns the value of attribute html_remove_elements.
-
#html_strip ⇒ Object
Returns the value of attribute html_strip.
-
#ignore_chars ⇒ Object
Returns the value of attribute ignore_chars.
-
#max_matches ⇒ Object
Returns the value of attribute max_matches.
-
#mem_limit ⇒ Object
Returns the value of attribute mem_limit.
-
#min_infix_len ⇒ Object
Returns the value of attribute min_infix_len.
-
#min_prefix_len ⇒ Object
Returns the value of attribute min_prefix_len.
-
#morphology ⇒ Object
Returns the value of attribute morphology.
-
#pid_file ⇒ Object
Returns the value of attribute pid_file.
-
#port ⇒ Object
Returns the value of attribute port.
-
#query_log_file ⇒ Object
Returns the value of attribute query_log_file.
-
#searchd_file_path ⇒ Object
Returns the value of attribute searchd_file_path.
-
#searchd_log_file ⇒ Object
Returns the value of attribute searchd_log_file.
Class Method Summary collapse
Instance Method Summary collapse
-
#build(file_path = nil) ⇒ Object
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.
-
#initialize(app_root = Dir.pwd) ⇒ Configuration
constructor
Load in the configuration settings - this will look for config/sphinx.yml and parse it according to the current environment.
-
#load_models ⇒ Object
Make sure all models are loaded - without reloading any that ActiveRecord::Base is already aware of (otherwise we start to hit some messy dependencies issues).
Constructor Details
#initialize(app_root = Dir.pwd) ⇒ Configuration
Load in the configuration settings - this will look for config/sphinx.yml and parse it according to the current environment.
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/thinking_sphinx/configuration.rb', line 55 def initialize(app_root = Dir.pwd) self.app_root = RAILS_ROOT if defined?(RAILS_ROOT) self.app_root = Merb.root if defined?(Merb) self.app_root ||= app_root self.config_file = "#{self.app_root}/config/#{environment}.sphinx.conf" self.searchd_log_file = "#{self.app_root}/log/searchd.log" self.query_log_file = "#{self.app_root}/log/searchd.query.log" self.pid_file = "#{self.app_root}/log/searchd.#{environment}.pid" self.searchd_file_path = "#{self.app_root}/db/sphinx/#{environment}" self.address = "127.0.0.1" self.port = 3312 self.allow_star = false self.enable_star = false self.min_prefix_len = nil self.min_infix_len = nil self.mem_limit = "64M" self.max_matches = 1000 self.morphology = "stem_en" self.charset_type = "utf-8" self.charset_table = nil self.ignore_chars = nil self.html_strip = false self.html_remove_elements = "" parse_config end |
Instance Attribute Details
#address ⇒ Object
Returns the value of attribute address.
44 45 46 |
# File 'lib/thinking_sphinx/configuration.rb', line 44 def address @address end |
#allow_star ⇒ Object
Returns the value of attribute allow_star.
44 45 46 |
# File 'lib/thinking_sphinx/configuration.rb', line 44 def allow_star @allow_star end |
#app_root ⇒ Object
Returns the value of attribute app_root.
44 45 46 |
# File 'lib/thinking_sphinx/configuration.rb', line 44 def app_root @app_root end |
#charset_table ⇒ Object
Returns the value of attribute charset_table.
44 45 46 |
# File 'lib/thinking_sphinx/configuration.rb', line 44 def charset_table @charset_table end |
#charset_type ⇒ Object
Returns the value of attribute charset_type.
44 45 46 |
# File 'lib/thinking_sphinx/configuration.rb', line 44 def charset_type @charset_type end |
#config_file ⇒ Object
Returns the value of attribute config_file.
44 45 46 |
# File 'lib/thinking_sphinx/configuration.rb', line 44 def config_file @config_file end |
#enable_star ⇒ Object
Returns the value of attribute enable_star.
44 45 46 |
# File 'lib/thinking_sphinx/configuration.rb', line 44 def enable_star @enable_star end |
#environment ⇒ Object (readonly)
Returns the value of attribute environment.
50 51 52 |
# File 'lib/thinking_sphinx/configuration.rb', line 50 def environment @environment end |
#html_remove_elements ⇒ Object
Returns the value of attribute html_remove_elements.
44 45 46 |
# File 'lib/thinking_sphinx/configuration.rb', line 44 def html_remove_elements @html_remove_elements end |
#html_strip ⇒ Object
Returns the value of attribute html_strip.
44 45 46 |
# File 'lib/thinking_sphinx/configuration.rb', line 44 def html_strip @html_strip end |
#ignore_chars ⇒ Object
Returns the value of attribute ignore_chars.
44 45 46 |
# File 'lib/thinking_sphinx/configuration.rb', line 44 def ignore_chars @ignore_chars end |
#max_matches ⇒ Object
Returns the value of attribute max_matches.
44 45 46 |
# File 'lib/thinking_sphinx/configuration.rb', line 44 def max_matches @max_matches end |
#mem_limit ⇒ Object
Returns the value of attribute mem_limit.
44 45 46 |
# File 'lib/thinking_sphinx/configuration.rb', line 44 def mem_limit @mem_limit end |
#min_infix_len ⇒ Object
Returns the value of attribute min_infix_len.
44 45 46 |
# File 'lib/thinking_sphinx/configuration.rb', line 44 def min_infix_len @min_infix_len end |
#min_prefix_len ⇒ Object
Returns the value of attribute min_prefix_len.
44 45 46 |
# File 'lib/thinking_sphinx/configuration.rb', line 44 def min_prefix_len @min_prefix_len end |
#morphology ⇒ Object
Returns the value of attribute morphology.
44 45 46 |
# File 'lib/thinking_sphinx/configuration.rb', line 44 def morphology @morphology end |
#pid_file ⇒ Object
Returns the value of attribute pid_file.
44 45 46 |
# File 'lib/thinking_sphinx/configuration.rb', line 44 def pid_file @pid_file end |
#port ⇒ Object
Returns the value of attribute port.
44 45 46 |
# File 'lib/thinking_sphinx/configuration.rb', line 44 def port @port end |
#query_log_file ⇒ Object
Returns the value of attribute query_log_file.
44 45 46 |
# File 'lib/thinking_sphinx/configuration.rb', line 44 def query_log_file @query_log_file end |
#searchd_file_path ⇒ Object
Returns the value of attribute searchd_file_path.
44 45 46 |
# File 'lib/thinking_sphinx/configuration.rb', line 44 def searchd_file_path @searchd_file_path end |
#searchd_log_file ⇒ Object
Returns the value of attribute searchd_log_file.
44 45 46 |
# File 'lib/thinking_sphinx/configuration.rb', line 44 def searchd_log_file @searchd_log_file end |
Class Method Details
.environment ⇒ Object
83 84 85 86 87 |
# File 'lib/thinking_sphinx/configuration.rb', line 83 def self.environment @@environment ||= ( defined?(Merb) ? ENV['MERB_ENV'] : ENV['RAILS_ENV'] ) || "development" end |
Instance Method Details
#build(file_path = nil) ⇒ Object
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.
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 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 |
# File 'lib/thinking_sphinx/configuration.rb', line 97 def build(file_path=nil) load_models file_path ||= "#{self.config_file}" database_confs = YAML::load(ERB.new(IO.read("#{app_root}/config/database.yml")).result) database_confs.symbolize_keys! database_conf = database_confs[environment.to_sym] database_conf.symbolize_keys! open(file_path, "w") do |file| file.write <<-CONFIG indexer { mem_limit = #{self.mem_limit} } searchd { address = #{self.address} port = #{self.port} log = #{self.searchd_log_file} query_log = #{self.query_log_file} read_timeout = 5 max_children = 30 pid_file = #{self.pid_file} max_matches = #{self.max_matches} } CONFIG ThinkingSphinx.indexed_models.each do |model| model = model.constantize sources = [] delta_sources = [] prefixed_fields = [] infixed_fields = [] model.indexes.each_with_index do |index, i| file.write index.to_config(i, database_conf, charset_type) create_array_accum if index.adapter == :postgres sources << "#{model.indexes.first.name}_#{i}_core" delta_sources << "#{model.indexes.first.name}_#{i}_delta" if index.delta? end source_list = sources.collect { |s| "source = #{s}" }.join("\n") delta_list = delta_sources.collect { |s| "source = #{s}" }.join("\n") file.write core_index_for_model(model, source_list) unless delta_list.blank? file.write delta_index_for_model(model, delta_list) end file.write distributed_index_for_model(model) end end end |
#load_models ⇒ Object
Make sure all models are loaded - without reloading any that ActiveRecord::Base is already aware of (otherwise we start to hit some messy dependencies issues).
157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 |
# File 'lib/thinking_sphinx/configuration.rb', line 157 def load_models base = "#{app_root}/app/models/" Dir["#{base}**/*.rb"].each do |file| model_name = file.gsub(/^#{base}([\w_\/\\]+)\.rb/, '\1') next if model_name.nil? next if ::ActiveRecord::Base.send(:subclasses).detect { |model| model.name == model_name } begin model_name.camelize.constantize rescue LoadError model_name.gsub!(/.*[\/\\]/, '') retry rescue NameError next end end end |