Class: Esse::Cluster

Inherits:
Object
  • Object
show all
Extended by:
Deprecations::Deprecate
Defined in:
lib/esse/cluster.rb,
lib/esse/deprecations/cluster.rb

Constant Summary collapse

ATTRIBUTES =
%i[index_prefix settings mappings client wait_for_status readonly].freeze
WAIT_FOR_STATUSES =
%w[green yellow red].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Deprecations::Deprecate

extended

Constructor Details

#initialize(id:, **options) ⇒ Cluster

Returns a new instance of Cluster.



43
44
45
46
47
48
49
# File 'lib/esse/cluster.rb', line 43

def initialize(id:, **options)
  @id = id.to_sym
  @settings = {}
  @mappings = {}
  @readonly = false
  assign(options)
end

Instance Attribute Details

#idObject (readonly)

Returns the value of attribute id.



41
42
43
# File 'lib/esse/cluster.rb', line 41

def id
  @id
end

#index_prefixObject

The index prefix. For example an index named UsersIndex. With ‘index_prefix = ’app1’‘. Final index/alias is: ’app1_users’



19
20
21
# File 'lib/esse/cluster.rb', line 19

def index_prefix
  @index_prefix
end

#mappingsObject

This global mappings will be applied to all indices



26
27
28
# File 'lib/esse/cluster.rb', line 26

def mappings
  @mappings
end

#readonly=(value) ⇒ Object (writeonly)

Disable all writes from the application to the underlying Elasticsearch instance while keeping the application running and handling search requests.



39
40
41
# File 'lib/esse/cluster.rb', line 39

def readonly=(value)
  @readonly = value
end

#settingsObject

This global settings will be passed through all indices



22
23
24
# File 'lib/esse/cluster.rb', line 22

def settings
  @settings
end

#wait_for_statusObject

if this option set, actions such as creating or deleting index, importing data will wait for the status specified. Extremely useful for tests under heavy indices manipulations. Value can be set to ‘red`, `yellow` or `green`.

Example:

wait_for_status: green


35
36
37
# File 'lib/esse/cluster.rb', line 35

def wait_for_status
  @wait_for_status
end

Instance Method Details

#apiEsse::Transport

Return the proxy object used to perform low level actions on the elasticsearch cluster through the official api client

Returns:



157
158
159
# File 'lib/esse/cluster.rb', line 157

def api
  Esse::Transport.new(self)
end

#assign(hash) ⇒ Object



51
52
53
54
55
56
57
58
59
60
# File 'lib/esse/cluster.rb', line 51

def assign(hash)
  return unless hash.is_a?(Hash)

  hash.each do |key, value|
    method = (ATTRIBUTES & [key.to_s, key.to_sym]).first
    next unless method

    public_send(:"#{method}=", value)
  end
end

#clientObject



62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/esse/cluster.rb', line 62

def client
  @client ||= if defined? Elasticsearch::Client
    Elasticsearch::Client.new
  elsif defined? OpenSearch::Client
    OpenSearch::Client.new
  else
    raise Esse::Error, <<~ERROR
      Elasticsearch::Client or OpenSearch::Client is not defined.
      Please install elasticsearch or opensearch-ruby gem.
    ERROR
  end
end

#client=(es_client) ⇒ Object

Define the elasticsearch client connection

Parameters:

  • es_client (Elasticsearch::Client, OpenSearch::Client, Hash)

    an instance of elasticsearch/api client or an hash with the settings that will be used to initialize the Client



89
90
91
92
93
94
95
96
97
98
99
# File 'lib/esse/cluster.rb', line 89

def client=(es_client)
  @client = if es_client.is_a?(Hash) && defined?(Elasticsearch::Client)
    settings = es_client.each_with_object({}) { |(k, v), r| r[k.to_sym] = v }
    Elasticsearch::Client.new(settings)
  elsif es_client.is_a?(Hash) && defined?(OpenSearch::Client)
    settings = es_client.each_with_object({}) { |(k, v), r| r[k.to_sym] = v }
    OpenSearch::Client.new(settings)
  else
    es_client
  end
end

#engineObject Also known as: warm_up!



141
142
143
# File 'lib/esse/cluster.rb', line 141

def engine
  @engine ||= ClusterEngine.new(**info)
end

#index_mappingsObject



17
18
19
# File 'lib/esse/deprecations/cluster.rb', line 17

def index_mappings
  mappings
end

#index_mappings=(value) ⇒ Object



22
23
24
# File 'lib/esse/deprecations/cluster.rb', line 22

def index_mappings=(value)
  self.mappings = value
end

#index_settingsObject



7
8
9
# File 'lib/esse/deprecations/cluster.rb', line 7

def index_settings
  settings
end

#index_settings=(value) ⇒ Object



12
13
14
# File 'lib/esse/deprecations/cluster.rb', line 12

def index_settings=(value)
  self.settings = value
end

#infoObject



131
132
133
134
135
136
137
138
139
# File 'lib/esse/cluster.rb', line 131

def info
  @info ||= begin
    resp = client.info
    {
      distribution: (resp.dig('version', 'distribution') || 'elasticsearch'),
      version: resp.dig('version', 'number'),
    }
  end
end

#inspectObject



101
102
103
104
105
106
107
108
109
# File 'lib/esse/cluster.rb', line 101

def inspect
  attrs = ([:id] + ATTRIBUTES - [:client, :readonly]).map do |method|
    value = public_send(method)
    format('%<k>s=%<v>p', k: method, v: value) if value
  end.compact
  attrs << 'readonly=true' if readonly?
  attrs << format('client=%p', @client)
  format('#<Esse::Cluster %<attrs>s>', attrs: attrs.join(' '))
end

#may_update_type!(hash, key: :type) ⇒ void

This method returns an undefined value.



123
124
125
126
127
128
129
# File 'lib/esse/cluster.rb', line 123

def may_update_type!(hash, key: :type)
  if (single_type = engine.mapping_default_type)
    hash[key] = single_type
    return
  end
  hash.delete(key) if engine.mapping_single_type?
end

#readonly?Boolean

Return true if the cluster is readonly

Returns:

  • (Boolean)

    Return true if the cluster is readonly



76
77
78
# File 'lib/esse/cluster.rb', line 76

def readonly?
  !!@readonly
end

#search(*indices, **kwargs, &block) ⇒ Esse::Search::Query

Build a search query for the given indices

Parameters:

  • indices (Array<Esse::Index, String>)

    The indices class or the index name

Returns:



150
151
152
# File 'lib/esse/cluster.rb', line 150

def search(*indices, **kwargs, &block)
  Esse::Search::Query.new(api, *indices, **kwargs, &block)
end

#throw_error_when_readonly!void

This method returns an undefined value.

Raises:



82
83
84
# File 'lib/esse/cluster.rb', line 82

def throw_error_when_readonly!
  raise Esse::Transport::ReadonlyClusterError if readonly?
end

#wait_for_status!(status: nil, **kwargs) ⇒ Object

Wait until cluster is in a specific state

Parameters:

  • [String] (Hash)

    a customizable set of options



115
116
117
118
119
120
# File 'lib/esse/cluster.rb', line 115

def wait_for_status!(status: nil, **kwargs)
  status ||= wait_for_status
  return unless WAIT_FOR_STATUSES.include?(status.to_s)

  api.health(**kwargs, wait_for_status: status.to_s)
end