Search in sources :

Example 21 with IndicesAdminClient

use of org.elasticsearch.client.IndicesAdminClient in project incubator-skywalking by apache.

the class ElasticSearchClient method createIndex.

public boolean createIndex(String indexName, String indexType, Settings settings, XContentBuilder mappingBuilder) {
    if (!ready) {
        throw new ElasticSearchClientNotReadyException();
    }
    IndicesAdminClient adminClient = client.admin().indices();
    indexName = formatIndexName(indexName);
    CreateIndexResponse response = adminClient.prepareCreate(indexName).setSettings(settings).addMapping(indexType, mappingBuilder).get();
    logger.info("create {} index with type of {} finished, isAcknowledged: {}", indexName, indexType, response.isAcknowledged());
    return response.isShardsAcked();
}
Also used : IndicesAdminClient(org.elasticsearch.client.IndicesAdminClient) CreateIndexResponse(org.elasticsearch.action.admin.indices.create.CreateIndexResponse)

Example 22 with IndicesAdminClient

use of org.elasticsearch.client.IndicesAdminClient in project components by Talend.

the class ElasticsearchTestUtils method deleteIndex.

/**
 * Deletes an index and block until deletion is complete.
 *
 * @param index The index to delete
 * @param client The client which points to the Elasticsearch instance
 * @throws InterruptedException if blocking thread is interrupted or index existence check failed
 * @throws java.util.concurrent.ExecutionException if index existence check failed
 * @throws IOException if deletion failed
 */
static void deleteIndex(String index, Client client) throws InterruptedException, java.util.concurrent.ExecutionException, IOException {
    IndicesAdminClient indices = client.admin().indices();
    IndicesExistsResponse indicesExistsResponse = indices.exists(new IndicesExistsRequest(index)).get();
    if (indicesExistsResponse.isExists()) {
        indices.prepareClose(index).get();
        // delete index is an asynchronous request, neither refresh or upgrade
        // delete all docs before starting tests. WaitForYellow() and delete directory are too slow,
        // so block thread until it is done (make it synchronous!!!)
        AtomicBoolean indexDeleted = new AtomicBoolean(false);
        AtomicBoolean waitForIndexDeletion = new AtomicBoolean(true);
        indices.delete(Requests.deleteIndexRequest(index), new DeleteActionListener(indexDeleted, waitForIndexDeletion));
        while (waitForIndexDeletion.get()) {
            Thread.sleep(100);
        }
        if (!indexDeleted.get()) {
            throw new IOException("Failed to delete index " + index);
        }
    }
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) IndicesExistsResponse(org.elasticsearch.action.admin.indices.exists.indices.IndicesExistsResponse) IndicesAdminClient(org.elasticsearch.client.IndicesAdminClient) IOException(java.io.IOException) IndicesExistsRequest(org.elasticsearch.action.admin.indices.exists.indices.IndicesExistsRequest)

Example 23 with IndicesAdminClient

use of org.elasticsearch.client.IndicesAdminClient in project play2-elasticsearch by cleverage.

the class IndexService method existsIndex.

/**
 * Test if an indice Exists
 * @return true if exists
 */
public static boolean existsIndex(String indexName) {
    Client client = IndexClient.client;
    AdminClient admin = client.admin();
    IndicesAdminClient indices = admin.indices();
    IndicesExistsRequestBuilder indicesExistsRequestBuilder = indices.prepareExists(indexName);
    IndicesExistsResponse response = indicesExistsRequestBuilder.execute().actionGet();
    return response.isExists();
}
Also used : IndicesExistsRequestBuilder(org.elasticsearch.action.admin.indices.exists.indices.IndicesExistsRequestBuilder) IndicesExistsResponse(org.elasticsearch.action.admin.indices.exists.indices.IndicesExistsResponse) IndicesAdminClient(org.elasticsearch.client.IndicesAdminClient) Client(org.elasticsearch.client.Client) IndicesAdminClient(org.elasticsearch.client.IndicesAdminClient) AdminClient(org.elasticsearch.client.AdminClient) IndicesAdminClient(org.elasticsearch.client.IndicesAdminClient) AdminClient(org.elasticsearch.client.AdminClient)

Example 24 with IndicesAdminClient

use of org.elasticsearch.client.IndicesAdminClient in project bw-calendar-engine by Bedework.

the class BwIndexEsImpl method deleteIndexes.

