Search in sources :

Example 21 with IndexTemplateMetadata

use of org.elasticsearch.cluster.metadata.IndexTemplateMetadata in project crate by crate.

the class IndexTemplateUpgraderTest method test__all_is_removed_from_template_mapping.

@Test
public void test__all_is_removed_from_template_mapping() throws Throwable {
    String templateName = PartitionName.templateName("doc", "events");
    var template = IndexTemplateMetadata.builder(templateName).patterns(List.of("*")).putMapping(Constants.DEFAULT_MAPPING_TYPE, "{" + "   \"default\": {" + "       \"_all\": {\"enabled\": false}," + "       \"properties\": {" + "           \"name\": {" + "               \"type\": \"keyword\"" + "           }" + "       }" + "   }" + "}").build();
    IndexTemplateUpgrader upgrader = new IndexTemplateUpgrader();
    Map<String, IndexTemplateMetadata> result = upgrader.apply(Map.of(templateName, template));
    IndexTemplateMetadata updatedTemplate = result.get(templateName);
    CompressedXContent compressedXContent = updatedTemplate.mappings().get(Constants.DEFAULT_MAPPING_TYPE);
    assertThat(compressedXContent.string(), is("{\"default\":{\"properties\":{\"name\":{\"type\":\"keyword\"}}}}"));
}
Also used : IndexTemplateMetadata(org.elasticsearch.cluster.metadata.IndexTemplateMetadata) CompressedXContent(org.elasticsearch.common.compress.CompressedXContent) Test(org.junit.Test)

Example 22 with IndexTemplateMetadata

use of org.elasticsearch.cluster.metadata.IndexTemplateMetadata in project crate by crate.

the class ColumnPolicyIntegrationTest method testStrictPartitionedTableInsert.

@Test
public void testStrictPartitionedTableInsert() throws Exception {
    execute("create table numbers (" + "  num int, " + "  odd boolean," + "  prime boolean" + ") partitioned by (odd) with (column_policy='strict', number_of_replicas=0)");
    ensureYellow();
    GetIndexTemplatesResponse response = client().admin().indices().prepareGetTemplates(PartitionName.templateName(sqlExecutor.getCurrentSchema(), "numbers")).execute().actionGet();
    assertThat(response.getIndexTemplates().size(), is(1));
    IndexTemplateMetadata template = response.getIndexTemplates().get(0);
    CompressedXContent mappingStr = template.mappings().get(Constants.DEFAULT_MAPPING_TYPE);
    assertThat(mappingStr, is(notNullValue()));
    Tuple<XContentType, Map<String, Object>> typeAndMap = XContentHelper.convertToMap(mappingStr.compressedReference(), false, XContentType.JSON);
    @SuppressWarnings("unchecked") Map<String, Object> mapping = (Map<String, Object>) typeAndMap.v2().get(Constants.DEFAULT_MAPPING_TYPE);
    assertThat(decodeMappingValue(mapping.get("dynamic")), is(ColumnPolicy.STRICT));
    execute("insert into numbers (num, odd, prime) values (?, ?, ?)", new Object[] { 6, true, false });
    execute("refresh table numbers");
    Map<String, Object> sourceMap = getSourceMap(new PartitionName(new RelationName("doc", "numbers"), Arrays.asList("true")).asIndexName());
    assertThat(decodeMappingValue(sourceMap.get("dynamic")), is(ColumnPolicy.STRICT));
    assertThrowsMatches(() -> execute("insert into numbers (num, odd, prime, perfect) values (?, ?, ?, ?)", new Object[] { 28, true, false, true }), isSQLError(is("Column perfect unknown"), UNDEFINED_COLUMN, NOT_FOUND, 4043));
}
Also used : PartitionName(io.crate.metadata.PartitionName) XContentType(org.elasticsearch.common.xcontent.XContentType) GetIndexTemplatesResponse(org.elasticsearch.action.admin.indices.template.get.GetIndexTemplatesResponse) IndexTemplateMetadata(org.elasticsearch.cluster.metadata.IndexTemplateMetadata) CompressedXContent(org.elasticsearch.common.compress.CompressedXContent) RelationName(io.crate.metadata.RelationName) Matchers.containsString(org.hamcrest.Matchers.containsString) Map(java.util.Map) Test(org.junit.Test)

Example 23 with IndexTemplateMetadata

use of org.elasticsearch.cluster.metadata.IndexTemplateMetadata in project crate by crate.

the class ElasticsearchAssertions method assertIndexTemplateExists.

/**
 * Assert that an index template exists
 */
