Jelajahi Sumber

全文检索

suxiaoyang 6 tahun lalu
induk
melakukan
4d46f07c1c

+ 44 - 14
src/main/java/com/yihu/ehr/resource/controller/TextSearchEndPoint.java

@ -6,7 +6,7 @@ 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.resource.service.TextSearchService;
import com.yihu.ehr.solr.SolrUtil;
import com.yihu.ehr.util.rest.Envelop;
import io.swagger.annotations.Api;
@ -20,10 +20,7 @@ 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;
import java.util.*;
/**
 * @author progr1mmer.
@ -39,30 +36,33 @@ public class TextSearchEndPoint extends EnvelopRestEndPoint {
    @Autowired
    private HBaseDao hBaseDao;
    @Autowired
    private ProfileSearchService profileSearchService;
    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 = "page", value = "页数", required = true)
            @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 = "分页大小", required = true)
            @ApiParam(name = "size", value = "分页大小", defaultValue = "15", required = true)
            @RequestParam(value = "size") Integer size) throws Exception {
        if (page == 0) {
        if (0 == page) {
            page ++;
        }
        //更新热搜
        profileSearchService.saveOrUpdateHotWords(keyword, 1);
        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), null, sortMap, page - 1, size, ResourceCells.SEARCH_FIELD, null);
        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));
@ -76,6 +76,13 @@ public class TextSearchEndPoint extends EnvelopRestEndPoint {
            } 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));
@ -94,7 +101,7 @@ public class TextSearchEndPoint extends EnvelopRestEndPoint {
            @RequestParam(value = "reCount") int reCount,
            @ApiParam(name = "type", value = "类型,1:病人档案热搜", required = true)
            @RequestParam(value = "type") int type){
        List<String> hotWords = profileSearchService.getHotWords(type,reCount);
        List<String> hotWords = textSearchService.getHotWords(type, reCount);
        return hotWords;
    }
@ -105,10 +112,10 @@ public class TextSearchEndPoint extends EnvelopRestEndPoint {
            @RequestParam(value = "searchText") String searchText,
            @ApiParam(name = "type", value = "类型,1:病人档案热搜", required = true)
            @RequestParam(value = "type") int type){
        return profileSearchService.saveOrUpdateHotWords(searchText,type);
        return textSearchService.saveOrUpdateHotWords(searchText, type);
    }
    private String generateQ(String keyword) {
    private String generateQ(String keyword, String filters) {
        if (StringUtils.isEmpty(keyword)) {
            return "*:*";
        }
@ -136,6 +143,29 @@ public class TextSearchEndPoint extends EnvelopRestEndPoint {
        } 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();
    }
}

+ 3 - 6
src/main/java/com/yihu/ehr/resource/job/HotWordsTask.java

@ -1,20 +1,17 @@
package com.yihu.ehr.resource.job;
import com.yihu.ehr.resource.service.ProfileSearchService;
import com.yihu.ehr.resource.service.TextSearchService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ZSetOperations;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import java.util.Set;
@Component
public class HotWordsTask {
    @Autowired
    private ProfileSearchService profileSearchService;
    private TextSearchService textSearchService;
    @Autowired
    private RedisTemplate redisTemplate;
@ -28,7 +25,7 @@ public class HotWordsTask {
        try {
            lock = redisTemplate.opsForValue().setIfAbsent(KEY, LOCK);
            if (lock) {
                profileSearchService.synRedisHotWords(1);
                textSearchService.synRedisHotWords(1);
            }
        } finally {
            if (lock) {

+ 5 - 2
src/main/java/com/yihu/ehr/resource/service/ProfileSearchService.java

@ -19,11 +19,14 @@ import java.util.regex.Pattern;
@Service
@Transactional
public class ProfileSearchService extends BaseJpaService<HotWord, HotWordDao> {
public class TextSearchService extends BaseJpaService<HotWord, HotWordDao> {
    private static final String PrefixRedis = "HOTWORDS_";
    private static final Pattern p = Pattern.compile("\"(.*?)\"");//正则提取引号的内容
    /**
     * 正则提取引号的内容
     */
    private static final Pattern p = Pattern.compile("\"(.*?)\"");
    @Autowired
    private RedisTemplate redisTemplate;