Class: ElasticsearchRecord::Result
- Inherits:
-
Object
- Object
- ElasticsearchRecord::Result
- Includes:
- Enumerable
- Defined in:
- lib/elasticsearch_record/result.rb
Instance Attribute Summary collapse
-
#column_types ⇒ Object
readonly
Returns the value of attribute column_types.
-
#columns ⇒ Object
readonly
Returns the value of attribute columns.
-
#response ⇒ Object
readonly
Returns the value of attribute response.
Class Method Summary collapse
-
.empty ⇒ ElasticsearchRecord::Result (frozen)
creates an empty response.
Instance Method Summary collapse
- #[](idx) ⇒ Object
-
#aggregations ⇒ ActiveSupport::HashWithIndifferentAccess, Hash
returns the response RAW aggregations hash.
-
#buckets ⇒ ActiveSupport::HashWithIndifferentAccess
returns the (nested) bucket values (and aggregated values) from the response aggregations.
-
#cancel ⇒ Object
used by ActiveRecord.
-
#cast_values(type_overrides = {}) ⇒ Object
used by ActiveRecord.
-
#each(&block) ⇒ Object
Calls the given block once for each element in row collection, passing row as parameter.
-
#empty? ⇒ Boolean
Returns true if there are no records, otherwise false.
-
#hits ⇒ ActiveSupport::HashWithIndifferentAccess, Hash
returns the response RAW hits hash.
-
#includes_column?(name) ⇒ Boolean
Returns true if this result set includes the column named +name+.
-
#initialize(response, columns = [], column_types = {}) ⇒ Result
constructor
initializes a new result object.
-
#last(n = nil) ⇒ Object
Returns the last record from the rows collection.
-
#length ⇒ Integer
Returns the number of elements in the response array.
-
#result ⇒ String
returns the response result string.
-
#results ⇒ Array
(also: #rows)
Returns the RAW +_source+ data from each hit - aka.
-
#to_ary ⇒ Object
(also: #to_a)
Returns an array of hashes representing each row record.
-
#took ⇒ Integer
returns the response duration time.
-
#total ⇒ Integer
returns the response total value.
Constructor Details
#initialize(response, columns = [], column_types = {}) ⇒ Result
initializes a new result object
19 20 21 22 23 24 25 26 27 28 |
# File 'lib/elasticsearch_record/result.rb', line 19 def initialize(response, columns = [], column_types = {}) # contains either the response or creates a empty hash (if nil) @response = response.presence || {} # used to build computed_results @columns = columns # used to cast values @column_types = column_types end |
Instance Attribute Details
#column_types ⇒ Object (readonly)
Returns the value of attribute column_types.
13 14 15 |
# File 'lib/elasticsearch_record/result.rb', line 13 def column_types @column_types end |
#columns ⇒ Object (readonly)
Returns the value of attribute columns.
13 14 15 |
# File 'lib/elasticsearch_record/result.rb', line 13 def columns @columns end |
#response ⇒ Object (readonly)
Returns the value of attribute response.
13 14 15 |
# File 'lib/elasticsearch_record/result.rb', line 13 def response @response end |
Class Method Details
.empty ⇒ ElasticsearchRecord::Result (frozen)
creates an empty response
9 10 11 |
# File 'lib/elasticsearch_record/result.rb', line 9 def self.empty new(nil).freeze end |
Instance Method Details
#[](idx) ⇒ Object
138 139 140 |
# File 'lib/elasticsearch_record/result.rb', line 138 def [](idx) computed_results[idx] end |
#aggregations ⇒ ActiveSupport::HashWithIndifferentAccess, Hash
returns the response RAW aggregations hash.
65 66 67 |
# File 'lib/elasticsearch_record/result.rb', line 65 def aggregations response.key?('aggregations') ? response['aggregations'].with_indifferent_access : {} end |
#buckets ⇒ ActiveSupport::HashWithIndifferentAccess
returns the (nested) bucket values (and aggregated values) from the response aggregations.
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/elasticsearch_record/result.rb', line 71 def buckets # aggregations are already a hash with key => data, but to prevent reference manipulation on the hash # we have to create a new one here... aggregations.reduce({}) { |buckets, (key, agg)| # check if this agg has a bucket if agg.key?(:buckets) buckets[key] = agg[:buckets].reduce({}) { |m, b| # buckets can be a Hash or Array (of Hashes) bucket_key, bucket = b.is_a?(Hash) ? [b[:key], b] : b m[bucket_key] = bucket.except(:key, :doc_count).transform_values { |val| val[:value] } m } elsif agg.key?(:value) buckets[key] = agg[:value] elsif agg.key?(:values) buckets[key] = agg[:values] end buckets }.with_indifferent_access end |
#cancel ⇒ Object
used by ActiveRecord
154 155 156 |
# File 'lib/elasticsearch_record/result.rb', line 154 def cancel # :nodoc: self end |
#cast_values(type_overrides = {}) ⇒ Object
used by ActiveRecord
159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 |
# File 'lib/elasticsearch_record/result.rb', line 159 def cast_values(type_overrides = {}) # :nodoc: if columns.one? # Separated to avoid allocating an array per row key = columns.first type = if type_overrides.is_a?(Array) type_overrides.first else column_type(columns.first, type_overrides) end computed_results.map do |result| type.deserialize(result[key]) end else types = if type_overrides.is_a?(Array) type_overrides else columns.map { |name| column_type(name, type_overrides) } end size = types.size computed_results.map do |result| Array.new(size) { |i| key = columns[i] types[i].deserialize(result[key]) } end end end |
#each(&block) ⇒ Object
Calls the given block once for each element in row collection, passing row as parameter.
Returns an +Enumerator+ if no block is given.
118 119 120 121 122 123 124 |
# File 'lib/elasticsearch_record/result.rb', line 118 def each(&block) if block_given? computed_results.each(&block) else computed_results.to_enum { @computed_results.size } end end |
#empty? ⇒ Boolean
Returns true if there are no records, otherwise false.
127 128 129 |
# File 'lib/elasticsearch_record/result.rb', line 127 def empty? length == 0 end |
#hits ⇒ ActiveSupport::HashWithIndifferentAccess, Hash
returns the response RAW hits hash. PLEASE NOTE: Does not return the nested hits (+response['hits']['hits']+) array!
47 48 49 |
# File 'lib/elasticsearch_record/result.rb', line 47 def hits response.key?('hits') ? response['hits'].with_indifferent_access : {} end |
#includes_column?(name) ⇒ Boolean
Returns true if this result set includes the column named +name+. used by ActiveRecord
96 97 98 |
# File 'lib/elasticsearch_record/result.rb', line 96 def includes_column?(name) @columns&.include?(name) end |
#last(n = nil) ⇒ Object
Returns the last record from the rows collection.
143 144 145 |
# File 'lib/elasticsearch_record/result.rb', line 143 def last(n = nil) n ? computed_results.last(n) : computed_results.last end |
#length ⇒ Integer
Returns the number of elements in the response array. Either uses the +hits+ length or the +responses+ length (msearch).
103 104 105 106 107 108 109 110 111 112 |
# File 'lib/elasticsearch_record/result.rb', line 103 def length if response.key?('hits') response['hits']['hits'].length elsif response.key?('responses') # used by +msearch+ response['responses'].length else 0 end end |
#result ⇒ String
returns the response result string
149 150 151 |
# File 'lib/elasticsearch_record/result.rb', line 149 def result response['result'] || '' end |
#results ⇒ Array Also known as: rows
Returns the RAW +_source+ data from each hit - aka. +rows+. PLEASE NOTE: The array will only contain the RAW data from each +_source+ (meta info like '_score' is not included)
54 55 56 57 58 |
# File 'lib/elasticsearch_record/result.rb', line 54 def results return [] unless response['hits'] response['hits']['hits'].map { |result| result['_source'] } end |
#to_ary ⇒ Object Also known as: to_a
Returns an array of hashes representing each row record.
132 133 134 |
# File 'lib/elasticsearch_record/result.rb', line 132 def to_ary computed_results end |
#took ⇒ Integer
returns the response duration time
32 33 34 |
# File 'lib/elasticsearch_record/result.rb', line 32 def took response['took'] end |
#total ⇒ Integer
returns the response total value. either chops the +total+ value directly from response, from hits or aggregations.
39 40 41 42 |
# File 'lib/elasticsearch_record/result.rb', line 39 def total # chop total only @total ||= _chop_total end |