Module: Sunspot::Search::HitEnumerable
- Included in:
- AbstractSearch, Group
- Defined in:
- lib/sunspot/search/hit_enumerable.rb
Overview
:nodoc:
Instance Method Summary collapse
-
#data_accessor_for(clazz) ⇒ Object
Get the data accessor that will be used to load a particular class out of persistent storage.
-
#each_hit_with_result ⇒ Object
Convenience method to iterate over hit and result objects.
- #hits(options = {}) ⇒ Object
-
#populate_hits ⇒ Object
Populate the Hit objects with their instances.
- #verified_hits ⇒ Object
Instance Method Details
#data_accessor_for(clazz) ⇒ Object
Get the data accessor that will be used to load a particular class out of persistent storage. Data accessors can implement any methods that may be useful for refining how data is loaded out of storage. When building a search manually (e.g., using the Sunspot#new_search method), this should be used before calling #execute(). Use the Sunspot::DSL::Search#data_accessor_for method when building searches using the block DSL.
63 64 65 66 67 68 69 |
# File 'lib/sunspot/search/hit_enumerable.rb', line 63 def data_accessor_for(clazz) #:nodoc: # FIXME: This method does not belong here, but I was getting bogged # down trying to figure out where it should go. Punted for now. # - AL 27 Nov 2011 (@data_accessors ||= {})[clazz.name.to_sym] ||= Adapters::DataAccessor.create(clazz) end |
#each_hit_with_result ⇒ Object
Convenience method to iterate over hit and result objects. Block is yielded a Sunspot::Server::Hit instance and a Sunspot::Server::Result instance.
Note that this method iterates over verified hits (see #hits method for more information).
48 49 50 51 52 |
# File 'lib/sunspot/search/hit_enumerable.rb', line 48 def each_hit_with_result verified_hits.each do |hit| yield(hit, hit.result) end end |
#hits(options = {}) ⇒ Object
4 5 6 7 8 9 10 11 12 |
# File 'lib/sunspot/search/hit_enumerable.rb', line 4 def hits( = {}) if [:verify] verified_hits elsif solr_docs solr_docs.map { |d| Hit.new(d, highlights_for(d), self) } else [] end end |
#populate_hits ⇒ Object
Populate the Hit objects with their instances. This is invoked the first time any hit has its instance requested, and all hits are loaded as a batch.
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/sunspot/search/hit_enumerable.rb', line 23 def populate_hits #:nodoc: id_hit_hash = Hash.new { |h, k| h[k] = {} } hits.each do |hit| id_hit_hash[hit.class_name][hit.primary_key] = hit end id_hit_hash.each_pair do |class_name, hits| ids = hits.map { |id, hit| hit.primary_key } data_accessor = data_accessor_for(Util.full_const_get(class_name)) hits_for_class = id_hit_hash[class_name] data_accessor.load_all(ids).each do |result| hit = hits_for_class.delete(Adapters::InstanceAdapter.adapt(result).id.to_s) hit.result = result end hits_for_class.values.each { |hit| hit.result = nil } end end |
#verified_hits ⇒ Object
14 15 16 |
# File 'lib/sunspot/search/hit_enumerable.rb', line 14 def verified_hits hits.select { |h| h.result } end |