Search in sources :

Example 1 with DeleteIndexTemplateResponse

use of org.elasticsearch.action.admin.indices.template.delete.DeleteIndexTemplateResponse in project crate by crate.

the class DropTableTask method execute.

@Override
public void execute(final BatchConsumer consumer, Row parameters) {
    if (tableInfo.isPartitioned()) {
        String templateName = PartitionName.templateName(tableInfo.ident().schema(), tableInfo.ident().name());
        deleteTemplateAction.execute(new DeleteIndexTemplateRequest(templateName), new ActionListener<DeleteIndexTemplateResponse>() {

            @Override
            public void onResponse(DeleteIndexTemplateResponse response) {
                if (!response.isAcknowledged()) {
                    warnNotAcknowledged();
                }
                if (!tableInfo.partitions().isEmpty()) {
                    deleteESIndex(tableInfo.ident().indexName(), consumer);
                } else {
                    consumer.accept(RowsBatchIterator.newInstance(ROW_ONE), null);
                }
            }

            @Override
            public void onFailure(Throwable e) {
                e = ExceptionsHelper.unwrapCause(e);
                if (e instanceof IndexTemplateMissingException && !tableInfo.partitions().isEmpty()) {
                    logger.warn(e.getMessage());
                    deleteESIndex(tableInfo.ident().indexName(), consumer);
                } else {
                    consumer.accept(null, e);
                }
            }
        });
    } else {
        deleteESIndex(tableInfo.ident().indexName(), consumer);
    }
}
Also used : IndexTemplateMissingException(org.elasticsearch.indices.IndexTemplateMissingException) DeleteIndexTemplateResponse(org.elasticsearch.action.admin.indices.template.delete.DeleteIndexTemplateResponse) DeleteIndexTemplateRequest(org.elasticsearch.action.admin.indices.template.delete.DeleteIndexTemplateRequest)

Example 2 with DeleteIndexTemplateResponse

use of org.elasticsearch.action.admin.indices.template.delete.DeleteIndexTemplateResponse in project graylog2-server by Graylog2.

the class IndicesTest method testIndexTemplateCanBeOverridden.

@Test
public void testIndexTemplateCanBeOverridden() throws Exception {
    final String customTemplateName = "custom-template";
    final IndicesAdminClient client = this.client.admin().indices();
    // Create custom index template
    final Map<String, Object> customMapping = ImmutableMap.of("_source", ImmutableMap.of("enabled", false), "properties", ImmutableMap.of("message", ImmutableMap.of("type", "string", "index", "not_analyzed")));
    final PutIndexTemplateResponse putIndexTemplateResponse = client.preparePutTemplate(customTemplateName).setTemplate(indexSet.getIndexWildcard()).setOrder(1).addMapping(IndexMapping.TYPE_MESSAGE, customMapping).get();
    assertThat(putIndexTemplateResponse.isAcknowledged()).isTrue();
    // Validate existing index templates
    final GetIndexTemplatesResponse getTemplatesResponse = client.prepareGetTemplates().get();
    final List<IndexTemplateMetaData> indexTemplates = getTemplatesResponse.getIndexTemplates();
    assertThat(indexTemplates).extracting(IndexTemplateMetaData::getName).containsExactly(customTemplateName);
    // Create index with custom template
    final String testIndexName = "graylog_override_template";
    indices.create(testIndexName, indexSet);
    // Check index mapping
    final GetMappingsResponse indexMappingResponse = client.prepareGetMappings(testIndexName).get();
    final String mapping = indexMappingResponse.getMappings().get(testIndexName).get(IndexMapping.TYPE_MESSAGE).source().string();
    final ReadContext ctx = JsonPath.parse(mapping);
    final boolean sourceEnabled = ctx.read("$.message._source.enabled");
    assertThat(sourceEnabled).isFalse();
    final String messageField = ctx.read("$.message.properties.message.index");
    assertThat(messageField).isEqualTo("not_analyzed");
    // Clean up
    final DeleteIndexTemplateResponse deleteResponse = client.prepareDeleteTemplate(customTemplateName).get();
    assertThat(deleteResponse.isAcknowledged()).isTrue();
    indices.delete(testIndexName);
}
Also used : DeleteIndexTemplateResponse(org.elasticsearch.action.admin.indices.template.delete.DeleteIndexTemplateResponse) GetIndexTemplatesResponse(org.elasticsearch.action.admin.indices.template.get.GetIndexTemplatesResponse) IndexTemplateMetaData(org.elasticsearch.cluster.metadata.IndexTemplateMetaData) ReadContext(com.jayway.jsonpath.ReadContext) PutIndexTemplateResponse(org.elasticsearch.action.admin.indices.template.put.PutIndexTemplateResponse) IndicesAdminClient(org.elasticsearch.client.IndicesAdminClient) GetMappingsResponse(org.elasticsearch.action.admin.indices.mapping.get.GetMappingsResponse) Test(org.junit.Test)

