Class: Stretchy::Results

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Enumerable, Utils::Methods
Defined in:
lib/stretchy/results.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Utils::Methods

#coerce_id, #dotify, #extract_options!, #is_empty?, #nestify, #require_params!

Constructor Details

#initialize(request, response) ⇒ Results

Returns a new instance of Results.



19
20
21
22
# File 'lib/stretchy/results.rb', line 19

def initialize(request, response)
  @request  = request
  @response = response
end

Instance Attribute Details

#requestObject (readonly)

Returns the value of attribute request.



9
10
11
# File 'lib/stretchy/results.rb', line 9

def request
  @request
end

#responseObject (readonly)

Returns the value of attribute response.



9
10
11
# File 'lib/stretchy/results.rb', line 9

def response
  @response
end

Class Method Details

.fakeObject

implements null object pattern



12
13
14
15
16
17
# File 'lib/stretchy/results.rb', line 12

def self.fake
  self.new(
    {size: API::DEFAULT_PER_PAGE, fake: true},
    {'hits' => {'total' => 0, 'hits' => [], 'aggregations' => {}}}
  )
end

Instance Method Details

#aggregations(*args) ⇒ Object



91
92
93
94
95
96
97
98
99
# File 'lib/stretchy/results.rb', line 91

def aggregations(*args)
  key = args.map(&:to_s).join('.')
  @aggregations ||= {}
  @aggregations[key] ||= begin
    args.reduce(response['aggregations']) do |agg, name|
      agg = agg[name.to_s] unless agg.nil?
    end
  end
end

#current_pageObject



41
42
43
# File 'lib/stretchy/results.rb', line 41

def current_page
  Utils.current_page(offset, limit)
end

#explanationsObject



85
86
87
88
89
# File 'lib/stretchy/results.rb', line 85

def explanations
  @explanations ||= Hash[results.map {|r|
    [coerce_id(r['_id']), r['_explanation']]
  }]
end

#fake?Boolean

Returns:

  • (Boolean)


24
25
26
# File 'lib/stretchy/results.rb', line 24

def fake?
  !!request[:fake]
end

#idsObject



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

def ids
  @ids ||= response['hits']['hits'].map {|r| coerce_id r['_id'] }
end

#limitObject Also known as: limit_value, per_page



28
29
30
# File 'lib/stretchy/results.rb', line 28

def limit
  (request['size'] || request[:size] || API::DEFAULT_PER_PAGE).to_i
end

#offsetObject Also known as: from, offset_value



35
36
37
# File 'lib/stretchy/results.rb', line 35

def offset
  (request['from'] || request[:from] || 0).to_i
end

#resultsObject Also known as: hits, to_a



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/stretchy/results.rb', line 57

def results
  @results ||= response['hits']['hits'].map do |r|
    fields        = r.reject {|k, _| k == '_source' || k == 'fields'}
    fields['_id'] = coerce_id(fields['_id']) if fields['_id']
    source        = r['_source'] || {}

    # Elasticsearch always returns array values when specific
    # fields are selected. Undesirable for single values, so
    # coerce to single values when appropriate
    selected      = r['fields']  || {}
    selected      = Hash[selected.map do |k,v|
      v.is_a?(Array) && v.count == 1 ? [k,v.first] : [k,v]
    end]

    source.merge(selected).merge(fields)
  end
end

#scoresObject



81
82
83
# File 'lib/stretchy/results.rb', line 81

def scores
  @scores ||= Hash[results.map {|r| [coerce_id(r['_id']), r['_score']]}]
end

#totalObject Also known as: size, total_count, count, length



45
46
47
# File 'lib/stretchy/results.rb', line 45

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

#total_pagesObject



53
54
55
# File 'lib/stretchy/results.rb', line 53

def total_pages
  (total.to_f / limit_value).ceil
end