module Mongoid::Giza::ClassMethods

Attributes

dynamic_sphinx_indexes[R]
generated_sphinx_indexes[R]
giza_configuration[R]
static_sphinx_indexes[R]

Public Instance Methods

add_dynamic_sphinx_index(settings, block) click to toggle source

Adds an dynamic index to the class. Will also automatically generate the dynamic index for each object of

the class

@param settings [Hash] settings for the index and it's source @param block [Proc] a block that will be evaluated on an

{Mongoid::Giza::Index}.
The block receives one argument that is the current object of the
class for which the index will be generated
# File lib/mongoid/giza.rb, line 98
def add_dynamic_sphinx_index(settings, block)
  dynamic_index = DynamicIndex.new(self, settings, block)
  dynamic_sphinx_indexes << dynamic_index
  process_dynamic_sphinx_index(dynamic_index)
end
add_static_sphinx_index(settings, block) click to toggle source

Adds an static index to the class

@param settings [Hash] settings for the index and it's source @param block [Proc] a block that will be evaluated on an

{Mongoid::Giza::Index}.
# File lib/mongoid/giza.rb, line 109
def add_static_sphinx_index(settings, block)
  index = Index.new(self, settings)
  Docile.dsl_eval(index, &block)
  static_sphinx_indexes[index.name] = index
  giza_configuration.add_index(index)
end
process_dynamic_sphinx_index(dynamic_index) click to toggle source

Generates the indexes from the dynamic index and registers them on the class and on the configuration

@param dynamic_index [Mongoid::Giza::DynamicIndex] the dynamic index

which will generate the static indexes from
# File lib/mongoid/giza.rb, line 121
def process_dynamic_sphinx_index(dynamic_index)
  generated = dynamic_index.generate!
  generated_sphinx_indexes.merge!(generated)
  generated.each { |_, index| giza_configuration.add_index(index, true) }
end
regenerate_sphinx_indexes() click to toggle source

Regenerates all dynamic indexes of the class

# File lib/mongoid/giza.rb, line 148
def regenerate_sphinx_indexes
  giza_configuration
    .remove_generated_indexes(generated_sphinx_indexes.keys)
  generated_sphinx_indexes.clear
  dynamic_sphinx_indexes.each do |dynamic_index|
    process_dynamic_sphinx_index(dynamic_index)
  end
end
remove_generated_sphinx_indexes(*names) click to toggle source

Removes the generated indexes.

@param names [Array] a list of generated index names that should be

removed
# File lib/mongoid/giza.rb, line 161
def remove_generated_sphinx_indexes(*names)
  names.each { |name| generated_sphinx_indexes.delete(name) }
  giza_configuration.remove_generated_indexes(names)
end
sphinx_index(settings = {}, &block) click to toggle source

Class method that defines a index relative to the current class objects. If an argument is given in the block then a dynamic index will be

created.

Otherwise it will create a static index.

@param settings [Hash] optional settings for the index and it's source @param block [Proc] a block that will be evaluated on an

{Mongoid::Giza::Index}
# File lib/mongoid/giza.rb, line 80
def sphinx_index(settings = {}, &block)
  return unless block_given?
  if block.arity > 0
    add_dynamic_sphinx_index(settings, block)
  else
    add_static_sphinx_index(settings, block)
  end
end
sphinx_indexer!(*names) click to toggle source

Execute the indexing routines of the indexes defined on the class. This means (re)create the sphinx configuration file and then execute the

indexer program on it.

If no index names are supplied than all indexes defined on the class

will be indexed.

If none of the index names supplied are on this class then nothing is

indexed

@param names [Array] a list of index names of this class that will be

indexed
# File lib/mongoid/giza.rb, line 176
def sphinx_indexer!(*names)
  if !names.empty?
    indexes_names =
      sphinx_indexes_names.select { |name| names.include?(name) }
  else
    indexes_names = sphinx_indexes_names
  end
  Indexer.instance.index!(*indexes_names) unless indexes_names.empty?
end
sphinx_indexes() click to toggle source

Retrieves all the sphinx indexes defined on this class, static and

dynamic

@return [Hash] a Hash with indexes names as keys and the actual indexes

as the values
# File lib/mongoid/giza.rb, line 191
def sphinx_indexes
  static_sphinx_indexes.merge(generated_sphinx_indexes)
end
sphinx_indexes_names() click to toggle source

Retrieves all the names of sphinx indexes defined on this class, static

and dynamic

@return [Array] an Array of names of indexes from the current class

{Mongoid::Giza::Index}
# File lib/mongoid/giza.rb, line 200
def sphinx_indexes_names
  static_sphinx_indexes.merge(generated_sphinx_indexes).keys
end

Private Instance Methods

map_to_mongoid(result) click to toggle source

Creates the Mongoid::Criteria that return the objects matched by the

sphinx search

@param result [Hash] the query result created by Riddle @return [Hash] the result hash with the Mongoid::Criteria on the indexed

class' name key
# File lib/mongoid/giza.rb, line 212
def map_to_mongoid(result)
  result[name.to_sym] =
    self.in(_giza_id: result[:matches].map { |match| match[:doc] })
  result
end