module Tire::Model::Search::InstanceMethods

Public Instance Methods

index() click to toggle source

Returns a Tire::Index instance for this instance of the model.

Example usage: `@article.index.refresh`.

# File lib/tire/model/search.rb, line 117
def index
  instance.class.tire.index
end
matches() click to toggle source
# File lib/tire/model/search.rb, line 180
def matches
  @attributes['matches']
end
matches=(value) click to toggle source
# File lib/tire/model/search.rb, line 184
def matches=(value)
  @attributes ||= {}; @attributes['matches'] = value
end
to_indexed_json() click to toggle source

The default JSON serialization of the model, based on its `to_hash` representation.

If you don't define any mapping, the model is serialized as-is.

If you do define the mapping for ElasticSearch, only attributes declared in the mapping are serialized.

For properties declared with the `:as` option, the passed String or Proc is evaluated in the instance context.

# File lib/tire/model/search.rb, line 157
def to_indexed_json
  if instance.class.tire.mapping.empty?
    # Reject the id and type keys
    instance.to_hash.reject {|key,_| key.to_s == 'id' || key.to_s == 'type' }.to_json
  else
    mapping = instance.class.tire.mapping
    # Reject keys not declared in mapping
    hash = instance.to_hash.reject { |key, value| ! mapping.keys.map(&:to_s).include?(key.to_s) }

    # Evalute the `:as` options
    mapping.each do |key, options|
      case options[:as]
        when String
          hash[key] = instance.instance_eval(options[:as])
        when Proc
          hash[key] = instance.instance_eval(&options[:as])
      end
    end

    hash.to_json
  end
end
update_elastic_search_index() click to toggle source
Alias for: update_index
update_elasticsearch_index() click to toggle source
Alias for: update_index
update_index() click to toggle source

Updates the index in ElasticSearch.

On model instance create or update, it will store its serialized representation in the index.

On model destroy, it will remove the corresponding document from the index.

It will also execute any `<after|before>_update_elasticsearch_index` callback hooks.

# File lib/tire/model/search.rb, line 129
def update_index
  instance.send :_run_update_elasticsearch_index_callbacks do
    if instance.destroyed?
      index.remove instance
    else
      response  = index.store( instance, {:percolate => percolator} )
      instance.id     ||= response['_id']      if instance.respond_to?(:id=)
      instance._index   = response['_index']   if instance.respond_to?(:_index=)
      instance._type    = response['_type']    if instance.respond_to?(:_type=)
      instance._version = response['_version'] if instance.respond_to?(:_version=)
      instance.matches  = response['matches']  if instance.respond_to?(:matches=)
      self
    end
  end
end