Example 3 with DeleteIndexTemplateResponse

use of org.elasticsearch.action.admin.indices.template.delete.DeleteIndexTemplateResponse in project graylog2-server by Graylog2.

the class IndicesTest method testCreateEnsuresIndexTemplateExists.

@Test
public void testCreateEnsuresIndexTemplateExists() throws Exception {
    final String templateName = indexSetConfig.indexTemplateName();
    final IndicesAdminClient client = this.client.admin().indices();
    final GetIndexTemplatesRequest request = client.prepareGetTemplates(templateName).request();
    final GetIndexTemplatesResponse responseBefore = client.getTemplates(request).actionGet();
    assertThat(responseBefore.getIndexTemplates()).isEmpty();
    indices.create("index_template_test", indexSet);
    final GetIndexTemplatesResponse responseAfter = client.getTemplates(request).actionGet();
    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 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) GetIndexTemplatesResponse(org.elasticsearch.action.admin.indices.template.get.GetIndexTemplatesResponse) IndexTemplateMetaData(org.elasticsearch.cluster.metadata.IndexTemplateMetaData) GetIndexTemplatesRequest(org.elasticsearch.action.admin.indices.template.get.GetIndexTemplatesRequest) IndicesAdminClient(org.elasticsearch.client.IndicesAdminClient) DeleteIndexTemplateRequest(org.elasticsearch.action.admin.indices.template.delete.DeleteIndexTemplateRequest) Test(org.junit.Test)

Example 4 with DeleteIndexTemplateResponse

use of org.elasticsearch.action.admin.indices.template.delete.DeleteIndexTemplateResponse 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

DeleteIndexTemplateResponse (org.elasticsearch.action.admin.indices.template.delete.DeleteIndexTemplateResponse)4 DeleteIndexTemplateRequest (org.elasticsearch.action.admin.indices.template.delete.DeleteIndexTemplateRequest)3 GetIndexTemplatesResponse (org.elasticsearch.action.admin.indices.template.get.GetIndexTemplatesResponse)3 IndicesAdminClient (org.elasticsearch.client.IndicesAdminClient)3 IndexTemplateMetaData (org.elasticsearch.cluster.metadata.IndexTemplateMetaData)3 Test (org.junit.Test)3 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 ImmutableMap (com.google.common.collect.ImmutableMap)1 ReadContext (com.jayway.jsonpath.ReadContext)1 Map (java.util.Map)1 GetMappingsResponse (org.elasticsearch.action.admin.indices.mapping.get.GetMappingsResponse)1 GetIndexTemplatesRequest (org.elasticsearch.action.admin.indices.template.get.GetIndexTemplatesRequest)1 PutIndexTemplateResponse (org.elasticsearch.action.admin.indices.template.put.PutIndexTemplateResponse)1 ImmutableOpenMap (org.elasticsearch.common.collect.ImmutableOpenMap)1 CompressedXContent (org.elasticsearch.common.compress.CompressedXContent)1 IndexTemplateMissingException (org.elasticsearch.indices.IndexTemplateMissingException)1 IndexMapping (org.graylog2.indexer.IndexMapping)1 ObjectMapperProvider (org.graylog2.shared.bindings.providers.ObjectMapperProvider)1