Parcourir la source

更换rest客户端版本7.10.0

qxm il y a 4 ans
Parent
commit
0b016f0cbc

+ 9 - 2
dgtis-modules/dgtis-modules-data/pom.xml

@@ -116,12 +116,19 @@
             <version>7.6.2</version>
         </dependency>-->
 
+<!--        <dependency>-->
+<!--            <groupId>org.elasticsearch.client</groupId>-->
+<!--            <artifactId>rest</artifactId>-->
+<!--            <version>5.5.1</version>-->
+<!--        </dependency>-->
+        <!-- https://mvnrepository.com/artifact/org.elasticsearch.client/elasticsearch-rest-client -->
         <dependency>
             <groupId>org.elasticsearch.client</groupId>
-            <artifactId>rest</artifactId>
-            <version>5.5.1</version>
+            <artifactId>elasticsearch-rest-client</artifactId>
+            <version>7.10.0</version>
         </dependency>
 
+
         <dependency>
             <groupId>junit</groupId>
             <artifactId>junit</artifactId>

+ 62 - 29
dgtis-modules/dgtis-modules-data/src/main/java/com/dgtis/data/api/CustomerApiController.java

@@ -16,6 +16,7 @@ import org.apache.http.HttpEntity;
 import org.apache.http.entity.ContentType;
 import org.apache.http.nio.entity.NStringEntity;
 import org.apache.http.util.EntityUtils;
+import org.elasticsearch.client.Request;
 import org.elasticsearch.client.Response;
 import org.elasticsearch.client.RestClient;
 import org.slf4j.Logger;
@@ -28,6 +29,8 @@ import org.springframework.web.bind.annotation.*;
 
 import javax.annotation.Resource;
 import java.io.IOException;
