Class: Esse::IndexSetting

Inherits:
Object
  • Object
show all
Defined in:
lib/esse/index_setting.rb

Overview

Constant Summary collapse

INDEX_SIMPLIFIED_SETTINGS =

Top-level keys that Elasticsearch/OpenSearch accept either flat or nested under ‘index:`. We always promote them to the nested form so that values from different sources (cluster globals vs per-index template) merge predictably regardless of which form each side was authored in.

%i[
  number_of_shards
  number_of_replicas
  refresh_interval
  mapping
].freeze

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(body: {}, paths: [], globals: nil) ⇒ IndexSetting

Returns a new instance of IndexSetting.

Parameters:

  • options (Hash)


21
22
23
24
25
# File 'lib/esse/index_setting.rb', line 21

def initialize(body: {}, paths: [], globals: nil)
  @globals = globals || -> { {} }
  @paths = Array(paths)
  @settings = body
end

Class Method Details

.normalize(hash) ⇒ Object

Normalize a settings hash by:

* symbolizing keys
* stripping the `:settings` root if present
* exploding dotted keys ('index.number_of_replicas' -> { index: { number_of_replicas: ... } })
* promoting simplified flat keys (number_of_shards, etc.) into the
  nested `:index` form, preserving any value already present under
  `:index` (we never overwrite an explicit nested setting with a
  flat value from the same source).


66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/esse/index_setting.rb', line 66

def self.normalize(hash)
  values = HashUtils.deep_transform_keys(hash || {}, &:to_sym)
  values = values[Esse::SETTING_ROOT_KEY] if values.key?(Esse::SETTING_ROOT_KEY)
  values = HashUtils.explode_keys(values)
  INDEX_SIMPLIFIED_SETTINGS.each do |key|
    next unless values.key?(key)
    value = values.delete(key)
    next if value.nil?

    values[:index] ||= {}
    values[:index][key] = value unless values[:index].key?(key)
  end
  values
end

Instance Method Details

#bodyObject



43
44
45
46
47
# File 'lib/esse/index_setting.rb', line 43

def body
  global = HashUtils.deep_transform_keys(@globals.call, &:to_sym)
  local = HashUtils.deep_transform_keys(to_h, &:to_sym)
  HashUtils.deep_merge(global, local)
end

#globalsObject

Returns the raw (unsymbolized) global settings as supplied by the globals proc. Public so that callers like Esse::Index.settings_hash can normalize it independently before merging it with the local template — preventing a flat global value from clobbering a nested local value once both are merged.



54
55
56
# File 'lib/esse/index_setting.rb', line 54

def globals
  @globals.call || {}
end

#to_hObject

This method will be overwrited when passing a block during the settings defination on index class.

Example:

class UserIndex < Esse::Index
  settings do
    # do something to load settings..
  end
end


37
38
39
40
41
# File 'lib/esse/index_setting.rb', line 37

def to_h
  return @settings unless @settings.empty?

  from_template || @settings
end