Class: Elasticsearch::Persistence::Repository::Response::Results

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/elasticsearch/persistence/repository/response/results.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(repository, response, options = {}) ⇒ Results

Returns a new instance of Results.



13
14
15
16
17
# File 'lib/elasticsearch/persistence/repository/response/results.rb', line 13

def initialize(repository, response, options = {})
  @repository = repository
  @response = Elasticsearch::Model::HashWrapper.new(response)
  @options = options
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *arguments, &block) ⇒ Object

rubocop:disable Style/MissingRespondToMissing



19
20
21
# File 'lib/elasticsearch/persistence/repository/response/results.rb', line 19

def method_missing(method_name, *arguments, &block) # rubocop:disable Style/MissingRespondToMissing
  results.respond_to?(method_name) ? results.__send__(method_name, *arguments, &block) : super
end

Instance Attribute Details

#repositoryObject (readonly)

Returns the value of attribute repository.



11
12
13
# File 'lib/elasticsearch/persistence/repository/response/results.rb', line 11

def repository
  @repository
end

#responseObject (readonly)

Returns the value of attribute response.



11
12
13
# File 'lib/elasticsearch/persistence/repository/response/results.rb', line 11

def response
  @response
end

Instance Method Details

#current_pageObject



63
64
65
# File 'lib/elasticsearch/persistence/repository/response/results.rb', line 63

def current_page
  @pagination_attrs[:current_page]
end

#each_with_hit(&block) ⇒ Object



35
36
37
# File 'lib/elasticsearch/persistence/repository/response/results.rb', line 35

def each_with_hit(&block)
  results.zip(response['hits']['hits']).each(&block)
end

#map_with_hit(&block) ⇒ Object



39
40
41
# File 'lib/elasticsearch/persistence/repository/response/results.rb', line 39

def map_with_hit(&block)
  results.zip(response['hits']['hits']).map(&block)
end

#max_scoreObject



31
32
33
# File 'lib/elasticsearch/persistence/repository/response/results.rb', line 31

def max_score
  response['hits']['max_score']
end

#paginate(options) ⇒ Object



49
50
51
52
53
54
55
56
57
# File 'lib/elasticsearch/persistence/repository/response/results.rb', line 49

def paginate(options)
  @pagination_attrs = {
    total_pages: (total.to_f / options[:per_page]).ceil,
    current_page: options[:page],
    per_page: options[:per_page],
    total_entries: total
  }
  self
end

#per_pageObject



67
68
69
# File 'lib/elasticsearch/persistence/repository/response/results.rb', line 67

def per_page
  @pagination_attrs[:per_page]
end

#respond_to?(method_name, include_private = false) ⇒ Boolean

rubocop:disable Style/OptionalBooleanParameter

Returns:

  • (Boolean)


23
24
25
# File 'lib/elasticsearch/persistence/repository/response/results.rb', line 23

def respond_to?(method_name, include_private = false) # rubocop:disable Style/OptionalBooleanParameter
  results.respond_to?(method_name) || super
end

#resultsObject



43
44
45
46
47
# File 'lib/elasticsearch/persistence/repository/response/results.rb', line 43

def results
  @results ||= response['hits']['hits'].map do |document|
    repository.deserialize(document.to_hash)
  end
end

#totalObject



27
28
29
# File 'lib/elasticsearch/persistence/repository/response/results.rb', line 27

def total
  response['hits']['total']
end

#total_entriesObject



71
72
73
# File 'lib/elasticsearch/persistence/repository/response/results.rb', line 71

def total_entries
  @pagination_attrs[:total_entries]
end

#total_pagesObject



59
60
61
# File 'lib/elasticsearch/persistence/repository/response/results.rb', line 59

def total_pages
  @pagination_attrs[:total_pages]
end