Search in sources :

Example 6 with GetIndexTemplatesRequest

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);
    }
}
Also used : RestClient(org.elasticsearch.client.RestClient) XContentFactory(org.elasticsearch.common.xcontent.XContentFactory) Arrays(java.util.Arrays) XContentType(org.elasticsearch.common.xcontent.XContentType) Property(org.phoebus.olog.entity.Property) URL(java.net.URL) URISyntaxException(java.net.URISyntaxException) PropertySource(org.springframework.context.annotation.PropertySource) GetIndexTemplatesResponse(org.elasticsearch.client.indices.GetIndexTemplatesResponse) Level(java.util.logging.Level) Value(org.springframework.beans.factory.annotation.Value) WriteRequest(org.elasticsearch.action.support.WriteRequest) IndexRequest(org.elasticsearch.action.index.IndexRequest) Settings(org.elasticsearch.common.settings.Settings) PutIndexTemplateRequest(org.elasticsearch.action.admin.indices.template.put.PutIndexTemplateRequest) Map(java.util.Map) CreateIndexRequest(org.elasticsearch.action.admin.indices.create.CreateIndexRequest) RequestOptions(org.elasticsearch.client.RequestOptions) IndexResponse(org.elasticsearch.action.index.IndexResponse) URI(java.net.URI) TypeReference(com.fasterxml.jackson.core.type.TypeReference) Tag(org.phoebus.olog.entity.Tag) Properties(java.util.Properties) GetRequest(org.elasticsearch.action.get.GetRequest) GetIndexRequest(org.elasticsearch.action.admin.indices.get.GetIndexRequest) GetIndexTemplatesRequest(org.elasticsearch.client.indices.GetIndexTemplatesRequest) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) IOException(java.io.IOException) Logger(java.util.logging.Logger) RestHighLevelClient(org.elasticsearch.client.RestHighLevelClient) ComponentScan(org.springframework.context.annotation.ComponentScan) Logbook(org.phoebus.olog.entity.Logbook) Configuration(org.springframework.context.annotation.Configuration) List(java.util.List) Bean(org.springframework.context.annotation.Bean) HttpHost(org.apache.http.HttpHost) InputStream(java.io.InputStream) InputStream(java.io.InputStream) GetIndexTemplatesResponse(org.elasticsearch.client.indices.GetIndexTemplatesResponse) GetIndexRequest(org.elasticsearch.action.admin.indices.get.GetIndexRequest) GetIndexTemplatesRequest(org.elasticsearch.client.indices.GetIndexTemplatesRequest) PutIndexTemplateRequest(org.elasticsearch.action.admin.indices.template.put.PutIndexTemplateRequest) IOException(java.io.IOException) CreateIndexRequest(org.elasticsearch.action.admin.indices.create.CreateIndexRequest) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Example 7 with GetIndexTemplatesRequest

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());
}
Also used : GetIndexTemplatesResponse(org.elasticsearch.client.indices.GetIndexTemplatesResponse) GetIndexTemplatesRequest(org.elasticsearch.client.indices.GetIndexTemplatesRequest)

