ElasticSearch index exists not working / reliable

前端 未结 5 1558
攒了一身酷
攒了一身酷 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: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 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!

提交回复
热议问题