Class: Solr::GroupedDocumentCollection

Inherits:
DocumentCollection show all
Defined in:
lib/solr/grouped_document_collection.rb

Instance Attribute Summary collapse

Attributes inherited from DocumentCollection

#documents, #total_count

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from DocumentCollection

#each, #find, from_ids, #unshift

Constructor Details

#initialize(documents:, total_count:, group_counts:) ⇒ GroupedDocumentCollection

Returns a new instance of GroupedDocumentCollection.



9
10
11
12
# File 'lib/solr/grouped_document_collection.rb', line 9

def initialize(documents:, total_count:, group_counts:)
  super(documents: documents, total_count: total_count)
  @group_counts = group_counts
end

Instance Attribute Details

#group_countsObject (readonly)

Returns the value of attribute group_counts.



3
4
5
# File 'lib/solr/grouped_document_collection.rb', line 3

def group_counts
  @group_counts
end

Class Method Details

.emptyObject



5
6
7
# File 'lib/solr/grouped_document_collection.rb', line 5

def self.empty
  new(documents: [], total_count: 0, group_counts: {})
end

Instance Method Details

#+(other) ⇒ Object



29
30
31
32
33
34
35
36
# File 'lib/solr/grouped_document_collection.rb', line 29

def +(other)
  other_group_counts = other.is_a?(Solr::GroupedDocumentCollection) ? other.group_counts : {}
  self.class.new(
    documents: documents + other.documents,
    total_count: total_count + other.total_count,
    group_counts: group_counts.merge(other_group_counts)
  )
end

#first(n) ⇒ Object



14
15
16
17
18
# File 'lib/solr/grouped_document_collection.rb', line 14

def first(n)
  new_documents = documents.first(n)
  self.class.new(documents: new_documents,
                 total_count: new_documents.count, group_counts: group_counts)
end

#slice(range) ⇒ Object



20
21
22
23
24
25
26
27
# File 'lib/solr/grouped_document_collection.rb', line 20

def slice(range)
  new_documents = documents[range]
  if new_documents
    self.class.new(documents: new_documents, total_count: new_documents.count, group_counts: group_counts)
  else
    self.class.empty
  end
end