Skip to content

Commit

Permalink
Add versioned mapping handling for index creation as well
Browse files Browse the repository at this point in the history
  • Loading branch information
ywangd committed Mar 30, 2021
1 parent f4ff07d commit 16baedc
Showing 1 changed file with 14 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -348,7 +348,7 @@ public void prepareIndexIfNeededThenExecute(final Consumer<Exception> consumer,
// system indices, we nonetheless specify them here so that the values from `this.systemIndexDescriptor` are used.
CreateIndexRequest request = new CreateIndexRequest(indexState.concreteIndexName)
.origin(systemIndexDescriptor.getOrigin())
.mapping(MapperService.SINGLE_MAPPING_NAME, systemIndexDescriptor.getMappings(), XContentType.JSON)
.mapping(MapperService.SINGLE_MAPPING_NAME, getIndexMappings(indexState), XContentType.JSON)
.settings(systemIndexDescriptor.getSettings())
.alias(new Alias(systemIndexDescriptor.getAliasName()))
.waitForActiveShards(ActiveShardCount.ALL);
Expand Down Expand Up @@ -386,16 +386,8 @@ public void onFailure(Exception e) {
indexState.concreteIndexName,
systemIndexDescriptor.getAliasName()
);
final String mappings;
// Flattened field is available since 7.3.0 and it is used by API key metadata in 7.13.0.
// Therefore if the minimum node version is before 7.3.0, we fallback to use the mapping from 7.12.0.
if (indexState.minimumNodeVersion.before(Version.V_7_3_0)) {
mappings = Strings.toString(Security.getIndexMappings(Version.V_7_12_0));
} else {
mappings = systemIndexDescriptor.getMappings();
}
PutMappingRequest request = new PutMappingRequest(indexState.concreteIndexName).source(
mappings,
getIndexMappings(indexState),
XContentType.JSON
).type(MapperService.SINGLE_MAPPING_NAME).origin(systemIndexDescriptor.getOrigin());
executeAsyncWithOrigin(client.threadPool().getThreadContext(), systemIndexDescriptor.getOrigin(), request,
Expand All @@ -415,6 +407,18 @@ public void onFailure(Exception e) {
}
}

private String getIndexMappings(State indexState) {
final String mappings;
// Flattened field is available since 7.3.0 and it is used by API key metadata in 7.13.0.
// Therefore if the minimum node version is before 7.3.0, we fallback to use the mapping from 7.12.0.
if (indexState.minimumNodeVersion.before(Version.V_7_3_0)) {
mappings = Strings.toString(Security.getIndexMappings(Version.V_7_12_0));
} else {
mappings = systemIndexDescriptor.getMappings();
}
return mappings;
}

/**
* Return true if the state moves from an unhealthy ("RED") index state to a healthy ("non-RED") state.
*/
Expand Down

0 comments on commit 16baedc

Please sign in to comment.