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

9312

allow star

false

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

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 collapse

SourceOptions =
%w( mysql_connect_flags mysql_ssl_cert mysql_ssl_key
mysql_ssl_ca sql_range_step sql_query_pre sql_query_post
sql_query_killlist sql_ranged_throttle sql_query_post_index unpack_zlib
unpack_mysqlcompress unpack_mysqlcompress_maxsize )
IndexOptions =
%w( blend_chars charset_table charset_type charset_dictpath
docinfo enable_star exceptions expand_keywords hitless_words
html_index_attrs html_remove_elements html_strip index_exact_words
ignore_chars inplace_docinfo_gap inplace_enable inplace_hit_gap
inplace_reloc_factor inplace_write_factor min_infix_len min_prefix_len
min_stemming_len min_word_len mlock morphology ngram_chars ngram_len
ondisk_dict overshort_step phrase_boundary phrase_boundary_step preopen
stopwords stopwords_step wordforms )
CustomOptions =
%w( disable_range )
@@environment =
nil

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.



81
82
83
# File 'lib/thinking_sphinx/configuration.rb', line 81

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

Instance Attribute Details

#allow_starObject

Returns the value of attribute allow_star.



68
69
70
# File 'lib/thinking_sphinx/configuration.rb', line 68

def allow_star
  @allow_star
end

#app_rootObject

Returns the value of attribute app_root.



68
69
70
# File 'lib/thinking_sphinx/configuration.rb', line 68

def app_root
  @app_root
end

#configurationObject (readonly)

Returns the value of attribute configuration.



74
75
76
# File 'lib/thinking_sphinx/configuration.rb', line 74

def configuration
  @configuration
end

#controllerObject (readonly)

Returns the value of attribute controller.



74
75
76
# File 'lib/thinking_sphinx/configuration.rb', line 74

def controller
  @controller
end

#database_yml_fileObject

Returns the value of attribute database_yml_file.



68
69
70
# File 'lib/thinking_sphinx/configuration.rb', line 68

def database_yml_file
  @database_yml_file
end

#delayed_job_priorityObject

Returns the value of attribute delayed_job_priority.



68
69
70
# File 'lib/thinking_sphinx/configuration.rb', line 68

def delayed_job_priority
  @delayed_job_priority
end

#environmentObject (readonly)

Returns the value of attribute environment.



74
75
76
# File 'lib/thinking_sphinx/configuration.rb', line 74

def environment
  @environment
end

#index_optionsObject

Returns the value of attribute index_options.



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

def index_options
  @index_options
end

#indexed_modelsObject

Returns the value of attribute indexed_models.



68
69
70
# File 'lib/thinking_sphinx/configuration.rb', line 68

def indexed_models
  @indexed_models
end

#model_directoriesObject

Returns the value of attribute model_directories.



68
69
70
# File 'lib/thinking_sphinx/configuration.rb', line 68

def model_directories
  @model_directories
end

#searchd_file_pathObject

Returns the value of attribute searchd_file_path.



68
69
70
# File 'lib/thinking_sphinx/configuration.rb', line 68

def searchd_file_path
  @searchd_file_path
end

#section_optionsObject

Returns the value of attribute section_options.



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

def section_options
  @section_options
end

#source_optionsObject

Returns the value of attribute source_options.



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

def source_options
  @source_options
end

#timeoutObject

Returns the value of attribute timeout.



258
259
260
# File 'lib/thinking_sphinx/configuration.rb', line 258

def timeout
  @timeout
end

#versionObject

Returns the value of attribute version.



72
73
74
# File 'lib/thinking_sphinx/configuration.rb', line 72

def version
  @version
end

Class Method Details

.configure {|instance| ... } ⇒ Object

Yields:

  • (instance)


85
86
87
88
# File 'lib/thinking_sphinx/configuration.rb', line 85

def self.configure(&block)
  yield instance
  instance.reset(instance.app_root)
end

.environmentObject



131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/thinking_sphinx/configuration.rb', line 131

def self.environment
  if @@environment.nil?
    ThinkingSphinx.mutex.synchronize do
      @@environment ||= if defined?(Merb)
        Merb.environment
      elsif defined?(Sinatra)
        Sinatra::Application.environment.to_s
      elsif defined?(Rails)
        Rails.env
      else
        ENV['RAILS_ENV'] || 'development'
      end
    end
  end
  
  @@environment
end

.reset_environmentObject



149
150
151
152
153
# File 'lib/thinking_sphinx/configuration.rb', line 149

def self.reset_environment
  ThinkingSphinx.mutex.synchronize do
    @@environment = nil
  end
end

Instance Method Details

#addressObject



184
185
186
# File 'lib/thinking_sphinx/configuration.rb', line 184

def address
  @address
end

#address=(address) ⇒ Object



188
189
190
191
# File 'lib/thinking_sphinx/configuration.rb', line 188

def address=(address)
  @address = address
  @configuration.searchd.address = address
end

#bin_pathObject



234
235
236
# File 'lib/thinking_sphinx/configuration.rb', line 234

