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

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

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

- (Configuration) initialize(app_root = Dir.pwd)

Load in the configuration settings - this will look for config/sphinx.yml and parse it according to the current environment.



78
79
80
# File 'lib/thinking_sphinx/configuration.rb', line 78

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

Instance Attribute Details

- (Object) allow_star

Returns the value of attribute allow_star



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

def allow_star
  @allow_star
end

- (Object) app_root

Returns the value of attribute app_root



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

def app_root
  @app_root
end

- (Object) configuration (readonly)

Returns the value of attribute configuration



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

def configuration
  @configuration
end

- (Object) controller (readonly)

Returns the value of attribute controller



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

def controller
  @controller
end

- (Object) delayed_job_priority

Returns the value of attribute delayed_job_priority



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

def delayed_job_priority
  @delayed_job_priority
end

- (Object) index_options

Returns the value of attribute index_options



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

def index_options
  @index_options
end

- (Object) indexed_models

Returns the value of attribute indexed_models



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

def indexed_models
  @indexed_models
end

- (Object) model_directories

Returns the value of attribute model_directories



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

def model_directories
  @model_directories
end

- (Object) searchd_file_path

Returns the value of attribute searchd_file_path



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

def searchd_file_path
  @searchd_file_path
end

- (Object) shuffle

Returns the value of attribute shuffle



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

def shuffle
  @shuffle
end

- (Object) source_options

Returns the value of attribute source_options



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

def source_options
  @source_options
end

- (Object) stop_timeout

Returns the value of attribute stop_timeout



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

def stop_timeout
  @stop_timeout
end

- (Object) timeout

Returns the value of attribute timeout



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

def timeout
  @timeout
end

- (Object) touched_reindex_file

Returns the value of attribute touched_reindex_file



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

def touched_reindex_file
  @touched_reindex_file
end

- (Object) use_64_bit

Returns the value of attribute use_64_bit



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

def use_64_bit
  @use_64_bit
end

- (Object) version

Returns the value of attribute version



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

def version
  @version
end

Class Method Details

+ (Object) configure {|instance| ... }

Yields:

  • (instance)


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

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

+ (Object) environment



131
132
133
134
135
136
137
138
139
140
141
# File 'lib/thinking_sphinx/configuration.rb', line 131

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

+ (Object) reset_environment



143
144
145
146
147
# File 'lib/thinking_sphinx/configuration.rb', line 143

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

Instance Method Details

- (Object) address



179
180
181
# File 'lib/thinking_sphinx/configuration.rb', line 179

def address
  @address
end

- (Object) address=(address)



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

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

- (Object) bin_path



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

def bin_path
  @controller.bin_path
end

- (Object) bin_path=(path)



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

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

- (Object) build(file_path = nil)

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.



169
170
171
172
173
174
175
176
177
# File 'lib/thinking_sphinx/configuration.rb', line 169

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

  generate

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

- (Object) client



255
256
257
258
259
260
261
# File 'lib/thinking_sphinx/configuration.rb', line 255

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

- (Object) config_file



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

def config_file
  @controller.path
end

- (Object) config_file=(file)



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

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

- (Object) environment



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

def environment
  self.class.environment
end

- (Object) generate



153
154
155
156
157
158
159
160
161
162
163
# File 'lib/thinking_sphinx/configuration.rb', line 153

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

- (Object) indexer_binary_name



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

def indexer_binary_name
  @controller.indexer_binary_name
end

- (Object) indexer_binary_name=(name)



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

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

- (Object) models_by_crc



263
264
265
266
267
268
269
270
271
272
273
# File 'lib/thinking_sphinx/configuration.rb', line 263

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

- (Object) pid_file



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

def pid_file
  @configuration.searchd.pid_file
end

- (Object) pid_file=(pid_file)



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

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

- (Object) port



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

def port
  @port
end

- (Object) port=(port)



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

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

- (Object) query_log_file



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

def query_log_file
  @configuration.searchd.query_log
end

- (Object) query_log_file=(file)



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

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

- (Object) reset(custom_app_root = nil)



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

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              = true

  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

- (Object) searchd_binary_name



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

def searchd_binary_name
  @controller.searchd_binary_name
end

- (Object) searchd_binary_name=(name)



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

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

- (Object) searchd_log_file



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

def searchd_log_file
  @configuration.searchd.log
end

- (Object) searchd_log_file=(file)



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

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

- (Object) touch_reindex_file(output)



275
276
277
278
# File 'lib/thinking_sphinx/configuration.rb', line 275

def touch_reindex_file(output)
  return FileUtils.touch(@touched_reindex_file) if @touched_reindex_file and output =~ /succesfully sent SIGHUP to searchd/
  false
end