class ThinkingSphinx::Index

Attributes

additional_indices[RW]
delta_object[RW]
model[RW]
name[RW]
sources[RW]

Public Class Methods

name_for(model) click to toggle source
# File lib/thinking_sphinx/index.rb, line 54
def self.name_for(model)
  model.name.underscore.tr(':/\', '_')
end
new(model, &block) click to toggle source

Create a new index instance by passing in the model it is tied to, and a block to build it with (optional but recommended). For documentation on the syntax for inside the block, the Builder class is what you want.

Quick Example:

Index.new(User) do
  indexes login, email

  has created_at

  set_property :delta => true
end
# File lib/thinking_sphinx/index.rb, line 22
def initialize(model, &block)
  @name         = self.class.name_for model
  @model        = model
  @sources      = []
  @options      = {}
  @delta_object = nil
  @additional_indices = []
end

Public Instance Methods

all_names() click to toggle source
# File lib/thinking_sphinx/index.rb, line 47
def all_names
  names  = [core_name]
  names << delta_name if delta?

  names
end
attributes() click to toggle source
# File lib/thinking_sphinx/index.rb, line 35
def attributes
  @sources.collect { |source| source.attributes }.flatten
end
core_name() click to toggle source
# File lib/thinking_sphinx/index.rb, line 39
def core_name
  "#{name}_core"
end
delta?() click to toggle source
# File lib/thinking_sphinx/index.rb, line 79
def delta?
  !@delta_object.nil?
end
delta_name() click to toggle source
# File lib/thinking_sphinx/index.rb, line 43
def delta_name
  "#{name}_delta"
end
fields() click to toggle source
# File lib/thinking_sphinx/index.rb, line 31
def fields
  @sources.collect { |source| source.fields }.flatten
end
infix_fields() click to toggle source
# File lib/thinking_sphinx/index.rb, line 62
def infix_fields
  fields.select { |field| field.infixes }
end
local_options() click to toggle source
# File lib/thinking_sphinx/index.rb, line 66
def local_options
  @options
end
options() click to toggle source
# File lib/thinking_sphinx/index.rb, line 70
def options
  all_index_options = config.index_options.clone
  @options.keys.select { |key|
    ThinkingSphinx::Configuration::IndexOptions.include?(key.to_s) ||
    ThinkingSphinx::Configuration::CustomOptions.include?(key.to_s)
  }.each { |key| all_index_options[key.to_sym] = @options[key] }
  all_index_options
end
prefix_fields() click to toggle source
# File lib/thinking_sphinx/index.rb, line 58
def prefix_fields
  fields.select { |field| field.prefixes }
end
to_riddle(offset) click to toggle source
# File lib/thinking_sphinx/index.rb, line 83
def to_riddle(offset)
  indexes = [to_riddle_for_core(offset)]
  indexes << to_riddle_for_delta(offset) if delta?
  indexes << to_riddle_for_distributed
end

Private Instance Methods

adapter() click to toggle source
# File lib/thinking_sphinx/index.rb, line 91
def adapter
  @adapter ||= @model.sphinx_database_adapter
end
config() click to toggle source
# File lib/thinking_sphinx/index.rb, line 103
def config
  @config ||= ThinkingSphinx::Configuration.instance
end
set_configuration_options_for_indexes(index) click to toggle source
# File lib/thinking_sphinx/index.rb, line 141
def set_configuration_options_for_indexes(index)
  config.index_options.each do |key, value|
    method = "#{key}=".to_sym
    index.send(method, value) if index.respond_to?(method)
  end

  options.each do |key, value|
    index.send("#{key}=".to_sym, value) if ThinkingSphinx::Configuration::IndexOptions.include?(key.to_s) && !value.nil?
  end
end
set_field_settings_for_indexes(index) click to toggle source
# File lib/thinking_sphinx/index.rb, line 152
def set_field_settings_for_indexes(index)
  field_names = lambda { |field| field.unique_name.to_s }

  index.prefix_field_names += prefix_fields.collect(&field_names)
  index.infix_field_names  += infix_fields.collect(&field_names)
end
sql_query_pre_for_delta() click to toggle source
# File lib/thinking_sphinx/index.rb, line 99
def sql_query_pre_for_delta
  [""]
end
to_riddle_for_core(offset) click to toggle source
# File lib/thinking_sphinx/index.rb, line 107
def to_riddle_for_core(offset)
  index = Riddle::Configuration::Index.new core_name
  index.path = File.join config.searchd_file_path, index.name

  set_configuration_options_for_indexes index
  set_field_settings_for_indexes        index

  sources.each_with_index do |source, i|
    index.sources << source.to_riddle_for_core(offset, i)
  end

  index
end
to_riddle_for_delta(offset) click to toggle source
# File lib/thinking_sphinx/index.rb, line 121
def to_riddle_for_delta(offset)
  index = Riddle::Configuration::Index.new delta_name
  index.parent = core_name
  index.path = File.join config.searchd_file_path, index.name

  sources.each_with_index do |source, i|
    index.sources << source.to_riddle_for_delta(offset, i)
  end

  index
end
to_riddle_for_distributed() click to toggle source
# File lib/thinking_sphinx/index.rb, line 133
def to_riddle_for_distributed
  index = Riddle::Configuration::DistributedIndex.new name
  index.local_indices << core_name
  index.local_indices += additional_indices
  index.local_indices.unshift delta_name if delta?
  index
end
utf8?() click to toggle source
# File lib/thinking_sphinx/index.rb, line 95
def utf8?
  options[:charset_type] == "utf-8"
end