Class: Noiseless::Response::Results

Inherits:
Base
  • Object
show all
Defined in:
lib/noiseless/response/results.rb

Defined Under Namespace

Classes: Result

Instance Method Summary collapse

Methods inherited from Base

#aggregations, #count, #each_with_index, #empty?, #hits, #include_pagination_info, #initialize, #length, #response, #result, #results, #suggestions, #took, #total, #total_count

Methods included from Pagination::ResponsePagination

#current_page, #first_page?, #last_page?, #limit_value, #next_page, #offset_value, #out_of_range?, #pagination_metadata, #prev_page, #total_pages

Constructor Details

This class inherits a constructor from Noiseless::Response::Base

Instance Method Details

#[](index) ⇒ Object



44
45
46
47
# File 'lib/noiseless/response/results.rb', line 44

def [](index)
  hit = hits[index]
  hit ? Result.new(hit) : nil
end

#each ⇒ Object



6
7
8
9
10
11
12
# File 'lib/noiseless/response/results.rb', line 6

def each
  return enum_for(__method__) unless block_given?

  hits.each do |hit|
    yield Result.new(hit)
  end
end

#each_source ⇒ Object



22
23
24
25
26
# File 'lib/noiseless/response/results.rb', line 22

def each_source(&)
  return enum_for(__method__) unless block_given?

  sources.each(&)
end

#find ⇒ Object

Make results enumerable with source property



82
83
84
85
86
87
88
89
90
# File 'lib/noiseless/response/results.rb', line 82

def find
  return enum_for(__method__) unless block_given?

  hits.each do |hit|
    result = Result.new(hit)
    return result if yield(result)
  end
  nil
end

#first ⇒ Object



34
35
36
37
# File 'lib/noiseless/response/results.rb', line 34

def first
  hit = hits.first
  hit ? Result.new(hit) : nil
end

#ids ⇒ Object



73
74
75
# File 'lib/noiseless/response/results.rb', line 73

def ids
  @ids ||= hits.map { |hit| hit["_id"] }
end

#last ⇒ Object



39
40
41
42
# File 'lib/noiseless/response/results.rb', line 39

def last
  hit = hits.last
  hit ? Result.new(hit) : nil
end

#map ⇒ Object



53
54
55
56
57
58
59
60
61
# File 'lib/noiseless/response/results.rb', line 53

def map
  return enum_for(__method__) unless block_given?

  results = []
  each do |result|
    results << yield(result)
  end
  results
end

#map_source ⇒ Object



28
29
30
31
32
# File 'lib/noiseless/response/results.rb', line 28

def map_source(&)
  return enum_for(__method__) unless block_given?

  sources.map(&)
end

#scores ⇒ Object



77
78
79
# File 'lib/noiseless/response/results.rb', line 77

def scores
  @scores ||= hits.map { |hit| hit["_score"] }
end

#select ⇒ Object



63
64
65
66
67
68
69
70
71
# File 'lib/noiseless/response/results.rb', line 63

def select
  return enum_for(__method__) unless block_given?

  results = []
  each do |result|
    results << result if yield(result)
  end
  results
end

#sources ⇒ Object Also known as: records



14
15
16
17
18
# File 'lib/noiseless/response/results.rb', line 14

def sources
  # Indifferent access so callers can pluck(:id) or read ["id"] regardless
  # of which adapter produced the hit.
  @sources ||= hits.map { |hit| (hit["_source"] || {}).with_indifferent_access }
end

#to_a ⇒ Object



49
50
51
# File 'lib/noiseless/response/results.rb', line 49

def to_a
  hits.map { |hit| Result.new(hit) }
end