Class: Sequel::Plugins::Elasticsearch::Result
- Inherits:
-
Object
- Object
- Sequel::Plugins::Elasticsearch::Result
- 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
-
#model ⇒ Object
readonly
The model class associated with this result.
-
#result ⇒ Object
readonly
The original result returned from the Elasticsearch client.
-
#scroll_id ⇒ Object
readonly
The scroll id, if set, from the result.
-
#timed_out ⇒ Object
readonly
If the Elasticsearch call timed out or note.
-
#took ⇒ Object
readonly
The time, in miliseconds, the Elasticsearch call took to complete.
-
#total ⇒ Object
readonly
The total number of documents in the Elasticsearch result.
Instance Method Summary collapse
-
#each ⇒ Object
Each implementation for the Enumerable.
-
#initialize(result, model = nil) ⇒ Result
constructor
Initialize the Result.
-
#method_missing(meth, *args, &block) ⇒ Object
Send all undefined methods to the result[‘hits’] array.
-
#respond_to_missing?(meth, include_private = false) ⇒ Boolean
Send all undefined methods to the result[‘hits’] array.
Constructor Details
#initialize(result, model = nil) ⇒ Result
Initialize the Result
-
resultThe result returns from the Elasticsearch client /.escall. -
modelThe model class on which the results should be applied.
25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/sequel/plugins/elasticsearch/result.rb', line 25 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.
46 47 48 |
# File 'lib/sequel/plugins/elasticsearch/result.rb', line 46 def method_missing(meth, *args, &block) respond_to_missing?(meth) ? result['hits']['hits'].send(meth, *args, &block) : super end |
Instance Attribute Details
#model ⇒ Object (readonly)
The model class associated with this result
19 20 21 |
# File 'lib/sequel/plugins/elasticsearch/result.rb', line 19 def model @model end |
#result ⇒ Object (readonly)
The original result returned from the Elasticsearch client
9 10 11 |
# File 'lib/sequel/plugins/elasticsearch/result.rb', line 9 def result @result end |
#scroll_id ⇒ Object (readonly)
The scroll id, if set, from the result
11 12 13 |
# File 'lib/sequel/plugins/elasticsearch/result.rb', line 11 def scroll_id @scroll_id end |
#timed_out ⇒ Object (readonly)
If the Elasticsearch call timed out or note
17 18 19 |
# File 'lib/sequel/plugins/elasticsearch/result.rb', line 17 def timed_out @timed_out end |
#took ⇒ Object (readonly)
The time, in miliseconds, the Elasticsearch call took to complete
15 16 17 |
# File 'lib/sequel/plugins/elasticsearch/result.rb', line 15 def took @took end |
#total ⇒ Object (readonly)
The total number of documents in the Elasticsearch result
13 14 15 |
# File 'lib/sequel/plugins/elasticsearch/result.rb', line 13 def total @total end |
Instance Method Details
#each ⇒ Object
Each implementation for the Enumerable. Yield each element in the result[‘hits’] array.
39 40 41 42 43 |
# File 'lib/sequel/plugins/elasticsearch/result.rb', line 39 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.
51 52 53 |
# File 'lib/sequel/plugins/elasticsearch/result.rb', line 51 def respond_to_missing?(meth, include_private = false) result['hits']['hits'].respond_to?(meth, include_private) || super end |