class Sequel::Plugins::Elasticsearch::Result
A wrapper around Elasticsearch
results to make it behave more like a Sequel
Dataset.
Attributes
The model class associated with this result
The original result returned from the Elasticsearch
client
The scroll id, if set, from the result
If the Elasticsearch
call timed out or note
The time, in miliseconds, the Elasticsearch
call took to complete
The total number of documents in the Elasticsearch
result
Public Class Methods
Initialize the Result
-
result
The result returns from theElasticsearch
client /.es
call. -
model
The model class on which the results should be applied.
# 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
Public Instance Methods
Send back the complete result set
# File lib/sequel/plugins/elasticsearch/result.rb, line 48 def all result['hits']['hits'] end
Each implementation for the Enumerable. Yield each element in the +result['hits']+ array.
# 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
Send all undefined methods to the +result['hits']+ array.
# 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
Send all undefined methods to the +result['hits']+ array.
# 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
Private Instance Methods
Convert an Elasticsearch
hit to a Sequel::Model
# File lib/sequel/plugins/elasticsearch/result.rb, line 65 def convert(hit) return hit unless model source = hit['_source'].each_with_object({}) { |(k, v), h| h[k.to_sym] = v } model.call source end