class Sunspot::Rails::Configuration

Sunspot::Rails is configured via the config/sunspot.yml file, which contains properties keyed by environment name. A sample sunspot.yml file would look like:

development:
  solr:
    hostname: localhost
    port: 8982
    memory: 1G
    solr_jar: /some/path/solr15/start.jar
    bind_address: 0.0.0.0
    proxy: false
  disabled: false
test:
  solr:
    hostname: localhost
    port: 8983
    log_level: OFF
    open_timeout: 0.5
    read_timeout: 2
    proxy: false
production:
  solr:
    scheme: http
    user: username
    pass: password
    hostname: localhost
    port: 8983
    path: /solr/myindex
    log_level: WARNING
    solr_home: /some/path
    open_timeout: 0.5
    read_timeout: 2
    proxy: http://proxy.com:12345
  master_solr:
    hostname: localhost
    port: 8982
    path: /solr
  auto_index_callback: after_commit
  auto_remove_callback: after_commit
  auto_commit_after_request: true

Sunspot::Rails uses the configuration to set up the Solr connection, as well as for starting Solr with the appropriate port using the rake sunspot:solr:start task.

If the master_solr configuration is present, Sunspot will use the Solr instance specified here for all write operations, and the Solr configured under solr for all read operations.

Constants

LOG_LEVELS

ActiveSupport log levels are integers; this array maps them to the appropriate java.util.logging.Level constant

Attributes

user_configuration[W]

Public Instance Methods

auto_commit_after_delete_request?() click to toggle source

As for auto_commit_after_request? but only for deletes Default false

Returns

Boolean: auto_commit_after_delete_request?

# File lib/sunspot/rails/configuration.rb, line 235
def auto_commit_after_delete_request?
  @auto_commit_after_delete_request ||=
    (user_configuration_from_key('auto_commit_after_delete_request') || false)
end
auto_commit_after_request?() click to toggle source

Should the solr index receive a commit after each http-request. Default true

Returns

Boolean: auto_commit_after_request?

# File lib/sunspot/rails/configuration.rb, line 222
def auto_commit_after_request?
  @auto_commit_after_request ||=
    user_configuration_from_key('auto_commit_after_request') != false
end
auto_index_callback() click to toggle source

The callback to use when automatically indexing records. Defaults to after_save.

# File lib/sunspot/rails/configuration.rb, line 324
def auto_index_callback
  @auto_index_callback ||=
    (user_configuration_from_key('auto_index_callback') || 'after_save')
end
auto_remove_callback() click to toggle source

The callback to use when automatically removing records after deletation. Defaults to after_destroy.

# File lib/sunspot/rails/configuration.rb, line 333
def auto_remove_callback
  @auto_remove_callback ||=
    (user_configuration_from_key('auto_remove_callback') || 'after_destroy')
end
bind_address() click to toggle source

Interface on which to run Solr

# File lib/sunspot/rails/configuration.rb, line 292
def bind_address
  @bind_address ||= user_configuration_from_key('solr', 'bind_address')
end
disabled?() click to toggle source

Whether or not to disable Solr. Defaults to false.

# File lib/sunspot/rails/configuration.rb, line 316
def disabled?
  @disabled ||= (user_configuration_from_key('disabled') || false)
end
has_master?() click to toggle source

True if there is a master Solr instance configured, otherwise false.

Returns

Boolean

bool

# File lib/sunspot/rails/configuration.rb, line 193
def has_master?
  @has_master = !!user_configuration_from_key('master_solr')
end
hostname() click to toggle source

The host name at which to connect to Solr. Default 'localhost'.

Returns

String

host name

# File lib/sunspot/rails/configuration.rb, line 70
def hostname
  unless defined?(@hostname)
    @hostname   = solr_url.host if solr_url
    @hostname ||= user_configuration_from_key('solr', 'hostname')
    @hostname ||= default_hostname
  end
  @hostname
