Skip to content

Commit 2b9463c

Browse files
Add customize_config API for customizing index datastore configuration (#1366)
Implements the `i.customize_config` proposal from #1364 (#1364 (comment)). ## Schema definition API ```ruby schema.object_type "Campaign" do |t| # ... t.index "campaigns" do |i| i.rollover :monthly, "createdAt" i.customize_config do |config| # Index aliases config["aliases"] = { "campaigns_read" => {}, "campaigns_active" => {"filter" => {"term" => {"status" => "ACTIVE"}}} } # Field alias: lets direct datastore queries reference `created` as an alias of `createdAt` config["mappings"]["properties"]["created"] = {"type" => "alias", "path" => "createdAt"} end end end ``` Semantics match the proposal: - The block mutates the yielded config hash; the return value is ignored. ElasticGraph yields a defensive deep copy so mutations can't corrupt its internal structures (parts of `mappings` are shared across indices). - The yielded hash is "the config every concrete index for this index definition gets" — the template body for rollover index defs, the index config otherwise. - The result is baked into `datastore_config.yaml`, so it's diffable in PRs and `elasticgraph-admin` treats it identically to built-in config. - Multiple `customize_config` blocks compose in registration order. - Nested customizations — like the field alias above — work for both indices and index templates, since the block mutates the full config in place. On the admin side they ride the existing mappings reconciliation: included at index/template creation, added to already-existing indices (including pre-existing rollover indices) via the mapping update path, and idempotent on reconvergence. - The YARD docs carry the "ElasticGraph passes this through and makes no claims about safety/correctness — use with care!" caveat. ## Admin changes so customizations are fully respected The three changes outlined in the proposal: 1. **Absent means "leave alone".** On template updates, desired aliases are merged over the template's current ones instead of replacing them — the same policy admin already applies to no-longer-desired mapping fields. This fixes the out-of-band alias clobbering regardless of whether anyone uses the new API. 2. **Template update trigger.** `ForIndexTemplate#configure!` now diffs the full desired vs. current template body (not just mappings/settings), so a customization-only change still reaches the datastore. To keep this comparison stable, `IndexConfigNormalizer` now treats an empty `aliases` hash the same as an absent one (the datastore doesn't consistently echo the key back). 3. **Reconciliation of existing concrete indices.** `ForIndex#configure!` gained an `_aliases` step (after mappings, since alias filters can reference newly-added fields): it adds/updates desired aliases and never removes undeclared ones. A newly declared alias now lands on pre-existing rollover indices rather than only on ones created afterward. Both datastore clients gained an `update_index_aliases` method (plus dry-run decorator no-op, RBS interface entry, and parallel-spec-runner support). ## Testing - Unit specs for the schema definition API (application to index config vs. template body, block composition, defensive copy, nested customizations, no-block error). - Shared admin integration specs (run for both `ForIndex` and `ForIndexTemplate`): create-with-aliases + idempotent reconvergence, adding/updating aliases on existing indices and templates, and leaving undeclared (out-of-band) aliases alone. A template-specific spec verifies aliases are stamped onto concrete rollover indices at creation and reconciled onto pre-existing ones. - A shared integration spec covers field aliases end-to-end for both configurators: declared via `customize_config`, applied at creation, reconciled onto an already-existing index/template via the mapping update path, and idempotent on re-run. - `datastore_config.yaml` emission is unchanged for schemas that don't use the new API (repo artifacts show no diff after `schema_artifacts:dump`). - Full suite green (5315 examples, 0 failures), `script/type_check` clean, 100% doc coverage maintained, `site:doctest` passes. 🤖 Generated with [Claude Code](https://claude.com/claude-code) --------- Co-authored-by: Myron Marston <myron.marston@gmail.com>
1 parent cde8809 commit 2b9463c

25 files changed

Lines changed: 596 additions & 47 deletions

File tree

‎config/schema/artifacts/datastore_config.yaml‎

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,12 @@ index_templates:
66
index_patterns:
77
- teams_rollover__*
88
template:
9-
aliases: {}
9+
aliases:
10+
teams_all: {}
11+
teams_nfl:
12+
filter:
13+
term:
14+
league: NFL
1015
mappings:
1116
dynamic: strict
1217
properties:
@@ -1268,6 +1273,9 @@ index_templates:
12681273
__typename:
12691274
type: constant_keyword
12701275
value: Team
1276+
formed:
1277+
type: alias
1278+
path: formed_on
12711279
_routing:
12721280
required: true
12731281
_size:

‎config/schema/artifacts_with_apollo/datastore_config.yaml‎

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,12 @@ index_templates:
66
index_patterns:
77
- teams_rollover__*
88
template:
9-
aliases: {}
9+
aliases:
10+
teams_all: {}
11+
teams_nfl:
12+
filter:
13+
term:
14+
league: NFL
1015
mappings:
1116
dynamic: strict
1217
properties:
@@ -1268,6 +1273,9 @@ index_templates:
12681273
__typename:
12691274
type: constant_keyword
12701275
value: Team
1276+
formed:
1277+
type: alias
1278+
path: formed_on
12711279
_routing:
12721280
required: true
12731281
_size:

‎config/schema/teams.rb‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,15 @@
100100
i.route_with "league"
101101
i.rollover :yearly, "formed_on"
102102
i.has_had_multiple_sources!
103+
# Exercises `customize_config` with both index aliases (plain and filtered) and a field alias.
104+
i.customize_config do |config|
105+
config["aliases"] = {
106+
"teams_all" => {},
107+
"teams_nfl" => {"filter" => {"term" => {"league" => "NFL"}}}
108+
}
109+
110+
config["mappings"]["properties"]["formed"] = {"type" => "alias", "path" => "formed_on"}
111+
end
103112
end
104113
end
105114

‎elasticgraph-admin/lib/elastic_graph/admin/datastore_client_dry_run_decorator.rb‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,8 @@ def put_index_mapping(*) = nil
6565

6666
def put_index_settings(*) = nil
6767

68+
def update_index_aliases(*) = nil
69+
6870
# Document APIs
6971
def_delegators :@wrapped_client, :get, :search, :msearch
7072

‎elasticgraph-admin/lib/elastic_graph/admin/index_definition_configurator/for_index.rb‎

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,14 @@ def initialize(datastore_client, index, env_agnostic_index_config, output)
3232
# exposed by the `IndexDefinition` object. Based on the configuration of the passed index
3333
# and the state of the index in the datastore, does one of the following:
3434
#
35-
# - If the index did not already exist: creates the index with the desired mappings and settings.
35+
# - If the index did not already exist: creates the index with the desired aliases, mappings and settings.
3636
# - If the desired mapping has fewer fields than what is in the index: leaves the existing fields
3737
# alone, because the datastore provides no way to remove fields from a mapping.
3838
# - If the settings have desired changes: updates the settings, restoring any setting that
3939
# no longer has a desired value to its default.
4040
# - If the mapping has desired changes: updates the mappings.
41+
# - If the aliases have desired changes: adds or updates the desired aliases, leaving aliases
42+
# that were not declared (e.g. ones created outside of ElasticGraph) alone.
4143
#
4244
# Note that any of the writes to the index may fail. There are many things that cannot
4345
# be changed on an existing index (such as static settings, field mapping types, etc). We do not attempt
@@ -57,6 +59,10 @@ def configure!
5759
update_settings if settings_updates.any?
5860

5961
update_mapping if has_mapping_updates?
62+
63+
# Update aliases after mappings, since an alias with a `filter` can reference fields that are
64+
# only available once the mapping updates have been applied.
65+
update_aliases if alias_updates.any?
6066
end
6167

6268
def validate
@@ -86,6 +92,17 @@ def update_settings
8692
report_action "Updated settings for index `#{@index.name}`:\n#{settings_diff}"
8793
end
8894

95+
def update_aliases
96+
# An `add` action is an upsert: when the named alias already exists on the index, the
97+
# datastore replaces its definition with the one provided here (rather than merging).
98+
actions = alias_updates.map do |name, definition|
99+
{"add" => definition.merge({"index" => @index.name, "alias" => name})}
100+
end
101+
102+
@datastore_client.update_index_aliases(body: {"actions" => actions})
103+
report_action "Updated aliases for index `#{@index.name}`:\n#{alias_diff}"
104+
end
105+
89106
def cannot_modify_mapping_field_type_error
90107
"The datastore does not support modifying the type of a field from an existing index definition. " \
91108
"You are attempting to update type of fields (#{mapping_type_changes.inspect}) from the #{@index.name} index definition."
@@ -118,6 +135,22 @@ def settings_updates
118135
end
119136
end
120137

138+
# Note: aliases that exist on the index but are not desired are intentionally left alone rather
139+
# than removed. Undeclared aliases may have been created outside of ElasticGraph (which does
140+
# nothing with aliases itself), so their absence from our desired configuration just means
141+
# ElasticGraph doesn't manage them.
142+
def alias_updates
143+
@alias_updates ||= desired_aliases.reject { |name, definition| current_aliases[name] == definition }
144+
end
145+
146+
def desired_aliases
147+
desired_config["aliases"] || {}
148+
end
149+
150+
def current_aliases
151+
current_config["aliases"] || {}
152+
end
153+
121154
def desired_mapping_for_update
122155
@desired_mapping_for_update ||= MappingUpdate.build_mapping_update(desired: desired_mapping, current: current_mapping)
123156
end
@@ -169,6 +202,10 @@ def settings_diff
169202
@settings_diff ||= Indexer::HashDiffer.diff(current_settings, desired_settings) || "(no diff)"
170203
end
171204

205+
def alias_diff
206+
@alias_diff ||= Indexer::HashDiffer.diff(current_aliases, current_aliases.merge(alias_updates)) || "(no diff)"
207+
end
208+
172209
def report_action(message)
173210
@reporter.report_action(message)
174211
end

‎elasticgraph-admin/lib/elastic_graph/admin/index_definition_configurator/for_index_template.rb‎

Lines changed: 39 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,14 @@ def initialize(datastore_client, index_template, env_agnostic_index_config_paren
3636
# exposed by the `IndexDefinition` object. Based on the configuration of the passed index
3737
# and the state of the index in the datastore, does one of the following:
3838
#
39-
# - If the index did not already exist: creates the index with the desired mappings and settings.
39+
# - If the index template did not already exist: creates the index template with the desired
40+
# aliases, mappings and settings.
4041
# - If the desired mapping has fewer fields than what is in the index template: leaves the existing
4142
# fields alone (see `put_index_template` for why).
42-
# - If the settings have desired changes: updates the settings, restoring any setting that
43-
# no longer has a desired value to its default.
44-
# - If the mapping has desired changes: updates the mappings.
43+
# - If the aliases have desired changes: adds or updates the desired aliases, leaving aliases
44+
# that were not declared (e.g. ones created outside of ElasticGraph) alone.
45+
# - If any other part of the template configuration (settings, mappings, etc.) has desired
46+
# changes: updates the template.
4547
#
4648
# Note that any of the writes to the index may fail. There are many things that cannot
4749
# be changed on an existing index (such as static settings, field mapping types, etc). We do not attempt
@@ -50,8 +52,10 @@ def initialize(datastore_client, index_template, env_agnostic_index_config_paren
5052
def configure!
5153
related_index_configurators.each(&:configure!)
5254

53-
# there is no partial update for index template config and the same API both creates and updates it
54-
put_index_template if has_mapping_updates? || settings_updates.any?
55+
# There is no partial update for index template config and the same API both creates and updates it.
56+
# Note that we diff the full template config (rather than just mappings and settings) so that a
57+
# change to any part of it--such as `customize_config` customizations--triggers an update.
58+
put_index_template if has_config_updates?
5559
end
5660

5761
def validate
@@ -104,35 +108,46 @@ def mapping_type_changes
104108
end
105109
end
106110

107-
def has_mapping_updates?
108-
current_mapping != desired_mapping_for_update
109-
end
110-
111-
def settings_updates
112-
@settings_updates ||= begin
113-
# Updating a setting to null will cause the datastore to restore the default value of the setting.
114-
restore_to_defaults = (current_settings.keys - desired_settings.keys).to_h { |key| [key, nil] }
115-
desired_settings.select { |key, value| current_settings[key] != value }.merge(restore_to_defaults)
116-
end
111+
def has_config_updates?
112+
desired_config_parent_for_update.fetch("template") != (current_config_parent["template"] || {})
117113
end
118114

119115
def desired_mapping_for_update
120116
@desired_mapping_for_update ||= MappingUpdate.build_mapping_update(desired: desired_mapping, current: current_mapping)
121117
end
122118

123119
def desired_config_parent_for_update
124-
@desired_config_parent_for_update ||= Support::HashUtil.deep_merge(
125-
desired_config_parent,
126-
{"template" => {"mappings" => desired_mapping_for_update}}
127-
)
120+
@desired_config_parent_for_update ||= begin
121+
template = DatastoreCore::IndexConfigNormalizer.normalize(
122+
desired_config_parent.fetch("template").merge({
123+
"mappings" => desired_mapping_for_update,
124+
"aliases" => aliases_for_update
125+
})
126+
)
127+
128+
desired_config_parent.merge({"template" => template})
129+
end
128130
end
129131

130-
def desired_mapping
131-
desired_config_parent.fetch("template").fetch("mappings")
132+
# Aliases that exist on the current template but are not desired are preserved rather than removed
133+
# (the same policy `MappingUpdate` applies to no-longer-desired mapping fields). Undeclared aliases
134+
# may have been created outside of ElasticGraph (which does nothing with aliases itself), and since
135+
# `put_index_template` replaces the entire template, omitting them here would silently drop them
136+
# from all future rollover indices.
137+
def aliases_for_update
138+
current_aliases.merge(desired_aliases)
139+
end
140+
141+
def desired_aliases
142+
desired_config_parent.fetch("template")["aliases"] || {}
132143
end
133144

134-
def desired_settings
135-
@desired_settings ||= desired_config_parent.fetch("template").fetch("settings")
145+
def current_aliases
146+
current_config_parent.dig("template", "aliases") || {}
147+
end
148+
149+
def desired_mapping
150+
desired_config_parent.fetch("template").fetch("mappings")
136151
end
137152

138153
def desired_config_parent
@@ -159,10 +174,6 @@ def current_mapping
159174
current_config_parent.dig("template", "mappings") || {}
160175
end
161176

162-
def current_settings
163-
@current_settings ||= current_config_parent.dig("template", "settings")
164-
end
165-
166177
def current_config_parent
167178
@current_config_parent ||= begin
168179
config = @datastore_client.get_index_template(@index_template.name)

‎elasticgraph-admin/sig/elastic_graph/admin/index_definition_configurator/for_index.rbs‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ module ElasticGraph
2525
def create_new_index: () -> void
2626
def update_mapping: () -> void
2727
def update_settings: () -> void
28+
def update_aliases: () -> void
2829
def cannot_modify_mapping_field_type_error: () -> ::String
2930
def index_exists?: () -> bool
3031

@@ -36,6 +37,13 @@ module ElasticGraph
3637
@settings_updates: DatastoreCore::indexSettingsHash?
3738
def settings_updates: () -> DatastoreCore::indexSettingsHash
3839

40+
@alias_updates: DatastoreCore::indexAliasesHash?
41+
def alias_updates: () -> DatastoreCore::indexAliasesHash
42+
43+
def desired_aliases: () -> DatastoreCore::indexAliasesHash
44+
45+
def current_aliases: () -> DatastoreCore::indexAliasesHash
46+
3947
@desired_mapping_for_update: DatastoreCore::indexMappingHash?
4048
def desired_mapping_for_update: () -> DatastoreCore::indexMappingHash
4149

@@ -61,6 +69,9 @@ module ElasticGraph
6169
@settings_diff: ::String?
6270
def settings_diff: () -> ::String
6371

72+
@alias_diff: ::String?
73+
def alias_diff: () -> ::String
74+
6475
def report_action: (::String) -> void
6576
end
6677
end

‎elasticgraph-admin/sig/elastic_graph/admin/index_definition_configurator/for_index_template.rbs‎

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -32,30 +32,27 @@ module ElasticGraph
3232
@mapping_type_changes: ::Array[::String]?
3333
def mapping_type_changes: () -> ::Array[::String]
3434

35-
def has_mapping_updates?: () -> bool
36-
37-
@settings_updates: DatastoreCore::indexSettingsHash?
38-
def settings_updates: () -> DatastoreCore::indexSettingsHash
35+
def has_config_updates?: () -> bool
3936

4037
@desired_mapping_for_update: DatastoreCore::indexMappingHash?
4138
def desired_mapping_for_update: () -> DatastoreCore::indexMappingHash
4239

4340
@desired_config_parent_for_update: ::Hash[::String, untyped]?
4441
def desired_config_parent_for_update: () -> ::Hash[::String, untyped]
4542

46-
def desired_mapping: () -> DatastoreCore::indexMappingHash
43+
def aliases_for_update: () -> DatastoreCore::indexAliasesHash
44+
45+
def desired_aliases: () -> DatastoreCore::indexAliasesHash
4746

48-
@desired_settings: DatastoreCore::indexSettingsHash?
49-
def desired_settings: () -> DatastoreCore::indexSettingsHash
47+
def current_aliases: () -> DatastoreCore::indexAliasesHash
48+
49+
def desired_mapping: () -> DatastoreCore::indexMappingHash
5050

5151
@desired_config_parent: ::Hash[::String, untyped]
5252
def desired_config_parent: () -> ::Hash[::String, untyped]
5353

5454
def current_mapping: () -> DatastoreCore::indexMappingHash
5555

56-
@current_settings: DatastoreCore::indexSettingsHash?
57-
def current_settings: () -> DatastoreCore::indexSettingsHash
58-
5956
@current_config_parent: ::Hash[::String, untyped]
6057
def current_config_parent: () -> ::Hash[::String, untyped]
6158

‎elasticgraph-admin/spec/integration/elastic_graph/admin/index_definition_configurator/for_index_spec.rb‎

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,31 @@ module IndexDefinitionConfigurator
2525
.and log_warning(/Can't update non dynamic setting/)
2626
end
2727

28+
it "applies mapping updates before alias updates so that an alias filter can reference a newly declared field" do
29+
nested_alias = "#{unique_index_name}_nested"
30+
# The datastore rejects a `nested` alias filter when the path is not yet in the index mapping.
31+
nested_filter = {"nested" => {"path" => "nested_options", "query" => {"term" => {"nested_options.size" => "large"}}}}
32+
33+
configure_index_definition(schema_def)
34+
35+
expect {
36+
configure_index_definition(schema_def(
37+
configure_widget: ->(t) {
38+
t.field "nested_options", "[WidgetOptions!]!" do |f|
39+
f.mapping type: "nested"
40+
end
41+
},
42+
configure_index: ->(index) {
43+
index.customize_config do |config|
44+
config["aliases"] = {nested_alias => {"filter" => nested_filter}}
45+
end
46+
}
47+
))
48+
}.to change { aliases_of(unique_index_name) }
49+
.from({})
50+
.to({nested_alias => {"filter" => nested_filter}})
51+
end
52+
2853
it "handles empty indexed types" do
2954
schema = schema_def(define_no_widget_fields: true)
3055

@@ -39,6 +64,10 @@ def make_datastore_calls_to_configure_index_def(index_name, subresource = nil)
3964
make_datastore_write_calls("main", "PUT #{put_index_definition_url(index_name, subresource)}")
4065
end
4166

67+
def make_datastore_calls_to_update_aliases(_index_name)
68+
make_datastore_write_calls("main", "POST /_aliases")
69+
end
70+
4271
def fetch_artifact_configuration(schema_artifacts, index_def_name)
4372
schema_artifacts.indices.fetch(index_def_name)
4473
end

0 commit comments

Comments
 (0)