TextSearchEndPoint.java 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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.ProfileSearchService;
  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.ArrayList;
  22. import java.util.HashMap;
  23. import java.util.List;
  24. import java.util.Map;
  25. /**
  26. * @author progr1mmer.
  27. * @date Created on 2018/9/18.
  28. */
  29. @RestController
  30. @RequestMapping(value = ApiVersion.Version1_0)
  31. @Api(value = "TextSearchEndPoint", description = "全文检索", tags = {"资源服务(大数据应用) - 全文检索"})
  32. public class TextSearchEndPoint extends EnvelopRestEndPoint {
  33. @Autowired
  34. private SolrUtil solrUtil;
  35. @Autowired
  36. private HBaseDao hBaseDao;
  37. @Autowired
  38. private ProfileSearchService profileSearchService;
  39. @ApiOperation(value = "全文检索")
  40. @RequestMapping(value = "/resources/text/search", method = RequestMethod.GET)
  41. public Envelop countEhrCenter(
  42. @ApiParam(name = "keyword", value = "关键词")
  43. @RequestParam(value = "keyword", required = false) String keyword,
  44. @ApiParam(name = "page", value = "页数", required = true)
  45. @RequestParam(value = "page") Integer page,
  46. @ApiParam(name = "size", value = "分页大小", required = true)
  47. @RequestParam(value = "size") Integer size) throws Exception {
  48. if (page == 0) {
  49. page ++;
  50. }
  51. //更新热搜
  52. profileSearchService.saveOrUpdateHotWords(keyword, 1);
  53. Map<String, String> sortMap = new HashMap<>(1);
  54. sortMap.put(ResourceCells.EVENT_DATE, "desc");
  55. QueryResponse queryResponse = solrUtil.highlight(ResourceCore.MasterTable, generateQ(keyword), null, sortMap, page - 1, size, ResourceCells.SEARCH_FIELD, null);
  56. List<Map<String, Object>> dataList = new ArrayList<>(queryResponse.getHighlighting().size());
  57. queryResponse.getResults().forEach(document -> {
  58. Map<String, Object> result = new HashMap<>();
  59. String rowkey = (String) document.get(ResourceCells.ROWKEY);
  60. Map<String, Object> data = hBaseDao.getResultMap(ResourceCore.MasterTable, rowkey);
  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. result.put(ResourceCells.ORG_NAME, data.get(ResourceCells.ORG_NAME));
  75. if (queryResponse.getHighlighting().get(rowkey).get(ResourceCells.SEARCH_FIELD) != null) {
  76. result.put("hl", queryResponse.getHighlighting().get(rowkey).get(ResourceCells.SEARCH_FIELD).get(0));
  77. } else {
  78. result.put("hl", "");
  79. }
  80. dataList.add(result);
  81. });
  82. return success(dataList, (int)queryResponse.getResults().getNumFound(), page, size);
  83. }
  84. @ApiOperation("获取热搜")
  85. @RequestMapping(value = MicroServiceApi.ProfileSearch.Hotwords, method = RequestMethod.GET)
  86. public List<String> getHotWords(
  87. @ApiParam(name = "reCount", value = "返回个数", required = true)
  88. @RequestParam(value = "reCount") int reCount,
  89. @ApiParam(name = "type", value = "类型,1:病人档案热搜", required = true)
  90. @RequestParam(value = "type") int type){
  91. List<String> hotWords = profileSearchService.getHotWords(type,reCount);
  92. return hotWords;
  93. }
  94. @ApiOperation("更新热搜")
  95. @RequestMapping(value = MicroServiceApi.ProfileSearch.Hotwords, method = RequestMethod.POST)
  96. public boolean updateHotWords(
  97. @ApiParam(name = "searchText", value = "搜索关键词", required = true)
  98. @RequestParam(value = "searchText") String searchText,
  99. @ApiParam(name = "type", value = "类型,1:病人档案热搜", required = true)
  100. @RequestParam(value = "type") int type){
  101. return profileSearchService.saveOrUpdateHotWords(searchText,type);
  102. }
  103. private String generateQ(String keyword) {
  104. if (StringUtils.isEmpty(keyword)) {
  105. return "*:*";
  106. }
  107. StringBuilder q = new StringBuilder();
  108. String plus = "+";
  109. String or = "or";
  110. if (keyword.contains(plus)) {
  111. String [] keys = keyword.split("\\+");
  112. for (String key : keys) {
  113. if (0 == q.length()) {
  114. q.append(ResourceCells.SEARCH_FIELD + ":" + key.trim());
  115. } else {
  116. q.append(" AND " + ResourceCells.SEARCH_FIELD + ":" + key.trim());
  117. }
  118. }
  119. } else if (keyword.contains(or)) {
  120. String [] keys = keyword.split(or);
  121. for (String key : keys) {
  122. if (0 == q.length()) {
  123. q.append(ResourceCells.SEARCH_FIELD + ":" + key.trim());
  124. } else {
  125. q.append(" OR " + ResourceCells.SEARCH_FIELD + ":" + key.trim());
  126. }
  127. }
  128. } else {
  129. q.append(ResourceCells.SEARCH_FIELD + ":" + keyword);
  130. }
  131. return q.toString();
  132. }
  133. }