Search in sources :

Example 1 with GetIndexTemplatesResponse

use of org.elasticsearch.client.indices.GetIndexTemplatesResponse in project graylog2-server by Graylog2.

the class ClientES7 method templateExists.

@Override
public boolean templateExists(String templateName) {
    final GetIndexTemplatesRequest request = new GetIndexTemplatesRequest("*");
    final GetIndexTemplatesResponse result = client.execute((c, requestOptions) -> c.indices().getIndexTemplate(request, requestOptions));
    return result.getIndexTemplates().stream().anyMatch(indexTemplate -> indexTemplate.name().equals(templateName));
}
Also used : GetIndexTemplatesResponse(org.graylog.shaded.elasticsearch7.org.elasticsearch.client.indices.GetIndexTemplatesResponse) GetIndexTemplatesRequest(org.graylog.shaded.elasticsearch7.org.elasticsearch.client.indices.GetIndexTemplatesRequest)

Example 2 with GetIndexTemplatesResponse

use of org.elasticsearch.client.indices.GetIndexTemplatesResponse in project spring-data-elasticsearch by spring-projects.

the class RestIndexTemplate method getTemplate.

@Override
public TemplateData getTemplate(GetTemplateRequest getTemplateRequest) {
    Assert.notNull(getTemplateRequest, "getTemplateRequest must not be null");
    // getIndexTemplate throws an error on non-existing template names
    if (!existsTemplate(new ExistsTemplateRequest(getTemplateRequest.getTemplateName()))) {
        return null;
    }
    GetIndexTemplatesRequest getIndexTemplatesRequest = requestFactory.getIndexTemplatesRequest(getTemplateRequest);
    GetIndexTemplatesResponse getIndexTemplatesResponse = restTemplate.execute(client -> client.indices().getIndexTemplate(getIndexTemplatesRequest, RequestOptions.DEFAULT));
    return ResponseConverter.getTemplateData(getIndexTemplatesResponse, getTemplateRequest.getTemplateName());
}
Also used : GetIndexTemplatesResponse(org.elasticsearch.client.indices.GetIndexTemplatesResponse) ExistsTemplateRequest(org.springframework.data.elasticsearch.core.index.ExistsTemplateRequest) GetIndexTemplatesRequest(org.elasticsearch.client.indices.GetIndexTemplatesRequest)

Example 3 with GetIndexTemplatesResponse

use of org.elasticsearch.client.indices.GetIndexTemplatesResponse in project hopsworks by logicalclocks.

the class PyPiLibraryElasticIndexer method execute.