public static void assertIndexTemplateExists(GetIndexTemplatesResponse templatesResponse, String name) {
    List<String> templateNames = new ArrayList<>();
    for (IndexTemplateMetadata indexTemplateMetadata : templatesResponse.getIndexTemplates()) {
        templateNames.add(indexTemplateMetadata.name());
    }
    assertThat(templateNames, hasItem(name));
}
Also used : IndexTemplateMetadata(org.elasticsearch.cluster.metadata.IndexTemplateMetadata) ArrayList(java.util.ArrayList)

Example 24 with IndexTemplateMetadata

use of org.elasticsearch.cluster.metadata.IndexTemplateMetadata in project elasticsearch by elastic.

the class ClusterState method toXContent.

@Override
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
    EnumSet<Metric> metrics = Metric.parseString(params.param("metric", "_all"), true);
    if (metrics.contains(Metric.VERSION)) {
        builder.field("version", version);
        builder.field("state_uuid", stateUUID);
    }
    if (metrics.contains(Metric.MASTER_NODE)) {
        builder.field("master_node", nodes().getMasterNodeId());
    }
    if (metrics.contains(Metric.BLOCKS)) {
        builder.startObject("blocks");
        if (!blocks().global().isEmpty()) {
            builder.startObject("global");
            for (ClusterBlock block : blocks().global()) {
                block.toXContent(builder, params);
            }
            builder.endObject();
        }
        if (!blocks().indices().isEmpty()) {
            builder.startObject("indices");
            for (ObjectObjectCursor<String, Set<ClusterBlock>> entry : blocks().indices()) {
                builder.startObject(entry.key);
                for (ClusterBlock block : entry.value) {
                    block.toXContent(builder, params);
                }
                builder.endObject();
            }
            builder.endObject();
        }
        builder.endObject();
    }
    // nodes
    if (metrics.contains(Metric.NODES)) {
        builder.startObject("nodes");
        for (DiscoveryNode node : nodes) {
            node.toXContent(builder, params);
        }
        builder.endObject();
    }
    // meta data
    if (metrics.contains(Metric.METADATA)) {
        builder.startObject("metadata");
        builder.field("cluster_uuid", metaData().clusterUUID());
        builder.startObject("templates");
        for (ObjectCursor<IndexTemplateMetaData> cursor : metaData().templates().values()) {
            IndexTemplateMetaData templateMetaData = cursor.value;
            builder.startObject(templateMetaData.name());
            builder.field("index_patterns", templateMetaData.patterns());
            builder.field("order", templateMetaData.order());
            builder.startObject("settings");
            Settings settings = templateMetaData.settings();
            settings.toXContent(builder, params);
            builder.endObject();
            builder.startObject("mappings");
            for (ObjectObjectCursor<String, CompressedXContent> cursor1 : templateMetaData.mappings()) {
                Map<String, Object> mapping = XContentHelper.convertToMap(new BytesArray(cursor1.value.uncompressed()), false).v2();
                if (mapping.size() == 1 && mapping.containsKey(cursor1.key)) {
                    // the type name is the root value, reduce it
                    mapping = (Map<String, Object>) mapping.get(cursor1.key);
                }
                builder.field(cursor1.key);
                builder.map(mapping);
            }
            builder.endObject();
            builder.endObject();
        }
        builder.endObject();
        builder.startObject("indices");
        for (IndexMetaData indexMetaData : metaData()) {
            builder.startObject(indexMetaData.getIndex().getName());
            builder.field("state", indexMetaData.getState().toString().toLowerCase(Locale.ENGLISH));
            builder.startObject("settings");
            Settings settings = indexMetaData.getSettings();
            settings.toXContent(builder, params);
            builder.endObject();
            builder.startObject("mappings");
            for (ObjectObjectCursor<String, MappingMetaData> cursor : indexMetaData.getMappings()) {
                Map<String, Object> mapping = XContentHelper.convertToMap(new BytesArray(cursor.value.source().uncompressed()), false).v2();
                if (mapping.size() == 1 && mapping.containsKey(cursor.key)) {
                    // the type name is the root value, reduce it
                    mapping = (Map<String, Object>) mapping.get(cursor.key);
                }
                builder.field(cursor.key);
                builder.map(mapping);
            }
            builder.endObject();
            builder.startArray("aliases");
            for (ObjectCursor<String> cursor : indexMetaData.getAliases().keys()) {
                builder.value(cursor.value);
            }
            builder.endArray();
            builder.startObject(IndexMetaData.KEY_PRIMARY_TERMS);
            for (int shard = 0; shard < indexMetaData.getNumberOfShards(); shard++) {
                builder.field(Integer.toString(shard), indexMetaData.primaryTerm(shard));
            }
            builder.endObject();
            builder.startObject(IndexMetaData.KEY_IN_SYNC_ALLOCATIONS);
            for (IntObjectCursor<Set<String>> cursor : indexMetaData.getInSyncAllocationIds()) {
                builder.startArray(String.valueOf(cursor.key));
                for (String allocationId : cursor.value) {
                    builder.value(allocationId);
                }
                builder.endArray();
            }
            builder.endObject();
            // index metadata
            builder.endObject();
        }
        builder.endObject();
        for (ObjectObjectCursor<String, MetaData.Custom> cursor : metaData.customs()) {
            builder.startObject(cursor.key);
            cursor.value.toXContent(builder, params);
            builder.endObject();
        }
        builder.endObject();
    }
    // routing table
    if (metrics.contains(Metric.ROUTING_TABLE)) {
        builder.startObject("routing_table");
        builder.startObject("indices");
        for (IndexRoutingTable indexRoutingTable : routingTable()) {
            builder.startObject(indexRoutingTable.getIndex().getName());
            builder.startObject("shards");
            for (IndexShardRoutingTable indexShardRoutingTable : indexRoutingTable) {
                builder.startArray(Integer.toString(indexShardRoutingTable.shardId().id()));
                for (ShardRouting shardRouting : indexShardRoutingTable) {
                    shardRouting.toXContent(builder, params);
                }
                builder.endArray();
            }
            builder.endObject();
            builder.endObject();
        }
        builder.endObject();
        builder.endObject();
    }
    // routing nodes
    if (metrics.contains(Metric.ROUTING_NODES)) {
        builder.startObject("routing_nodes");
        builder.startArray("unassigned");
        for (ShardRouting shardRouting : getRoutingNodes().unassigned()) {
            shardRouting.toXContent(builder, params);
        }
        builder.endArray();
        builder.startObject("nodes");
        for (RoutingNode routingNode : getRoutingNodes()) {
            builder.startArray(routingNode.nodeId() == null ? "null" : routingNode.nodeId());
            for (ShardRouting shardRouting : routingNode) {
                shardRouting.toXContent(builder, params);
            }
            builder.endArray();
        }
        builder.endObject();
        builder.endObject();
    }
    if (metrics.contains(Metric.CUSTOMS)) {
        for (ObjectObjectCursor<String, Custom> cursor : customs) {
            builder.startObject(cursor.key);
            cursor.value.toXContent(builder, params);
            builder.endObject();
        }
    }
    return builder;
}
Also used : IndexRoutingTable(org.elasticsearch.cluster.routing.IndexRoutingTable) IndexShardRoutingTable(org.elasticsearch.cluster.routing.IndexShardRoutingTable) DiscoveryNode(org.elasticsearch.cluster.node.DiscoveryNode) EnumSet(java.util.EnumSet) Set(java.util.Set) ClusterBlock(org.elasticsearch.cluster.block.ClusterBlock) RoutingNode(org.elasticsearch.cluster.routing.RoutingNode) IndexTemplateMetaData(org.elasticsearch.cluster.metadata.IndexTemplateMetaData) CompressedXContent(org.elasticsearch.common.compress.CompressedXContent) Settings(org.elasticsearch.common.settings.Settings) BytesArray(org.elasticsearch.common.bytes.BytesArray) MappingMetaData(org.elasticsearch.cluster.metadata.MappingMetaData) IndexMetaData(org.elasticsearch.cluster.metadata.IndexMetaData) ShardRouting(org.elasticsearch.cluster.routing.ShardRouting)

Example 25 with IndexTemplateMetadata

use of org.elasticsearch.cluster.metadata.IndexTemplateMetadata in project crate by crate.

the class DefaultTemplateService method createCopyWithDefaultTemplateAdded.

private static ImmutableOpenMap<String, IndexTemplateMetaData> createCopyWithDefaultTemplateAdded(ImmutableOpenMap<String, IndexTemplateMetaData> currentTemplates) throws IOException {
    IndexTemplateMetaData defaultTemplate = IndexTemplateMetaData.builder(TEMPLATE_NAME).order(0).putMapping(Constants.DEFAULT_MAPPING_TYPE, DEFAULT_MAPPING_SOURCE).template("*").build();
    ImmutableOpenMap.Builder<String, IndexTemplateMetaData> builder = ImmutableOpenMap.builder(currentTemplates);
    builder.put(TEMPLATE_NAME, defaultTemplate);
    return builder.build();
}
Also used : IndexTemplateMetaData(org.elasticsearch.cluster.metadata.IndexTemplateMetaData) ImmutableOpenMap(org.elasticsearch.common.collect.ImmutableOpenMap)

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