Example 8 with GetIndexTemplatesRequest

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;
}
Also used : ExplainRequest(org.elasticsearch.action.explain.ExplainRequest) GetMappingsRequest(org.elasticsearch.action.admin.indices.mapping.get.GetMappingsRequest) SearchTemplateRequest(org.elasticsearch.script.mustache.SearchTemplateRequest) DeleteRequest(org.elasticsearch.action.delete.DeleteRequest) IndexRequest(org.elasticsearch.action.index.IndexRequest) DeleteByQueryRequest(org.elasticsearch.index.reindex.DeleteByQueryRequest) FieldCapabilitiesRequest(org.elasticsearch.action.fieldcaps.FieldCapabilitiesRequest) CountRequest(org.elasticsearch.client.core.CountRequest) IndexTemplatesExistRequest(org.elasticsearch.client.indices.IndexTemplatesExistRequest) GetStoredScriptRequest(org.elasticsearch.action.admin.cluster.storedscripts.GetStoredScriptRequest) GetRequest(org.elasticsearch.action.get.GetRequest) RankEvalRequest(org.elasticsearch.index.rankeval.RankEvalRequest) GetAliasesRequest(org.elasticsearch.action.admin.indices.alias.get.GetAliasesRequest) IndicesAliasesRequest(org.elasticsearch.action.admin.indices.alias.IndicesAliasesRequest) AbstractBulkByScrollRequest(org.elasticsearch.index.reindex.AbstractBulkByScrollRequest) DeleteIndexTemplateRequest(org.elasticsearch.action.admin.indices.template.delete.DeleteIndexTemplateRequest) ClusterHealthRequest(org.elasticsearch.action.admin.cluster.health.ClusterHealthRequest) GetSettingsRequest(org.elasticsearch.action.admin.indices.settings.get.GetSettingsRequest) PutStoredScriptRequest(org.elasticsearch.action.admin.cluster.storedscripts.PutStoredScriptRequest) ClearScrollRequest(org.elasticsearch.action.search.ClearScrollRequest) PutIndexTemplateRequest(org.elasticsearch.client.indices.PutIndexTemplateRequest) DeleteStoredScriptRequest(org.elasticsearch.action.admin.cluster.storedscripts.DeleteStoredScriptRequest) MultiGetRequest(org.elasticsearch.action.get.MultiGetRequest) SearchRequest(org.elasticsearch.action.search.SearchRequest) OpenIndexRequest(org.elasticsearch.action.admin.indices.open.OpenIndexRequest) MultiSearchRequest(org.elasticsearch.action.search.MultiSearchRequest) CreateIndexRequest(org.elasticsearch.action.admin.indices.create.CreateIndexRequest) RefreshRequest(org.elasticsearch.action.admin.indices.refresh.RefreshRequest) UpdateByQueryRequest(org.elasticsearch.index.reindex.UpdateByQueryRequest) DeleteIndexRequest(org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest) GetIndexRequest(org.elasticsearch.action.admin.indices.get.GetIndexRequest) GetIndexTemplatesRequest(org.elasticsearch.client.indices.GetIndexTemplatesRequest) GetFieldMappingsRequest(org.elasticsearch.client.indices.GetFieldMappingsRequest) UpdateRequest(org.elasticsearch.action.update.UpdateRequest) DocWriteRequest(org.elasticsearch.action.DocWriteRequest) Request(org.elasticsearch.client.Request) AnalyzeRequest(org.elasticsearch.client.indices.AnalyzeRequest) CloseIndexRequest(org.elasticsearch.action.admin.indices.close.CloseIndexRequest) RethrottleRequest(org.elasticsearch.client.RethrottleRequest) FlushRequest(org.elasticsearch.action.admin.indices.flush.FlushRequest) PutMappingRequest(org.elasticsearch.action.admin.indices.mapping.put.PutMappingRequest) SearchScrollRequest(org.elasticsearch.action.search.SearchScrollRequest) BulkRequest(org.elasticsearch.action.bulk.BulkRequest) ReindexRequest(org.elasticsearch.index.reindex.ReindexRequest)

Aggregations

GetIndexTemplatesRequest (org.elasticsearch.client.indices.GetIndexTemplatesRequest)6 GetIndexTemplatesResponse (org.elasticsearch.client.indices.GetIndexTemplatesResponse)3 CreateIndexRequest (org.elasticsearch.action.admin.indices.create.CreateIndexRequest)2 GetIndexRequest (org.elasticsearch.action.admin.indices.get.GetIndexRequest)2 GetRequest (org.elasticsearch.action.get.GetRequest)2 IndexRequest (org.elasticsearch.action.index.IndexRequest)2 TypeReference (com.fasterxml.jackson.core.type.TypeReference)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 URI (java.net.URI)1 URISyntaxException (java.net.URISyntaxException)1 URL (java.net.URL)1 Arrays (java.util.Arrays)1 List (java.util.List)1 Map (java.util.Map)1 Properties (java.util.Properties)1 Level (java.util.logging.Level)1 Logger (java.util.logging.Logger)1 HttpHost (org.apache.http.HttpHost)1