Class: Swiftype::ResultSet

Inherits:
Object
  • Object
show all
Defined in:
lib/swiftype/result_set.rb

Overview

The Swiftype::ResultSet class represents a search or suggest result returned by the Swiftype API.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(results) ⇒ ResultSet

Create a Swiftype::ResultSet from deserialized JSON.



19
20
21
22
23
# File 'lib/swiftype/result_set.rb', line 19

def initialize(results)
  @records = results['records']
  @info = results['info']
  @errors = results['errors']
end

Instance Attribute Details

#errorsObject (readonly)

a hash of errors for the search (for example filtering on a missing attribute) keyed by DocumentType slug



7
8
9
# File 'lib/swiftype/result_set.rb', line 7

def errors
  @errors
end

#infoObject (readonly)

a hash of extra query info (for example, facets and number of results) keyed by DocumentType slug. Use the convenience methods of this class for easier access.



16
17
18
# File 'lib/swiftype/result_set.rb', line 16

def info
  @info
end

#recordsObject (readonly)

a hash of results for the search keyed by DocumentType slug. Use `[]` to access results more easily.



11
12
13
# File 'lib/swiftype/result_set.rb', line 11

def records
  @records
end

Instance Method Details

#[](document_type) ⇒ Object

Get results for the provided DocumentType

Parameters:

  • document_type (String)

    the DocumentType slug to get results for



28
29
30
# File 'lib/swiftype/result_set.rb', line 28

def [](document_type)
  records[document_type]
end

#current_pageObject

Return the page of results for this ResultSet



47
48
49
# File 'lib/swiftype/result_set.rb', line 47

def current_page
  info[info.keys.first]['current_page']
end

#document_typesObject

Return a list of DocumentType slugs represented in the ResultSet.



33
34
35
# File 'lib/swiftype/result_set.rb', line 33

def document_types
  records.keys
end

#facets(document_type) ⇒ Object

Return the search facets for the provided DocumentType. Will be empty unless a facets parameter was provided when calling the search API.

Parameters:

  • document_type (String)

    the DocumentType slug to get facets for



42
43
44
# File 'lib/swiftype/result_set.rb', line 42

def facets(document_type)
  info[document_type]['facets']
end

#num_pages(document_type = nil) ⇒ Object

Return the number of pages. Since a search can cover multiple DocumentTypes with different numbers of results, the number of pages can vary between DocumentTypes. With no argument, it returns the maximum num_pages for all DocumentTypes in this ResultSet. With a DocumentType slug, it returns the number of pages for that DocumentType.

Parameters:

  • document_type (String) (defaults to: nil)

    the DocumentType slug to return the number of pages for



64
65
66
67
68
69
70
# File 'lib/swiftype/result_set.rb', line 64

def num_pages(document_type=nil)
  if document_type
    info[document_type]['num_pages']
  else
    info.values.map { |v| v['num_pages'] }.max
  end
end

#per_pageObject

Return the number of results per page.



52
53
54
# File 'lib/swiftype/result_set.rb', line 52

def per_page
  info[info.keys.first]['per_page']
end

#queryObject

Return the query used for this search



78
79
80
# File 'lib/swiftype/result_set.rb', line 78

def query
  info[info.keys.first]['query']
end

#total_result_count(document_type) ⇒ Object

Return the total number of results for the query



73
74
75
# File 'lib/swiftype/result_set.rb', line 73

def total_result_count(document_type)
  info[document_type]['total_result_count']
end