Search in sources :

Example 1 with IndicesExistsRequestBuilder

use of org.elasticsearch.action.admin.indices.exists.indices.IndicesExistsRequestBuilder in project pentaho-kettle by pentaho.

the class ElasticSearchBulkDialog method test.

private void test(TestType testType) {
    // Save off the thread's context class loader to restore after the test
    ClassLoader originalClassloader = Thread.currentThread().getContextClassLoader();
    // Now ensure that the thread's context class loader is the plugin's classloader
    Thread.currentThread().setContextClassLoader(this.getClass().getClassLoader());
    Client client = null;
    Node node = null;
    try {
        ElasticSearchBulkMeta tempMeta = new ElasticSearchBulkMeta();
        toModel(tempMeta);
        Settings.Builder settingsBuilder = Settings.settingsBuilder();
        // keep default classloader
        settingsBuilder.put(Settings.Builder.EMPTY_SETTINGS);
        settingsBuilder.put(tempMeta.getSettingsMap());
        TransportClient.Builder tClientBuilder = TransportClient.builder().settings(settingsBuilder);
        if (!tempMeta.getServers().isEmpty()) {
            node = null;
            TransportClient tClient = tClientBuilder.build();
            for (ElasticSearchBulkMeta.Server s : tempMeta.getServers()) {
                tClient.addTransportAddress(s.getAddr());
            }
            client = tClient;
        } else {
            NodeBuilder nodeBuilder = NodeBuilder.nodeBuilder();
            nodeBuilder.settings(settingsBuilder);
            node = nodeBuilder.client(true).node();
            client = node.client();
            node.start();
        }
        AdminClient admin = client.admin();
        switch(testType) {
            case INDEX:
                if (StringUtils.isBlank(tempMeta.getIndex())) {
                    showError(BaseMessages.getString(PKG, "ElasticSearchBulk.Error.NoIndex"));
                    break;
                }
                // First check to see if the index exists
                IndicesExistsRequestBuilder indicesExistBld = admin.indices().prepareExists(tempMeta.getIndex());
                IndicesExistsResponse indicesExistResponse = indicesExistBld.execute().get();
                if (!indicesExistResponse.isExists()) {
                    showError(BaseMessages.getString(PKG, "ElasticSearchBulkDialog.Error.NoIndex"));
                    return;
                }
                RecoveryRequestBuilder indicesBld = admin.indices().prepareRecoveries(tempMeta.getIndex());
                ListenableActionFuture<RecoveryResponse> lafInd = indicesBld.execute();
                String shards = "" + lafInd.get().getSuccessfulShards() + "/" + lafInd.get().getTotalShards();
                showMessage(BaseMessages.getString(PKG, "ElasticSearchBulkDialog.TestIndex.TestOK", shards));
                break;
            case CLUSTER:
                ClusterStateRequestBuilder clusterBld = admin.cluster().prepareState();
                ListenableActionFuture<ClusterStateResponse> lafClu = clusterBld.execute();
                ClusterStateResponse cluResp = lafClu.actionGet();
                String name = cluResp.getClusterName().value();
                ClusterState cluState = cluResp.getState();
                int numNodes = cluState.getNodes().size();
                showMessage(BaseMessages.getString(PKG, "ElasticSearchBulkDialog.TestCluster.TestOK", name, numNodes));
                break;
            default:
                break;
        }
    } catch (NoNodeAvailableException e) {
        showError(BaseMessages.getString(PKG, "ElasticSearchBulkDialog.Error.NoNodesFound"));
    } catch (MasterNotDiscoveredException e) {
        showError(BaseMessages.getString(PKG, "ElasticSearchBulkDialog.Error.NoNodesFound"));
    } catch (Exception e) {
        showError(e.getLocalizedMessage());
    } finally {
        if (client != null) {
            client.close();
        }
        if (node != null) {
            node.close();
        }
    }
    // Restore the original classloader
    Thread.currentThread().setContextClassLoader(originalClassloader);
}
Also used : Node(org.elasticsearch.node.Node) ClusterStateRequestBuilder(org.elasticsearch.action.admin.cluster.state.ClusterStateRequestBuilder) NodeBuilder(org.elasticsearch.node.NodeBuilder) RecoveryResponse(org.elasticsearch.action.admin.indices.recovery.RecoveryResponse) IndicesExistsResponse(org.elasticsearch.action.admin.indices.exists.indices.IndicesExistsResponse) RecoveryRequestBuilder(org.elasticsearch.action.admin.indices.recovery.RecoveryRequestBuilder) ElasticSearchBulkMeta(org.pentaho.di.trans.steps.elasticsearchbulk.ElasticSearchBulkMeta) AdminClient(org.elasticsearch.client.AdminClient) TransportClient(org.elasticsearch.client.transport.TransportClient) Client(org.elasticsearch.client.Client) Settings(org.elasticsearch.common.settings.Settings) ClusterState(org.elasticsearch.cluster.ClusterState) TransportClient(org.elasticsearch.client.transport.TransportClient) ClusterStateResponse(org.elasticsearch.action.admin.cluster.state.ClusterStateResponse) NoNodeAvailableException(org.elasticsearch.client.transport.NoNodeAvailableException) MasterNotDiscoveredException(org.elasticsearch.discovery.MasterNotDiscoveredException) MasterNotDiscoveredException(org.elasticsearch.discovery.MasterNotDiscoveredException) NoNodeAvailableException(org.elasticsearch.client.transport.NoNodeAvailableException) KettleException(org.pentaho.di.core.exception.KettleException) IndicesExistsRequestBuilder(org.elasticsearch.action.admin.indices.exists.indices.IndicesExistsRequestBuilder) AdminClient(org.elasticsearch.client.AdminClient)

