// Path: elasticservice/src/main/java/elasticsearch/ElasticRestClient.java
// @Component
// public class ElasticRestClient extends AbstractFactoryBean<RestClient> {
//     private static Logger logger = Logger.getLogger(ElasticRestClient.class);
//     private ClusterFailureListener clusterFailureListener;
//     private volatile RestClient restClient;
//     private static List<String> hosts = new ArrayList<>();
//     private Sniffer sniffer;
// 
//     static {
//         hosts.add("10.128.161.107:9200");
//     }
// 
//     @Override
//     public Class<?> getObjectType() {
//         return RestClient.class;
//     }
// 
// 
//     @Override
//     public RestClient createInstance() throws Exception {
//         if (this.restClient != null) {
//             return this.restClient;
//         }
// 
//         HttpHost[] addresses = new HttpHost[hosts.size()];
//         for (int i = 0; i < hosts.size(); i++) {
//             addresses[i] = HttpHost.create(hosts.get(i));
//         }
// 
//         this.restClient = RestClient
//                 .builder(addresses)
//                 .setMaxRetryTimeoutMillis(ESConstants.RESTCLIENT_TIMEOUT)
//                 .build();
// 
// //        this.sniffer = Sniffer.builder(this.restClient)
// //                                 .setSniffIntervalMillis(60000).build();
// 
//         return this.restClient;
//     }
// 
//     @Override
//     protected void destroyInstance(RestClient instance) throws Exception {
//         try {
//             logger.info("Closing sniffer");
//             instance.close();
//             this.sniffer.close();
//         } catch (IOException e) {
//             logger.warn("Error during close sniffer", e);
//         }
//     }
// 
//     public void setHosts(List<String> hosts) {
//         this.hosts = hosts;
//     }
// }
// 
// Path: elasticservice/src/main/java/elasticsearch/constant/ESConstants.java
// public class ESConstants {
//     public      static  final   String STORE_INDEX = "stores";
//     public      static  final   String PRODUCT_TYPE = "product";
//     public      static  final   String SKU_TYPE = "sku";
//     public      static  final   String PRODUCT_PROPERTY = "productproperty";
//     public      static  final   String CHANNEL_TYPE = "channels";
//     public      static  final   int SNIFFER_INTERVAL = 15000;
//     public      static  final   int RESTCLIENT_TIMEOUT = 20000; // 20s
// 
// 
//     public      static  final   Pattern ROUTINGKEY_PATTERN = Pattern.compile("([\\w\\.]+)\\.(\\w+)\\.(\\d+)");
// 
// }


