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 131
def index
  instance.class.tire.index
end
matches() click to toggle source
# File lib/tire/model/search.rb, line 192
def matches
  instance.instance_eval do
    @tire__attributes ||= {}
    @tire__attributes['matches']
  end
end
matches=(value) click to toggle source
# File lib/tire/model/search.rb, line 199
def matches=(value)
  instance.instance_eval do
    @tire__attributes ||= {}
    @tire__attributes['matches'] = value
  end
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. Other objects are indexed "as is".

# File lib/tire/model/search.rb, line 167
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])
        else
          hash[key] = options[:as]
      end if options[:as]
    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 143
def update_index
  instance.run_callbacks :update_elasticsearch_index do
    if instance.destroyed?
      index.remove instance
    else
      @response = index.store( instance, {:percolate => percolator} )
      instance.tire.matches = @response['matches'] if instance.tire.respond_to?(:matches=)
      self
    end
  end
end