end
log_file() click to toggle source

The log directory for solr logfiles

Returns

String

log_dir

# File lib/sunspot/rails/configuration.rb, line 248
def log_file
  @log_file ||= (user_configuration_from_key('solr', 'log_file') || default_log_file_location )
end
log_level() click to toggle source

The default log_level that should be passed to solr. You can change the individual log_levels in the solr admin interface. If no level is specified in the sunspot configuration file, use a level similar to Rails own logging level.

Returns

String

log_level

# File lib/sunspot/rails/configuration.rb, line 207
def log_level
  @log_level ||= (
    user_configuration_from_key('solr', 'log_level') ||
    LOG_LEVELS[::Rails.logger.level]
  )
end
master_hostname() click to toggle source

The host name at which to connect to the master Solr instance. Defaults to the 'hostname' configuration option.

Returns

String

host name

# File lib/sunspot/rails/configuration.rb, line 158
def master_hostname
  @master_hostname ||= (user_configuration_from_key('master_solr', 'hostname') || hostname)
end
master_path() click to toggle source

The path to the master Solr servlet (useful if you are running multicore). Defaults to the value of the 'path' configuration option.

Returns

String

path

# File lib/sunspot/rails/configuration.rb, line 182
def master_path
  @master_path ||= (user_configuration_from_key('master_solr', 'path') || path)
end
master_port() click to toggle source

The port at which to connect to the master Solr instance. Defaults to the 'port' configuration option.

Returns

Integer

port

# File lib/sunspot/rails/configuration.rb, line 170
def master_port
  @master_port ||= (user_configuration_from_key('master_solr', 'port') || port).to_i
end
memory() click to toggle source

java heap size for Solr instance

# File lib/sunspot/rails/configuration.rb, line 285
def memory
  @memory ||= user_configuration_from_key('solr', 'memory')
end
open_timeout() click to toggle source
# File lib/sunspot/rails/configuration.rb, line 300
def open_timeout
  @open_timeout ||= user_configuration_from_key('solr', 'open_timeout')
end
path() click to toggle source

The url path to the Solr servlet (useful if you are running multicore). Default '/solr/default'.

Returns

String

path

# File lib/sunspot/rails/configuration.rb, line 141
def path
  unless defined?(@path)
    @path   = solr_url.path if solr_url
    @path ||= user_configuration_from_key('solr', 'path')
    @path ||= default_path
  end
  @path
end
pid_dir() click to toggle source
# File lib/sunspot/rails/configuration.rb, line 252
def pid_dir
  @pid_dir ||= user_configuration_from_key('solr', 'pid_dir') || File.join(::Rails.root, 'solr', 'pids', ::Rails.env)
end
port() click to toggle source

The port at which to connect to Solr. Defaults to 8981 in test, 8982 in development and 8983 in production.

Returns

Integer

port

# File lib/sunspot/rails/configuration.rb, line 87
def port
  unless defined?(@port)
    @port   = solr_url.port if solr_url
    @port ||= user_configuration_from_key('solr', 'port')
    @port ||= default_port
    @port   = @port.to_i
  end
  @port
end
proxy() click to toggle source
# File lib/sunspot/rails/configuration.rb, line 308
def proxy
  @proxy ||= user_configuration_from_key('solr', 'proxy')
end
read_timeout() click to toggle source
# File lib/sunspot/rails/configuration.rb, line 296
def read_timeout
  @read_timeout ||= user_configuration_from_key('solr', 'read_timeout')
end
scheme() click to toggle source

The scheme to use, http or https. Defaults to http

Returns

String

scheme

# File lib/sunspot/rails/configuration.rb, line 105
def scheme
  unless defined?(@scheme)
    @scheme   = solr_url.scheme if solr_url
    @scheme ||= user_configuration_from_key('solr', 'scheme')
    @scheme ||= default_scheme
  end
  @scheme
