Module: Sequel::Plugins::Elasticsearch::ClassMethods

Defined in:
lib/sequel/plugins/elasticsearch.rb

Overview

The class methods that will be added to the Sequel::Model

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#elasticsearch_indexObject

The Elasticsearch index to which the documents will be written.



38
39
40
# File 'lib/sequel/plugins/elasticsearch.rb', line 38

def elasticsearch_index
  @elasticsearch_index
end

#elasticsearch_optsObject

The extra options that will be passed to the Elasticsearch client.



36
37
38
# File 'lib/sequel/plugins/elasticsearch.rb', line 36

def elasticsearch_opts
  @elasticsearch_opts
end

#elasticsearch_typeObject

The Elasticsearch type to which the documents will be written.



40
41
42
# File 'lib/sequel/plugins/elasticsearch.rb', line 40

def elasticsearch_type
  @elasticsearch_type
end

Instance Method Details

#call_esObject

Wrapper method in which error handling is done for Elasticsearch calls.



72
73
74
75
76
77
78
79
# File 'lib/sequel/plugins/elasticsearch.rb', line 72

def call_es
  yield
rescue ::Elasticsearch::Transport::Transport::Errors::NotFound,
       ::Elasticsearch::Transport::Transport::Error,
       Faraday::ConnectionFailed => e
  db.loggers.first.warn e if db.loggers.count.positive?
  nil
end

#es(query = '', opts = {}) ⇒ Object

Execute a search or a scroll on the Model’s Elasticsearch index. This method is “safe” in that it will catch the more common Errors.



67
68
69
# File 'lib/sequel/plugins/elasticsearch.rb', line 67

def es(query = '', opts = {})
  call_es { query.is_a?(Result) ? scroll!(query, opts) : es!(query, opts) }
end

#es!(query = '', opts = {}) ⇒ Object

Execute a search on the Model’s Elasticsearch index without catching Errors.



48
49
50
51
52
53
54
55
# File 'lib/sequel/plugins/elasticsearch.rb', line 48

def es!(query = '', opts = {})
  opts = {
    index: elasticsearch_index,
    type: elasticsearch_type
  }.merge(opts)
  query.is_a?(String) ? opts[:q] = query : opts[:body] = query
  Result.new es_client.search(opts), self
end

#es_clientObject

Return the Elasticsearch client used to communicate with the cluster.



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

def es_client
  @es_client = ::Elasticsearch::Client.new elasticsearch_opts
end

#import!Object

Import the whole dataset into Elasticsearch



82
83
# File 'lib/sequel/plugins/elasticsearch.rb', line 82

def import!
end

#scroll!(scroll_id, duration) ⇒ Object

Fetch the next page in a scroll without catching Errors.



58
59
60
61
62
63
# File 'lib/sequel/plugins/elasticsearch.rb', line 58

def scroll!(scroll_id, duration)
  scroll_id = scroll_id.scroll_id if scroll_id.is_a? Result
  return nil unless scroll_id

  Result.new es_client.scroll(scroll_id: scroll_id, scroll: duration), self
end