ElasticSearch index exists not working / reliable

前端 未结 5 1584
攒了一身酷
攒了一身酷 2021-02-19 09:41

I am writing a simple Java wrapper around ElasticSearch\'s admin client. To test it I have a main method that first checks if an index exists (IndicesExistsRequest), if so delet

相关标签:
5条回答
  • 2021-02-19 10:19

    The skgemini's answer is ok if you want to check if index is available by the actual index name or any of its aliases.

    If you however want to check only by the index name, here is how.

    public boolean checkIfIndexExists(String index) {
    
        IndexMetaData indexMetaData = client.admin().cluster()
                .state(Requests.clusterStateRequest())
                .actionGet()
                .getState()
                .getMetaData()
                .index(index);
    
        return (indexMetaData != null);
    
    }
    
    0 讨论(0)
  • 2021-02-19 10:27

    Here is my solution when using RestHighLevelClient client;

    Here a code-snippet: :

    public boolean checkIfIndexExists(String indexName) throws IOException {
                Response response = client.getLowLevelClient().performRequest("HEAD", "/" + indexName);
                int statusCode = response.getStatusLine().getStatusCode(); 
                return (statusCode != 404);
        }
    

    A contribution for someone else !

    0 讨论(0)
  • 2021-02-19 10:30

    You can also execute a synchronous request like this:

    boolean exists = client.admin().indices()
        .prepareExists(INDEX_NAME)
        .execute().actionGet().isExists();
    
    0 讨论(0)
  • 2021-02-19 10:30

    OK, I figured out a solution. Since the java client's calls are done asynchronously you have to use the variant which takes an action listener. The solution still gets a bit contrived though:

    // Inner class because it's just used to be thrown out of
    // the action listener implementation to signal that the
    // index exists
    private class ExistsException extends RuntimeException {
    }
    
    public boolean exists() {
        logger.info(String.format("Verifying existence of index \"%s\"", indexName));
        IndicesExistsRequest request = new IndicesExistsRequest(indexName);
        try {
            adminClient.exists(request, new ActionListener<IndicesExistsResponse>() {
                public void onResponse(IndicesExistsResponse response) {
                    if (response.isExists()) {
                        throw new ExistsException();
                    }
                }
                public void onFailure(Throwable e) {
                    ExceptionUtil.smash(e);
                }
            });
        }
        catch (ExistsException e) {
            return true;
        }
        return false;
    }
    
    0 讨论(0)
  • 2021-02-19 10:32

    I had the same issue but i didn't like the solution which uses an ActionListener. ElasticSearch also offers a Future variant (at least at version 6.1.0).

    Here a code-snippet:

    public boolean doesIndexExists(String indexName, TransportClient client) {
        IndicesExistsRequest request = new IndicesExistsRequest(indexName);
        ActionFuture<IndicesExistsResponse> future = client.admin().indices().exists(request);
        try {
            IndicesExistsResponse response = future.get();
            boolean result = response.isExists();
            logger.info("Existence of index '" + indexName + "' result is " + result);
            return result;
        } catch (InterruptedException | ExecutionException e) {
            logger.error("Exception at waiting for IndicesExistsResponse", e);
            return false;//do some clever exception handling
        }
    }
    

    May be this helps someone else too. Cheers!

    0 讨论(0)
提交回复
热议问题