import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.google.gson.JsonPrimitive;
import elasticsearch.ElasticRestClient;
import elasticsearch.constant.ESConstants;
import elasticsearch.esapi.resp.*;
import org.apache.commons.io.IOUtils;
import org.apache.http.HttpEntity;
import org.apache.http.entity.StringEntity;
import org.elasticsearch.client.Response;
import org.elasticsearch.client.RestClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.util.*;
import java.util.concurrent.Executor;

    public void postCheckUpdate(Long size) {
        if (size <= 0) {
            return;
        }

        // post check should be ok, during upgrade should be less 10000 new created product.
        if (size >= 10000) {
            size = 9999L;
        }

        logger.info("Post check update result");
        String body = "{  \"query\": {\"bool\" : {  \"must_not\" : {\"term\" : {  \"upgradeTag\" : 1612}  }}  }}";
        HashMap<String, String> param = new HashMap<>();
        param.put("size", size.toString());

        ESQueryResponse res = searchAll(param, body);
        if (res.getHit().getTotal() > 0) {
            logger.warn("Some product miss update, recover it totalCount=" + res.getHit().getTotal());

            // do it again
            doUpdate(res);
        }

        logger.info("Upgrade to 1612 end.");
    }

    public void flush() {
        try {
            client.performRequest(
                    "POST",

// Path: elasticservice/src/main/java/elasticsearch/ElasticRestClient.java
// @Component
// public class ElasticRestClient extends AbstractFactoryBean<RestClient> {
//     private static Logger logger = Logger.getLogger(ElasticRestClient.class);
//     private ClusterFailureListener clusterFailureListener;
//     private volatile RestClient restClient;
//     private static List<String> hosts = new ArrayList<>();
//     private Sniffer sniffer;
// 
//     static {
//         hosts.add("10.128.161.107:9200");
//     }
// 
//     @Override
//     public Class<?> getObjectType() {
//         return RestClient.class;
//     }
// 
// 
//     @Override
//     public RestClient createInstance() throws Exception {
//         if (this.restClient != null) {
//             return this.restClient;
//         }
// 
//         HttpHost[] addresses = new HttpHost[hosts.size()];
//         for (int i = 0; i < hosts.size(); i++) {
//             addresses[i] = HttpHost.create(hosts.get(i));
//         }
// 
//         this.restClient = RestClient
//                 .builder(addresses)
//                 .setMaxRetryTimeoutMillis(ESConstants.RESTCLIENT_TIMEOUT)
//                 .build();
// 
// //        this.sniffer = Sniffer.builder(this.restClient)
// //                                 .setSniffIntervalMillis(60000).build();
// 
//         return this.restClient;
//     }
// 
//     @Override
//     protected void destroyInstance(RestClient instance) throws Exception {
//         try {
//             logger.info("Closing sniffer");
//             instance.close();
//             this.sniffer.close();
//         } catch (IOException e) {
//             logger.warn("Error during close sniffer", e);
//         }
//     }
// 
//     public void setHosts(List<String> hosts) {
//         this.hosts = hosts;
//     }
// }
// 
// Path: elasticservice/src/main/java/elasticsearch/constant/ESConstants.java
// public class ESConstants {
//     public      static  final   String STORE_INDEX = "stores";
//     public      static  final   String PRODUCT_TYPE = "product";
//     public      static  final   String SKU_TYPE = "sku";
//     public      static  final   String PRODUCT_PROPERTY = "productproperty";
//     public      static  final   String CHANNEL_TYPE = "channels";
//     public      static  final   int SNIFFER_INTERVAL = 15000;
//     public      static  final   int RESTCLIENT_TIMEOUT = 20000; // 20s
// 
// 
//     public      static  final   Pattern ROUTINGKEY_PATTERN = Pattern.compile("([\\w\\.]+)\\.(\\w+)\\.(\\d+)");
// 
// }


// Path: elasticservice/src/main/java/elasticsearch/exportimport/LoadData.java
import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.google.gson.JsonPrimitive;
import elasticsearch.ElasticRestClient;
import elasticsearch.constant.ESConstants;
import elasticsearch.esapi.resp.*;
import org.apache.commons.io.IOUtils;
import org.apache.http.HttpEntity;
import org.apache.http.entity.StringEntity;
import org.elasticsearch.client.Response;
import org.elasticsearch.client.RestClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.util.*;
import java.util.concurrent.Executor;

    public void postCheckUpdate(Long size) {
        if (size <= 0) {
            return;
        }

        // post check should be ok, during upgrade should be less 10000 new created product.
        if (size >= 10000) {
            size = 9999L;
        }

        logger.info("Post check update result");
        String body = "{  \"query\": {\"bool\" : {  \"must_not\" : {\"term\" : {  \"upgradeTag\" : 1612}  }}  }}";
        HashMap<String, String> param = new HashMap<>();
        param.put("size", size.toString());

        ESQueryResponse res = searchAll(param, body);
        if (res.getHit().getTotal() > 0) {
            logger.warn("Some product miss update, recover it totalCount=" + res.getHit().getTotal());

            // do it again
            doUpdate(res);
        }

        logger.info("Upgrade to 1612 end.");
    }

    public void flush() {
        try {
            client.performRequest(
                    "POST",

                    ESConstants.STORE_INDEX + "/_flush?wait_if_ongoing=true");
