package com.yihu.ehr.resource.controller; import com.yihu.ehr.constants.ApiVersion; import com.yihu.ehr.controller.EnvelopRestEndPoint; import com.yihu.ehr.hbase.HBaseDao; import com.yihu.ehr.profile.core.ResourceCore; import com.yihu.ehr.profile.family.ResourceCells; import com.yihu.ehr.resource.constants.MicroServiceApi; import com.yihu.ehr.resource.service.ProfileSearchService; import com.yihu.ehr.solr.SolrUtil; import com.yihu.ehr.util.rest.Envelop; import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; import io.swagger.annotations.ApiParam; import org.apache.commons.lang3.StringUtils; import org.apache.solr.client.solrj.response.QueryResponse; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; /** * @author progr1mmer. * @date Created on 2018/9/18. */ @RestController @RequestMapping(value = ApiVersion.Version1_0) @Api(value = "TextSearchEndPoint", description = "全文检索", tags = {"资源服务(大数据应用) - 全文检索"}) public class TextSearchEndPoint extends EnvelopRestEndPoint { @Autowired private SolrUtil solrUtil; @Autowired private HBaseDao hBaseDao; @Autowired private ProfileSearchService profileSearchService; @ApiOperation(value = "全文检索") @RequestMapping(value = "/resources/text/search", method = RequestMethod.GET) public Envelop countEhrCenter( @ApiParam(name = "keyword", value = "关键词") @RequestParam(value = "keyword", required = false) String keyword, @ApiParam(name = "page", value = "页数", required = true) @RequestParam(value = "page") Integer page, @ApiParam(name = "size", value = "分页大小", required = true) @RequestParam(value = "size") Integer size) throws Exception { if (page == 0) { page ++; } //更新热搜 profileSearchService.saveOrUpdateHotWords(keyword, 1); Map sortMap = new HashMap<>(1); sortMap.put(ResourceCells.EVENT_DATE, "desc"); QueryResponse queryResponse = solrUtil.highlight(ResourceCore.MasterTable, generateQ(keyword), null, sortMap, page - 1, size, ResourceCells.SEARCH_FIELD, null); List> dataList = new ArrayList<>(queryResponse.getHighlighting().size()); queryResponse.getResults().forEach(document -> { Map result = new HashMap<>(); String rowkey = (String) document.get(ResourceCells.ROWKEY); Map data = hBaseDao.getResultMap(ResourceCore.MasterTable, rowkey); result.put(ResourceCells.ROWKEY, data.get(ResourceCells.ROWKEY)); result.put(ResourceCells.EVENT_DATE, data.get(ResourceCells.EVENT_DATE)); result.put(ResourceCells.EVENT_TYPE, data.get(ResourceCells.EVENT_TYPE)); result.put(ResourceCells.PATIENT_NAME, data.get(ResourceCells.PATIENT_NAME) != null? data.get(ResourceCells.PATIENT_NAME) : ""); result.put(ResourceCells.PATIENT_SEX, data.get(ResourceCells.PATIENT_SEX) != null? data.get(ResourceCells.PATIENT_SEX) : ""); result.put("patient_birthday", data.get("EHR_000007")); if (data.get("EHR_000082")!= null) { result.put("dept_name", data.get("EHR_000082")); } else if (data.get("EHR_000229") != null) { result.put("dept_name", data.get("EHR_000229")); } else { result.put("dept_name", ""); } result.put(ResourceCells.ORG_NAME, data.get(ResourceCells.ORG_NAME)); if (queryResponse.getHighlighting().get(rowkey).get(ResourceCells.SEARCH_FIELD) != null) { result.put("hl", queryResponse.getHighlighting().get(rowkey).get(ResourceCells.SEARCH_FIELD).get(0)); } else { result.put("hl", ""); } dataList.add(result); }); return success(dataList, (int)queryResponse.getResults().getNumFound(), page, size); } @ApiOperation("获取热搜") @RequestMapping(value = MicroServiceApi.ProfileSearch.Hotwords, method = RequestMethod.GET) public List getHotWords( @ApiParam(name = "reCount", value = "返回个数", required = true) @RequestParam(value = "reCount") int reCount, @ApiParam(name = "type", value = "类型,1:病人档案热搜", required = true) @RequestParam(value = "type") int type){ List hotWords = profileSearchService.getHotWords(type,reCount); return hotWords; } @ApiOperation("更新热搜") @RequestMapping(value = MicroServiceApi.ProfileSearch.Hotwords, method = RequestMethod.POST) public boolean updateHotWords( @ApiParam(name = "searchText", value = "搜索关键词", required = true) @RequestParam(value = "searchText") String searchText, @ApiParam(name = "type", value = "类型,1:病人档案热搜", required = true) @RequestParam(value = "type") int type){ return profileSearchService.saveOrUpdateHotWords(searchText,type); } private String generateQ(String keyword) { if (StringUtils.isEmpty(keyword)) { return "*:*"; } StringBuilder q = new StringBuilder(); String plus = "+"; String or = "or"; if (keyword.contains(plus)) { String [] keys = keyword.split("\\+"); for (String key : keys) { if (0 == q.length()) { q.append(ResourceCells.SEARCH_FIELD + ":" + key.trim()); } else { q.append(" AND " + ResourceCells.SEARCH_FIELD + ":" + key.trim()); } } } else if (keyword.contains(or)) { String [] keys = keyword.split(or); for (String key : keys) { if (0 == q.length()) { q.append(ResourceCells.SEARCH_FIELD + ":" + key.trim()); } else { q.append(" OR " + ResourceCells.SEARCH_FIELD + ":" + key.trim()); } } } else { q.append(ResourceCells.SEARCH_FIELD + ":" + keyword); } return q.toString(); } }