class Flex::InstanceProxy::ModelIndexer

Constants

BASE62_DIGITS

Attributes

index[W]
parent[W]
routing[W]
type[W]

Public Instance Methods

each_parent() { |pi| ... } click to toggle source

helper that iterates through the parent record chain record.flex.each_parent{|p| p.do_something }

# File lib/flex/instance_proxy/model_indexer.rb, line 49
def each_parent
  pi = parent_instance
  while pi do
    yield pi
    pi = pi.flex.parent_instance
  end
end
full_get(*vars) click to toggle source

like get, but it returns all the fields after a refresh

# File lib/flex/instance_proxy/model_indexer.rb, line 36
def full_get(*vars)
  return unless instance.flex_indexable?
  Flex.search_by_id(metainfo, {:refresh => true, :params => {:fields => '*,_source'}}, *vars)
end
get(*vars) click to toggle source

gets the document from ES

# File lib/flex/instance_proxy/model_indexer.rb, line 30
def get(*vars)
  return unless instance.flex_indexable?
  Flex.get(metainfo, *vars)
end
id() click to toggle source
# File lib/flex/instance_proxy/model_indexer.rb, line 71
def id
  @id ||= instance.respond_to?(:flex_id) ? instance.flex_id : instance.id.to_s
end
index() click to toggle source
# File lib/flex/instance_proxy/model_indexer.rb, line 57
def index
  @index ||= instance.respond_to?(:flex_index) ? instance.flex_index : class_flex.index
end
metainfo() click to toggle source
# File lib/flex/instance_proxy/model_indexer.rb, line 93
def metainfo
  meta = Vars.new( :index => index, :type => type, :id => id )
  params = {}
  params[:routing] = routing if routing
  params[:parent]  = parent  if parent
  meta.merge!(:params => params) unless params.empty?
  meta
end
parent() click to toggle source
# File lib/flex/instance_proxy/model_indexer.rb, line 84
def parent
  @parent ||= case
              when instance.respond_to?(:flex_parent) then instance.flex_parent
              when is_child?                          then parent_instance.id.to_s
              else nil
              end
end
parent_instance() click to toggle source
# File lib/flex/instance_proxy/model_indexer.rb, line 41
def parent_instance
  return unless is_child?
  @parent_instance ||= instance.send(class_flex.parent_association) ||
                         raise(MissingParentError, "missing parent instance for document #{instance.inspect}.")
end
remove(*vars) click to toggle source

removes the document from the index (called from after_destroy)

# File lib/flex/instance_proxy/model_indexer.rb, line 24
def remove(*vars)
  return unless instance.flex_indexable?
  Flex.remove(metainfo, *vars)
end
routing() click to toggle source
# File lib/flex/instance_proxy/model_indexer.rb, line 75
def routing
  @routing ||= case
               when instance.respond_to?(:flex_routing) then instance.flex_routing
               when is_child?                           then parent_instance.flex.routing
               when is_parent?                          then create_routing
               end
end
store(*vars) click to toggle source

indexes the document usually called from after_save, you can eventually call it explicitly for example from another callback or whenever the DB doesn’t get updated by the model you can also pass the :data=>flex_source explicitly (useful for example to override the flex_source in the model)

# File lib/flex/instance_proxy/model_indexer.rb, line 15
def store(*vars)
  if instance.flex_indexable?
    Flex.store(metainfo, {:data => instance.flex_source}, *vars)
  else
    Flex.remove(metainfo, *vars) if Flex.get(metainfo, *vars, :raise => false)
  end
end
sync_self() click to toggle source
# File lib/flex/instance_proxy/model_indexer.rb, line 102
def sync_self
  instance.destroyed? ? remove : store
end
type() click to toggle source
# File lib/flex/instance_proxy/model_indexer.rb, line 62
def type
  @type ||= case
            when instance.respond_to?(:flex_type) then instance.flex_type
            when is_child?                        then class_flex.parent_child_map[parent_instance.flex.type]
            else                                       class_flex.type
            end
end

Private Instance Methods

create_routing() click to toggle source
# File lib/flex/instance_proxy/model_indexer.rb, line 110
def create_routing
  string    = [index, type, id].join
  remainder = Digest::MD5.hexdigest(string).to_i(16)
  result    = []
  max_power = ( Math.log(remainder) / Math.log(62) ).floor
  max_power.downto(0) do |power|
    digit, remainder = remainder.divmod(62**power)
    result << digit
  end
  result << remainder if remainder > 0
  result.map{|digit| BASE62_DIGITS[digit]}.join
end