Class: Esse::Cluster
- Inherits:
-
Object
- Object
- Esse::Cluster
- 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
-
#id ⇒ Object
readonly
Returns the value of attribute id.
-
#index_prefix ⇒ Object
The index prefix.
-
#mappings ⇒ Object
This global mappings will be applied to all indices.
-
#readonly ⇒ Object
writeonly
Disable all writes from the application to the underlying Elasticsearch instance while keeping the application running and handling search requests.
-
#settings ⇒ Object
This global settings will be passed through all indices.
-
#wait_for_status ⇒ Object
if this option set, actions such as creating or deleting index, importing data will wait for the status specified.
Instance Method Summary collapse
-
#api ⇒ Esse::Transport
Return the proxy object used to perform low level actions on the elasticsearch cluster through the official api client.
- #assign(hash) ⇒ Object
- #client ⇒ Object
-
#client=(es_client) ⇒ Object
Define the elasticsearch client connection.
- #engine ⇒ Object (also: #warm_up!)
- #index_mappings ⇒ Object
- #index_mappings=(value) ⇒ Object
- #index_settings ⇒ Object
- #index_settings=(value) ⇒ Object
- #info ⇒ Object
-
#initialize(id:, **options) ⇒ Cluster
constructor
A new instance of Cluster.
- #inspect ⇒ Object
- #may_update_type!(hash, key: :type) ⇒ void
-
#readonly? ⇒ Boolean
Return true if the cluster is readonly.
-
#search(*indices, **kwargs, &block) ⇒ Esse::Search::Query
Build a search query for the given indices.
- #throw_error_when_readonly! ⇒ void
-
#wait_for_status!(status: nil, **kwargs) ⇒ Object
Wait until cluster is in a specific state.
Methods included from Deprecations::Deprecate
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:, **) @id = id.to_sym @settings = {} @mappings = {} @readonly = false assign() end |
Instance Attribute Details
#id ⇒ Object (readonly)
Returns the value of attribute id.
41 42 43 |
# File 'lib/esse/cluster.rb', line 41 def id @id end |
#index_prefix ⇒ Object
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 |
#mappings ⇒ Object
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 |
#settings ⇒ Object
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_status ⇒ Object
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
#api ⇒ Esse::Transport
Return the proxy object used to perform low level actions on the elasticsearch cluster through the official api client
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 |
#client ⇒ Object
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
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 |
#engine ⇒ Object Also known as: warm_up!
141 142 143 |
# File 'lib/esse/cluster.rb', line 141 def engine @engine ||= ClusterEngine.new(**info) end |
#index_mappings ⇒ Object
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_settings ⇒ Object
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 |
#info ⇒ Object
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 |
#inspect ⇒ Object
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
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
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.
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
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 |