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



42
43
44
45
# File 'lib/noiseless/response/results.rb', line 42

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

#eachObject



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_sourceObject



20
21
22
23
24
# File 'lib/noiseless/response/results.rb', line 20

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

  sources.each(&)
end

#findObject

Make results enumerable with source property



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

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

#firstObject



32
33
34
35
# File 'lib/noiseless/response/results.rb', line 32

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

#idsObject



71
72
73
# File 'lib/noiseless/response/results.rb', line 71

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

#lastObject



37
38
39
40
# File 'lib/noiseless/response/results.rb', line 37

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

#mapObject



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

def map
  return enum_for(__method__) unless block_given?

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

#map_sourceObject



26
27
28
29
30
# File 'lib/noiseless/response/results.rb', line 26

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

  sources.map(&)
end

#scoresObject



75
76
77
# File 'lib/noiseless/response/results.rb', line 75

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

#selectObject



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

def select
  return enum_for(__method__) unless block_given?

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

#sourcesObject Also known as: records



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

def sources
  @sources ||= hits.map { |hit| hit["_source"] }
end

#to_aObject



47
48
49
# File 'lib/noiseless/response/results.rb', line 47

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