Search in sources :

Example 1 with PutIndexTemplateResponse

use of org.elasticsearch.action.admin.indices.template.put.PutIndexTemplateResponse 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 2 with PutIndexTemplateResponse

use of org.elasticsearch.action.admin.indices.template.put.PutIndexTemplateResponse 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 3 with PutIndexTemplateResponse

use of org.elasticsearch.action.admin.indices.template.put.PutIndexTemplateResponse in project MSEC by Tencent.

the class ESHelper method ClusterStatus.

public void ClusterStatus(ArrayList<String> ips, String cluster_name, QueryESClusterDetailResponse response) {
    Logger logger = Logger.getLogger(ESHelper.class);
    TransportClient client = null;
    HashSet<String> total_ips = new HashSet<>(ips);
    try {
        Settings settings = Settings.builder().put("cluster.name", cluster_name).put("client.transport.sniff", true).build();
        for (String ip : ips) {
            if (client == null) {
                client = new PreBuiltTransportClient(settings).addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName(ip), default_client_port));
            } else
                client.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName(ip), default_client_port));
        }
        ClusterHealthResponse healthResponse = client.admin().cluster().prepareHealth().setTimeout(TimeValue.timeValueSeconds(5)).execute().actionGet();
        if (healthResponse != null && !healthResponse.isTimedOut()) {
            response.setActive_shards(healthResponse.getActiveShards());
            response.setTotal_shards(healthResponse.getActiveShards() + healthResponse.getUnassignedShards());
            response.setHealth_status(healthResponse.getStatus().toString().toLowerCase());
            response.setServer_port(default_port);
            logger.info(healthResponse);
            if (healthResponse.getNumberOfNodes() > 0) {
                NodesStatsResponse nodeStats = client.admin().cluster().prepareNodesStats().all().get();
                if (nodeStats != null) {
                    for (NodeStats stats : nodeStats.getNodes()) {
                        QueryESClusterDetailResponse.RTInfo info = new QueryESClusterDetailResponse().new RTInfo();
                        info.setOK(true);
                        info.setAvail_disk_size(stats.getFs().getTotal().getAvailable().getBytes());
                        info.setDoc_count(stats.getIndices().getDocs().getCount());
                        info.setDoc_disk_size(stats.getIndices().getStore().getSizeInBytes());
                        response.getInfo_map().put(stats.getNode().getAddress().getHost(), info);
                        total_ips.add(stats.getNode().getAddress().getHost());
                    }
                }
            }
        }
        // update Zen settings
        client.admin().cluster().prepareUpdateSettings().setPersistentSettings(Settings.builder().put("discovery.zen.minimum_master_nodes", total_ips.size() / 2 + 1)).setTransientSettings(Settings.builder().put("discovery.zen.minimum_master_nodes", total_ips.size() / 2 + 1)).get();
        // update template settings
        PutIndexTemplateResponse tpl_resp = client.admin().indices().preparePutTemplate("template_msec").setTemplate("msec_*").setSettings(Settings.builder().put("number_of_replicas", 1).put("refresh_interval", "30s")).addMapping("logs", MAPPING).execute().get();
        logger.info("Create mapping: " + tpl_resp.isAcknowledged());
    } catch (UnknownHostException e) {
        logger.error(e);
    } catch (Exception e) {
        logger.error(e);
    } finally {
        if (client != null)
            client.close();
    }
}
Also used : TransportClient(org.elasticsearch.client.transport.TransportClient) PreBuiltTransportClient(org.elasticsearch.transport.client.PreBuiltTransportClient) ClusterHealthResponse(org.elasticsearch.action.admin.cluster.health.ClusterHealthResponse) UnknownHostException(java.net.UnknownHostException) Logger(org.apache.log4j.Logger) InetSocketTransportAddress(org.elasticsearch.common.transport.InetSocketTransportAddress) MasterNotDiscoveredException(org.elasticsearch.discovery.MasterNotDiscoveredException) ElasticsearchTimeoutException(org.elasticsearch.ElasticsearchTimeoutException) NoNodeAvailableException(org.elasticsearch.client.transport.NoNodeAvailableException) IOException(java.io.IOException) UnknownHostException(java.net.UnknownHostException) NodesStatsResponse(org.elasticsearch.action.admin.cluster.node.stats.NodesStatsResponse) NodeStats(org.elasticsearch.action.admin.cluster.node.stats.NodeStats) PreBuiltTransportClient(org.elasticsearch.transport.client.PreBuiltTransportClient) PutIndexTemplateResponse(org.elasticsearch.action.admin.indices.template.put.PutIndexTemplateResponse) Settings(org.elasticsearch.common.settings.Settings) QueryESClusterDetailResponse(beans.response.QueryESClusterDetailResponse)

Aggregations

PutIndexTemplateResponse (org.elasticsearch.action.admin.indices.template.put.PutIndexTemplateResponse)3 IndexTemplateMetaData (org.elasticsearch.cluster.metadata.IndexTemplateMetaData)2 Settings (org.elasticsearch.common.settings.Settings)2 QueryESClusterDetailResponse (beans.response.QueryESClusterDetailResponse)1 ReadContext (com.jayway.jsonpath.ReadContext)1 FutureActionListener (io.crate.action.FutureActionListener)1 IOException (java.io.IOException)1 UnknownHostException (java.net.UnknownHostException)1 Logger (org.apache.log4j.Logger)1 ElasticsearchTimeoutException (org.elasticsearch.ElasticsearchTimeoutException)1 ClusterHealthResponse (org.elasticsearch.action.admin.cluster.health.ClusterHealthResponse)1 NodeStats (org.elasticsearch.action.admin.cluster.node.stats.NodeStats)1 NodesStatsResponse (org.elasticsearch.action.admin.cluster.node.stats.NodesStatsResponse)1 Alias (org.elasticsearch.action.admin.indices.alias.Alias)1 GetMappingsResponse (org.elasticsearch.action.admin.indices.mapping.get.GetMappingsResponse)1 DeleteIndexTemplateResponse (org.elasticsearch.action.admin.indices.template.delete.DeleteIndexTemplateResponse)1 GetIndexTemplatesResponse (org.elasticsearch.action.admin.indices.template.get.GetIndexTemplatesResponse)1 PutIndexTemplateRequest (org.elasticsearch.action.admin.indices.template.put.PutIndexTemplateRequest)1 IndicesAdminClient (org.elasticsearch.client.IndicesAdminClient)1 NoNodeAvailableException (org.elasticsearch.client.transport.NoNodeAvailableException)1