def bin_path
  @controller.bin_path
end

#bin_path=(path) ⇒ Object



238
239
240
# File 'lib/thinking_sphinx/configuration.rb', line 238

def bin_path=(path)
  @controller.bin_path = path
end

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



174
175
176
177
178
179
180
181
182
# File 'lib/thinking_sphinx/configuration.rb', line 174

def build(file_path=nil)
  file_path ||= "#{self.config_file}"

  generate

  open(file_path, "w") do |file|
    file.write @configuration.render
  end
end

#clientObject



260
261
262
263
264
265
266
# File 'lib/thinking_sphinx/configuration.rb', line 260

def client
  client = Riddle::Client.new address, port,
    configuration.searchd.client_key
  client.max_matches = configuration.searchd.max_matches || 1000
  client.timeout = timeout || 0
  client
end

#config_fileObject



226
227
228
# File 'lib/thinking_sphinx/configuration.rb', line 226

def config_file
  @controller.path
end

#config_file=(file) ⇒ Object



230
231
232
# File 'lib/thinking_sphinx/configuration.rb', line 230

def config_file=(file)
  @controller.path = file
end

#generateObject



159
160
161
162
163
164
165
166
167
168
# File 'lib/thinking_sphinx/configuration.rb', line 159

def generate
  @configuration.indexes.clear

  ThinkingSphinx.context.indexed_models.each do |model|
    model = model.constantize
    model.define_indexes
    @configuration.indexes.concat merge_with_section_options!(model.to_riddle)
    enforce_common_attribute_types
  end
end

#indexer_binary_nameObject



250
251
252
# File 'lib/thinking_sphinx/configuration.rb', line 250

def indexer_binary_name
  @controller.indexer_binary_name
end

#indexer_binary_name=(name) ⇒ Object



254
255
256
# File 'lib/thinking_sphinx/configuration.rb', line 254

def indexer_binary_name=(name)
  @controller.indexer_binary_name = name
end

#models_by_crcObject



268
269
270
271
272
273
274
275
276
277
278
# File 'lib/thinking_sphinx/configuration.rb', line 268

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

#pid_fileObject



202
203
204
# File 'lib/thinking_sphinx/configuration.rb', line 202

def pid_file
  @configuration.searchd.pid_file
end

#pid_file=(pid_file) ⇒ Object



206
207
208
# File 'lib/thinking_sphinx/configuration.rb', line 206

def pid_file=(pid_file)
  @configuration.searchd.pid_file = pid_file
end

#portObject



193
194
195
# File 'lib/thinking_sphinx/configuration.rb', line 193

def port
  @port
end

#port=(port) ⇒ Object



197
198
199
200
# File 'lib/thinking_sphinx/configuration.rb', line 197

def port=(port)
  @port = port
  @configuration.searchd.port = port
end

#query_log_fileObject



218
219
220
# File 'lib/thinking_sphinx/configuration.rb', line 218

def query_log_file
  @configuration.searchd.query_log
end

#query_log_file=(file) ⇒ Object



222
223
224
# File 'lib/thinking_sphinx/configuration.rb', line 222

def query_log_file=(file)
  @configuration.searchd.query_log = file
end

#reset(custom_app_root = nil) ⇒ Object



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
# File 'lib/thinking_sphinx/configuration.rb', line 90

def reset(custom_app_root=nil)
  if custom_app_root
    self.app_root = custom_app_root
  else
    self.app_root   = Rails.root                 if defined?(Rails)
    self.app_root   = Merb.root                  if defined?(Merb)
    self.app_root   = Sinatra::Application.root  if defined?(Sinatra)
    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.database_yml_file    = "#{self.app_root}/config/database.yml"
  self.searchd_file_path    = "#{self.app_root}/db/sphinx/#{environment}"
  self.allow_star           = false
  self.model_directories    = ["#{app_root}/app/models/"] +
    Dir.glob("#{app_root}/vendor/plugins/*/app/models/")
  self.delayed_job_priority = 0
  self.indexed_models       = []

  self.source_options  = {}
  self.section_options  = {}
  self.index_options   = {
    :charset_type => "utf-8"
  }
  self.section_options  = {}
  self.version = nil
  parse_config
  self.version ||= @controller.sphinx_version

  self
end

#searchd_binary_nameObject



242
243
244
# File 'lib/thinking_sphinx/configuration.rb', line 242

def searchd_binary_name
  @controller.searchd_binary_name
end

#searchd_binary_name=(name) ⇒ Object



246
247
248
# File 'lib/thinking_sphinx/configuration.rb', line 246

def searchd_binary_name=(name)
  @controller.searchd_binary_name = name
end

#searchd_log_fileObject



210
211
212
# File 'lib/thinking_sphinx/configuration.rb', line 210

def searchd_log_file
  @configuration.searchd.log
end

#searchd_log_file=(file) ⇒ Object



214
215
216
# File 'lib/thinking_sphinx/configuration.rb', line 214

def searchd_log_file=(file)
  @configuration.searchd.log = file
end