Class: Sunspot::Search::QueryGroup

Inherits:
Object
  • Object
show all
Defined in:
lib/sunspot/search/query_group.rb

Instance Method Summary collapse

Constructor Details

#initialize(queries, search) ⇒ QueryGroup

:nodoc:



6
7
8
# File 'lib/sunspot/search/query_group.rb', line 6

def initialize(queries, search) #:nodoc:
  @queries, @search = queries, search
end

Instance Method Details

#groupsObject



10
11
12
13
14
15
16
17
# File 'lib/sunspot/search/query_group.rb', line 10

def groups
  @groups ||=
    if solr_response_for_first
      groups = @queries.map { |query| Group.new(query.label, doclist_for(query), @search) }

      paginate_collection(groups)
    end
end

#matchesObject



19
20
21
22
23
# File 'lib/sunspot/search/query_group.rb', line 19

def matches
  if solr_response_for_first
    solr_response_for_first['matches'].to_i
  end
end

#populate_all_hitsObject

It populates all grouped hits at once. Useful for eager loading fall grouped results at once.



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/sunspot/search/query_group.rb', line 33

def populate_all_hits
  # Init a 2 dimension Hash that contains an array per key
  id_hit_hash = Hash.new { |hash, key| hash[key] = Hash.new{ |h, k| h[k] = [] } }
  groups.each do |g|
    # Take all hits to being populated later on
    g.hits.each do |hit|
      id_hit_hash[hit.class_name][hit.primary_key] |= [hit]
    end
  end
  # Go for each class and load the results' objects into each of the hits
  id_hit_hash.each_pair do |class_name, many_hits|
    ids = many_hits.keys
    data_accessor = @search.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|
      hits = hits_for_class.delete(Adapters::InstanceAdapter.adapt(result).id.to_s)
      hits.each{ |hit| hit.result = result }
    end
    hits_for_class.values.each { |hits| hits.each{|hit| hit.result = nil } }
  end
end

#totalObject



25
26
27
# File 'lib/sunspot/search/query_group.rb', line 25

def total
  @queries.count
end