Skip to content

Commit 28dde91

Browse files
Add support for specifying reindexing script for system index migration (#119001)
Adds support for setting a reindexing script for system index migration during /_migration/system_features task. Script is set using SystemIndexDescriptor.Builder.setMigrationScript method.
1 parent b1fb31e commit 28dde91

File tree

6 files changed

+54
-1
lines changed

6 files changed

+54
-1
lines changed

docs/changelog/119001.yaml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
pr: 119001
2+
summary: Add support for specifying reindexing script for system index migration
3+
area: Infra/Core
4+
type: enhancement
5+
issues: []

server/src/internalClusterTest/java/org/elasticsearch/indices/TestSystemIndexDescriptor.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ public class TestSystemIndexDescriptor extends SystemIndexDescriptor {
5151
INDEX_NAME,
5252
0,
5353
"stack",
54+
null,
5455
Type.INTERNAL_MANAGED,
5556
List.of(),
5657
List.of(),
@@ -70,6 +71,7 @@ public class TestSystemIndexDescriptor extends SystemIndexDescriptor {
7071
name,
7172
0,
7273
"stack",
74+
null,
7375
Type.INTERNAL_MANAGED,
7476
List.of(),
7577
List.of(),

server/src/main/java/org/elasticsearch/action/admin/cluster/migration/TransportGetFeatureUpgradeStatusAction.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ public class TransportGetFeatureUpgradeStatusAction extends TransportMasterNodeA
5353
GetFeatureUpgradeStatusResponse> {
5454

5555
/**
56-
* Once all feature migrations for 8.x -> 9.x have been tested, we can bump this to Version.V_8_0_0
56+
* Once all feature migrations for 9.x -> 10.x have been tested, we can bump this to Version.V_9_0_0
5757
*/
5858
public static final Version NO_UPGRADE_REQUIRED_VERSION = Version.V_8_0_0;
5959
public static final IndexVersion NO_UPGRADE_REQUIRED_INDEX_VERSION = IndexVersions.V_8_0_0;

server/src/main/java/org/elasticsearch/indices/SystemIndexDescriptor.java

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import org.apache.lucene.util.automaton.CharacterRunAutomaton;
1414
import org.apache.lucene.util.automaton.Operations;
1515
import org.apache.lucene.util.automaton.RegExp;
16+
import org.elasticsearch.action.admin.cluster.migration.TransportGetFeatureUpgradeStatusAction;
1617
import org.elasticsearch.action.admin.indices.create.AutoCreateAction;
1718
import org.elasticsearch.action.admin.indices.create.TransportCreateIndexAction;
1819
import org.elasticsearch.cluster.metadata.IndexMetadata;
@@ -145,6 +146,24 @@ public class SystemIndexDescriptor implements IndexPatternMatcher, Comparable<Sy
145146
/** For internally-managed indices, specifies the origin to use when creating or updating the index */
146147
private final String origin;
147148

149+
/**
150+
* An optional reindexing script to use when migrating an index created
151+
* before {@link TransportGetFeatureUpgradeStatusAction#NO_UPGRADE_REQUIRED_INDEX_VERSION}.
152+
* This script can be used to modify documents before they are added to the new index.
153+
* For example, it can be used to remove deprecated fields from the index.
154+
* <br>
155+
* Note: the script usually should only exist in the versions supporting migration to the next major release -
156+
* specifically, the last (two) minors of the current major.
157+
* It should be created once the last minor branch has diverged from the next major branch (main).
158+
* This ensures the script is available only in the versions where it is needed
159+
* and avoids removing and maintaining it in the next major branch.
160+
* For example: In order to migrate an index created in v7 when upgrading to v9,
161+
* the script should be in the v8 minors supporting upgrade to v9 - 8.18 and 8.19.
162+
* <br>
163+
* See: <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-reindex.html#reindex-scripts">Reindex scripts</a>
164+
*/
165+
private final String migrationScript;
166+
148167
/** Mapping version from the descriptor */
149168
private final MappingsVersion mappingsVersion;
150169

@@ -197,6 +216,7 @@ public class SystemIndexDescriptor implements IndexPatternMatcher, Comparable<Sy
197216
* @param origin the client origin to use when creating this index. Internal system indices must not provide an origin, while external
198217
* system indices must do so.
199218
* @param type The {@link Type} of system index
219+
* @param migrationScript The script to apply when migrating this system index, or null
200220
* @param allowedElasticProductOrigins A list of allowed origin values that should be allowed access in the case of external system
201221
* indices
202222
* @param priorSystemIndexDescriptors A list of system index descriptors that describe the same index in a way that is compatible with
@@ -212,6 +232,7 @@ protected SystemIndexDescriptor(
212232
String aliasName,
213233
int indexFormat,
214234
String origin,
235+
String migrationScript,
215236
Type type,
216237
List<String> allowedElasticProductOrigins,
217238
List<SystemIndexDescriptor> priorSystemIndexDescriptors,
@@ -346,6 +367,7 @@ protected SystemIndexDescriptor(
346367

347368
this.description = description;
348369
this.mappings = mappings;
370+
this.migrationScript = migrationScript;
349371

350372
settings = Objects.isNull(settings) ? Settings.EMPTY : settings;
351373

@@ -557,6 +579,10 @@ public ExecutorNames getThreadPoolNames() {
557579
return this.executorNames;
558580
}
559581

582+
public String getMigrationScript() {
583+
return migrationScript;
584+
}
585+
560586
public static Builder builder() {
561587
return new Builder();
562588
}
@@ -659,6 +685,7 @@ public static class Builder {
659685
private String aliasName = null;
660686
private int indexFormat = 0;
661687
private String origin = null;
688+
private String migrationScript;
662689
private Type type = Type.INTERNAL_MANAGED;
663690
private List<String> allowedElasticProductOrigins = List.of();
664691
private List<SystemIndexDescriptor> priorSystemIndexDescriptors = List.of();
@@ -721,6 +748,11 @@ public Builder setOrigin(String origin) {
721748
return this;
722749
}
723750

751+
public Builder setMigrationScript(String migrationScript) {
752+
this.migrationScript = migrationScript;
753+
return this;
754+
}
755+
724756
public Builder setType(Type type) {
725757
this.type = type;
726758
return this;
@@ -765,6 +797,7 @@ public SystemIndexDescriptor build() {
765797
aliasName,
766798
indexFormat,
767799
origin,
800+
migrationScript,
768801
type,
769802
allowedElasticProductOrigins,
770803
priorSystemIndexDescriptors,

server/src/main/java/org/elasticsearch/upgrades/SystemIndexMigrationInfo.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ class SystemIndexMigrationInfo implements Comparable<SystemIndexMigrationInfo> {
4444
private final Settings settings;
4545
private final String mapping;
4646
private final String origin;
47+
private final String migrationScript;
4748
private final SystemIndices.Feature owningFeature;
4849
private final boolean allowsTemplates;
4950

@@ -57,6 +58,7 @@ private SystemIndexMigrationInfo(
5758
Settings settings,
5859
String mapping,
5960
String origin,
61+
String migrationScript,
6062
SystemIndices.Feature owningFeature,
6163
boolean allowsTemplates
6264
) {
@@ -65,6 +67,7 @@ private SystemIndexMigrationInfo(
6567
this.settings = settings;
6668
this.mapping = mapping;
6769
this.origin = origin;
70+
this.migrationScript = migrationScript;
6871
this.owningFeature = owningFeature;
6972
this.allowsTemplates = allowsTemplates;
7073
}
@@ -118,6 +121,10 @@ String getOrigin() {
118121
return origin;
119122
}
120123

124+
String getMigrationScript() {
125+
return migrationScript;
126+
}
127+
121128
/**
122129
* By default, system indices should not be affected by user defined templates, so this
123130
* method should return false in almost all cases. At the moment certain Kibana indices use
@@ -217,6 +224,7 @@ static SystemIndexMigrationInfo build(
217224
settings,
218225
mapping,
219226
descriptor.getOrigin(),
227+
descriptor.getMigrationScript(),
220228
feature,
221229
descriptor.allowsTemplates()
222230
);

server/src/main/java/org/elasticsearch/upgrades/SystemIndexMigrator.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
import org.elasticsearch.index.reindex.ReindexRequest;
4646
import org.elasticsearch.indices.SystemIndices;
4747
import org.elasticsearch.persistent.AllocatedPersistentTask;
48+
import org.elasticsearch.script.Script;
4849
import org.elasticsearch.tasks.TaskId;
4950

5051
import java.util.LinkedList;
@@ -563,6 +564,10 @@ private void reindex(SystemIndexMigrationInfo migrationInfo, ActionListener<Bulk
563564
reindexRequest.setSourceIndices(migrationInfo.getCurrentIndexName());
564565
reindexRequest.setDestIndex(migrationInfo.getNextIndexName());
565566
reindexRequest.setRefresh(true);
567+
String migrationScript = migrationInfo.getMigrationScript();
568+
if (Strings.isNullOrEmpty(migrationScript) == false) {
569+
reindexRequest.setScript(Script.parse(migrationScript));
570+
}
566571
migrationInfo.createClient(baseClient).execute(ReindexAction.INSTANCE, reindexRequest, listener);
567572
}
568573

0 commit comments

Comments
 (0)