lib/thinking_sphinx/configuration.rb in DrMark-thinking-sphinx-0.9.8 vs lib/thinking_sphinx/configuration.rb in DrMark-thinking-sphinx-0.9.9

- old
+ new

@@ -27,28 +27,27 @@ # # 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, 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. + # 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. # class Configuration attr_accessor :config_file, :searchd_log_file, :query_log_file, - :pid_file, :searchd_file_path, :address, :port, :allow_star, - :min_prefix_len, :min_infix_len, :mem_limit, :max_matches, :morphology, - :charset_type, :charset_table, :ignore_chars, :html_strip, - :html_remove_elements, :app_root + :pid_file, :searchd_file_path, :address, :port, :enable_star, + :allow_star, :min_prefix_len, :min_infix_len, :mem_limit, :max_matches, + :morphology, :charset_type, :charset_table, :ignore_chars, :html_strip, + :html_remove_elements, :database_yml_file, :app_root attr_reader :environment # Load in the configuration settings - this will look for config/sphinx.yml # and parse it according to the current environment. @@ -56,20 +55,22 @@ 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.database_yml_file = "#{self.app_root}/config/database.yml" 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.min_prefix_len = 1 - self.min_infix_len = 1 + 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 @@ -93,13 +94,12 @@ # 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. # 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 = YAML::load(ERB.new(IO.read("#{self.database_yml_file}")).result) database_confs.symbolize_keys! database_conf = database_confs[environment.to_sym] database_conf.symbolize_keys! open(file_path, "w") do |file| @@ -120,26 +120,28 @@ pid_file = #{self.pid_file} max_matches = #{self.max_matches} } CONFIG - ThinkingSphinx.indexed_models.each do |model| + ThinkingSphinx.indexed_models.each_with_index do |model, model_index| 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) + model.indexes.select { |index| index.model == model }.each_with_index do |index, i| + file.write index.to_config(model, i, database_conf, charset_type, model_index) 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? + sources << "#{ThinkingSphinx::Index.name(model)}_#{i}_core" + delta_sources << "#{ThinkingSphinx::Index.name(model)}_#{i}_delta" if index.delta? end - source_list = sources.collect { |s| "source = #{s}" }.join("\n") + next if sources.empty? + + 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) @@ -165,12 +167,11 @@ } begin model_name.camelize.constantize rescue LoadError - model_name.gsub!(/.*[\/\\]/, '') - retry + model_name.gsub!(/.*[\/\\]/, '').nil? ? next : retry rescue NameError next end end end @@ -192,14 +193,14 @@ end def core_index_for_model(model, sources) output = <<-INDEX -index #{model.indexes.first.name}_core +index #{ThinkingSphinx::Index.name(model)}_core { #{sources} -path = #{self.searchd_file_path}/#{model.indexes.first.name}_core +path = #{self.searchd_file_path}/#{ThinkingSphinx::Index.name(model)}_core charset_type = #{self.charset_type} INDEX morphology = model.indexes.inject(self.morphology) { |morph, index| index.options[:morphology] || morph @@ -207,46 +208,56 @@ output += " morphology = #{morphology}\n" unless morphology.blank? output += " charset_table = #{self.charset_table}\n" unless self.charset_table.nil? output += " ignore_chars = #{self.ignore_chars}\n" unless self.ignore_chars.nil? if self.allow_star + # Ye Olde way of turning on enable_star output += " enable_star = 1\n" output += " min_prefix_len = #{self.min_prefix_len}\n" - output += " min_infix_len = #{self.min_infix_len}\n" + else + # New, better way of turning on enable_star - thanks to James Healy + output += " enable_star = 1\n" if self.enable_star + output += " min_prefix_len = #{self.min_prefix_len}\n" unless self.min_prefix_len.nil? + output += " min_infix_len = #{self.min_infix_len}\n" unless self.min_infix_len.nil? end + output += " html_strip = 1\n" if self.html_strip output += " html_remove_elements = #{self.html_remove_elements}\n" unless self.html_remove_elements.blank? unless model.indexes.collect(&:prefix_fields).flatten.empty? - output += " prefix_fields = #{model.indexes.collect(&:prefix_fields).flatten.join(', ')}\n" + output += " prefix_fields = #{model.indexes.collect(&:prefix_fields).flatten.map(&:unique_name).join(', ')}\n" + else + output += " prefix_fields = _\n" unless model.indexes.collect(&:infix_fields).flatten.empty? end unless model.indexes.collect(&:infix_fields).flatten.empty? - output += " infix_fields = #{model.indexes.collect(&:infix_fields).flatten.join(', ')}\n" + output += " infix_fields = #{model.indexes.collect(&:infix_fields).flatten.map(&:unique_name).join(', ')}\n" + else + output += " infix_fields = -\n" unless model.indexes.collect(&:prefix_fields).flatten.empty? end output + "}\n" end def delta_index_for_model(model, sources) <<-INDEX -index #{model.indexes.first.name}_delta : #{model.indexes.first.name}_core +index #{ThinkingSphinx::Index.name(model)}_delta : #{ThinkingSphinx::Index.name(model)}_core { #{sources} - path = #{self.searchd_file_path}/#{model.indexes.first.name}_delta + path = #{self.searchd_file_path}/#{ThinkingSphinx::Index.name(model)}_delta } INDEX end def distributed_index_for_model(model) - sources = ["local = #{model.indexes.first.name}_core"] + sources = ["local = #{ThinkingSphinx::Index.name(model)}_core"] if model.indexes.any? { |index| index.delta? } - sources << "local = #{model.indexes.first.name}_delta" + sources << "local = #{ThinkingSphinx::Index.name(model)}_delta" end <<-INDEX -index #{model.indexes.first.name} +index #{ThinkingSphinx::Index.name(model)} { type = distributed #{ sources.join("\n ") } charset_type = #{self.charset_type} }