Class: Sunspot::Search::FieldGroup

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

Instance Method Summary collapse

Constructor Details

#initialize(field, search) ⇒ FieldGroup

:nodoc:



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

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

Instance Method Details

#groupsObject



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

def groups
  @groups ||=
    if solr_response
      paginate_collection(
        solr_response['groups'].map do |group|
          Group.new(group['groupValue'], group['doclist'], @search)
        end
      )
    end
end

#matchesObject



21
22
23
24
25
# File 'lib/sunspot/search/field_group.rb', line 21

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

#populate_all_hitsObject

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



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/sunspot/search/field_group.rb', line 37

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



27
28
29
30
31
# File 'lib/sunspot/search/field_group.rb', line 27

def total
  if solr_response
    solr_response['ngroups'].to_i
  end
end