有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

spring boot Check elastic search部署当前可用或不使用Java

我为elastic search CRUD操作创建了一个Java服务。我在下面给出了我的业务逻辑。我正在进行所有服务器端验证,在将数据插入索引之前,我必须检查elastic search部署是否当前可用,或者是否使用elastic search的RestHighLevelClient。我在下面给出了我的代码

public String createProfileDocument(EmployeeInformation document, String IndexName) throws Exception {
        String Id = document.getId();
        if (Strings.isNullOrEmpty(Id)) {
            return "ID is null or empty";
        }
        else if(Strings.isNullOrEmpty(IndexName)) {
            return "Index name is null or empty";
        }
        else if(isTextContainUpperCase(IndexName)){
            return "Index name should be in lowercase";
        }
        else if(logic to check the deployment)
        {
            return "Elasticsearch deployment is not reachable";
        }

        IndexRequest indexRequest = new IndexRequest(IndexName);
        indexRequest.id(document.getId());
        indexRequest.source(convertProfileDocumentToMap(document), XContentType.JSON);

        IndexResponse indexResponse = client.index(indexRequest, RequestOptions.DEFAULT);
        return indexResponse.getResult().name();

    }

有人能帮我在上面的代码中实现这一点吗?提前谢谢


共 (2) 个答案

  1. # 1 楼答案

    1. 创建REST客户端并考虑使用^ {A1}以实现连接故障转移< /LI>
    2. 检查是否cluster is ready
    3. 检查是否index exists
    4. 如果不是,则初始化索引(使用index api
  2. # 2 楼答案

    由于您只询问了有关检查状态的问题,我假设您可以访问RestHighLevelClient实例

    import org.elasticsearch.client.RequestOptions;
    
    ...
    
    public boolean checkTempConnection(RestHighLevelClient client) {
        try {
            return client.ping(RequestOptions.DEFAULT);
        } catch (Exception e) {
            log.warn("Can't get status : {}", e.getMessage());
            return false; // Not available
        }
    }
    

    祝你好运