Module: Tire::Model::Search::InstanceMethods

Included in:
InstanceMethodsProxy
Defined in:
lib/tire/model/search.rb

Instance Method Summary collapse

Instance Method Details

#indexObject

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

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



119
120
121
# File 'lib/tire/model/search.rb', line 119

def index
  instance.class.tire.index
end

#matchesObject



182
183
184
# File 'lib/tire/model/search.rb', line 182

def matches
  @attributes['matches']
end

#matches=(value) ⇒ Object



186
187
188
# File 'lib/tire/model/search.rb', line 186

def matches=(value)
  @attributes ||= {}; @attributes['matches'] = value
end

#to_indexed_jsonObject

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.



159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
# File 'lib/tire/model/search.rb', line 159

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_indexObject Also known as: update_elasticsearch_index, update_elastic_search_index

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.



131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/tire/model/search.rb', line 131

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