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

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 collapse

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

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.



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

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

Instance Attribute Details

#allow_starObject

Returns the value of attribute allow_star.



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

def allow_star
  @allow_star
end

#app_rootObject

Returns the value of attribute app_root.



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

def app_root
  @app_root
end

#configurationObject (readonly)

Returns the value of attribute configuration.



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

def configuration
  @configuration
end

#controllerObject (readonly)

Returns the value of attribute controller.



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

def controller
  @controller
end

#delayed_job_priorityObject

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

#hard_retry_countObject

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

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



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

def indexed_models
  @indexed_models
end

#model_directoriesObject

Returns the value of attribute model_directories.



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

def model_directories
  @model_directories
end

#searchd_file_pathObject

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

#shuffleObject

Returns the value of attribute shuffle.



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

def shuffle
  @shuffle
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

#stop_timeoutObject

Returns the value of attribute stop_timeout.



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

def stop_timeout
  @stop_timeout
end

#timeoutObject

Returns the value of attribute timeout.



264
265
266
# File 'lib/thinking_sphinx/configuration.rb', line 264

def timeout
  @timeout
end

#touched_reindex_fileObject

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

#use_64_bitObject

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

#versionObject

Returns the value of attribute version.



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

def version
  @version
end

Class Method Details

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

Yields:

  • (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

.environmentObject



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

.reset_environmentObject



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

#addressObject



182
183
184
# File 'lib/thinking_sphinx/configuration.rb', line 182

def address
  @address
end

#address=(address) ⇒ Object



186
187
188
189
# File 'lib/thinking_sphinx/configuration.rb', line 186

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

#bin_pathObject



240
241
242
# File 'lib/thinking_sphinx/configuration.rb', line 240

def bin_path
  @controller.bin_path
end

#bin_path=(path) ⇒ Object



244
245
246
# File 'lib/thinking_sphinx/configuration.rb', line 244

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.



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

#clientObject



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

#config_fileObject



232
233
234
# File 'lib/thinking_sphinx/configuration.rb', line 232

def config_file
  @controller.path
end

#config_file=(file) ⇒ Object



236
237
238
# File 'lib/thinking_sphinx/configuration.rb', line 236

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

#environmentObject



152
153
154
# File 'lib/thinking_sphinx/configuration.rb', line 152

def environment
  self.class.environment
end

#generateObject



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

#indexer_binary_nameObject



256
257
258
# File 'lib/thinking_sphinx/configuration.rb', line 256

def indexer_binary_name
  @controller.indexer_binary_name
end

#indexer_binary_name=(name) ⇒ Object



260
261
262
# File 'lib/thinking_sphinx/configuration.rb', line 260

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

#models_by_crcObject



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

#pid_fileObject



208
209
210
# File 'lib/thinking_sphinx/configuration.rb', line 208

def pid_file
  @configuration.searchd.pid_file
end

#pid_file=(pid_file) ⇒ Object



212
213
214
# File 'lib/thinking_sphinx/configuration.rb', line 212

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

#portObject



191
192
193
# File 'lib/thinking_sphinx/configuration.rb', line 191

def port
  @port
end

#port=(port) ⇒ Object



195
196
197
198
# File 'lib/thinking_sphinx/configuration.rb', line 195

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

#query_log_fileObject



224
225
226
# File 'lib/thinking_sphinx/configuration.rb', line 224

def query_log_file
  @configuration.searchd.query_log
end

#query_log_file=(file) ⇒ Object



228
229
230
# File 'lib/thinking_sphinx/configuration.rb', line 228

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

#reset(custom_app_root = nil) ⇒ Object



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.source_options  = {}
  self.index_options   = {
    :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

#searchd_binary_nameObject



248
249
250
# File 'lib/thinking_sphinx/configuration.rb', line 248

def searchd_binary_name
  @controller.searchd_binary_name
end

#searchd_binary_name=(name) ⇒ Object



252
253
254
# File 'lib/thinking_sphinx/configuration.rb', line 252

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

#searchd_log_fileObject



216
217
218
# File 'lib/thinking_sphinx/configuration.rb', line 216

def searchd_log_file
  @configuration.searchd.log
end

#searchd_log_file=(file) ⇒ Object



220
221
222
# File 'lib/thinking_sphinx/configuration.rb', line 220

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

#touch_reindex_file(output) ⇒ Object



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

#use_socket=(use_socket) ⇒ Object



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