+import java.net.URLDecoder;
+import java.net.URLEncoder;
 import java.util.*;
 
 /**
@@ -105,27 +108,27 @@ public class CustomerApiController {
             //拼装查询条件
             StringBuilder builder =new StringBuilder();
             builder.append("{");
-            builder.append("\"query\": {");//query开始
-            builder.append("\"bool\": {");//boot开始
-            builder.append("\"must\": [");//must开始
+            builder.append("\"query\":{");//query开始
+            builder.append("\"bool\":{");//boot开始
+            builder.append("\"must\":[");//must开始
             if(StringUtils.isNotEmpty(gender)){
-                builder.append("{\"prefix\": { \"gender\": "+gender+"}},");
+                builder.append("{\"prefix\":{\"gender\":"+gender+"}},");
             }
             if(StringUtils.isNotEmpty(NOVPolicy)){
-                builder.append("{\"prefix\": { \"label25\": \""+NOVPolicy+"\"}},");
+                builder.append("{\"prefix\":{\"label25\":\""+NOVPolicy+"\"}},");
             }
             if(StringUtils.isNotEmpty(custclass)){
-                builder.append("{\"term\": { \"custclass\": \""+custclass+"\"}},");
+                builder.append("{\"term\":{\"custclass\":\""+custclass+"\"}},");
             }
             if(StringUtils.isNotEmpty(sobirth)){
-                builder.append("{\"wildcard\": { \"sobirth\": \"*"+sobirth+"*\"}},");//模糊匹配
+                builder.append("{\"wildcard\":{\"sobirth\":\"*"+sobirth+"*\"}},");//模糊匹配
             }
 
             if(StringUtils.isNotEmpty(SCustID)){
-                builder.append("{\"wildcard\": { \"custid\": \"*"+SCustID+"*\"}},");//模糊匹配
+                builder.append("{\"wildcard\":{\"custid\":\"*"+SCustID+"*\"}},");//模糊匹配
             }
             if(StringUtils.isNotEmpty(custtype)){
-                builder.append("{\"prefix\": { \"custtype\": \""+custtype+"\"}},");
+                builder.append("{\"prefix\":{\"custtype\":\""+custtype+"\"}},");
             }
 
             if(StringUtils.isNotEmpty(beginBirthDay) && StringUtils.isNotEmpty(endBirthDay)){
@@ -154,7 +157,7 @@ public class CustomerApiController {
 
             if(StringUtils.isNotEmpty(education)){
                 String[] edus = education.split("&");
-                builder.append("{\"terms\":{\"education\":[" );
+                builder.append("{\"terms\":{\"education\":[");
                 for (String edu:edus) {
                     builder.append("\""+edu+"\",");
                 }
@@ -163,7 +166,7 @@ public class CustomerApiController {
             }
 
             if(customerIds.size()>0){
-                builder.append("{\"terms\":{\"_id\":[" );
+                builder.append("{\"terms\":{\"_id\":[");
                 for (String id:customerIds) {
                     builder.append("\""+id+"\",");
                 }
@@ -173,11 +176,11 @@ public class CustomerApiController {
 
 
             if(StringUtils.isNotEmpty(gzgw)){
-                builder.append("{\"prefix\": { \"label73\": \"关注官微\"}},");
+                builder.append("{\"prefix\":{\"label73\":\"关注官微\"}},");
             }
 
             if(StringUtils.isNotEmpty(zcgw)){
-                builder.append("{\"prefix\": { \"label74\": \"注册官微\"}},");
+                builder.append("{\"prefix\":{\"label74\":\"注册官微\"}},");
             }
 
 
@@ -190,19 +193,22 @@ public class CustomerApiController {
             }
 
             builder.append("],");//must结束
-            builder.append("\"must_not\": [ ],");//must_not
-            builder.append("\"should\": [ ]");//should
+            builder.append("\"must_not\":[],");//must_not
+            builder.append("\"should\":[]");//should
             builder.append("}");//boot结束
             builder.append("},");//query结束
-            builder.append("\"from\": "+(pageNum*10+1)+",");//第几页
-            builder.append("\"size\": "+pageSize+",");//查询几个
-            builder.append(" \"sort\": [{\"created_time\":{\"order\":\"desc\"}}],");//排序
-            builder.append(" \"aggs\": {}");//
+            builder.append("\"from\":"+(pageNum*10+1)+",");//第几页
+            builder.append("\"size\":"+pageSize+",");//查询几个
+            builder.append("\"sort\":[{\"created_time\":{\"order\":\"desc\"}}],");//排序
+            builder.append("\"aggs\":{}");//
             builder.append("}");
 
             HttpEntity entity = new NStringEntity(builder.toString(), ContentType.APPLICATION_JSON);
 
-            Response response= restClient.performRequest("GET", "/shanglifeecif.individual/_search",Collections.<String, String>emptyMap(),entity);
+            Request scriptRequest = new Request("GET", "/shanglifeecif.individual/_search?q="+ URLEncoder.encode("\""+builder.toString()+"\""));
+//            Request scriptRequest = new Request("GET", "/shanglifeecif.individual/_search?q=%22{%22query%22:{%22bool%22:{%22must%22:[],%22must_not%22:[],%22should%22:[]}},%22from%22:11,%22size%22:10,%22sort%22:[{%22created_time%22:{%22order%22:%22desc%22}}],%22aggs%22:{}}%22");
+            Response response = restClient.performRequest(scriptRequest);
+//            Response response= restClient.performRequest("GET", "/shanglifeecif.individual/_search",Collections.<String, String>emptyMap(),entity);
 
             String result = EntityUtils.toString(response.getEntity());
             JSONObject jsonObject = JSON.parseObject(result);
@@ -304,7 +310,10 @@ public class CustomerApiController {
 
         HttpEntity entity = new NStringEntity(builder.toString(), ContentType.APPLICATION_JSON);
 
-        Response response= restClient.performRequest("GET", "/shanglifeecif.insurancearrangement/_search?scroll=1m",Collections.<String, String>emptyMap(),entity);
+        Request scriptRequest = new Request("GET", "/shanglifeecif.insurancearrangement/_search?scroll=1m&q="+ URLEncoder.encode("\""+builder.toString()+"\""));
+
+        Response response = restClient.performRequest(scriptRequest);
+//        Response response= restClient.performRequest("GET", "/shanglifeecif.insurancearrangement/_search?scroll=1m",Collections.<String, String>emptyMap(),entity);
 
         String result = EntityUtils.toString(response.getEntity());
         JSONObject jsonObject = JSON.parseObject(result);
@@ -328,7 +337,10 @@ public class CustomerApiController {
             }
             // 继续向后查询
            /* entity = new NStringEntity(JSONUtil.toJsonStr(obj), ContentType.APPLICATION_JSON);
-            response= restClient.performRequest("GET", "/_search/scroll",Collections.<String, String>emptyMap(),entity);
+           Request scriptRequest = new Request("GET", "/_search/scroll?);
+
+        Response response = restClient.performRequest(scriptRequest);
+//            response= restClient.performRequest("GET", "/_search/scroll",Collections.<String, String>emptyMap(),entity);
             result = EntityUtils.toString(response.getEntity());
             jsonObject = JSON.parseObject(result);
             hitsobject = (JSONObject) jsonObject.get("hits");
@@ -346,7 +358,10 @@ public class CustomerApiController {
 
             Map<String, String> paramMap = new HashMap<String, String>();
             paramMap.put("pretty", "true");
-            Response response = restClient.performRequest("GET", "/shanglifeecif.individual/default_type_/"+id, paramMap);
+          Request scriptRequest = new Request("GET", "/shanglifeecif.individual/default_type_/"+id+"?pretty=true");
+
+          Response response = restClient.performRequest(scriptRequest);
+//            Response response = restClient.performRequest("GET", "/shanglifeecif.individual/default_type_/"+id, paramMap);
             String result = EntityUtils.toString(response.getEntity());
             Map map = new HashMap();
             JSONObject jsonObject = JSON.parseObject(result);
@@ -371,7 +386,10 @@ public class CustomerApiController {
           Map query  =new HashMap();
           query.put("indid1",indid);
           HttpEntity entity = new NStringEntity(EsJsonUtil.QuerygetMust(query), ContentType.APPLICATION_JSON);
-          response= restClient.performRequest("GET", "/shanglifeecif.indrelationship/_search",Collections.<String, String>emptyMap(),entity);
+           scriptRequest = new Request("GET", "/shanglifeecif.indrelationship/_search?pretty=true&q=indid1:"+indid);
+
+          response = restClient.performRequest(scriptRequest);
+//          response= restClient.performRequest("GET", "/shanglifeecif.indrelationship/_search",Collections.<String, String>emptyMap(),entity);
           result = EntityUtils.toString(response.getEntity());
           jsonObject = JSON.parseObject(result);
           JSONObject hitsobject = (JSONObject) jsonObject.get("hits");
@@ -388,7 +406,10 @@ public class CustomerApiController {
           query  =new HashMap();
           query.put("applicantid",indid);
           entity = new NStringEntity(EsJsonUtil.QuerygetMust(query), ContentType.APPLICATION_JSON);
-          response= restClient.performRequest("GET", "/shanglifeecif.insuranceclaimthread/_search",Collections.<String, String>emptyMap(),entity);
+          scriptRequest = new Request("GET", "/shanglifeecif.insuranceclaimthread/_search?pretty=true&q=applicantid:"+indid);
+
+          response = restClient.performRequest(scriptRequest);
+//          response= restClient.performRequest("GET", "/shanglifeecif.insuranceclaimthread/_search",Collections.<String, String>emptyMap(),entity);
           result = EntityUtils.toString(response.getEntity());
           jsonObject = JSON.parseObject(result);
           hitsobject = (JSONObject) jsonObject.get("hits");
@@ -406,7 +427,10 @@ public class CustomerApiController {
           query  =new HashMap();
           query.put("applicantid",indid);
           entity = new NStringEntity(EsJsonUtil.QuerygetMust(query), ContentType.APPLICATION_JSON);
-          response= restClient.performRequest("GET", "/shanglifeecif.insurancearrangement/_search",Collections.<String, String>emptyMap(),entity);
+          scriptRequest = new Request("GET", "/shanglifeecif.insurancearrangement/_search?pretty=true&q=applicantid:"+indid);
+
+          response = restClient.performRequest(scriptRequest);
+//          response= restClient.performRequest("GET", "/shanglifeecif.insurancearrangement/_search",Collections.<String, String>emptyMap(),entity);
           result = EntityUtils.toString(response.getEntity());
           jsonObject = JSON.parseObject(result);
           hitsobject = (JSONObject) jsonObject.get("hits");
@@ -440,7 +464,10 @@ public class CustomerApiController {
         try {
             Map<String, String> paramMap = new HashMap<String, String>();
             paramMap.put("pretty", "true");
-            Response response = restClient.performRequest("GET", "shanglifeecif.individual/default_type_/"+id, paramMap);
+            Request scriptRequest = new Request("GET", "shanglifeecif.individual/default_type_/"+id+"?pretty=true");
+
+            Response response = restClient.performRequest(scriptRequest);
+//            Response response = restClient.performRequest("GET", "shanglifeecif.individual/default_type_/"+id, paramMap);
             String result = EntityUtils.toString(response.getEntity());
             Map map = new HashMap();
             JSONObject jsonObject = JSON.parseObject(result);
@@ -460,7 +487,10 @@ public class CustomerApiController {
             Map query  =new HashMap();
             query.put("applicantid",indid);
             HttpEntity entity = new NStringEntity(EsJsonUtil.QuerygetMust(query), ContentType.APPLICATION_JSON);
-            response= restClient.performRequest("GET", "/shanglifeecif.insurancearrangement/_search",Collections.<String, String>emptyMap(),entity);
+            scriptRequest = new Request("GET", "/shanglifeecif.insurancearrangement/_search?pretty=true&q=applicantid:"+indid);
+
+            response = restClient.performRequest(scriptRequest);
+//            response= restClient.performRequest("GET", "/shanglifeecif.insurancearrangement/_search",Collections.<String, String>emptyMap(),entity);
             result = EntityUtils.toString(response.getEntity());
             jsonObject = JSON.parseObject(result);
             JSONObject hitsobject = (JSONObject) jsonObject.get("hits");
@@ -504,7 +534,10 @@ public class CustomerApiController {
             query  =new HashMap();
             query.put("applicantid",indid);
             entity = new NStringEntity(EsJsonUtil.QuerygetMust(query), ContentType.APPLICATION_JSON);
-            response= restClient.performRequest("GET", "/shanglifeecif.insuranceclaimthread/_search",Collections.<String, String>emptyMap(),entity);
+            scriptRequest = new Request("GET", "/shanglifeecif.insuranceclaimthread/_search?pretty=true&q=applicantid:"+indid);
+
+            response = restClient.performRequest(scriptRequest);
+//            response= restClient.performRequest("GET", "/shanglifeecif.insuranceclaimthread/_search",Collections.<String, String>emptyMap(),entity);
             result = EntityUtils.toString(response.getEntity());
             jsonObject = JSON.parseObject(result);
             hitsobject = (JSONObject) jsonObject.get("hits");

+ 18 - 4
dgtis-modules/dgtis-modules-data/src/main/java/com/dgtis/data/api/IndexApiController.java

@@ -9,6 +9,7 @@ import com.dgtis.common.core.utils.StringUtils;
 import com.dgtis.common.core.web.controller.BaseController;
 import com.dgtis.common.core.web.domain.AjaxResult;
 import org.apache.http.util.EntityUtils;
+import org.elasticsearch.client.Request;
 import org.elasticsearch.client.Response;
 import org.elasticsearch.client.RestClient;
 import org.springframework.beans.factory.annotation.Autowired;
@@ -50,9 +51,16 @@ public class IndexApiController extends BaseController {
         List xaxisData = new ArrayList();//存放险种
         List seriesData = new ArrayList();//客户数量和保单数量
         try {
+
             Map<String, String> paramMap = new HashMap<String, String>();
             paramMap.put("pretty", "true");
-            Response response = restClient.performRequest("GET", "shanglifeecif.riskcode_statistics/_search?", paramMap);
+            Request scriptRequest = new Request("GET", "/shanglifeecif.riskcode_statistics/_search?pretty=true");
+
+            Response response = restClient.performRequest(scriptRequest);
+
+            /*Map<String, String> paramMap = new HashMap<String, String>();
+            paramMap.put("pretty", "true");*/
+//            Response response = restClient.performRequest("GET", "shanglifeecif.riskcode_statistics/_search?", paramMap);
             String jsonStr = EntityUtils.toString(response.getEntity());
             if(StringUtils.isNotEmpty(jsonStr)){
                 JSONObject jsonObject = JSONUtil.parseObj(jsonStr);
@@ -115,7 +123,10 @@ public class IndexApiController extends BaseController {
 
             Map<String, String> paramMap = new HashMap<String, String>();
             paramMap.put("pretty", "true");
-            Response response = restClient.performRequest("GET", "shanglifeecif.bdnum_distribution/_search?", paramMap);
+            Request scriptRequest = new Request("GET", "/shanglifeecif.bdnum_distribution/_search?pretty=true");
+
+            Response response = restClient.performRequest(scriptRequest);
+//            Response response = restClient.performRequest("GET", "shanglifeecif.bdnum_distribution/_search?", paramMap);
             String jsonStr = EntityUtils.toString(response.getEntity());
             if(StringUtils.isNotEmpty(jsonStr)){
                 JSONObject jsonObject = JSONUtil.parseObj(jsonStr);
@@ -129,7 +140,7 @@ public class IndexApiController extends BaseController {
                                 JSONObject source = object.getJSONObject("_source");
                                 Map map = new HashMap();
                                 String labelname = source.getStr("labelname").trim();
-                               /* map.put("value",source.getStr("bdnum"));
+                                /*map.put("value",source.getStr("bdnum"));
                                 map.put("name",source.getStr("labelname").trim());
                                 seriesData.add(map);*/
                                 Map m = new HashMap();
@@ -189,7 +200,10 @@ public class IndexApiController extends BaseController {
 
             Map<String, String> paramMap = new HashMap<String, String>();
             paramMap.put("pretty", "true");
-            Response response = restClient.performRequest("GET", "shanglifeecif.age_sex_distribution/_search?", paramMap);
+            Request scriptRequest = new Request("GET", "/shanglifeecif.age_sex_distribution/_search?pretty=true");
+
+            Response response = restClient.performRequest(scriptRequest);
+//            Response response = restClient.performRequest("GET", "shanglifeecif.age_sex_distribution/_search?", paramMap);
             String jsonStr = EntityUtils.toString(response.getEntity());
             if(StringUtils.isNotEmpty(jsonStr)){
                 JSONObject jsonObject = JSONUtil.parseObj(jsonStr);

+ 48 - 3
dgtis-modules/dgtis-modules-data/src/main/java/com/dgtis/data/config/ElasticSearchConfig.java

@@ -2,14 +2,21 @@
 package com.dgtis.data.config;
 
 
+import org.apache.http.Header;
 import org.apache.http.HttpHost;
+import org.apache.http.auth.AuthScope;
+import org.apache.http.auth.UsernamePasswordCredentials;
+import org.apache.http.client.CredentialsProvider;
 import org.apache.http.client.config.RequestConfig;
+import org.apache.http.impl.client.BasicCredentialsProvider;
+import org.apache.http.impl.nio.client.HttpAsyncClientBuilder;
+import org.apache.http.message.BasicHeader;
 import org.elasticsearch.client.RestClient;
+import org.elasticsearch.client.RestClientBuilder;
 import org.springframework.beans.factory.annotation.Value;
 import org.springframework.context.annotation.Bean;
 import org.springframework.context.annotation.Configuration;
 
-import static org.elasticsearch.client.RestClientBuilder.DEFAULT_CONNECTION_REQUEST_TIMEOUT_MILLIS;
 
 /**
  * @author koucx
@@ -41,7 +48,7 @@ public class ElasticSearchConfig {
 
     @Bean
     public RestClient esClient() {
-        RestClient restClient = null;
+        /*RestClient restClient = null;
         try {
             restClient = RestClient.builder(httpHost()).setMaxRetryTimeoutMillis(TIME_OUT)
                     .setHttpClientConfigCallback(httpClientBuilder -> {
@@ -58,7 +65,45 @@ public class ElasticSearchConfig {
         } catch (Exception e) {
             e.printStackTrace();
         }
-        return restClient;
+        return restClient;*/
+
+        //配置可选参数
+        RestClientBuilder builder = RestClient.builder(
+                httpHost());
+        Header[] defaultHeaders = new Header[]{new BasicHeader("header", "value")};
+        //设置每个请求需要发送的默认headers,这样就不用在每个请求中指定它们。
+        builder.setDefaultHeaders(defaultHeaders);
+        // 设置应该授予的超时时间,以防对相同的请求进行多次尝试。默认值是30秒,与默认socket超时时间相同。
+        // 如果自定义socket超时时间,则应相应地调整最大重试超时时间。
+//        builder.set.setMaxRetryTimeoutMillis(10000);
+        builder.setFailureListener(new RestClient.FailureListener() {
+            public void onFailure(HttpHost host) {
+                //设置一个监听程序,每次节点发生故障时都会收到通知,这样就可以采取相应的措施。
+                //Used internally when sniffing on failure is enabled.(这句话没搞懂啥意思)
+            }
+        });
+        builder.setRequestConfigCallback(new RestClientBuilder.RequestConfigCallback() {
+            @Override
+            public RequestConfig.Builder customizeRequestConfig(RequestConfig.Builder requestConfigBuilder) {
+                //设置允许修改默认请求配置的回调
+                // (例如,请求超时,身份验证或org.apache.http.client.config.RequestConfig.Builder允许设置的任何内容)
+//                return requestConfigBuilder.setSocketTimeout(TIME_OUT);
+                return requestConfigBuilder
+                        .setConnectTimeout(TIME_OUT)
+                        .setSocketTimeout(TIME_OUT);
+            }
+        });
+        /*builder.setHttpClientConfigCallback(new RestClientBuilder.HttpClientConfigCallback() {
+            @Override
+            public HttpAsyncClientBuilder customizeHttpClient(HttpAsyncClientBuilder httpClientBuilder) {
+                //设置允许修改http客户端配置的回调
+                // (例如,通过SSL的加密通信,或者org.apache.http.impl.nio.client.HttpAsyncClientBuilder允许设置的任何内容)
+                final CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
+                credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials("cluster", null));
+                return httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider);
+            }
+        });*/
+        return builder.build();
     }
 
     public HttpHost[] httpHost(){