class Springy::Results

Attributes

request[R]
response[R]

Public Class Methods

fake() click to toggle source

implements null object pattern

# File lib/springy/results.rb, line 12
def self.fake
  self.new(
    {size: API::DEFAULT_PER_PAGE, fake: true},
    {'hits' => {'total' => 0, 'hits' => [], 'aggregations' => {}}}
  )
end
new(request, response) click to toggle source
# File lib/springy/results.rb, line 19
def initialize(request, response)
  @request  = request
  @response = response
end

Public Instance Methods

aggregations(*args) click to toggle source
# File lib/springy/results.rb, line 93
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
count()
Alias for: total
current_page() click to toggle source
# File lib/springy/results.rb, line 42
def current_page
  Utils.current_page(offset, limit)
end
explanations() click to toggle source
# File lib/springy/results.rb, line 87
def explanations
  @explanations ||= Hash[results.map {|r|
    [coerce_id(r['_id']), r['_explanation']]
  }]
end
fake?() click to toggle source
# File lib/springy/results.rb, line 24
def fake?
  !!request[:fake]
end
from()
Alias for: offset
hits()
Alias for: results
ids() click to toggle source
# File lib/springy/results.rb, line 79
def ids
  @ids ||= response['hits']['hits'].map {|r| coerce_id r['_id'] }
end
length()
Alias for: total
limit() click to toggle source
# File lib/springy/results.rb, line 28
def limit
  (request['size'] || request[:size] || API::DEFAULT_PER_PAGE).to_i
end
Also aliased as: size, limit_value, per_page
limit_value()
Alias for: limit
offset() click to toggle source
# File lib/springy/results.rb, line 36
def offset
  (request['from'] || request[:from] || 0).to_i
end
Also aliased as: from, offset_value
offset_value()
Alias for: offset
per_page()
Alias for: limit
results() click to toggle source
# File lib/springy/results.rb, line 58
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
Also aliased as: hits, to_a
scores() click to toggle source
# File lib/springy/results.rb, line 83
def scores
  @scores ||= Hash[results.map {|r| [coerce_id(r['_id']), r['_score']]}]
end
size()
Alias for: limit
to_a()
Alias for: results
total() click to toggle source
# File lib/springy/results.rb, line 46
def total
  response['hits']['total']
end
Also aliased as: total_count, count, length, size
total_count()
Alias for: total
total_pages() click to toggle source
# File lib/springy/results.rb, line 54
def total_pages
  (total.to_f / limit_value).ceil
end