Search in sources :

Example 31 with GetIndexTemplatesResponse

use of org.elasticsearch.action.admin.indices.template.get.GetIndexTemplatesResponse in project elasticsearch by elastic.

the class IndexTemplateBlocksIT method testIndexTemplatesWithBlocks.

public void testIndexTemplatesWithBlocks() throws IOException {
    // creates a simple index template
    client().admin().indices().preparePutTemplate("template_blocks").setPatterns(Collections.singletonList("te*")).setOrder(0).addMapping("type1", XContentFactory.jsonBuilder().startObject().startObject("type1").startObject("properties").startObject("field1").field("type", "text").field("store", true).endObject().startObject("field2").field("type", "keyword").field("store", true).endObject().endObject().endObject().endObject()).execute().actionGet();
    try {
        setClusterReadOnly(true);
        GetIndexTemplatesResponse response = client().admin().indices().prepareGetTemplates("template_blocks").execute().actionGet();
        assertThat(response.getIndexTemplates(), hasSize(1));
        assertBlocked(client().admin().indices().preparePutTemplate("template_blocks_2").setPatterns(Collections.singletonList("block*")).setOrder(0).addAlias(new Alias("alias_1")));
        assertBlocked(client().admin().indices().prepareDeleteTemplate("template_blocks"));
    } finally {
        setClusterReadOnly(false);
    }
}
Also used : GetIndexTemplatesResponse(org.elasticsearch.action.admin.indices.template.get.GetIndexTemplatesResponse) Alias(org.elasticsearch.action.admin.indices.alias.Alias)

Example 32 with GetIndexTemplatesResponse

use of org.elasticsearch.action.admin.indices.template.get.GetIndexTemplatesResponse in project elasticsearch by elastic.

the class SimpleIndexTemplateIT method testBrokenMapping.

public void testBrokenMapping() throws Exception {
    // clean all templates setup by the framework.
    client().admin().indices().prepareDeleteTemplate("*").get();
    // check get all templates on an empty index.
    GetIndexTemplatesResponse response = client().admin().indices().prepareGetTemplates().get();
    assertThat(response.getIndexTemplates(), empty());
    MapperParsingException e = expectThrows(MapperParsingException.class, () -> client().admin().indices().preparePutTemplate("template_1").setPatterns(Collections.singletonList("te*")).addMapping("type1", "{\"foo\": \"abcde\"}", XContentType.JSON).get());
    assertThat(e.getMessage(), containsString("Failed to parse mapping "));
    response = client().admin().indices().prepareGetTemplates().get();
    assertThat(response.getIndexTemplates(), hasSize(0));
}
Also used : MapperParsingException(org.elasticsearch.index.mapper.MapperParsingException) GetIndexTemplatesResponse(org.elasticsearch.action.admin.indices.template.get.GetIndexTemplatesResponse)

Example 33 with GetIndexTemplatesResponse

use of org.elasticsearch.action.admin.indices.template.get.GetIndexTemplatesResponse in project elasticsearch by elastic.

the class SimpleIndexTemplateIT method testAliasInvalidFilterInvalidJson.

public void testAliasInvalidFilterInvalidJson() throws Exception {
    //invalid json: put index template fails
    PutIndexTemplateRequestBuilder putIndexTemplateRequestBuilder = client().admin().indices().preparePutTemplate("template_1").setPatterns(Collections.singletonList("te*")).addAlias(new Alias("invalid_alias").filter("abcde"));
    IllegalArgumentException e = expectThrows(IllegalArgumentException.class, () -> putIndexTemplateRequestBuilder.get());
    assertThat(e.getMessage(), equalTo("failed to parse filter for alias [invalid_alias]"));
    GetIndexTemplatesResponse response = client().admin().indices().prepareGetTemplates("template_1").get();
    assertThat(response.getIndexTemplates().size(), equalTo(0));
}
Also used : Alias(org.elasticsearch.action.admin.indices.alias.Alias) GetIndexTemplatesResponse(org.elasticsearch.action.admin.indices.template.get.GetIndexTemplatesResponse) PutIndexTemplateRequestBuilder(org.elasticsearch.action.admin.indices.template.put.PutIndexTemplateRequestBuilder)

Example 34 with GetIndexTemplatesResponse

use of org.elasticsearch.action.admin.indices.template.get.GetIndexTemplatesResponse in project zipkin by openzipkin.

the class NativeClient method ensureTemplate.

@Override
public void ensureTemplate(String name, String indexTemplate) {
    GetIndexTemplatesResponse existingTemplates = client.admin().indices().getTemplates(new GetIndexTemplatesRequest(name)).actionGet();
    if (!existingTemplates.getIndexTemplates().isEmpty()) {
        return;
    }
    client.admin().indices().putTemplate(new PutIndexTemplateRequest(name).source(indexTemplate)).actionGet();
}
Also used : GetIndexTemplatesResponse(org.elasticsearch.action.admin.indices.template.get.GetIndexTemplatesResponse) GetIndexTemplatesRequest(org.elasticsearch.action.admin.indices.template.get.GetIndexTemplatesRequest) PutIndexTemplateRequest(org.elasticsearch.action.admin.indices.template.put.PutIndexTemplateRequest)

Example 35 with GetIndexTemplatesResponse

use of org.elasticsearch.action.admin.indices.template.get.GetIndexTemplatesResponse 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)

Aggregations

GetIndexTemplatesResponse (org.elasticsearch.action.admin.indices.template.get.GetIndexTemplatesResponse)35 Test (org.junit.Test)18 IndexTemplateMetaData (org.elasticsearch.cluster.metadata.IndexTemplateMetaData)11 PartitionName (io.crate.metadata.PartitionName)10 BytesRef (org.apache.lucene.util.BytesRef)10 GetSettingsResponse (org.elasticsearch.action.admin.indices.settings.get.GetSettingsResponse)7 Settings (org.elasticsearch.common.settings.Settings)7 Map (java.util.Map)5 GetIndexTemplatesRequest (org.elasticsearch.action.admin.indices.template.get.GetIndexTemplatesRequest)5 MappingMetaData (org.elasticsearch.cluster.metadata.MappingMetaData)5 CompressedXContent (org.elasticsearch.common.compress.CompressedXContent)5 HashMap (java.util.HashMap)4 Alias (org.elasticsearch.action.admin.indices.alias.Alias)4 XContentType (org.elasticsearch.common.xcontent.XContentType)4 DeleteIndexTemplateResponse (org.elasticsearch.action.admin.indices.template.delete.DeleteIndexTemplateResponse)3 IndicesAdminClient (org.elasticsearch.client.IndicesAdminClient)3 MapperParsingException (org.elasticsearch.index.mapper.MapperParsingException)3 CreateSnapshotResponse (org.elasticsearch.action.admin.cluster.snapshots.create.CreateSnapshotResponse)2 RestoreSnapshotResponse (org.elasticsearch.action.admin.cluster.snapshots.restore.RestoreSnapshotResponse)2 DeleteIndexTemplateRequest (org.elasticsearch.action.admin.indices.template.delete.DeleteIndexTemplateRequest)2