Search in sources :

Example 21 with DeleteIndexRequest

use of org.graylog.shaded.elasticsearch7.org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest in project syncope by apache.

the class ElasticsearchReindex method doExecute.

@Override
protected String doExecute(final boolean dryRun) throws JobExecutionException {
    if (!dryRun) {
        try {
            LOG.debug("Start rebuild index {}", AuthContextUtils.getDomain().toLowerCase());
            IndicesExistsResponse existsIndexResponse = client.admin().indices().exists(new IndicesExistsRequest(AuthContextUtils.getDomain().toLowerCase())).get();
            if (existsIndexResponse.isExists()) {
                DeleteIndexResponse deleteIndexResponse = client.admin().indices().delete(new DeleteIndexRequest(AuthContextUtils.getDomain().toLowerCase())).get();
                LOG.debug("Successfully removed {}: {}", AuthContextUtils.getDomain().toLowerCase(), deleteIndexResponse);
            }
            XContentBuilder settings = XContentFactory.jsonBuilder().startObject().startObject("analysis").startObject("analyzer").startObject("string_lowercase").field("type", "custom").field("tokenizer", "standard").field("filter").startArray().value("lowercase").endArray().endObject().endObject().endObject().endObject();
            XContentBuilder mapping = XContentFactory.jsonBuilder().startObject().startArray("dynamic_templates").startObject().startObject("strings").field("match_mapping_type", "string").startObject("mapping").field("type", "keyword").field("analyzer", "string_lowercase").endObject().endObject().endObject().endArray().endObject();
            CreateIndexResponse createIndexResponse = client.admin().indices().create(new CreateIndexRequest(AuthContextUtils.getDomain().toLowerCase()).settings(settings).mapping(AnyTypeKind.USER.name(), mapping).mapping(AnyTypeKind.GROUP.name(), mapping).mapping(AnyTypeKind.ANY_OBJECT.name(), mapping)).get();
            LOG.debug("Successfully created {}: {}", AuthContextUtils.getDomain().toLowerCase(), createIndexResponse);
            LOG.debug("Indexing users...");
            for (int page = 1; page <= (userDAO.count() / AnyDAO.DEFAULT_PAGE_SIZE) + 1; page++) {
                for (User user : userDAO.findAll(page, AnyDAO.DEFAULT_PAGE_SIZE)) {
                    IndexResponse response = client.prepareIndex(AuthContextUtils.getDomain().toLowerCase(), AnyTypeKind.USER.name(), user.getKey()).setSource(elasticsearchUtils.builder(user)).get();
                    LOG.debug("Index successfully created for {}: {}", user, response);
                }
            }
            LOG.debug("Indexing groups...");
            for (int page = 1; page <= (groupDAO.count() / AnyDAO.DEFAULT_PAGE_SIZE) + 1; page++) {
                for (Group group : groupDAO.findAll(page, AnyDAO.DEFAULT_PAGE_SIZE)) {
                    IndexResponse response = client.prepareIndex(AuthContextUtils.getDomain().toLowerCase(), AnyTypeKind.GROUP.name(), group.getKey()).setSource(elasticsearchUtils.builder(group)).get();
                    LOG.debug("Index successfully created for {}: {}", group, response);
                }
            }
            LOG.debug("Indexing any objects...");
            for (int page = 1; page <= (anyObjectDAO.count() / AnyDAO.DEFAULT_PAGE_SIZE) + 1; page++) {
                for (AnyObject anyObject : anyObjectDAO.findAll(page, AnyDAO.DEFAULT_PAGE_SIZE)) {
                    IndexResponse response = client.prepareIndex(AuthContextUtils.getDomain().toLowerCase(), AnyTypeKind.ANY_OBJECT.name(), anyObject.getKey()).setSource(elasticsearchUtils.builder(anyObject)).get();
                    LOG.debug("Index successfully created for {}: {}", anyObject, response);
                }
            }
            LOG.debug("Rebuild index {} successfully completed", AuthContextUtils.getDomain().toLowerCase());
        } catch (Exception e) {
            throw new JobExecutionException("While rebuilding index " + AuthContextUtils.getDomain().toLowerCase(), e);
        }
    }
    return "SUCCESS";
}
Also used : Group(org.apache.syncope.core.persistence.api.entity.group.Group) User(org.apache.syncope.core.persistence.api.entity.user.User) DeleteIndexRequest(org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest) JobExecutionException(org.quartz.JobExecutionException) DeleteIndexResponse(org.elasticsearch.action.admin.indices.delete.DeleteIndexResponse) AnyObject(org.apache.syncope.core.persistence.api.entity.anyobject.AnyObject) JobExecutionException(org.quartz.JobExecutionException) DeleteIndexResponse(org.elasticsearch.action.admin.indices.delete.DeleteIndexResponse) CreateIndexResponse(org.elasticsearch.action.admin.indices.create.CreateIndexResponse) IndexResponse(org.elasticsearch.action.index.IndexResponse) IndicesExistsResponse(org.elasticsearch.action.admin.indices.exists.indices.IndicesExistsResponse) CreateIndexResponse(org.elasticsearch.action.admin.indices.create.CreateIndexResponse) CreateIndexRequest(org.elasticsearch.action.admin.indices.create.CreateIndexRequest) IndicesExistsRequest(org.elasticsearch.action.admin.indices.exists.indices.IndicesExistsRequest) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder)

