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);
}
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;
}
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();
}
}
Aggregations