private void deleteIndexes(final List<String> names) throws CalFacadeException {
    try {
        final IndicesAdminClient idx = getAdminIdx();
        final DeleteIndexRequestBuilder dirb = getAdminIdx().prepareDelete(names.toArray(new String[names.size()]));
        final ActionFuture<DeleteIndexResponse> dr = idx.delete(dirb.request());
        /*DeleteIndexResponse dir = */
        dr.actionGet();
    } catch (final Throwable t) {
        throw new CalFacadeException(t);
    }
}
Also used : DeleteIndexResponse(org.elasticsearch.action.admin.indices.delete.DeleteIndexResponse) IndicesAdminClient(org.elasticsearch.client.IndicesAdminClient) DeleteIndexRequestBuilder(org.elasticsearch.action.admin.indices.delete.DeleteIndexRequestBuilder) CalFacadeException(org.bedework.calfacade.exc.CalFacadeException)

Example 25 with IndicesAdminClient

use of org.elasticsearch.client.IndicesAdminClient in project bw-calendar-engine by Bedework.

the class BwIndexEsImpl method getIndexInfo.

@Override
public Set<IndexInfo> getIndexInfo() throws CalFacadeException {
    final Set<IndexInfo> res = new TreeSet<>();
    try {
        final IndicesAdminClient idx = getAdminIdx();
        final IndicesStatusRequestBuilder isrb = idx.prepareStatus(Strings.EMPTY_ARRAY);
        final ActionFuture<IndicesStatusResponse> sr = idx.status(isrb.request());
        final IndicesStatusResponse sresp = sr.actionGet();
        for (final String inm : sresp.getIndices().keySet()) {
            final IndexInfo ii = new IndexInfo(inm);
            res.add(ii);
            final ClusterStateRequest clusterStateRequest = Requests.clusterStateRequest().routingTable(true).nodes(true).indices(inm);
            final Iterator<String> it = getAdminCluster().state(clusterStateRequest).actionGet().getState().getMetaData().aliases().keysIt();
            while (it.hasNext()) {
                ii.addAlias(it.next());
            }
        }
        return res;
    } catch (final Throwable t) {
        throw new CalFacadeException(t);
    }
}
Also used : IndicesStatusRequestBuilder(org.elasticsearch.action.admin.indices.status.IndicesStatusRequestBuilder) TreeSet(java.util.TreeSet) ClusterStateRequest(org.elasticsearch.action.admin.cluster.state.ClusterStateRequest) IndicesStatusResponse(org.elasticsearch.action.admin.indices.status.IndicesStatusResponse) IndicesAdminClient(org.elasticsearch.client.IndicesAdminClient) CalFacadeException(org.bedework.calfacade.exc.CalFacadeException)

Aggregations

IndicesAdminClient (org.elasticsearch.client.IndicesAdminClient)27 IndicesExistsResponse (org.elasticsearch.action.admin.indices.exists.indices.IndicesExistsResponse)6 Test (org.junit.Test)6 CalFacadeException (org.bedework.calfacade.exc.CalFacadeException)4 IndicesAliasesResponse (org.elasticsearch.action.admin.indices.alias.IndicesAliasesResponse)3 CreateIndexResponse (org.elasticsearch.action.admin.indices.create.CreateIndexResponse)3 DeleteIndexTemplateResponse (org.elasticsearch.action.admin.indices.template.delete.DeleteIndexTemplateResponse)3 GetIndexTemplatesResponse (org.elasticsearch.action.admin.indices.template.get.GetIndexTemplatesResponse)3 ImmutableOpenMap (org.elasticsearch.common.collect.ImmutableOpenMap)3 UsingDataSet (com.lordofthejars.nosqlunit.annotation.UsingDataSet)2 IOException (java.io.IOException)2 ArrayList (java.util.ArrayList)2 List (java.util.List)2 ElasticsearchException (org.elasticsearch.ElasticsearchException)2 IndicesAliasesRequest (org.elasticsearch.action.admin.indices.alias.IndicesAliasesRequest)2 GetAliasesResponse (org.elasticsearch.action.admin.indices.alias.get.GetAliasesResponse)2 DeleteIndexResponse (org.elasticsearch.action.admin.indices.delete.DeleteIndexResponse)2 IndicesExistsRequest (org.elasticsearch.action.admin.indices.exists.indices.IndicesExistsRequest)2 GetIndexResponse (org.elasticsearch.action.admin.indices.get.GetIndexResponse)2 GetMappingsResponse (org.elasticsearch.action.admin.indices.mapping.get.GetMappingsResponse)2