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
 
21
 
22
 {
22
 {
23
   "agencyPath": "/1",
23
   "agencyPath": "/1",
24
-  "keyword": "",
24
+  "keyword": "教学楼四教",
25
   "page": 1,
25
   "page": 1,
26
   "size": 10,
26
   "size": 10,
27
   "regulatoryLevelId": "",
27
   "regulatoryLevelId": "",

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

14
     private String message;
14
     private String message;
15
     private int errorCode;
15
     private int errorCode;
16
     private Object data;
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
 import com.vcarecity.publish.api.ApiStatus;
5
 import com.vcarecity.publish.api.ApiStatus;
6
 import com.vcarecity.publish.constants.ElasticConstant;
6
 import com.vcarecity.publish.constants.ElasticConstant;
7
 import com.vcarecity.publish.elastic.service.ElasticLoadDataService;
7
 import com.vcarecity.publish.elastic.service.ElasticLoadDataService;
8
+import com.vcarecity.publish.pojo.dto.TotalResultDTO;
8
 import com.vcarecity.publish.pojo.dto.UnitAgencyDTO;
9
 import com.vcarecity.publish.pojo.dto.UnitAgencyDTO;
9
 import com.vcarecity.publish.pojo.dto.UnitIdDTO;
10
 import com.vcarecity.publish.pojo.dto.UnitIdDTO;
10
 import com.vcarecity.publish.pojo.query.UnitAgencyQuery;
11
 import com.vcarecity.publish.pojo.query.UnitAgencyQuery;
73
     @PostMapping("/query-unit-agency")
74
     @PostMapping("/query-unit-agency")
74
     public ApiResult queryUnitAgency(@RequestBody @Valid UnitAgencyQuery unitAgencyQuery) throws IOException {
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
     @PostMapping("/test/query-unit-agency")
86
     @PostMapping("/test/query-unit-agency")
82
     public ApiResult testQueryUnitAgency(@RequestBody @Valid UnitAgencyQuery unitAgencyQuery) throws IOException {
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

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