@Timeout
@TransactionAttribute(TransactionAttributeType.NOT_SUPPORTED)
public void execute(Timer timer) throws ElasticException {
    long errorRescheduleTimeout = 600000;
    LOGGER.log(Level.INFO, "Running PyPi Indexer");
    try {
        ClusterHealthResponse clusterHealthResponse = elasticClientCtrl.clusterHealthGet();
        GetIndexTemplatesResponse templateResponse = elasticClientCtrl.templateGet(Settings.ELASTIC_PYPI_LIBRARIES_ALIAS);
        // If elasticsearch is down or template not in elastic reschedule the timer
        if (clusterHealthResponse.getStatus().equals(ClusterHealthStatus.RED)) {
            scheduleTimer(errorRescheduleTimeout);
            LOGGER.log(Level.INFO, "Elastic currently down, rescheduling indexing for pypi libraries");
            return;
        } else if (templateResponse.getIndexTemplates().isEmpty()) {
            scheduleTimer(errorRescheduleTimeout);
            LOGGER.log(Level.INFO, "Elastic template " + Settings.ELASTIC_PYPI_LIBRARIES_ALIAS + " currently missing, " + "rescheduling indexing for pypi libraries");
            return;
        }
    } catch (Exception e) {
        scheduleTimer(errorRescheduleTimeout);
        LOGGER.log(Level.SEVERE, "Exception occurred trying to index pypi libraries, rescheduling timer", e);
        return;
    }
    String newIndex = Settings.ELASTIC_PYPI_LIBRARIES_INDEX_PATTERN_PREFIX + System.currentTimeMillis();
    try {
        GetAliasesResponse pypiAlias = elasticClientCtrl.getAliases(Settings.ELASTIC_PYPI_LIBRARIES_ALIAS);
        if (!pypiAlias.getAliases().isEmpty()) {
            this.isIndexed = true;
        }
        String[] indicesToDelete = elasticClientCtrl.mngIndicesGetBySimplifiedRegex(Settings.ELASTIC_PYPI_LIBRARIES_INDEX_REGEX);
        Element body = Jsoup.connect(settings.getPyPiSimpleEndpoint()).maxBodySize(0).get().body();
        Elements elements = body.getElementsByTag("a");
        CreateIndexRequest createIndexRequest = new CreateIndexRequest(newIndex);
        elasticClientCtrl.mngIndexCreate(createIndexRequest);
        final int bulkSize = 100;
        int currentBulkSize = 0;
        int currentId = 0;
        BulkRequest bulkRequest = new BulkRequest();
        LOGGER.log(Level.INFO, "Starting to index libraries from pypi simple index");
        for (Element library : elements) {
            IndexRequest indexRequest = new IndexRequest().index(newIndex).id(String.valueOf(currentId)).source(jsonBuilder().startObject().field("library", library.text()).endObject());
            bulkRequest.add(indexRequest);
            currentBulkSize += 1;
            currentId += 1;
            if (currentBulkSize == bulkSize) {
                elasticClientCtrl.bulkUpdateDoc(bulkRequest);
                bulkRequest = new BulkRequest();
                currentBulkSize = 0;
            }
        }
        // Also send last batch
        if (bulkRequest.numberOfActions() > 0) {
            elasticClientCtrl.bulkUpdateDoc(bulkRequest);
        }
        if (pypiAlias.getAliases().isEmpty()) {
            elasticClientCtrl.createAlias(Settings.ELASTIC_PYPI_LIBRARIES_ALIAS, newIndex);
        } else {
            String currentSearchIndex = pypiAlias.getAliases().keySet().iterator().next();
            elasticClientCtrl.aliasSwitchIndex(Settings.ELASTIC_PYPI_LIBRARIES_ALIAS, currentSearchIndex, newIndex);
        }
        this.isIndexed = true;
        LOGGER.log(Level.INFO, "Finished indexing");
        for (String index : indicesToDelete) {
            DeleteIndexRequest deleteIndexRequest = new DeleteIndexRequest().indices(index);
            elasticClientCtrl.mngIndexDelete(deleteIndexRequest);
        }
    } catch (Exception ex) {
        LOGGER.log(Level.SEVERE, "Indexing pypi libraries failed", ex);
        scheduleTimer(errorRescheduleTimeout);
        if (elasticClientCtrl.mngIndexExists(newIndex)) {
            DeleteIndexRequest deleteIndexRequest = new DeleteIndexRequest().indices(newIndex);
            elasticClientCtrl.mngIndexDelete(deleteIndexRequest);
        }
        return;
    }
    String rawInterval = settings.getPyPiIndexerTimerInterval();
    Long intervalValue = settings.getConfTimeValue(rawInterval);
    TimeUnit intervalTimeunit = settings.getConfTimeTimeUnit(rawInterval);
    scheduleTimer(intervalTimeunit.toMillis(intervalValue));
}
Also used : ClusterHealthResponse(org.elasticsearch.action.admin.cluster.health.ClusterHealthResponse) GetAliasesResponse(org.elasticsearch.client.GetAliasesResponse) Element(org.jsoup.nodes.Element) DeleteIndexRequest(org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest) Elements(org.jsoup.select.Elements) IndexRequest(org.elasticsearch.action.index.IndexRequest) DeleteIndexRequest(org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest) CreateIndexRequest(org.elasticsearch.client.indices.CreateIndexRequest) ElasticException(io.hops.hopsworks.exceptions.ElasticException) GetIndexTemplatesResponse(org.elasticsearch.client.indices.GetIndexTemplatesResponse) BulkRequest(org.elasticsearch.action.bulk.BulkRequest) TimeUnit(java.util.concurrent.TimeUnit) CreateIndexRequest(org.elasticsearch.client.indices.CreateIndexRequest) TransactionAttribute(javax.ejb.TransactionAttribute) Timeout(javax.ejb.Timeout)

Example 4 with GetIndexTemplatesResponse

use of org.elasticsearch.client.indices.GetIndexTemplatesResponse in project graylog2-server by Graylog2.

the class ClientES7 method existingTemplates.

private String[] existingTemplates() {
    final GetIndexTemplatesRequest getIndexTemplatesRequest = new GetIndexTemplatesRequest();
    final GetIndexTemplatesResponse result = client.execute((c, requestOptions) -> c.indices().getIndexTemplate(getIndexTemplatesRequest, requestOptions));
    return result.getIndexTemplates().stream().map(IndexTemplateMetadata::name).toArray(String[]::new);
}
Also used : GetIndexTemplatesResponse(org.graylog.shaded.elasticsearch7.org.elasticsearch.client.indices.GetIndexTemplatesResponse) GetIndexTemplatesRequest(org.graylog.shaded.elasticsearch7.org.elasticsearch.client.indices.GetIndexTemplatesRequest)

Example 5 with GetIndexTemplatesResponse

use of org.elasticsearch.client.indices.GetIndexTemplatesResponse 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)

Aggregations

GetIndexTemplatesResponse (org.elasticsearch.client.indices.GetIndexTemplatesResponse)4 GetIndexTemplatesRequest (org.elasticsearch.client.indices.GetIndexTemplatesRequest)3 IndexRequest (org.elasticsearch.action.index.IndexRequest)2 GetIndexTemplatesRequest (org.graylog.shaded.elasticsearch7.org.elasticsearch.client.indices.GetIndexTemplatesRequest)2 GetIndexTemplatesResponse (org.graylog.shaded.elasticsearch7.org.elasticsearch.client.indices.GetIndexTemplatesResponse)2 TypeReference (com.fasterxml.jackson.core.type.TypeReference)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 ElasticException (io.hops.hopsworks.exceptions.ElasticException)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 TimeUnit (java.util.concurrent.TimeUnit)1 Level (java.util.logging.Level)1 Logger (java.util.logging.Logger)1