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.


131
132
133
# File 'lib/tire/model/search.rb', line 131

def index
  instance.class.tire.index
end

#matchesObject


192
193
194
195
196
197
# File 'lib/tire/model/search.rb', line 192

def matches
  instance.instance_eval do
    @tire__attributes ||= {}
    @tire__attributes['matches']
  end
end

#matches=(value) ⇒ Object


199
200
201
202
203
204
# 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_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. Other objects are indexed "as is".


167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
# 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_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.


143
144
145
146
147
148
149
150
151
152
153
# 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