TextSearchEndPoint.java 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. package com.yihu.ehr.resource.controller;
  2. import com.yihu.ehr.constants.ApiVersion;
  3. import com.yihu.ehr.controller.EnvelopRestEndPoint;
  4. import com.yihu.ehr.hbase.HBaseDao;
  5. import com.yihu.ehr.profile.core.ResourceCore;
  6. import com.yihu.ehr.profile.family.ResourceCells;
  7. import com.yihu.ehr.resource.constants.MicroServiceApi;
  8. import com.yihu.ehr.resource.service.TextSearchService;
  9. import com.yihu.ehr.solr.SolrUtil;
  10. import com.yihu.ehr.util.rest.Envelop;
  11. import io.swagger.annotations.Api;
  12. import io.swagger.annotations.ApiOperation;
  13. import io.swagger.annotations.ApiParam;
  14. import org.apache.commons.lang3.StringUtils;
  15. import org.apache.solr.client.solrj.response.QueryResponse;
  16. import org.springframework.beans.factory.annotation.Autowired;
  17. import org.springframework.web.bind.annotation.RequestMapping;
  18. import org.springframework.web.bind.annotation.RequestMethod;
  19. import org.springframework.web.bind.annotation.RequestParam;
  20. import org.springframework.web.bind.annotation.RestController;
  21. import java.util.*;
  22. /**
  23. * @author progr1mmer.
  24. * @date Created on 2018/9/18.
  25. */
  26. @RestController
  27. @RequestMapping(value = ApiVersion.Version1_0)
  28. @Api(value = "TextSearchEndPoint", description = "全文检索", tags = {"资源服务(大数据应用) - 全文检索"})
  29. public class TextSearchEndPoint extends EnvelopRestEndPoint {
  30. @Autowired
  31. private SolrUtil solrUtil;
  32. @Autowired
  33. private HBaseDao hBaseDao;
  34. @Autowired
  35. private TextSearchService textSearchService;
  36. @ApiOperation(value = "全文检索")
  37. @RequestMapping(value = MicroServiceApi.TextSearch.TextSearch, method = RequestMethod.GET)
  38. public Envelop countEhrCenter(
  39. @ApiParam(name = "keyword", value = "关键词")
  40. @RequestParam(value = "keyword", required = false) String keyword,
  41. @ApiParam(name = "filters", value = "过滤条件")
  42. @RequestParam(value = "filters", required = false) String filters,
  43. @ApiParam(name = "page", value = "页数", defaultValue = "1", required = true)
  44. @RequestParam(value = "page") Integer page,
  45. @ApiParam(name = "size", value = "分页大小", defaultValue = "15", required = true)
  46. @RequestParam(value = "size") Integer size) throws Exception {
  47. if (0 == page) {
  48. page ++;
  49. }
  50. //更新热搜
  51. textSearchService.saveOrUpdateHotWords(keyword, 1);
  52. Map<String, String> sortMap = new HashMap<>(1);
  53. sortMap.put(ResourceCells.EVENT_DATE, "desc");
  54. QueryResponse queryResponse = solrUtil.highlight(ResourceCore.MasterTable, generateQ(keyword, filters), null, sortMap, page - 1, size, ResourceCells.SEARCH_FIELD, null);
  55. List<Map<String, Object>> dataList = new ArrayList<>(queryResponse.getHighlighting().size());
  56. queryResponse.getResults().forEach(document -> {
  57. Map<String, Object> result = new HashMap<>();
  58. String rowkey = (String) document.get(ResourceCells.ROWKEY);
  59. Map<String, Object> data = hBaseDao.getResultMap(ResourceCore.MasterTable, rowkey);
  60. result.put(ResourceCells.DEMOGRAPHIC_ID, data.get(ResourceCells.DEMOGRAPHIC_ID));
  61. result.put(ResourceCells.ROWKEY, data.get(ResourceCells.ROWKEY));
  62. result.put(ResourceCells.EVENT_DATE, data.get(ResourceCells.EVENT_DATE));
  63. result.put(ResourceCells.EVENT_TYPE, data.get(ResourceCells.EVENT_TYPE));
  64. result.put(ResourceCells.PATIENT_NAME, data.get(ResourceCells.PATIENT_NAME) != null? data.get(ResourceCells.PATIENT_NAME) : "");
  65. result.put(ResourceCells.PATIENT_SEX, data.get(ResourceCells.PATIENT_SEX) != null? data.get(ResourceCells.PATIENT_SEX) : "");
  66. result.put("patient_birthday", data.get("EHR_000007"));
  67. if (data.get("EHR_000082")!= null) {
  68. result.put("dept_name", data.get("EHR_000082"));
  69. } else if (data.get("EHR_000229") != null) {
  70. result.put("dept_name", data.get("EHR_000229"));
  71. } else {
  72. result.put("dept_name", "");
  73. }
  74. if (data.get("EHR_000079") != null) {
  75. result.put("doctor", data.get("EHR_000079"));
  76. } else if (data.get("EHR_000172") != null) {
  77. result.put("doctor", data.get("EHR_000172"));
  78. } else {
  79. result.put("doctor", "");
  80. }
  81. result.put(ResourceCells.ORG_NAME, data.get(ResourceCells.ORG_NAME));
  82. if (queryResponse.getHighlighting().get(rowkey).get(ResourceCells.SEARCH_FIELD) != null) {
  83. result.put("hl", queryResponse.getHighlighting().get(rowkey).get(ResourceCells.SEARCH_FIELD).get(0));
  84. } else {
  85. result.put("hl", "");
  86. }
  87. dataList.add(result);
  88. });
  89. return success(dataList, (int)queryResponse.getResults().getNumFound(), page, size);
  90. }
  91. @ApiOperation("获取热搜")
  92. @RequestMapping(value = MicroServiceApi.TextSearch.HotWords, method = RequestMethod.GET)
  93. public List<String> getHotWords(
  94. @ApiParam(name = "reCount", value = "返回个数", required = true)
  95. @RequestParam(value = "reCount") int reCount,
  96. @ApiParam(name = "type", value = "类型,1:病人档案热搜", required = true)
  97. @RequestParam(value = "type") int type){
  98. List<String> hotWords = textSearchService.getHotWords(type, reCount);
  99. return hotWords;
  100. }
  101. @ApiOperation("更新热搜")
  102. @RequestMapping(value = MicroServiceApi.TextSearch.HotWords, method = RequestMethod.POST)
  103. public boolean updateHotWords(
  104. @ApiParam(name = "searchText", value = "搜索关键词", required = true)
  105. @RequestParam(value = "searchText") String searchText,
  106. @ApiParam(name = "type", value = "类型,1:病人档案热搜", required = true)
  107. @RequestParam(value = "type") int type){
  108. return textSearchService.saveOrUpdateHotWords(searchText, type);
  109. }
  110. private String generateQ(String keyword, String filters) {
  111. if (StringUtils.isEmpty(keyword)) {
  112. return "*:*";
  113. }
  114. StringBuilder q = new StringBuilder();
  115. String plus = "+";
  116. String or = "or";
  117. if (keyword.contains(plus)) {
  118. String [] keys = keyword.split("\\+");
  119. for (String key : keys) {
  120. if (0 == q.length()) {
  121. q.append(ResourceCells.SEARCH_FIELD + ":" + key.trim());
  122. } else {
  123. q.append(" AND " + ResourceCells.SEARCH_FIELD + ":" + key.trim());
  124. }
  125. }
  126. } else if (keyword.contains(or)) {
  127. String [] keys = keyword.split(or);
  128. for (String key : keys) {
  129. if (0 == q.length()) {
  130. q.append(ResourceCells.SEARCH_FIELD + ":" + key.trim());
  131. } else {
  132. q.append(" OR " + ResourceCells.SEARCH_FIELD + ":" + key.trim());
  133. }
  134. }
  135. } else {
  136. q.append(ResourceCells.SEARCH_FIELD + ":" + keyword);
  137. }
  138. if (StringUtils.isNotEmpty(filters)) {
  139. filters.split(":");
  140. }
  141. return getQuery(filters, q);
  142. }
  143. private String getQuery(String filters, StringBuilder q) {
  144. if (!StringUtils.isEmpty(filters)) {
  145. String [] conditions = filters.split(";");
  146. for (String condition : conditions) {
  147. if (condition.split("=").length == 2) {
  148. String key = condition.split("=")[0];
  149. String value = condition.split("=")[1];
  150. q.append(" AND " + key + ":" + value);
  151. }
  152. if (condition.split("\\?").length == 2) {
  153. String key = condition.split("\\?")[0];
  154. String value = condition.split("\\?")[1];
  155. q.append(" AND " + key + ":*" + value + "*");
  156. }
  157. continue;
  158. }
  159. }
  160. return q.toString();
  161. }
  162. }