123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171 |
- 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.TextSearchService;
- 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.*;
- /**
- * @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 TextSearchService textSearchService;
- @ApiOperation(value = "全文检索")
- @RequestMapping(value = MicroServiceApi.TextSearch.TextSearch, method = RequestMethod.GET)
- public Envelop countEhrCenter(
- @ApiParam(name = "keyword", value = "关键词")
- @RequestParam(value = "keyword", required = false) String keyword,
- @ApiParam(name = "filters", value = "过滤条件")
- @RequestParam(value = "filters", required = false) String filters,
- @ApiParam(name = "page", value = "页数", defaultValue = "1", required = true)
- @RequestParam(value = "page") Integer page,
- @ApiParam(name = "size", value = "分页大小", defaultValue = "15", required = true)
- @RequestParam(value = "size") Integer size) throws Exception {
- if (0 == page) {
- page ++;
- }
- //更新热搜
- textSearchService.saveOrUpdateHotWords(keyword, 1);
- Map<String, String> sortMap = new HashMap<>(1);
- sortMap.put(ResourceCells.EVENT_DATE, "desc");
- QueryResponse queryResponse = solrUtil.highlight(ResourceCore.MasterTable, generateQ(keyword, filters), null, sortMap, page - 1, size, ResourceCells.SEARCH_FIELD, null);
- List<Map<String, Object>> dataList = new ArrayList<>(queryResponse.getHighlighting().size());
- queryResponse.getResults().forEach(document -> {
- Map<String, Object> result = new HashMap<>();
- String rowkey = (String) document.get(ResourceCells.ROWKEY);
- Map<String, Object> data = hBaseDao.getResultMap(ResourceCore.MasterTable, rowkey);
- result.put(ResourceCells.DEMOGRAPHIC_ID, data.get(ResourceCells.DEMOGRAPHIC_ID));
- 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", "");
- }
- if (data.get("EHR_000079") != null) {
- result.put("doctor", data.get("EHR_000079"));
- } else if (data.get("EHR_000172") != null) {
- result.put("doctor", data.get("EHR_000172"));
- } else {
- result.put("doctor", "");
- }
- 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.TextSearch.HotWords, method = RequestMethod.GET)
- public List<String> 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<String> hotWords = textSearchService.getHotWords(type, reCount);
- return hotWords;
- }
- @ApiOperation("更新热搜")
- @RequestMapping(value = MicroServiceApi.TextSearch.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 textSearchService.saveOrUpdateHotWords(searchText, type);
- }
- private String generateQ(String keyword, String filters) {
- 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);
- }
- if (StringUtils.isNotEmpty(filters)) {
- filters.split(":");
- }
- return getQuery(filters, q);
- }
- private String getQuery(String filters, StringBuilder q) {
- if (!StringUtils.isEmpty(filters)) {
- String [] conditions = filters.split(";");
- for (String condition : conditions) {
- if (condition.split("=").length == 2) {
- String key = condition.split("=")[0];
- String value = condition.split("=")[1];
- q.append(" AND " + key + ":" + value);
- }
- if (condition.split("\\?").length == 2) {
- String key = condition.split("\\?")[0];
- String value = condition.split("\\?")[1];
- q.append(" AND " + key + ":*" + value + "*");
- }
- continue;
- }
- }
- return q.toString();
- }
- }
|