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
- #body ⇒ 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
80 81 82 |
# File 'lib/stretchy/results/base.rb', line 80 def aggregations @aggregations ||= response['aggregations'] end |
#body ⇒ Object
24 25 26 27 28 29 |
# File 'lib/stretchy/results/base.rb', line 24 def body return @body if @body @body = {query: base.to_search} @body[:aggs] = base.aggregate_builder if base.aggregate_builder.any? @body end |
#explanations ⇒ Object
74 75 76 77 78 |
# File 'lib/stretchy/results/base.rb', line 74 def explanations @scores ||= Hash[response['hits']['hits'].map do |hit| [hit['_id'], hit['_explanation']] end] end |
#hits ⇒ Object Also known as: results
52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/stretchy/results/base.rb', line 52 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
48 49 50 |
# File 'lib/stretchy/results/base.rb', line 48 def ids @ids ||= response['hits']['hits'].map{|h| h['_id'] =~ /\d+(\.\d+)?/ ? h['_id'].to_i : h['_id'] } end |
#max_score ⇒ Object
96 97 98 |
# File 'lib/stretchy/results/base.rb', line 96 def max_score @max_score ||= response['hits']['max_score'] end |
#request ⇒ Object
31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/stretchy/results/base.rb', line 31 def request return @request if @request @request = { type: type, body: body, from: offset, size: limit } @request[:fields] = fields if fields @request[:explain] = true if base.explain @request end |
#response ⇒ Object
44 45 46 |
# File 'lib/stretchy/results/base.rb', line 44 def response @response ||= Stretchy.search(request) end |
#scores ⇒ Object
68 69 70 71 72 |
# File 'lib/stretchy/results/base.rb', line 68 def scores @scores ||= Hash[response['hits']['hits'].map do |hit| [hit['_id'], hit['_score']] end] end |
#shards ⇒ Object
88 89 90 |
# File 'lib/stretchy/results/base.rb', line 88 def shards @shards ||= response['_shards'] end |
#took ⇒ Object
84 85 86 |
# File 'lib/stretchy/results/base.rb', line 84 def took @took ||= response['took'] end |
#total ⇒ Object
92 93 94 |
# File 'lib/stretchy/results/base.rb', line 92 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 |