Browse Source

add search total count

kerry 5 years ago
parent
commit
04d0225321

+ 1 - 1
elastic-publish-service/req/main-controller.http View File

@@ -21,7 +21,7 @@ Content-Type: application/json
21 21
 
22 22
 {
23 23
   "agencyPath": "/1",
24
-  "keyword": "",
24
+  "keyword": "教学楼四教",
25 25
   "page": 1,
26 26
   "size": 10,
27 27
   "regulatoryLevelId": "",

+ 1 - 0
elastic-publish-service/src/main/java/com/vcarecity/publish/api/ApiResult.java View File

@@ -14,4 +14,5 @@ public class ApiResult {
14 14
     private String message;
15 15
     private int errorCode;
16 16
     private Object data;
17
+    private Long total;
17 18
 }

+ 13 - 4
elastic-publish-service/src/main/java/com/vcarecity/publish/controller/MainController.java View File

@@ -5,6 +5,7 @@ import com.vcarecity.publish.api.ApiResult;
5 5
 import com.vcarecity.publish.api.ApiStatus;
6 6
 import com.vcarecity.publish.constants.ElasticConstant;
7 7
 import com.vcarecity.publish.elastic.service.ElasticLoadDataService;
8
+import com.vcarecity.publish.pojo.dto.TotalResultDTO;
8 9
 import com.vcarecity.publish.pojo.dto.UnitAgencyDTO;
9 10
 import com.vcarecity.publish.pojo.dto.UnitIdDTO;
10 11
 import com.vcarecity.publish.pojo.query.UnitAgencyQuery;
@@ -73,15 +74,23 @@ public class MainController implements PrepareController {
73 74
     @PostMapping("/query-unit-agency")
74 75
     public ApiResult queryUnitAgency(@RequestBody @Valid UnitAgencyQuery unitAgencyQuery) throws IOException {
75 76
 
76
-        List<UnitIdDTO> data = unitService.getUnitAgencyByQuery(unitAgencyQuery, UnitIdDTO.class, ElasticConstant.UNIT_AGENCY_INDEX);
77
+        TotalResultDTO<List<UnitIdDTO>> totalResult = unitService.getUnitAgencyByQuery(unitAgencyQuery, UnitIdDTO.class, ElasticConstant.UNIT_AGENCY_INDEX);
77 78
 
78
-        return ApiResult.builder().status(ApiStatus.SUCCESS_CODE).data(data).build();
79
+        return ApiResult.builder()
80
+                .status(ApiStatus.SUCCESS_CODE)
81
+                .data(totalResult.getData())
82
+                .total(totalResult.getTotal())
83
+                .build();
79 84
     }
80 85
 
81 86
     @PostMapping("/test/query-unit-agency")
82 87
     public ApiResult testQueryUnitAgency(@RequestBody @Valid UnitAgencyQuery unitAgencyQuery) throws IOException {
83
-        List<UnitAgencyDTO> data = unitService.getUnitAgencyByQuery(unitAgencyQuery, UnitAgencyDTO.class, ElasticConstant.UNIT_AGENCY_INDEX);
84
-        return ApiResult.builder().status(ApiStatus.SUCCESS_CODE).data(data).build();
88
+        TotalResultDTO<List<UnitAgencyDTO>> totalResult = unitService.getUnitAgencyByQuery(unitAgencyQuery, UnitAgencyDTO.class, ElasticConstant.UNIT_AGENCY_INDEX);
89
+        return ApiResult.builder()
90
+                .status(ApiStatus.SUCCESS_CODE)
91
+                .data(totalResult.getData())
92
+                .total(totalResult.getTotal())
93
+                .build();
85 94
     }
86 95
 
87 96
 

+ 17 - 0
elastic-publish-service/src/main/java/com/vcarecity/publish/pojo/dto/TotalResultDTO.java View File

@@ -0,0 +1,17 @@
1
+package com.vcarecity.publish.pojo.dto;
2
+
3
+import lombok.AllArgsConstructor;
4
+import lombok.Data;
5
+import lombok.NoArgsConstructor;
6
+
7
+/**
8
+ * @author VcKerry on 12/23/19
9
+ */
10
+
11
+@NoArgsConstructor
12
+@Data
13
+@AllArgsConstructor
14
+public class TotalResultDTO<T> {
15
+    private T data;
16
+    private Long total;
17
+}

+ 2 - 1
elastic-publish-service/src/main/java/com/vcarecity/publish/sql/service/UnitService.java View File

@@ -1,5 +1,6 @@
1 1
 package com.vcarecity.publish.sql.service;
2 2
 
3
+import com.vcarecity.publish.pojo.dto.TotalResultDTO;
3 4
 import com.vcarecity.publish.pojo.dto.UnitIdDTO;
4 5
 import com.vcarecity.publish.pojo.query.UnitAgencyQuery;
5 6
 
@@ -21,6 +22,6 @@ public interface UnitService {
21 22
      * @return
22 23
      * @throws IOException
23 24
      */
24
-    <T extends UnitIdDTO> List<T> getUnitAgencyByQuery(UnitAgencyQuery queryParam, Class<T> cls, String... indices) throws IOException;
25
+    <T extends UnitIdDTO> TotalResultDTO<List<T>> getUnitAgencyByQuery(UnitAgencyQuery queryParam, Class<T> cls, String... indices) throws IOException;
25 26
 
26 27
 }

+ 7 - 3
elastic-publish-service/src/main/java/com/vcarecity/publish/sql/service/impl/UnitServiceImpl.java View File

@@ -2,6 +2,7 @@ package com.vcarecity.publish.sql.service.impl;
2 2
 
3 3
 import com.fasterxml.jackson.databind.ObjectMapper;
4 4
 import com.vcarecity.elastic.util.AgencyPathUtil;
5
+import com.vcarecity.publish.pojo.dto.TotalResultDTO;
5 6
 import com.vcarecity.publish.pojo.dto.UnitIdDTO;
6 7
 import com.vcarecity.publish.pojo.query.UnitAgencyQuery;
7 8
 import com.vcarecity.publish.sql.service.UnitService;
@@ -60,12 +61,12 @@ public class UnitServiceImpl implements UnitService {
60 61
     }
61 62
 
62 63
     @Override
63
-    public <T extends UnitIdDTO> List<T> getUnitAgencyByQuery(UnitAgencyQuery queryParam, Class<T> cls, String... indices) throws IOException {
64
+    public <T extends UnitIdDTO> TotalResultDTO<List<T>> getUnitAgencyByQuery(UnitAgencyQuery queryParam, Class<T> cls, String... indices) throws IOException {
64 65
 
65 66
 
66 67
         List<Integer> agencyIdList = AgencyPathUtil.splitAgencyId(queryParam.getAgencyPath());
67 68
         if (agencyIdList.isEmpty()) {
68
-            return Collections.emptyList();
69
+            return new TotalResultDTO<>(Collections.emptyList(), 0L);
69 70
         }
70 71
 
71 72
         String[] queryFields = ObjectFieldToMap.getField(cls);
@@ -107,13 +108,16 @@ public class UnitServiceImpl implements UnitService {
107 108
 
108 109
         final SearchHits hits = searchResponse.getHits();
109 110
 
111
+        long total = hits.getTotalHits().value;
110 112
 
111 113
         List<T> res = new ArrayList<>(hits.getHits().length);
114
+
115
+
112 116
         for (SearchHit hit : hits.getHits()) {
113 117
             T t = objectMapper.readValue(hit.getSourceAsString(), cls);
114 118
             res.add(t);
115 119
         }
116
-        return res;
120
+        return new TotalResultDTO<>(res, total);
117 121
     }
118 122
 
119 123
     /**