Search in sources :

Example 21 with IndicesExistsRequest

use of org.elasticsearch.action.admin.indices.exists.indices.IndicesExistsRequest in project graylog2-server by Graylog2.

the class IndicesGetAllMessageFieldsTest method GetAllMessageFieldsForIndicesForNonexistingIndexShouldReturnEmptySet.

@Test
@UsingDataSet(loadStrategy = LoadStrategyEnum.DELETE_ALL)
public void GetAllMessageFieldsForIndicesForNonexistingIndexShouldReturnEmptySet() throws Exception {
    final String[] indexNames = new String[] { "graylog_0" };
    final IndicesExistsResponse response = client.admin().indices().exists(new IndicesExistsRequest("graylog_0")).get();
    assertThat(response.isExists()).isFalse();
    final Map<String, Set<String>> result = indices.getAllMessageFieldsForIndices(indexNames);
    assertThat(result).isNotNull().isEmpty();
}
Also used : Set(java.util.Set) UsingDataSet(com.lordofthejars.nosqlunit.annotation.UsingDataSet) IndicesExistsResponse(org.elasticsearch.action.admin.indices.exists.indices.IndicesExistsResponse) IndicesExistsRequest(org.elasticsearch.action.admin.indices.exists.indices.IndicesExistsRequest) UsingDataSet(com.lordofthejars.nosqlunit.annotation.UsingDataSet) Test(org.junit.Test)

Example 22 with IndicesExistsRequest

use of org.elasticsearch.action.admin.indices.exists.indices.IndicesExistsRequest in project beam by apache.

the class ElasticSearchIOTestUtils method deleteIndex.

/** Deletes the given index synchronously. */
static void deleteIndex(String index, Client client) throws Exception {
    IndicesAdminClient indices = client.admin().indices();
    IndicesExistsResponse indicesExistsResponse = indices.exists(new IndicesExistsRequest(index)).get();
    if (indicesExistsResponse.isExists()) {
        indices.prepareClose(index).get();
        indices.delete(Requests.deleteIndexRequest(index)).get();
    }
}
Also used : IndicesExistsResponse(org.elasticsearch.action.admin.indices.exists.indices.IndicesExistsResponse) IndicesAdminClient(org.elasticsearch.client.IndicesAdminClient) IndicesExistsRequest(org.elasticsearch.action.admin.indices.exists.indices.IndicesExistsRequest)

Example 23 with IndicesExistsRequest

use of org.elasticsearch.action.admin.indices.exists.indices.IndicesExistsRequest 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 24 with IndicesExistsRequest

use of org.elasticsearch.action.admin.indices.exists.indices.IndicesExistsRequest 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 25 with IndicesExistsRequest

use of org.elasticsearch.action.admin.indices.exists.indices.IndicesExistsRequest 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)

Aggregations

IndicesExistsRequest (org.elasticsearch.action.admin.indices.exists.indices.IndicesExistsRequest)34 Test (org.junit.Test)13 CreateIndexRequest (org.elasticsearch.action.admin.indices.create.CreateIndexRequest)12 DeleteIndexRequest (org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest)12 IOException (java.io.IOException)10 DeleteIndexResponse (org.elasticsearch.action.admin.indices.delete.DeleteIndexResponse)10 IndicesExistsResponse (org.elasticsearch.action.admin.indices.exists.indices.IndicesExistsResponse)10 PutMappingResponse (org.elasticsearch.action.admin.indices.mapping.put.PutMappingResponse)9 Matchers.containsString (org.hamcrest.Matchers.containsString)9 UsingDataSet (com.lordofthejars.nosqlunit.annotation.UsingDataSet)2 SQLTransportIntegrationTest (io.crate.integrationtests.SQLTransportIntegrationTest)2 PartitionName (io.crate.metadata.PartitionName)2 BytesRef (org.apache.lucene.util.BytesRef)2 CreateIndexResponse (org.elasticsearch.action.admin.indices.create.CreateIndexResponse)2 TypesExistsRequest (org.elasticsearch.action.admin.indices.exists.types.TypesExistsRequest)2 IndicesAdminClient (org.elasticsearch.client.IndicesAdminClient)2 FailedException (backtype.storm.topology.FailedException)1 ConsoleLogger (com.creditease.monitor.log.ConsoleLogger)1 TransportHookProxy (com.creditease.uav.hook.esclient.transport.TransportHookProxy)1 TitanException (com.thinkaurelius.titan.core.TitanException)1