Example 22 with DeleteIndexRequest

use of org.graylog.shaded.elasticsearch7.org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest in project ecs-dashboard by carone1.

the class ElasticBillingDAO method initBillingNamespaceIndex.

// =======================
// Private methods
// =======================
// ===========================
// Billing namespace methods
// ===========================
private void initBillingNamespaceIndex(Date collectionTime) {
    String collectionDayString = DATA_DATE_FORMAT.format(collectionTime);
    billingNamespaceIndexDayName = BILLING_NAMESPACE_INDEX_NAME + "-" + collectionDayString;
    if (elasticClient.admin().indices().exists(new IndicesExistsRequest(billingNamespaceIndexDayName)).actionGet().isExists()) {
        // Index already exists need to truncate it and recreate it
        DeleteIndexRequest deleteIndexRequest = new DeleteIndexRequest(billingNamespaceIndexDayName);
        ActionFuture<DeleteIndexResponse> futureResult = elasticClient.admin().indices().delete(deleteIndexRequest);
        // Wait until deletion is done
        while (!futureResult.isDone()) {
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }
    elasticClient.admin().indices().create(new CreateIndexRequest(billingNamespaceIndexDayName)).actionGet();
    try {
        PutMappingResponse putMappingResponse = elasticClient.admin().indices().preparePutMapping(billingNamespaceIndexDayName).setType(BILLING_NAMESPACE_INDEX_TYPE).setSource(XContentFactory.jsonBuilder().prettyPrint().startObject().startObject(BILLING_NAMESPACE_INDEX_TYPE).startObject("properties").startObject(NamespaceBillingInfo.TOTAL_SIZE_TAG).field("type", "long").endObject().startObject(NamespaceBillingInfo.TOTAL_SIZE_UNIT_TAG).field("type", "string").field("index", NOT_ANALYZED_INDEX).endObject().startObject(NamespaceBillingInfo.TOTAL_OBJECTS_TAG).field("type", "long").endObject().startObject(NamespaceBillingInfo.NAMESPACE_TAG).field("type", "string").field("index", NOT_ANALYZED_INDEX).endObject().startObject(NamespaceBillingInfo.NAMESPACE_TAG + ANALYZED_TAG).field("type", "string").field("index", ANALYZED_INDEX).endObject().startObject(COLLECTION_TIME).field("type", "date").field("format", "strict_date_optional_time||epoch_millis").endObject().endObject().startArray("dynamic_templates").startObject().startObject("notanalyzed").field("match", "*").field("match_mapping_type", "string").startObject("mapping").field("type", "string").field("index", NOT_ANALYZED_INDEX).endObject().endObject().endObject().endArray().endObject().endObject()).execute().actionGet();
        if (putMappingResponse.isAcknowledged()) {
            LOGGER.info("Index Created: " + billingNamespaceIndexDayName);
        } else {
            LOGGER.error("Index {" + billingNamespaceIndexDayName + "} did not exist. " + "While attempting to create the index in ElasticSearch " + "Templates we were unable to get an acknowledgement.", billingNamespaceIndexDayName);
            LOGGER.error("Error Message: {}", putMappingResponse.toString());
            throw new RuntimeException("Unable to create index " + billingNamespaceIndexDayName);
        }
    } catch (IOException e) {
        throw new RuntimeException("Unable to create index " + billingNamespaceIndexDayName + " " + e.getMessage());
    }
}
Also used : DeleteIndexResponse(org.elasticsearch.action.admin.indices.delete.DeleteIndexResponse) DeleteIndexRequest(org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest) PutMappingResponse(org.elasticsearch.action.admin.indices.mapping.put.PutMappingResponse) IOException(java.io.IOException) CreateIndexRequest(org.elasticsearch.action.admin.indices.create.CreateIndexRequest) IndicesExistsRequest(org.elasticsearch.action.admin.indices.exists.indices.IndicesExistsRequest)

Example 23 with DeleteIndexRequest

use of org.graylog.shaded.elasticsearch7.org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest in project ecs-dashboard by carone1.

the class ElasticBucketOwnerDAO method initBucketOwnerIndex.

private void initBucketOwnerIndex(Date collectionTime) {
    String collectionDayString = DATA_DATE_FORMAT.format(collectionTime);
    bucketownerIndexDayName = BUCKET_OWNER_INDEX_NAME + "-" + collectionDayString;
    if (elasticClient.admin().indices().exists(new IndicesExistsRequest(bucketownerIndexDayName)).actionGet().isExists()) {
        // Index already exists need to truncate it and recreate it
        DeleteIndexRequest deleteIndexRequest = new DeleteIndexRequest(bucketownerIndexDayName);
        ActionFuture<DeleteIndexResponse> futureResult = elasticClient.admin().indices().delete(deleteIndexRequest);
        // Wait until deletion is done
        while (!futureResult.isDone()) {
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
    elasticClient.admin().indices().create(new CreateIndexRequest(bucketownerIndexDayName)).actionGet();
    try {
        PutMappingResponse putMappingResponse = elasticClient.admin().indices().preparePutMapping(bucketownerIndexDayName).setType(BUCKET_OWNER_INDEX_TYPE).setSource(XContentFactory.jsonBuilder().prettyPrint().startObject().startObject(BUCKET_OWNER_INDEX_TYPE).startObject("properties").startObject(BucketOwner.VDC_ID).field("type", "string").field("index", NOT_ANALYZED_INDEX).endObject().startObject(BucketOwner.BUCKET_KEY).field("type", "string").field("index", NOT_ANALYZED_INDEX).endObject().startObject(COLLECTION_TIME).field("type", "date").field("format", "strict_date_optional_time||epoch_millis").endObject().endObject().startArray("dynamic_templates").startObject().startObject("notanalyzed").field("match", "*").field("match_mapping_type", "string").startObject("mapping").field("type", "string").field("index", NOT_ANALYZED_INDEX).endObject().endObject().endObject().endArray().endObject().endObject()).execute().actionGet();
        if (putMappingResponse.isAcknowledged()) {
            LOGGER.info("Index Created: " + bucketownerIndexDayName);
        } else {
            LOGGER.error("Index {" + bucketownerIndexDayName + "} did not exist. " + "While attempting to create the index in ElasticSearch " + "Templates we were unable to get an acknowledgement.", bucketownerIndexDayName);
            LOGGER.error("Error Message: {}", putMappingResponse.toString());
            throw new RuntimeException("Unable to create index " + bucketownerIndexDayName);
        }
    } catch (IOException e) {
        throw new RuntimeException("Unable to create index " + bucketownerIndexDayName + " " + e.getMessage());
    }
}
Also used : DeleteIndexResponse(org.elasticsearch.action.admin.indices.delete.DeleteIndexResponse) DeleteIndexRequest(org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest) PutMappingResponse(org.elasticsearch.action.admin.indices.mapping.put.PutMappingResponse) IOException(java.io.IOException) CreateIndexRequest(org.elasticsearch.action.admin.indices.create.CreateIndexRequest) IndicesExistsRequest(org.elasticsearch.action.admin.indices.exists.indices.IndicesExistsRequest)

Example 24 with DeleteIndexRequest

use of org.graylog.shaded.elasticsearch7.org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest in project ecs-dashboard by carone1.

the class ElasticIndexCleaner method truncateOldIndexes.

public static void truncateOldIndexes(TransportClient elasticClient, Date thresholdDate, String indexName, String indexType) {
    List<String> indexesToDelete = new ArrayList<String>();
    ImmutableOpenMap<String, IndexMetaData> indices = elasticClient.admin().cluster().prepareState().get().getState().getMetaData().getIndices();
    Iterator<String> itr = indices.keysIt();
    while (itr.hasNext()) {
        String idxName = itr.next();
        // only process index that are matching the main index name
        if (idxName.startsWith(indexName)) {
            // stripping out index name portion
            String dateString = idxName.replaceAll(indexName + "-", "");
            // assumption: string is following this format YYYY-MM-DD
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            try {
                // create date out of the constructed date
                Date indexDate = sdf.parse(dateString);
                // than threshold date
                if (indexDate.before(thresholdDate) || indexDate.equals(thresholdDate)) {
                    // Looks like this index is older than our
                    // threshold date
                    // index is marked for deletion deletion
                    indexesToDelete.add(idxName);
                }
            } catch (ParseException e) {
                LOGGER.error("Issue encountered when parsing index name: " + idxName + " error:" + e.getMessage());
            }
        }
    }
    // Delete identified indexes
    IndicesAdminClient indicesAdminClient = elasticClient.admin().indices();
    for (String indexToDelete : indexesToDelete) {
        DeleteIndexRequest deleteRequest = new DeleteIndexRequest(indexToDelete);
        indicesAdminClient.delete(deleteRequest);
    }
}
Also used : ArrayList(java.util.ArrayList) DeleteIndexRequest(org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest) IndicesAdminClient(org.elasticsearch.client.IndicesAdminClient) ParseException(java.text.ParseException) SimpleDateFormat(java.text.SimpleDateFormat) Date(java.util.Date) IndexMetaData(org.elasticsearch.cluster.metadata.IndexMetaData)

Example 25 with DeleteIndexRequest

use of org.graylog.shaded.elasticsearch7.org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest in project ecs-dashboard by carone1.

the class ElasticNamespaceDetailDAO method initNamespaceDetailIndex.

private void initNamespaceDetailIndex(Date collectionTime) {
    String collectionDayString = DATA_DATE_FORMAT.format(collectionTime);
    namespacedetailIndexDayName = NAMESPACE_DETAIL_INDEX_NAME + "-" + collectionDayString;
    if (elasticClient.admin().indices().exists(new IndicesExistsRequest(namespacedetailIndexDayName)).actionGet().isExists()) {
        // Index already exists need to truncate it and recreate it
        DeleteIndexRequest deleteIndexRequest = new DeleteIndexRequest(namespacedetailIndexDayName);
        ActionFuture<DeleteIndexResponse> futureResult = elasticClient.admin().indices().delete(deleteIndexRequest);
        // Wait until deletion is done
        while (!futureResult.isDone()) {
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
    elasticClient.admin().indices().create(new CreateIndexRequest(namespacedetailIndexDayName)).actionGet();
    try {
        PutMappingResponse putMappingResponse = elasticClient.admin().indices().preparePutMapping(namespacedetailIndexDayName).setType(DETAIL_NAMESPACE_INDEX_TYPE).setSource(XContentFactory.jsonBuilder().prettyPrint().startObject().startObject(DETAIL_NAMESPACE_INDEX_TYPE).startObject("properties").startObject(NamespaceDetail.ID).field("type", "string").field("index", NOT_ANALYZED_INDEX).endObject().startObject(NamespaceDetail.LINK).field("type", "string").endObject().startObject(NamespaceDetail.NAME).field("type", "string").field("index", NOT_ANALYZED_INDEX).endObject().startObject(NamespaceDetail.NAME + ANALYZED_TAG).field("type", "string").field("index", ANALYZED_INDEX).endObject().startObject(NamespaceDetail.DEFAULT_DATA_SERVCIES_VPOOL).field("type", "string").field("index", NOT_ANALYZED_INDEX).endObject().startObject(NamespaceDetail.ALLOWED_VPOOLS_LIST).field("type", "string").field("index", NOT_ANALYZED_INDEX).endObject().startObject(NamespaceDetail.DISALLOWED_VPOOLS_LIST).field("type", "string").field("index", NOT_ANALYZED_INDEX).endObject().startObject(NamespaceDetail.NAMESPACE_ADMINS).field("type", "string").field("index", NOT_ANALYZED_INDEX).endObject().startObject(NamespaceDetail.IS_ENCRYPTION_ENABLED).field("type", "boolean").endObject().startObject(NamespaceDetail.DEFAULT_BUCKET_BLOCK_SIZE).field("type", "long").endObject().startObject(NamespaceDetail.USER_MAPPING).field("type", "string").endObject().startObject(NamespaceDetail.IS_STALE_ALLOWED).field("type", "boolean").endObject().startObject(NamespaceDetail.IS_COMPLIANCE_ENABLED).field("type", "boolean").endObject().startObject(NamespaceDetail.EXTERNAL_GROUP_ADMINS).field("type", "string").endObject().startObject(NamespaceDetail.GLOBAL).field("type", "boolean").endObject().startObject(NamespaceDetail.INACTIVE).field("type", "boolean").endObject().startObject(NamespaceDetail.REMOTE).field("type", "boolean").endObject().startObject(NamespaceDetail.INTERNAL).field("type", "boolean").endObject().startObject(NamespaceDetail.VDC).field("type", "string").endObject().startObject(NamespaceDetail.CREATION_TIME).field("type", "date").field("format", "strict_date_optional_time||epoch_millis").endObject().startObject(COLLECTION_TIME).field("type", "date").field("format", "strict_date_optional_time||epoch_millis").endObject().endObject().startArray("dynamic_templates").startObject().startObject("notanalyzed").field("match", "*").field("match_mapping_type", "string").startObject("mapping").field("type", "string").field("index", NOT_ANALYZED_INDEX).endObject().endObject().endObject().endArray().endObject().endObject()).execute().actionGet();
        if (putMappingResponse.isAcknowledged()) {
            LOGGER.info("Index Created: " + namespacedetailIndexDayName);
        } else {
            LOGGER.error("Index {" + namespacedetailIndexDayName + "} did not exist. " + "While attempting to create the index in ElasticSearch " + "Templates we were unable to get an acknowledgement.", namespacedetailIndexDayName);
            LOGGER.error("Error Message: {}", putMappingResponse.toString());
            throw new RuntimeException("Unable to create index " + namespacedetailIndexDayName);
        }
    } catch (IOException e) {
        throw new RuntimeException("Unable to create index " + namespacedetailIndexDayName + " " + e.getMessage());
    }
}
Also used : DeleteIndexResponse(org.elasticsearch.action.admin.indices.delete.DeleteIndexResponse) DeleteIndexRequest(org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest) PutMappingResponse(org.elasticsearch.action.admin.indices.mapping.put.PutMappingResponse) IOException(java.io.IOException) CreateIndexRequest(org.elasticsearch.action.admin.indices.create.CreateIndexRequest) IndicesExistsRequest(org.elasticsearch.action.admin.indices.exists.indices.IndicesExistsRequest)

Aggregations

DeleteIndexRequest (org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest)37 IOException (java.io.IOException)15 DeleteIndexResponse (org.elasticsearch.action.admin.indices.delete.DeleteIndexResponse)15 CreateIndexRequest (org.elasticsearch.action.admin.indices.create.CreateIndexRequest)13 IndicesExistsRequest (org.elasticsearch.action.admin.indices.exists.indices.IndicesExistsRequest)12 PutMappingResponse (org.elasticsearch.action.admin.indices.mapping.put.PutMappingResponse)9 ArrayList (java.util.ArrayList)6 AcknowledgedResponse (org.elasticsearch.action.support.master.AcknowledgedResponse)6 List (java.util.List)5 Row1 (io.crate.data.Row1)3 OneRowActionListener (io.crate.execution.support.OneRowActionListener)3 BulkRequest (org.elasticsearch.action.bulk.BulkRequest)3 MultiGetRequest (org.elasticsearch.action.get.MultiGetRequest)3 MultiSearchRequest (org.elasticsearch.action.search.MultiSearchRequest)3 SearchRequest (org.elasticsearch.action.search.SearchRequest)3 FutureActionListener (io.crate.action.FutureActionListener)2 Row (io.crate.data.Row)2 RowConsumer (io.crate.data.RowConsumer)2 DependencyCarrier (io.crate.planner.DependencyCarrier)2 Plan (io.crate.planner.Plan)2