Search in sources :

Example 31 with IndexTemplateMetaData

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;
}
Also used : PutIndexTemplateRequest(org.elasticsearch.action.admin.indices.template.put.PutIndexTemplateRequest) FutureActionListener(io.crate.action.FutureActionListener) AliasMetaData(org.elasticsearch.cluster.metadata.AliasMetaData) IndexTemplateMetaData(org.elasticsearch.cluster.metadata.IndexTemplateMetaData) Alias(org.elasticsearch.action.admin.indices.alias.Alias) PutIndexTemplateResponse(org.elasticsearch.action.admin.indices.template.put.PutIndexTemplateResponse) Settings(org.elasticsearch.common.settings.Settings)

Example 32 with IndexTemplateMetaData

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");
}
Also used : DeleteIndexTemplateResponse(org.elasticsearch.action.admin.indices.template.delete.DeleteIndexTemplateResponse) IndicesAdminClient(org.elasticsearch.client.IndicesAdminClient) DeleteIndexTemplateRequest(org.elasticsearch.action.admin.indices.template.delete.DeleteIndexTemplateRequest) ObjectMapperProvider(org.graylog2.shared.bindings.providers.ObjectMapperProvider) IndexMapping(org.graylog2.indexer.IndexMapping) GetIndexTemplatesResponse(org.elasticsearch.action.admin.indices.template.get.GetIndexTemplatesResponse) IndexTemplateMetaData(org.elasticsearch.cluster.metadata.IndexTemplateMetaData) CompressedXContent(org.elasticsearch.common.compress.CompressedXContent) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) ImmutableOpenMap(org.elasticsearch.common.collect.ImmutableOpenMap) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Test(org.junit.Test)

Example 33 with IndexTemplateMetaData

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);
    }
}
Also used : IndexTemplateMetadata(org.elasticsearch.cluster.metadata.IndexTemplateMetadata) Index(org.elasticsearch.index.Index)

Example 34 with IndexTemplateMetaData

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);
    }
}
Also used : PartitionName(io.crate.metadata.PartitionName) IndexTemplateMetadata(org.elasticsearch.cluster.metadata.IndexTemplateMetadata) Index(org.elasticsearch.index.Index) IndexMetadata(org.elasticsearch.cluster.metadata.IndexMetadata)

Example 35 with IndexTemplateMetaData

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();
}
Also used : RelationUnknown(io.crate.exceptions.RelationUnknown) IndexTemplateMetadata(org.elasticsearch.cluster.metadata.IndexTemplateMetadata)

Aggregations

IndexTemplateMetadata (org.elasticsearch.cluster.metadata.IndexTemplateMetadata)36 IndexTemplateMetaData (org.elasticsearch.cluster.metadata.IndexTemplateMetaData)18 Test (org.junit.Test)16 Settings (org.elasticsearch.common.settings.Settings)14 IndexMetadata (org.elasticsearch.cluster.metadata.IndexMetadata)13 Map (java.util.Map)12 Metadata (org.elasticsearch.cluster.metadata.Metadata)11 CompressedXContent (org.elasticsearch.common.compress.CompressedXContent)11 GetIndexTemplatesResponse (org.elasticsearch.action.admin.indices.template.get.GetIndexTemplatesResponse)10 ClusterState (org.elasticsearch.cluster.ClusterState)10 PartitionName (io.crate.metadata.PartitionName)9 ArrayList (java.util.ArrayList)9 HashMap (java.util.HashMap)9 Index (org.elasticsearch.index.Index)9 RelationName (io.crate.metadata.RelationName)8 RoutingTable (org.elasticsearch.cluster.routing.RoutingTable)7 AliasMetadata (org.elasticsearch.cluster.metadata.AliasMetadata)6 ImmutableOpenMap (org.elasticsearch.common.collect.ImmutableOpenMap)6 IOException (java.io.IOException)5 Set (java.util.Set)5