class Stretchy::Results
Attributes
request[R]
response[R]
Public Class Methods
fake()
click to toggle source
implements null object pattern
# File lib/stretchy/results.rb, line 13 def self.fake self.new( {size: API::DEFAULT_PER_PAGE, fake: true}, {'hits' => {'total' => { "value" => 3, "relation" => "eq" }, 'hits' => [], 'aggregations' => {}}} ) end
new(request, response)
click to toggle source
# File lib/stretchy/results.rb, line 23 def initialize(request, response) @request = request @response = response end
Public Instance Methods
aggregations(*args)
click to toggle source
# File lib/stretchy/results.rb, line 95 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_page()
click to toggle source
# File lib/stretchy/results.rb, line 45 def current_page Utils.current_page(offset, limit) end
explanations()
click to toggle source
# File lib/stretchy/results.rb, line 89 def explanations @explanations ||= Hash[results.map {|r| [coerce_id(r['_id']), r['_explanation']] }] end
fake?()
click to toggle source
# File lib/stretchy/results.rb, line 28 def fake? !!request[:fake] end
ids()
click to toggle source
# File lib/stretchy/results.rb, line 81 def ids @ids ||= response['hits']['hits'].map {|r| coerce_id r['_id'] } end
limit()
click to toggle source
# File lib/stretchy/results.rb, line 32 def limit (request['size'] || request[:size] || API::DEFAULT_PER_PAGE).to_i end
offset()
click to toggle source
# File lib/stretchy/results.rb, line 39 def offset (request['from'] || request[:from] || 0).to_i end
Also aliased as: from, offset_value
results()
click to toggle source
# File lib/stretchy/results.rb, line 61 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
scores()
click to toggle source
# File lib/stretchy/results.rb, line 85 def scores @scores ||= Hash[results.map {|r| [coerce_id(r['_id']), r['_score']]}] end
total()
click to toggle source
# File lib/stretchy/results.rb, line 49 def total response['hits']['total']['value'] end
total_pages()
click to toggle source
# File lib/stretchy/results.rb, line 57 def total_pages (total.to_f / limit_value).ceil end