ElasticSearch index exists not working / reliable

前端 未结 5 1511
攒了一身酷
攒了一身酷 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: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() {
                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;
    }
    

提交回复
热议问题