Class: ThinkingSphinx::Configuration

Inherits:
Object
  • Object
show all
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

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, delayed_job_priority.

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 collapse

SourceOptions =
%w( mysql_connect_flags sql_range_step sql_query_pre
sql_query_post sql_ranged_throttle sql_query_post_index )
IndexOptions =
%w( charset_table charset_type docinfo enable_star
exceptions html_index_attrs html_remove_elements html_strip ignore_chars
min_infix_len min_prefix_len min_word_len mlock morphology ngram_chars
ngram_len phrase_boundary phrase_boundary_step preopen stopwords
wordforms )

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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.



70
71
72
# File 'lib/thinking_sphinx/configuration.rb', line 70

def initialize(app_root = Dir.pwd)
  self.reset
end

Instance Attribute Details

#addressObject

Returns the value of attribute address.



58
59
60
# File 'lib/thinking_sphinx/configuration.rb', line 58

def address
  @address
end

#allow_starObject

Returns the value of attribute allow_star.



58
59
60
# File 'lib/thinking_sphinx/configuration.rb', line 58

def allow_star
  @allow_star
end

#app_rootObject

Returns the value of attribute app_root.



58
59
60
# File 'lib/thinking_sphinx/configuration.rb', line 58

def app_root
  @app_root
end

#bin_pathObject

Returns the value of attribute bin_path.



58
59
60
# File 'lib/thinking_sphinx/configuration.rb', line 58

def bin_path
  @bin_path
end

#config_fileObject

Returns the value of attribute config_file.



58
59
60
# File 'lib/thinking_sphinx/configuration.rb', line 58

def config_file
  @config_file
end

#configurationObject (readonly)

Returns the value of attribute configuration.



65
66
67
# File 'lib/thinking_sphinx/configuration.rb', line 65

def configuration
  @configuration
end

#database_yml_fileObject

Returns the value of attribute database_yml_file.



58
59
60
# File 'lib/thinking_sphinx/configuration.rb', line 58

def database_yml_file
  @database_yml_file
end

#delayed_job_priorityObject

Returns the value of attribute delayed_job_priority.



58
59
60
# File 'lib/thinking_sphinx/configuration.rb', line 58

def delayed_job_priority
  @delayed_job_priority
end

#environmentObject (readonly)

Returns the value of attribute environment.



65
66
67
# File 'lib/thinking_sphinx/configuration.rb', line 65

def environment
  @environment
end

#index_optionsObject

Returns the value of attribute index_options.



63
64
65
# File 'lib/thinking_sphinx/configuration.rb', line 63

def index_options
  @index_options
end

#model_directoriesObject

Returns the value of attribute model_directories.



58
59
60
# File 'lib/thinking_sphinx/configuration.rb', line 58

def model_directories
  @model_directories
end

#pid_fileObject

Returns the value of attribute pid_file.



58
59
60
# File 'lib/thinking_sphinx/configuration.rb', line 58

def pid_file
  @pid_file
end

#portObject

Returns the value of attribute port.



58
59
60
# File 'lib/thinking_sphinx/configuration.rb', line 58

def port
  @port
end

#query_log_fileObject

Returns the value of attribute query_log_file.



58
59
60
# File 'lib/thinking_sphinx/configuration.rb', line 58

def query_log_file
  @query_log_file
end

#searchd_file_pathObject

Returns the value of attribute searchd_file_path.



58
59
60
# File 'lib/thinking_sphinx/configuration.rb', line 58

def searchd_file_path
  @searchd_file_path
end

#searchd_log_fileObject

Returns the value of attribute searchd_log_file.



58
59
60
# File 'lib/thinking_sphinx/configuration.rb', line 58

def searchd_log_file
  @searchd_log_file
end

#source_optionsObject

Returns the value of attribute source_options.



63
64
65
# File 'lib/thinking_sphinx/configuration.rb', line 63

def source_options
  @source_options
end

Class Method Details

.environmentObject



106
107
108
109
110
# File 'lib/thinking_sphinx/configuration.rb', line 106

def self.environment
  @@environment ||= (
    defined?(Merb) ? Merb.environment : 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.



124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/thinking_sphinx/configuration.rb', line 124

def build(file_path=nil)
  load_models
  file_path ||= "#{self.config_file}"
  
  @configuration.indexes.clear
  
  ThinkingSphinx.indexed_models.each_with_index do |model, model_index|
    @configuration.indexes.concat model.constantize.to_riddle(model_index)
  end
  
  open(file_path, "w") do |file|
    file.write @configuration.render
  end
end

#controllerObject



116
117
118
# File 'lib/thinking_sphinx/configuration.rb', line 116

def controller
  @controller ||= Riddle::Controller.new(@configuration, self.config_file)
end

#load_modelsObject

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).



143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
# File 'lib/thinking_sphinx/configuration.rb', line 143

def load_models
  self.model_directories.each do |base|
    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!(/.*[\/\\]/, '').nil? ? next : retry
      rescue NameError
        next
      end
    end
  end
end

#resetObject



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/thinking_sphinx/configuration.rb', line 74

def reset
  self.app_root          = RAILS_ROOT if defined?(RAILS_ROOT)
  self.app_root          = Merb.root  if defined?(Merb)
  self.app_root        ||= app_root
  
  @configuration = Riddle::Configuration.new
  @configuration.searchd.address    = "127.0.0.1"
  @configuration.searchd.port       = 3312
  @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"
  
  self.database_yml_file    = "#{self.app_root}/config/database.yml"
  self.config_file          = "#{self.app_root}/config/#{environment}.sphinx.conf"
  self.searchd_file_path    = "#{self.app_root}/db/sphinx/#{environment}"
  self.allow_star           = false
  self.bin_path             = ""
  self.model_directories    = ["#{app_root}/app/models/"] +
    Dir.glob("#{app_root}/vendor/plugins/*/app/models/")
  self.delayed_job_priority = 0
  
  self.source_options  = {}
  self.index_options   = {
    :charset_type => "utf-8",
    :morphology   => "stem_en"
  }
        
  parse_config
  
  self
end