Skip to content

Commit

Permalink
Support field type overrides by creating custom overrides component t…
Browse files Browse the repository at this point in the history
…emplate.
  • Loading branch information
dennisoelkers committed Jul 17, 2023
1 parent 60b1b7d commit a7e0c1d
Showing 1 changed file with 44 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,17 @@
import org.graylog.shaded.opensearch2.org.opensearch.client.GetAliasesResponse;
import org.graylog.shaded.opensearch2.org.opensearch.client.Requests;
import org.graylog.shaded.opensearch2.org.opensearch.client.indices.CloseIndexRequest;
import org.graylog.shaded.opensearch2.org.opensearch.client.indices.ComponentTemplatesExistRequest;
import org.graylog.shaded.opensearch2.org.opensearch.client.indices.ComposableIndexTemplateExistRequest;
import org.graylog.shaded.opensearch2.org.opensearch.client.indices.CreateIndexRequest;
import org.graylog.shaded.opensearch2.org.opensearch.client.indices.DeleteAliasRequest;
import org.graylog.shaded.opensearch2.org.opensearch.client.indices.GetMappingsRequest;
import org.graylog.shaded.opensearch2.org.opensearch.client.indices.GetMappingsResponse;
import org.graylog.shaded.opensearch2.org.opensearch.client.indices.PutComponentTemplateRequest;
import org.graylog.shaded.opensearch2.org.opensearch.client.indices.PutComposableIndexTemplateRequest;
import org.graylog.shaded.opensearch2.org.opensearch.client.indices.PutMappingRequest;
import org.graylog.shaded.opensearch2.org.opensearch.cluster.metadata.AliasMetadata;
import org.graylog.shaded.opensearch2.org.opensearch.cluster.metadata.ComponentTemplate;
import org.graylog.shaded.opensearch2.org.opensearch.cluster.metadata.ComposableIndexTemplate;
import org.graylog.shaded.opensearch2.org.opensearch.common.compress.CompressedXContent;
import org.graylog.shaded.opensearch2.org.opensearch.common.unit.TimeValue;
Expand Down Expand Up @@ -211,12 +214,50 @@ public Map<String, Object> getIndexMetaData(@Nonnull String index) {
return Map.of();
}

@Override
public boolean ensureIndexTemplate(String templateName, Template template) {
private boolean componentTemplateExists(String templateName) {
var request = new ComponentTemplatesExistRequest(templateName);
return client.execute((c, requestOptions) -> c.cluster().existsComponentTemplate(request, requestOptions));
}

private boolean createComponentTemplate(String templateName, Template template) {
var serializedMapping = serialize(template.mappings());
var settings = org.graylog.shaded.opensearch2.org.opensearch.common.settings.Settings.builder().loadFromMap(template.settings()).build();
var osTemplate = new org.graylog.shaded.opensearch2.org.opensearch.cluster.metadata.Template(settings, serializedMapping, null);
var indexTemplate = new ComposableIndexTemplate(template.indexPatterns(), osTemplate, null, template.order(), null, null);
var componentTemplate = new ComponentTemplate(osTemplate, 1L, Map.of());
var request = new PutComponentTemplateRequest()
.name(templateName)
.componentTemplate(componentTemplate);
final AcknowledgedResponse result = client.execute((c, requestOptions) -> c.cluster().putComponentTemplate(request, requestOptions),
"Unable to create component template " + templateName);

return result.isAcknowledged();
}

private boolean ensureComponentTemplate(String templateNameBase, Template template) {
var templateName = templateNameBase + "-base";

var baseResult = createComponentTemplate(templateName, template);
if (!baseResult) {
return false;
}

var overridesComponentName = templateNameBase + "-overrides";
if (componentTemplateExists(overridesComponentName)) {
return true;
}

return createComponentTemplate(overridesComponentName, new Template(List.of(), new Template.Mappings(Map.of()), 1L, new Template.Settings(Map.of())));
}

@Override
public boolean ensureIndexTemplate(String templateName, Template template) {
var componentTemplateBase = templateName + "-base";
var componentTemplateOverrides = templateName + "-overrides";
var componentTemplateExists = ensureComponentTemplate(templateName, template);
if (!componentTemplateExists) {
return false;
}
var indexTemplate = new ComposableIndexTemplate(template.indexPatterns(), null, List.of(componentTemplateBase, componentTemplateOverrides), template.order(), null, null);
var request = new PutComposableIndexTemplateRequest()
.name(templateName)
.indexTemplate(indexTemplate);
Expand Down

0 comments on commit a7e0c1d

Please sign in to comment.