end
solr_executable() click to toggle source

Solr start jar

# File lib/sunspot/rails/configuration.rb, line 278
def solr_executable
  @solr_executable ||= user_configuration_from_key('solr', 'solr_executable')
end
solr_home() click to toggle source

The solr home directory. Sunspot::Rails expects this directory to contain a config, data and pids directory. See Sunspot::Rails::Server.bootstrap for more information.

Returns

String

solr_home

# File lib/sunspot/rails/configuration.rb, line 266
def solr_home
  @solr_home ||=
    if user_configuration_from_key('solr', 'solr_home')
      user_configuration_from_key('solr', 'solr_home')
    else
      File.join(::Rails.root, 'solr')
    end
end
update_format() click to toggle source
# File lib/sunspot/rails/configuration.rb, line 304
def update_format
  @update_format ||= user_configuration_from_key('solr', 'update_format')
end
userinfo() click to toggle source

The userinfo used for authentication, a colon-delimited string like “user:pass” Defaults to nil, which means no authentication

Returns

String

userinfo

# File lib/sunspot/rails/configuration.rb, line 122
def userinfo
  unless defined?(@userinfo)
    @userinfo   = solr_url.userinfo if solr_url
    user = user_configuration_from_key('solr', 'user')
    pass = user_configuration_from_key('solr', 'pass')
    @userinfo ||= [ user, pass ].compact.join(":") if user && pass
    @userinfo ||= default_userinfo
  end
  @userinfo
end

Protected Instance Methods

default_hostname() click to toggle source
# File lib/sunspot/rails/configuration.rb, line 402
def default_hostname
  'localhost'
end
default_path() click to toggle source
# File lib/sunspot/rails/configuration.rb, line 421
def default_path
  '/solr/default'
end
default_port() click to toggle source
# File lib/sunspot/rails/configuration.rb, line 406
def default_port
  { 'test'        => 8981,
    'development' => 8982,
    'production'  => 8983
  }[::Rails.env]  || 8983
end
default_scheme() click to toggle source
# File lib/sunspot/rails/configuration.rb, line 413
def default_scheme
  'http'
end
default_userinfo() click to toggle source
# File lib/sunspot/rails/configuration.rb, line 417
def default_userinfo
  nil
end
solr_url() click to toggle source

When a specific hostname, port and path aren't provided in the sunspot.yml file, look for a key named 'url', then check the environment, then fall back to a sensible localhost default.

# File lib/sunspot/rails/configuration.rb, line 396
def solr_url
  if ENV['SOLR_URL'] || ENV['WEBSOLR_URL']
    URI.parse(ENV['SOLR_URL'] || ENV['WEBSOLR_URL'])
  end
end

Private Instance Methods

default_log_file_location() click to toggle source

Logging in rails_root/log as solr_<environment>.log as a default.

Returns
String

default_log_file_location

# File lib/sunspot/rails/configuration.rb, line 348
def default_log_file_location
  File.join(::Rails.root, 'log', "solr_" + ::Rails.env + ".log")
end
user_configuration() click to toggle source

Memoized hash of configuration options for the current Rails environment as specified in config/sunspot.yml

Returns

Hash

configuration options for current environment

# File lib/sunspot/rails/configuration.rb, line 373
def user_configuration
  @user_configuration ||=
    begin
      path = File.join(::Rails.root, 'config', 'sunspot.yml')
      if File.exist?(path)
        File.open(path) do |file|
          processed = ERB.new(file.read).result
          YAML.load(processed)[::Rails.env]
        end
      else
        {}
      end
    end
end
user_configuration_from_key( *keys ) click to toggle source

return a specific key from the user configuration in config/sunspot.yml

Returns

Mixed

requested_key or nil

# File lib/sunspot/rails/configuration.rb, line 359
def user_configuration_from_key( *keys )
  keys.inject(user_configuration) do |hash, key|
    hash[key] if hash
  end
end