Example 2 with IndicesExistsRequestBuilder

use of org.elasticsearch.action.admin.indices.exists.indices.IndicesExistsRequestBuilder in project molgenis by molgenis.

the class ClientFacade method indexesExist.

private boolean indexesExist(List<Index> indexes) {
    if (LOG.isTraceEnabled()) {
        LOG.trace("Determining index(es) '{}' existence ...", toString(indexes));
    }
    String[] indexNames = toIndexNames(indexes);
    IndicesExistsRequestBuilder indicesExistsRequest = client.admin().indices().prepareExists(indexNames);
    IndicesExistsResponse indicesExistsResponse;
    try {
        indicesExistsResponse = indicesExistsRequest.get();
    } catch (ElasticsearchException e) {
        LOG.error("", e);
        throw new IndexException(format("Error determining index(es) '%s' existence.", toString(indexes)));
    }
    boolean exists = indicesExistsResponse.isExists();
    if (LOG.isDebugEnabled()) {
        LOG.debug("Determined index(es) '{}' existence: {}.", toString(indexes), exists);
    }
    return exists;
}
Also used : IndexException(org.molgenis.data.index.exception.IndexException) UnknownIndexException(org.molgenis.data.index.exception.UnknownIndexException) IndicesExistsRequestBuilder(org.elasticsearch.action.admin.indices.exists.indices.IndicesExistsRequestBuilder) IndicesExistsResponse(org.elasticsearch.action.admin.indices.exists.indices.IndicesExistsResponse) ElasticsearchException(org.elasticsearch.ElasticsearchException)

Example 3 with IndicesExistsRequestBuilder

use of org.elasticsearch.action.admin.indices.exists.indices.IndicesExistsRequestBuilder in project elasticsearch by elastic.

the class IndicesExistsIT method testIndicesExists.

// Indices exists never throws IndexMissingException, the indices options control its behaviour (return true or false)
public void testIndicesExists() throws Exception {
    assertFalse(client().admin().indices().prepareExists("foo").get().isExists());
    assertFalse(client().admin().indices().prepareExists("foo*").get().isExists());
    assertFalse(client().admin().indices().prepareExists("_all").get().isExists());
    createIndex("foo", "foobar", "bar", "barbaz");
    IndicesExistsRequestBuilder indicesExistsRequestBuilder = client().admin().indices().prepareExists("foo*").setExpandWildcardsOpen(false);
    IndicesExistsRequest request = indicesExistsRequestBuilder.request();
    //check that ignore unavailable and allow no indices are set to false. That is their only valid value as it can't be overridden
    assertFalse(request.indicesOptions().ignoreUnavailable());
    assertFalse(request.indicesOptions().allowNoIndices());
    assertThat(indicesExistsRequestBuilder.get().isExists(), equalTo(false));
    assertAcked(client().admin().indices().prepareClose("foobar").get());
    assertThat(client().admin().indices().prepareExists("foo*").get().isExists(), equalTo(true));
    assertThat(client().admin().indices().prepareExists("foo*").setExpandWildcardsOpen(false).setExpandWildcardsClosed(false).get().isExists(), equalTo(false));
    assertThat(client().admin().indices().prepareExists("foobar").get().isExists(), equalTo(true));
    assertThat(client().admin().indices().prepareExists("foob*").setExpandWildcardsClosed(false).get().isExists(), equalTo(false));
    assertThat(client().admin().indices().prepareExists("bar*").get().isExists(), equalTo(true));
    assertThat(client().admin().indices().prepareExists("bar").get().isExists(), equalTo(true));
    assertThat(client().admin().indices().prepareExists("_all").get().isExists(), equalTo(true));
}
Also used : IndicesExistsRequestBuilder(org.elasticsearch.action.admin.indices.exists.indices.IndicesExistsRequestBuilder) IndicesExistsRequest(org.elasticsearch.action.admin.indices.exists.indices.IndicesExistsRequest)

Example 4 with IndicesExistsRequestBuilder

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

Aggregations

IndicesExistsRequestBuilder (org.elasticsearch.action.admin.indices.exists.indices.IndicesExistsRequestBuilder)4 IndicesExistsResponse (org.elasticsearch.action.admin.indices.exists.indices.IndicesExistsResponse)3 AdminClient (org.elasticsearch.client.AdminClient)2 Client (org.elasticsearch.client.Client)2 ElasticsearchException (org.elasticsearch.ElasticsearchException)1 ClusterStateRequestBuilder (org.elasticsearch.action.admin.cluster.state.ClusterStateRequestBuilder)1 ClusterStateResponse (org.elasticsearch.action.admin.cluster.state.ClusterStateResponse)1 IndicesExistsRequest (org.elasticsearch.action.admin.indices.exists.indices.IndicesExistsRequest)1 RecoveryRequestBuilder (org.elasticsearch.action.admin.indices.recovery.RecoveryRequestBuilder)1 RecoveryResponse (org.elasticsearch.action.admin.indices.recovery.RecoveryResponse)1 IndicesAdminClient (org.elasticsearch.client.IndicesAdminClient)1 NoNodeAvailableException (org.elasticsearch.client.transport.NoNodeAvailableException)1 TransportClient (org.elasticsearch.client.transport.TransportClient)1 ClusterState (org.elasticsearch.cluster.ClusterState)1 Settings (org.elasticsearch.common.settings.Settings)1 MasterNotDiscoveredException (org.elasticsearch.discovery.MasterNotDiscoveredException)1 Node (org.elasticsearch.node.Node)1 NodeBuilder (org.elasticsearch.node.NodeBuilder)1 IndexException (org.molgenis.data.index.exception.IndexException)1 UnknownIndexException (org.molgenis.data.index.exception.UnknownIndexException)1