use of org.elasticsearch.client.indices.GetIndexTemplatesRequest in project phoebus-olog by Olog.
the class ElasticConfig method elasticIndexValidation.
/**
* Checks for the existence of the elastic indices needed for Olog and creates
* them with the appropriate mapping is they are missing.
*
* @param indexClient the elastic client instance used to validate and create
* olog indices
*/
private synchronized void elasticIndexValidation(RestHighLevelClient indexClient) {
try {
if (!indexClient.indices().exists(new GetIndexRequest().indices(ES_TAG_INDEX), RequestOptions.DEFAULT)) {
CreateIndexRequest createRequest = new CreateIndexRequest(ES_TAG_INDEX);
ObjectMapper mapper = new ObjectMapper();
InputStream is = ElasticConfig.class.getResourceAsStream("/tag_mapping.json");
Map<String, String> jsonMap = mapper.readValue(is, Map.class);
createRequest.mapping(ES_TAG_TYPE, jsonMap);
indexClient.indices().create(createRequest, RequestOptions.DEFAULT);
logger.info("Successfully created index: " + ES_TAG_INDEX);
}
} catch (IOException e) {
logger.log(Level.WARNING, "Failed to create index " + ES_TAG_INDEX, e);
}
// Create/migrate the logbook index
try {
if (!indexClient.indices().exists(new GetIndexRequest().indices(ES_LOGBOOK_INDEX), RequestOptions.DEFAULT)) {
CreateIndexRequest createRequest = new CreateIndexRequest(ES_LOGBOOK_INDEX);
ObjectMapper mapper = new ObjectMapper();
InputStream is = ElasticConfig.class.getResourceAsStream("/logbook_mapping.json");
Map<String, String> jsonMap = mapper.readValue(is, Map.class);
createRequest.mapping(ES_LOGBOOK_TYPE, jsonMap);
indexClient.indices().create(createRequest, RequestOptions.DEFAULT);
logger.info("Successfully created index: " + ES_LOGBOOK_INDEX);
}
} catch (IOException e) {
logger.log(Level.WARNING, "Failed to create index " + ES_LOGBOOK_INDEX, e);
}
// Create/migrate the property index
try {
if (!indexClient.indices().exists(new GetIndexRequest().indices(ES_PROPERTY_INDEX), RequestOptions.DEFAULT)) {
CreateIndexRequest createRequest = new CreateIndexRequest(ES_PROPERTY_INDEX);
ObjectMapper mapper = new ObjectMapper();
InputStream is = ElasticConfig.class.getResourceAsStream("/property_mapping.json");
Map<String, String> jsonMap = mapper.readValue(is, Map.class);
createRequest.mapping(ES_PROPERTY_TYPE, jsonMap);
indexClient.indices().create(createRequest, RequestOptions.DEFAULT);
logger.info("Successfully created index: " + ES_PROPERTY_INDEX);
}
} catch (IOException e) {
logger.log(Level.WARNING, "Failed to create index " + ES_PROPERTY_INDEX, e);
}
// Create/migrate the sequence index
try {
if (!indexClient.indices().exists(new GetIndexRequest().indices(ES_SEQ_INDEX), RequestOptions.DEFAULT)) {
CreateIndexRequest createRequest = new CreateIndexRequest(ES_SEQ_INDEX);
createRequest.settings(Settings.builder().put("index.number_of_shards", 1).put("auto_expand_replicas", "0-all"));
ObjectMapper mapper = new ObjectMapper();
InputStream is = ElasticConfig.class.getResourceAsStream("/seq_mapping.json");
Map<String, String> jsonMap = mapper.readValue(is, Map.class);
createRequest.mapping(ES_SEQ_TYPE, jsonMap);
logger.info("Successfully created index: " + ES_SEQ_TYPE);
}
} catch (IOException e) {
logger.log(Level.WARNING, "Failed to create index " + ES_SEQ_INDEX, e);
}
// create/migrate log template
try {
GetIndexTemplatesResponse templates = indexClient.indices().getIndexTemplate(new GetIndexTemplatesRequest("*"), RequestOptions.DEFAULT);
if (!templates.getIndexTemplates().stream().anyMatch(i -> {
return i.name().equalsIgnoreCase(ES_LOG_INDEX + "_template");
})) {
PutIndexTemplateRequest templateRequest = new PutIndexTemplateRequest(ES_LOG_INDEX + "_template");
templateRequest.patterns(Arrays.asList(ES_LOG_INDEX));
ObjectMapper mapper = new ObjectMapper();
InputStream is = ElasticConfig.class.getResourceAsStream("/log_template_mapping.json");
Map<String, String> jsonMap = mapper.readValue(is, Map.class);
templateRequest.mapping(ES_LOG_TYPE, XContentFactory.jsonBuilder().map(jsonMap));
templateRequest.create(true);
indexClient.indices().putTemplate(templateRequest, RequestOptions.DEFAULT);
}
// Get the index templates again...
templates = indexClient.indices().getIndexTemplate(new GetIndexTemplatesRequest("*"), RequestOptions.DEFAULT);
if (templates.getIndexTemplates().stream().anyMatch(i -> {
return i.name().equalsIgnoreCase(ES_LOG_INDEX + "_template") && i.version() == null;
})) {
PutIndexTemplateRequest templateRequest = new PutIndexTemplateRequest(ES_LOG_INDEX + "_template");
templateRequest.patterns(Arrays.asList(ES_LOG_INDEX));
ObjectMapper mapper = new ObjectMapper();
InputStream is = ElasticConfig.class.getResourceAsStream("/log_template_mapping_with_title.json");
Map<String, String> jsonMap = mapper.readValue(is, Map.class);
templateRequest.mapping(ES_LOG_TYPE, XContentFactory.jsonBuilder().map(jsonMap)).version(2);
templateRequest.create(false);
indexClient.indices().putTemplate(templateRequest, RequestOptions.DEFAULT);
}
} catch (IOException e) {
logger.log(Level.WARNING, "Failed to create template for index " + ES_LOG_TYPE, e);
}
}
use of org.elasticsearch.client.indices.GetIndexTemplatesRequest in project hopsworks by logicalclocks.
the class ElasticClientController method templateGet.
public GetIndexTemplatesResponse templateGet(String template) throws ElasticException {
GetIndexTemplatesRequest request = new GetIndexTemplatesRequest(template);
FailableSupplier<GetIndexTemplatesResponse> query = () -> client.getClient().indices().getIndexTemplate(request, RequestOptions.DEFAULT);
return executeElasticQuery(query, "elastic get template", request.toString());
}
use of org.elasticsearch.client.indices.GetIndexTemplatesRequest in project spring-data-elasticsearch by spring-projects.
the class RequestConverters method getTemplates.
public static Request getTemplates(GetIndexTemplatesRequest getIndexTemplatesRequest) {
final String endpoint = new RequestConverters.EndpointBuilder().addPathPartAsIs("_template").addCommaSeparatedPathParts(getIndexTemplatesRequest.names()).build();
final Request request = new Request(HttpGet.METHOD_NAME, endpoint);
RequestConverters.Params params = new RequestConverters.Params(request);
params.withLocal(getIndexTemplatesRequest.isLocal());
params.withMasterTimeout(getIndexTemplatesRequest.getMasterNodeTimeout());
return request;
}
Aggregations