use of org.elasticsearch.cluster.metadata.IndexTemplateMetaData in project crate by crate.
the class AlterTableOperation method updateTemplate.
private CompletableFuture<Long> updateTemplate(Map<String, Object> newMappings, Settings newSettings, TableIdent tableIdent) {
String templateName = PartitionName.templateName(tableIdent.schema(), tableIdent.name());
IndexTemplateMetaData indexTemplateMetaData = clusterService.state().metaData().templates().get(templateName);
if (indexTemplateMetaData == null) {
return CompletableFutures.failedFuture(new RuntimeException("Template for partitioned table is missing"));
}
// merge mappings
Map<String, Object> mapping = mergeTemplateMapping(indexTemplateMetaData, newMappings);
// merge settings
Settings.Builder settingsBuilder = Settings.builder();
settingsBuilder.put(indexTemplateMetaData.settings());
settingsBuilder.put(newSettings);
PutIndexTemplateRequest request = new PutIndexTemplateRequest(templateName).create(false).mapping(Constants.DEFAULT_MAPPING_TYPE, mapping).order(indexTemplateMetaData.order()).settings(settingsBuilder.build()).template(indexTemplateMetaData.template());
for (ObjectObjectCursor<String, AliasMetaData> container : indexTemplateMetaData.aliases()) {
Alias alias = new Alias(container.key);
request.alias(alias);
}
FutureActionListener<PutIndexTemplateResponse, Long> listener = new FutureActionListener<>(LONG_NULL_FUNCTION);
transportActionProvider.transportPutIndexTemplateAction().execute(request, listener);
return listener;
}
use of org.elasticsearch.cluster.metadata.IndexTemplateMetaData in project graylog2-server by Graylog2.
the class IndicesTest method testCreateOverwritesIndexTemplate.
@Test
public void testCreateOverwritesIndexTemplate() throws Exception {
final ObjectMapper mapper = new ObjectMapperProvider().get();
final String templateName = indexSetConfig.indexTemplateName();
final IndicesAdminClient client = this.client.admin().indices();
final ImmutableMap<String, Object> beforeMapping = ImmutableMap.of("_source", ImmutableMap.of("enabled", false), "properties", ImmutableMap.of("message", ImmutableMap.of("type", "string", "index", "not_analyzed")));
assertThat(client.preparePutTemplate(templateName).setTemplate(indexSet.getIndexWildcard()).addMapping(IndexMapping.TYPE_MESSAGE, beforeMapping).get().isAcknowledged()).isTrue();
final GetIndexTemplatesResponse responseBefore = client.prepareGetTemplates(templateName).get();
final List<IndexTemplateMetaData> beforeIndexTemplates = responseBefore.getIndexTemplates();
assertThat(beforeIndexTemplates).hasSize(1);
final ImmutableOpenMap<String, CompressedXContent> beforeMappings = beforeIndexTemplates.get(0).getMappings();
final Map<String, Object> actualMapping = mapper.readValue(beforeMappings.get(IndexMapping.TYPE_MESSAGE).uncompressed(), new TypeReference<Map<String, Object>>() {
});
assertThat(actualMapping.get(IndexMapping.TYPE_MESSAGE)).isEqualTo(beforeMapping);
indices.create("index_template_test", indexSet);
final GetIndexTemplatesResponse responseAfter = client.prepareGetTemplates(templateName).get();
assertThat(responseAfter.getIndexTemplates()).hasSize(1);
final IndexTemplateMetaData templateMetaData = responseAfter.getIndexTemplates().get(0);
assertThat(templateMetaData.getName()).isEqualTo(templateName);
assertThat(templateMetaData.getMappings().keysIt()).containsExactly(IndexMapping.TYPE_MESSAGE);
final Map<String, Object> mapping = mapper.readValue(templateMetaData.getMappings().get(IndexMapping.TYPE_MESSAGE).uncompressed(), new TypeReference<Map<String, Object>>() {
});
final Map<String, Object> expectedTemplate = new IndexMapping().messageTemplate(indexSet.getIndexWildcard(), indexSetConfig.indexAnalyzer());
assertThat(mapping).isEqualTo(expectedTemplate.get("mappings"));
final DeleteIndexTemplateRequest deleteRequest = client.prepareDeleteTemplate(templateName).request();
final DeleteIndexTemplateResponse deleteResponse = client.deleteTemplate(deleteRequest).actionGet();
assertThat(deleteResponse.isAcknowledged()).isTrue();
indices.delete("index_template_test");
}
use of org.elasticsearch.cluster.metadata.IndexTemplateMetaData in project crate by crate.
the class AlterTableTarget method resolve.
public static AlterTableTarget resolve(IndexNameExpressionResolver indexNameResolver, ClusterState state, RelationName table, @Nullable String partition) {
if (partition == null) {
Index[] indices = indexNameResolver.concreteIndices(state, IndicesOptions.lenientExpandOpen(), table.indexNameOrAlias());
String templateName = PartitionName.templateName(table.schema(), table.name());
IndexTemplateMetadata indexTemplateMetadata = state.metadata().getTemplates().get(templateName);
return new AlterTableTarget(table, partition, indices, indexTemplateMetadata);
} else {
Index[] indices = indexNameResolver.concreteIndices(state, IndicesOptions.lenientExpandOpen(), partition);
return new AlterTableTarget(table, partition, indices);
}
}
use of org.elasticsearch.cluster.metadata.IndexTemplateMetaData in project crate by crate.
the class SwapRelationsOperation method addSourceIndicesRenamedToTargetName.
private void addSourceIndicesRenamedToTargetName(ClusterState state, Metadata metadata, Metadata.Builder updatedMetadata, ClusterBlocks.Builder blocksBuilder, RoutingTable.Builder routingBuilder, RelationName source, RelationName target, Consumer<String> onProcessedIndex) {
String sourceTemplateName = PartitionName.templateName(source.schema(), source.name());
IndexTemplateMetadata sourceTemplate = metadata.templates().get(sourceTemplateName);
for (Index sourceIndex : indexNameResolver.concreteIndices(state, IndicesOptions.LENIENT_EXPAND_OPEN, source.indexNameOrAlias())) {
String sourceIndexName = sourceIndex.getName();
IndexMetadata sourceMd = metadata.getIndexSafe(sourceIndex);
IndexMetadata targetMd;
if (sourceTemplate == null) {
targetMd = IndexMetadata.builder(sourceMd).removeAllAliases().index(target.indexNameOrAlias()).build();
onProcessedIndex.accept(target.indexNameOrAlias());
} else {
PartitionName partitionName = PartitionName.fromIndexOrTemplate(sourceIndexName);
String targetIndexName = IndexParts.toIndexName(target, partitionName.ident());
targetMd = IndexMetadata.builder(sourceMd).removeAllAliases().putAlias(AliasMetadata.builder(target.indexNameOrAlias()).build()).index(targetIndexName).build();
onProcessedIndex.accept(targetIndexName);
}
updatedMetadata.put(targetMd, true);
blocksBuilder.addBlocks(targetMd);
routingBuilder.addAsFromCloseToOpen(targetMd);
}
if (sourceTemplate != null) {
IndexTemplateMetadata.Builder templateBuilder = Templates.copyWithNewName(sourceTemplate, target);
updatedMetadata.put(templateBuilder);
}
}
use of org.elasticsearch.cluster.metadata.IndexTemplateMetaData in project crate by crate.
the class TableSettingsResolver method forPartitionedTable.
private static Settings forPartitionedTable(Metadata metadata, RelationName relationName) {
String templateName = PartitionName.templateName(relationName.schema(), relationName.name());
IndexTemplateMetadata templateMetadata = metadata.templates().get(templateName);
if (templateMetadata == null) {
throw new RelationUnknown(relationName);
}
return templateMetadata.getSettings();
}
Aggregations