Browse Source

全文检索

suxiaoyang 6 years ago
parent
commit
56995f9aab
1 changed files with 114 additions and 0 deletions
  1. 114 0
      src/main/java/com/yihu/ehr/resource/controller/TextSearchEndPoint.java

+ 114 - 0
src/main/java/com/yihu/ehr/resource/controller/TextSearchEndPoint.java

@ -0,0 +1,114 @@
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.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;
    @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 ++;
        }
        Map<String, String> 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<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.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);
    }
    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();
    }
}