Module: Tire::Model::Search::InstanceMethods
- Included in:
- InstanceMethodsProxy
- Defined in:
- lib/tire/model/search.rb
Instance Method Summary collapse
-
#index ⇒ Object
Returns a Tire::Index instance for this instance of the model.
- #tire_matches ⇒ Object
- #tire_matches=(value) ⇒ Object
-
#to_indexed_json ⇒ Object
The default JSON serialization of the model, based on its
#to_hash
representation. -
#update_index ⇒ Object
(also: #update_elasticsearch_index, #update_elastic_search_index)
Updates the index in Elasticsearch.
Instance Method Details
#index ⇒ Object
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 |
#tire_matches ⇒ Object
192 193 194 195 196 197 |
# File 'lib/tire/model/search.rb', line 192 def tire_matches instance.instance_eval do @tire__attributes ||= {} @tire__attributes['tire_matches'] end end |
#tire_matches=(value) ⇒ Object
199 200 201 202 203 204 |
# File 'lib/tire/model/search.rb', line 199 def tire_matches=(value) instance.instance_eval do @tire__attributes ||= {} @tire__attributes['tire_matches'] = value end end |
#to_indexed_json ⇒ Object
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, | case [:as] when String hash[key] = instance.instance_eval([:as]) when Proc hash[key] = instance.instance_eval(&[:as]) else hash[key] = [:as] end if [:as] end hash.to_json end end |
#update_index ⇒ Object 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.tire_matches = response['tire_matches'] if instance.tire.respond_to?(:tire_matches=) self end end end |