// Path: elasticservice/src/main/java/elasticsearch/exception/ElasticAPIException.java
// public class ElasticAPIException extends RuntimeException {
//     public ElasticAPIException(String message) {
//         super(message);
//     }
// 
//     public ElasticAPIException(String message, Throwable cause) {
//         super(message, cause);
//     }
// }
// 
// Path: elasticservice/src/main/java/elasticsearch/exception/ElasticQueryException.java
// public class ElasticQueryException extends RuntimeException {
//     public ElasticQueryException(String message) {
//         super(message);
//     }
// 
//     public ElasticQueryException(String message, Throwable cause) {
//         super(message, cause);
//     }
// }
// 
// Path: elasticservice/src/main/java/elasticsearch/exception/ElasticVersionConflictException.java
// public class ElasticVersionConflictException extends RuntimeException {
//     public ElasticVersionConflictException(String message) {
//         super(message);
//     }
// 
//     public ElasticVersionConflictException(String message, Throwable cause) {
//         super(message, cause);
//     }
// }


import com.google.gson.Gson;
import com.google.gson.JsonObject;
import elasticsearch.esapi.resp.*;
import elasticsearch.exception.ElasticAPIException;
import elasticsearch.exception.ElasticQueryException;
import elasticsearch.exception.ElasticVersionConflictException;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.io.Charsets;
import org.apache.commons.io.IOUtils;
import org.apache.http.HttpEntity;
import org.apache.http.entity.StringEntity;
import org.apache.log4j.Logger;
import org.elasticsearch.client.Response;
import org.elasticsearch.client.ResponseException;
import org.elasticsearch.client.RestClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.io.IOException;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

        return null;
    }

    public ESSaveResponse update(String index, String type, Long sourceId, Map<String, String> params, HttpEntity requestBody) {
        params = addTenantId2Param(params);

        // for real-time fetch
        //params.put("refresh", "true");
        try {
            Response response = client.performRequest(
                    "POST",
                    index + "/" + type + "/" + sourceId + "/_update",
                    params,
                    requestBody);

            int statusCode = response.getStatusLine().getStatusCode();
            if (statusCode > 299) {
                logger.warn("Problem while indexing a document: {}" +
                        response.getStatusLine().getReasonPhrase());
                throw new ElasticAPIException("Could not index a document, status code is " + statusCode);
            }

            ESSaveResponse esResponse = gson.fromJson(IOUtils.toString(response.getEntity().getContent()),
                    ESSaveResponse.class);
            return esResponse;
        } catch (ResponseException rex) {
            logger.warn("Got elasticsearch exception " + rex);
            Response res = rex.getResponse();
            if (res.getStatusLine().getStatusCode() == 409) {
                logger.warn("Conflict on store object");

// Path: elasticservice/src/main/java/elasticsearch/exception/ElasticAPIException.java
// public class ElasticAPIException extends RuntimeException {
//     public ElasticAPIException(String message) {
//         super(message);
//     }
// 
//     public ElasticAPIException(String message, Throwable cause) {
//         super(message, cause);
//     }
// }
// 
// Path: elasticservice/src/main/java/elasticsearch/exception/ElasticQueryException.java
// public class ElasticQueryException extends RuntimeException {
//     public ElasticQueryException(String message) {
//         super(message);
//     }
// 
//     public ElasticQueryException(String message, Throwable cause) {
//         super(message, cause);
//     }
// }
// 
// Path: elasticservice/src/main/java/elasticsearch/exception/ElasticVersionConflictException.java
// public class ElasticVersionConflictException extends RuntimeException {
//     public ElasticVersionConflictException(String message) {
//         super(message);
//     }
// 
//     public ElasticVersionConflictException(String message, Throwable cause) {
//         super(message, cause);
//     }
// }


// Path: elasticservice/src/main/java/elasticsearch/esapi/DocumentService.java
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import elasticsearch.esapi.resp.*;
import elasticsearch.exception.ElasticAPIException;
import elasticsearch.exception.ElasticQueryException;
import elasticsearch.exception.ElasticVersionConflictException;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.io.Charsets;
import org.apache.commons.io.IOUtils;
import org.apache.http.HttpEntity;
import org.apache.http.entity.StringEntity;
import org.apache.log4j.Logger;
import org.elasticsearch.client.Response;
import org.elasticsearch.client.ResponseException;
import org.elasticsearch.client.RestClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.io.IOException;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

        return null;
    }

    public ESSaveResponse update(String index, String type, Long sourceId, Map<String, String> params, HttpEntity requestBody) {
        params = addTenantId2Param(params);

        // for real-time fetch
        //params.put("refresh", "true");
        try {
            Response response = client.performRequest(
                    "POST",
                    index + "/" + type + "/" + sourceId + "/_update",
                    params,
                    requestBody);

            int statusCode = response.getStatusLine().getStatusCode();
            if (statusCode > 299) {
                logger.warn("Problem while indexing a document: {}" +
                        response.getStatusLine().getReasonPhrase());
                throw new ElasticAPIException("Could not index a document, status code is " + statusCode);
            }

            ESSaveResponse esResponse = gson.fromJson(IOUtils.toString(response.getEntity().getContent()),
                    ESSaveResponse.class);
            return esResponse;
        } catch (ResponseException rex) {
            logger.warn("Got elasticsearch exception " + rex);
            Response res = rex.getResponse();
            if (res.getStatusLine().getStatusCode() == 409) {
                logger.warn("Conflict on store object");

                throw new ElasticVersionConflictException("type:" + type + " params:" + params.toString()
