Class: Stretchy::Results::Base
- Inherits:
-
Object
- Object
- Stretchy::Results::Base
- Extended by:
- Forwardable
- Defined in:
- lib/stretchy/results/base.rb
Direct Known Subclasses
Instance Attribute Summary collapse
-
#base ⇒ Object
readonly
Returns the value of attribute base.
-
#index_name ⇒ Object
readonly
Returns the value of attribute index_name.
Instance Method Summary collapse
- #aggregations ⇒ Object
- #explanations ⇒ Object
- #hits ⇒ Object (also: #results)
- #ids ⇒ Object
-
#initialize(base) ⇒ Base
constructor
A new instance of Base.
- #max_score ⇒ Object
- #request ⇒ Object
- #response ⇒ Object
- #scores ⇒ Object
- #shards ⇒ Object
- #took ⇒ Object
- #total ⇒ Object
- #total_pages ⇒ Object
Constructor Details
#initialize(base) ⇒ Base
Returns a new instance of Base.
12 13 14 15 |
# File 'lib/stretchy/results/base.rb', line 12 def initialize(base) @base = base @index_name = base.index || Stretchy.index_name end |
Instance Attribute Details
#base ⇒ Object (readonly)
Returns the value of attribute base.
7 8 9 |
# File 'lib/stretchy/results/base.rb', line 7 def base @base end |
#index_name ⇒ Object (readonly)
Returns the value of attribute index_name.
7 8 9 |
# File 'lib/stretchy/results/base.rb', line 7 def index_name @index_name end |
Instance Method Details
#aggregations ⇒ Object
75 76 77 |
# File 'lib/stretchy/results/base.rb', line 75 def aggregations @aggregations ||= response['aggregations'] end |
#explanations ⇒ Object
69 70 71 72 73 |
# File 'lib/stretchy/results/base.rb', line 69 def explanations @scores ||= Hash[response['hits']['hits'].map do |hit| [hit['_id'], hit['_explanation']] end] end |
#hits ⇒ Object Also known as: results
47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/stretchy/results/base.rb', line 47 def hits @hits ||= response['hits']['hits'].map do |hit| merge_fields = hit.reject{|field, _| ['_source', 'fields'].include?(field) } source_fields = {} if hit['fields'] source_fields = Stretchy::Utils::DotHandler.convert_from_dotted_keys(hit['fields']) elsif hit['_source'] source_fields = hit['_source'] end source_fields.merge(merge_fields) end end |
#ids ⇒ Object
43 44 45 |
# File 'lib/stretchy/results/base.rb', line 43 def ids @ids ||= response['hits']['hits'].map{|h| h['_id'] =~ /\d+(\.\d+)?/ ? h['_id'].to_i : h['_id'] } end |
#max_score ⇒ Object
91 92 93 |
# File 'lib/stretchy/results/base.rb', line 91 def max_score @max_score ||= response['hits']['max_score'] end |
#request ⇒ Object
24 25 26 27 28 29 |
# File 'lib/stretchy/results/base.rb', line 24 def request return @request if @request @request = {query: base.to_search} @request[:aggs] = base.aggregate_builder if base.aggregate_builder.any? @request end |
#response ⇒ Object
31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/stretchy/results/base.rb', line 31 def response params = { type: type, body: request, from: offset, size: limit } params[:fields] = fields if fields params[:explain] = true if base.explain @response ||= Stretchy.search(params) end |
#scores ⇒ Object
63 64 65 66 67 |
# File 'lib/stretchy/results/base.rb', line 63 def scores @scores ||= Hash[response['hits']['hits'].map do |hit| [hit['_id'], hit['_score']] end] end |
#shards ⇒ Object
83 84 85 |
# File 'lib/stretchy/results/base.rb', line 83 def shards @shards ||= response['_shards'] end |
#took ⇒ Object
79 80 81 |
# File 'lib/stretchy/results/base.rb', line 79 def took @took ||= response['took'] end |
#total ⇒ Object
87 88 89 |
# File 'lib/stretchy/results/base.rb', line 87 def total @total ||= response['hits']['total'] end |
#total_pages ⇒ Object
20 21 22 |
# File 'lib/stretchy/results/base.rb', line 20 def total_pages [(total.to_f / limit).ceil, 1].max end |