Class: ElasticsearchRecord::Result

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/elasticsearch_record/result.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(response, columns = [], column_types = {}) ⇒ Result

initializes a new result object

Parameters:

  • response (Elasticsearch::API::Response, Object, nil)
  • columns (Array) (defaults to: [])
  • column_types (Hash) (defaults to: {})


25
26
27
28
29
30
31
32
33
34
# File 'lib/elasticsearch_record/result.rb', line 25

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_typesObject (readonly)

Returns the value of attribute column_types.



19
20
21
# File 'lib/elasticsearch_record/result.rb', line 19

def column_types
  @column_types
end

#columnsObject (readonly)

Returns the value of attribute columns.



19
20
21
# File 'lib/elasticsearch_record/result.rb', line 19

def columns
  @columns
end

#responseObject (readonly)

Returns the value of attribute response.



19
20
21
# File 'lib/elasticsearch_record/result.rb', line 19

def response
  @response
end

Class Method Details

.empty(async: false) ⇒ ElasticsearchRecord::Result (frozen)

creates an empty response

Returns:



11
12
13
14
15
16
17
# File 'lib/elasticsearch_record/result.rb', line 11

def self.empty(async: false) # :nodoc:
  if async
    EMPTY_ASYNC
  else
    EMPTY
  end
end

Instance Method Details

#[](idx) ⇒ Object



130
131
132
# File 'lib/elasticsearch_record/result.rb', line 130

def [](idx)
  computed_results[idx]
end

#aggregationsActiveSupport::HashWithIndifferentAccess, Hash

returns the response RAW aggregations hash.

Returns:

  • (ActiveSupport::HashWithIndifferentAccess, Hash)


71
72
73
# File 'lib/elasticsearch_record/result.rb', line 71

def aggregations
  response.key?('aggregations') ? response['aggregations'].with_indifferent_access : {}
end

#bucketsActiveSupport::HashWithIndifferentAccess

returns the (nested) bucket values (and aggregated values) from the response aggregations.

Returns:

  • (ActiveSupport::HashWithIndifferentAccess)


77
78
79
80
81
82
83
84
# File 'lib/elasticsearch_record/result.rb', line 77

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)|
    buckets[key] = _resolve_bucket(agg)
    buckets
  }.with_indifferent_access
end

#cancelObject

used by ActiveRecord



146
147
148
# File 'lib/elasticsearch_record/result.rb', line 146

def cancel # :nodoc:
  self
end

#cast_values(type_overrides = {}) ⇒ Object

used by ActiveRecord



151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
# File 'lib/elasticsearch_record/result.rb', line 151

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.



110
111
112
113
114
115
116
# File 'lib/elasticsearch_record/result.rb', line 110

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.

Returns:

  • (Boolean)


119
120
121
# File 'lib/elasticsearch_record/result.rb', line 119

def empty?
  length == 0
end

#hitsActiveSupport::HashWithIndifferentAccess, Hash

returns the response RAW hits hash. PLEASE NOTE: Does not return the nested hits (+response['hits']['hits']+) array!

Returns:

  • (ActiveSupport::HashWithIndifferentAccess, Hash)


53
54
55
# File 'lib/elasticsearch_record/result.rb', line 53

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

Returns:

  • (Boolean)


88
89
90
# File 'lib/elasticsearch_record/result.rb', line 88

def includes_column?(name)
  @columns&.include?(name)
end

#last(n = nil) ⇒ Object

Returns the last record from the rows collection.



135
136
137
# File 'lib/elasticsearch_record/result.rb', line 135

def last(n = nil)
  n ? computed_results.last(n) : computed_results.last
end

#lengthInteger

Returns the number of elements in the response array. Either uses the +hits+ length or the +responses+ length (msearch).

Returns:

  • (Integer)


95
96
97
98
99
100
101
102
103
104
# File 'lib/elasticsearch_record/result.rb', line 95

def length
  if response.key?('hits')
    response['hits']['hits'].length
  elsif response.key?('responses')
    # used by +msearch+
    response['responses'].length
  else
    0
  end
end

#resultString

returns the response result string

Returns:

  • (String)


141
142
143
# File 'lib/elasticsearch_record/result.rb', line 141

def result
  response['result'] || ''
end

#resultsArray 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 '_id' or '_score' are not included)

Returns:

  • (Array)


60
61
62
63
64
# File 'lib/elasticsearch_record/result.rb', line 60

def results
  return [] unless response['hits']

  response['hits']['hits'].map { |result| result['_source'] }
end

#to_aryObject Also known as: to_a

Returns an array of hashes representing each row record.



124
125
126
# File 'lib/elasticsearch_record/result.rb', line 124

def to_ary
  computed_results
end

#tookInteger

returns the response duration time

Returns:

  • (Integer)


38
39
40
# File 'lib/elasticsearch_record/result.rb', line 38

def took
  response['took']
end

#totalInteger

returns the response total value. either chops the +total+ value directly from response, from hits or aggregations.

Returns:

  • (Integer)


45
46
47
48
# File 'lib/elasticsearch_record/result.rb', line 45

def total
  # chop total only
  @total ||= _chop_total
end