Przeglądaj źródła

测试http远程请求连接es进行数据返回

chenendian 1 dzień temu
rodzic
commit
2bfccf9490

+ 2 - 0
siwei-modules/siwei-apply/src/main/java/com/siwei/apply/domain/vo/ProjectFilterVo.java

@@ -27,6 +27,8 @@ public class ProjectFilterVo {
 
     private List<String> keyWords;
 
+    private List<String> attachmentInfoIdList;
+
     // 分页参数
     private Integer pageNum = 1; // 当前页码
     private Integer pageSize = 10; // 每页条数

+ 16 - 0
siwei-modules/siwei-apply/src/main/java/com/siwei/apply/service/impl/ProjectImpl.java

@@ -18,6 +18,7 @@ import com.siwei.apply.service.NodeLandService;
 import com.siwei.apply.service.ProjectService;
 import com.siwei.apply.utils.ServiceFileUtil;
 import com.siwei.apply.utils.ServiceUtil;
+import com.siwei.common.core.domain.R;
 import lombok.extern.slf4j.Slf4j;
 import org.apache.commons.collections4.MapUtils;
 import org.geotools.data.DefaultTransaction;
@@ -1865,6 +1866,21 @@ public class ProjectImpl implements ProjectService {
             projectFilterVo.setKeyWords(keyWords);
         }
         projectFilterVo.setIsOnchain(true);
+        // todo 这里进行拦截,根据关键词从elasticsearch中召回附件信息获取数据
+        List <String> searchAttachmentIds = null;
+        log.info("==================entry to elasticsearch 的搜索环节,关键词:{}===================", projectFilterVo.getKeyWord());
+        try {
+            String[] res = ServiceUtil.getElasticsearchResultByWord(projectFilterVo.getKeyWord());
+            if(res.length>0){
+                searchAttachmentIds = Arrays.asList(res);
+            }
+            log.info("根据关键词从 elasticsearch 中召回附件信息获取数据,关键词:{},附件id:{}", projectFilterVo.getKeyWord(), searchAttachmentIds);
+        } catch (Exception e) {
+            e.printStackTrace();
+        }
+        projectFilterVo.setAttachmentInfoIdList(searchAttachmentIds);
+
+
         // todo 这里后期可以进行优化,把查询出的项目id写入redis中,分页时候可以取缓存
         List<SearchProjectAndAttachment> projects = projectMapper.getListSearch(projectFilterVo);
         Integer count = projectMapper.getSearchCount(projectFilterVo);

+ 4 - 1
siwei-modules/siwei-apply/src/main/java/com/siwei/apply/utils/ArcGisUtil.java

@@ -152,8 +152,11 @@ public class ArcGisUtil {
         ArcGisUtil client = new ArcGisUtil(arcgisBase, layerId, srid);
         boolean withGeometry = true;
         //String where = "BDCDYH='360425005005GN00002W00000000'";
-        String where = "BDCDYH='360425005005GB60012W00000000'";
+        //String where = "BDCDYH='360425005005GB60012W00000000'";
         //String where = "BDCDYH='360122014004GB00091F00040022'";
+
+        String where = "BDCDYH='360122011006GB00014W00000000'";
+
         JsonNode page = client.query(where, withGeometry, 0, PAGE_SIZE);
         JsonNode features = page.path("features");
         //int n = features.size();

+ 3 - 3
siwei-modules/siwei-apply/src/main/java/com/siwei/apply/utils/HttpClientUtil.java

@@ -47,9 +47,9 @@ public class HttpClientUtil {
             HttpGet httpGet = new HttpGet(uri);
             //设置超时设置
             RequestConfig requestConfig = RequestConfig.custom()
-                    .setConnectionRequestTimeout(10000)
-                    .setConnectTimeout(10000)
-                    .setSocketTimeout(10000).build();
+                    .setConnectionRequestTimeout(1000)
+                    .setConnectTimeout(1000)
+                    .setSocketTimeout(3000).build();
             httpGet.setConfig(requestConfig);
             // 执行请求
             response = httpclient.execute(httpGet);

+ 46 - 1
siwei-modules/siwei-apply/src/main/java/com/siwei/apply/utils/ServiceUtil.java

@@ -1,5 +1,8 @@
 package com.siwei.apply.utils;
 
+import com.alibaba.fastjson2.JSON;
+import com.alibaba.fastjson2.JSONObject;
+import com.alibaba.fastjson2.TypeReference;
 import com.fasterxml.jackson.databind.ObjectMapper;
 import com.siwei.apply.domain.NodeAttachment;
 import org.apache.commons.collections4.CollectionUtils;
@@ -32,9 +35,10 @@ public class ServiceUtil {
 
     public static final String THIRD_API_DOMAIN = "http://10.224.1.228:10001";
     public static final String THIRD_API_BASE_URL = "/dsp-register-server/dspRegisterService/proxyService/proxyThirdPlatformService";
-    protected static final Logger logger = LoggerFactory.getLogger(ServiceUtil.class);
 
+    public static final String ES_API_URL = "http://192.168.60.2:9288/search/getDocByWord";
 
+    protected static final Logger logger = LoggerFactory.getLogger(ServiceUtil.class);
 
 
     /**
@@ -243,4 +247,45 @@ public class ServiceUtil {
     }
 
 
+    /**
+     * 使用HttpClient 获取es查询数据
+     *
+     * @throws Exception
+     */
+    public static String[] getElasticsearchResultByWord(String keyWord) throws Exception {
+        String url = ES_API_URL;
+        Map<String, Object> dataMap = new HashMap<>(4);
+        dataMap.put("keyWord", keyWord);
+        String resp = HttpClientUtil.doGet(url, dataMap);
+        if (resp == null) {
+            logger.error("ES查询返回响应为空");
+            return new String[0];
+        }
+        JSONObject root = JSON.parseObject(resp);
+        logger.error("ES查询返回结果为: {} ", root);
+        if (Objects.nonNull(root) && root.containsKey("code") && root.getInteger("code") == 200) {
+            JSONObject innerData = root.getJSONObject("data");
+            String resJsonStr = innerData.getString("res");
+            // 先解析为 List<String>
+            List<String> list = JSON.parseObject(resJsonStr, new TypeReference<List<String>>() {});
+            if (list == null) {
+                return new String[0];
+            }
+            // List转String数组
+            return list.toArray(new String[0]);
+        } else {
+            logger.error("ES查询失败,响应原文:{} ", resp);
+            return new String[0];
+        }
+    }
+
+
+
+    public static void main(String[] args) throws Exception {
+        List lll = List.of(getElasticsearchResultByWord("不动产登记"));
+        logger.info("end result:{} ", lll);
+    }
+
+
+
 }

+ 31 - 0
siwei-modules/siwei-apply/src/main/resources/mapper/ProjectMapper.xml

@@ -514,7 +514,38 @@
                         AND on_chain_num > 0
                     </if>
                 </where>
+                <!-- 这里的if attachmentInfoIdList,仍然执行UNION查询,走es结果查询 -->
+                <if test="attachmentInfoIdList != null and attachmentInfoIdList.size() > 0">
+                        UNION
+                        SELECT
+                        project."id" AS ID,
+                        project."name" AS NAME,
+                        project."code" AS code,
+                        project."company" AS company,
+                        project."created_at" AS createAt,
+                        project."updated_at" AS updateAt,
+                        project."project_type" AS projectType,
+                        project."on_chain_num" AS onChainNum,
+                        project."creator_id" AS creatorId,
+                        project."updated_at" AS updatedAt,
+                        '2'  AS selectType,
+                        info.ID AS attachmentId,
+                        info.node_id AS nodeId,
+                        info.file_name AS  fileName,
+                        info.file_path AS filePath,
+                        ''  AS resourceCode,
+                        ''  AS zl,
+                        ''  AS area
+                        FROM  PUBLIC.t_project_attachment_info info
+                        LEFT JOIN t_project project   on  info.project_id=project.id
+                        WHERE   project.id is not null  AND on_chain_num > 0
+                        AND info.id  IN
+                        <foreach collection="attachmentInfoIdList" item="id" open="(" separator="," close=")">
+                            #{id}
+                        </foreach>
+                </if>  <!-- 这里的end内部的UNION  -->
             </if>
+
             <if test="filterType==0">
                 UNION
             </if>