class Sequel::Plugins::Elasticsearch::Result

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

Attributes

model[R]

The model class associated with this result

result[R]

The original result returned from the Elasticsearch client

scroll_id[R]

The scroll id, if set, from the result

timed_out[R]

If the Elasticsearch call timed out or note

took[R]

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

total[R]

The total number of documents in the Elasticsearch result

Public Class Methods

new(result, model = nil) click to toggle source

Initialize the Result

  • result The result returns from the Elasticsearch 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

all() click to toggle source

Send back the complete result set

# File lib/sequel/plugins/elasticsearch/result.rb, line 48
def all
  result['hits']['hits']
end
each() { |h| ... } click to toggle source

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
method_missing(meth, *args, &block) click to toggle source

Send all undefined methods to the +result['hits']+ array.

Calls superclass method
# 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
respond_to_missing?(meth, include_private = false) click to toggle source

Send all undefined methods to the +result['hits']+ array.

Calls superclass method
# 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(hit) click to toggle source

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