Class: Sequel::Plugins::Elasticsearch::Result

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/sequel/plugins/elasticsearch/result.rb

Overview

A wrapper around Elasticsearch results to make it behave more like a Sequel Dataset.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(result, model = nil) ⇒ Result

Initialize the Result

  • result The result returns from the Elasticsearch client / .es call.

  • model The model class on which the results should be applied.



27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/sequel/plugins/elasticsearch/result.rb', line 27

def initialize(result, model = nil)
  return unless result && result['hits']

  @result = result
  @scroll_id = result['_scroll_id']
  @total = result['hits']['total']
  @timed_out = result['timed_out']
  @took = result['took']
  @model = model

  result['hits']['hits'] = result['hits']['hits'].map { |h| convert(h) }
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(meth, *args, &block) ⇒ Object

Send all undefined methods to the result[‘hits’] array.



53
54
55
# File 'lib/sequel/plugins/elasticsearch/result.rb', line 53

def method_missing(meth, *args, &block)
  respond_to_missing?(meth) ? result['hits']['hits'].send(meth, *args, &block) : super
end

Instance Attribute Details

#modelObject (readonly)

The model class associated with this result



21
22
23
# File 'lib/sequel/plugins/elasticsearch/result.rb', line 21

def model
  @model
end

#resultObject (readonly)

The original result returned from the Elasticsearch client



11
12
13
# File 'lib/sequel/plugins/elasticsearch/result.rb', line 11

def result
  @result
end

#scroll_idObject (readonly)

The scroll id, if set, from the result



13
14
15
# File 'lib/sequel/plugins/elasticsearch/result.rb', line 13

def scroll_id
  @scroll_id
end

#timed_outObject (readonly)

If the Elasticsearch call timed out or note



19
20
21
# File 'lib/sequel/plugins/elasticsearch/result.rb', line 19

def timed_out
  @timed_out
end

#tookObject (readonly)

The time, in miliseconds, the Elasticsearch call took to complete



17
18
19
# File 'lib/sequel/plugins/elasticsearch/result.rb', line 17

def took
  @took
end

#totalObject (readonly)

The total number of documents in the Elasticsearch result



15
16
17
# File 'lib/sequel/plugins/elasticsearch/result.rb', line 15

def total
  @total
end

Instance Method Details

#allObject

Send back the complete result set



48
49
50
# File 'lib/sequel/plugins/elasticsearch/result.rb', line 48

def all
  result['hits']['hits']
end

#eachObject

Each implementation for the Enumerable. Yield each element in the result[‘hits’] array.



41
42
43
44
45
# File 'lib/sequel/plugins/elasticsearch/result.rb', line 41

def each
  return [] unless result['hits'] && result['hits']['hits'].count.positive?

  result['hits']['hits'].each { |h| yield h }
end

#respond_to_missing?(meth, include_private = false) ⇒ Boolean

Send all undefined methods to the result[‘hits’] array.

Returns:

  • (Boolean)


58
59
60
# File 'lib/sequel/plugins/elasticsearch/result.rb', line 58

def respond_to_missing?(meth, include_private = false)
  result['hits']['hits'].respond_to?(meth, include_private) || super
end