Prechádzať zdrojové kódy

中文分词可能产生的高亮问题

suxiaoyang 6 rokov pred
rodič
commit
c886da0e5e

+ 70 - 65
src/main/java/com/yihu/ehr/resource/controller/TextSearchEndPoint.java

@ -57,7 +57,7 @@ public class TextSearchEndPoint extends EnvelopRestEndPoint {
        textSearchService.saveOrUpdateHotWords(keyword, 1);
        Map<String, String> sortMap = new HashMap<>(1);
        sortMap.put(ResourceCells.EVENT_DATE, "desc");
        String q = getQueryBySearchText(keyword,filters);
        String q = getQueryBySearchText(keyword, filters);
        QueryResponse queryResponse = solrUtil.highlight(ResourceCore.MasterTable, q, null, sortMap, page - 1, size, ResourceCells.SEARCH_FIELD, null);
        List<Map<String, Object>> dataList = new ArrayList<>(queryResponse.getHighlighting().size());
        queryResponse.getResults().forEach(document -> {
@ -87,7 +87,12 @@ public class TextSearchEndPoint extends EnvelopRestEndPoint {
            }
            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));
                String hl = queryResponse.getHighlighting().get(rowkey).get(ResourceCells.SEARCH_FIELD).get(0);
                String pre = "<em>";
                if (!hl.contains(pre)) {
                    hl = hl.replaceAll(keyword, "<em>" + keyword + "</em>");
                }
                result.put("hl", hl);
            } else {
                result.put("hl", "");
            }
@ -171,17 +176,17 @@ public class TextSearchEndPoint extends EnvelopRestEndPoint {
        return q.toString();
    }
    private String getQueryBySearchText(String keys,String filters){
    private String getQueryBySearchText(String keys, String filters){
        String q = "";
        //用于存储某个词
        String temp = "";
        //用于存储某个词的开头
        String start="";
        String start = "";
        //未找过的字符串
        String otherString = "";
        //用于判断 and or ,上个词
        String preString ="and";
        if(StringUtils.isNotBlank(keys)) {
        String preString = "and";
        if (StringUtils.isNotBlank(keys)) {
            for (int i = 0; i < keys.length(); i++) {
                String ch = String.valueOf(keys.charAt(i));
                if (StringUtils.isBlank(start)) {
@ -195,33 +200,33 @@ public class TextSearchEndPoint extends EnvelopRestEndPoint {
                //以"开头  ,查找后面是否有 " ,有则截取
                if ("\"".equals(start)){
                    int fhIndex = otherString.indexOf("\"");
                    if(fhIndex > -1){
                    if (fhIndex > -1){
                        temp = keys.substring(i+1,i+fhIndex+1);
                        if(!"or".equalsIgnoreCase(preString)){
                        if (!"or".equalsIgnoreCase(preString)){
                            q = builderAndQuery(temp,q);
                        }else{
                        } else {
                            q = builderOrQuery(temp,q);
                        }
                        i = fhIndex+i+1;
                        start = "";
                        preString = temp;
                    }else{
                    } else {
                        int kgIndex = otherString.indexOf(" ");
                        if(kgIndex > -1){
                        if (kgIndex > -1){
                            temp = keys.substring(i,i+kgIndex+1);
                            if(!"or".equalsIgnoreCase(preString)){
                            if (!"or".equalsIgnoreCase(preString)){
                                q = builderAndQuery(temp,q);
                            }else{
                            } else {
                                q = builderOrQuery(temp,q);
                            }
                            i = kgIndex+i+1;
                            start = "";
                            preString = temp;
                        }else{
                        } else {
                            temp = start+otherString;
                            if(!"or".equalsIgnoreCase(preString)){
                            if (!"or".equalsIgnoreCase(preString)){
                                q = builderAndQuery(temp,q);
                            }else{
                            } else {
                                q = builderOrQuery(temp,q);
                            }
                            i= keys.length();
@ -229,24 +234,24 @@ public class TextSearchEndPoint extends EnvelopRestEndPoint {
                    }
                } else {
                    int kgIndex = otherString.indexOf(" ");
                    if(kgIndex > -1){
                    if (kgIndex > -1){
                        temp = keys.substring(i,i+kgIndex+1);
                        if(!"and".equalsIgnoreCase(temp) &&  !"or".equalsIgnoreCase(temp)){
                            if(!"or".equalsIgnoreCase(preString)){
                        if (!"and".equalsIgnoreCase(temp) &&  !"or".equalsIgnoreCase(temp)){
                            if (!"or".equalsIgnoreCase(preString)){
                                q = builderAndQuery(temp,q);
                            }else{
                            } else {
                                q = builderOrQuery(temp,q);
                            }
                        }
                        i=kgIndex+i+1;
                        start = "";
                        preString = temp;
                    }else{
                    } else {
                        temp = start+otherString;
                        if(!"and".equalsIgnoreCase(temp) &&  !"or".equalsIgnoreCase(temp)){
                            if(!"or".equalsIgnoreCase(preString)){
                        if (!"and".equalsIgnoreCase(temp) &&  !"or".equalsIgnoreCase(temp)){
                            if (!"or".equalsIgnoreCase(preString)){
                                q = builderAndQuery(temp,q);
                            }else{
                            } else {
                                q = builderOrQuery(temp,q);
                            }
                        }
@ -255,42 +260,42 @@ public class TextSearchEndPoint extends EnvelopRestEndPoint {
                }
            }
        }
        if(StringUtils.isBlank(q)){
        if (StringUtils.isBlank(q)){
            q = "rowkey:*";
        }
        q = getQuery(filters, new StringBuilder(q));
        return q;
    }
    private String builderAndQuery(String temp,String q){
        if(StringUtils.isNotBlank(temp)){
            if(temp.startsWith("-")){
    private String builderAndQuery(String temp, String q){
        if (StringUtils.isNotBlank(temp)){
            if (temp.startsWith("-")){
                String substring = temp.substring(1, temp.length());
                substring = ClientUtils.escapeQueryChars(substring);
                if(StringUtils.isNotBlank(substring)){
                    if(StringUtils.isBlank(q)){
                        q+="-" + ResourceCells.SEARCH_FIELD + ":" + substring ;
                    }else{
                        q+=" AND -" + ResourceCells.SEARCH_FIELD + ":" +  substring;
                if (StringUtils.isNotBlank(substring)){
                    if (StringUtils.isBlank(q)){
                        q += "-" + ResourceCells.SEARCH_FIELD + ":" + substring ;
                    } else {
                        q += " AND -" + ResourceCells.SEARCH_FIELD + ":" +  substring;
                    }
                }
            }else if(temp.startsWith("+")){
            } else if (temp.startsWith("+")){
                String substring = temp.substring(1, temp.length());
                substring = ClientUtils.escapeQueryChars(substring);
                if(StringUtils.isNotBlank(substring)){
                    if(StringUtils.isBlank(q)){
                        q+= ResourceCells.SEARCH_FIELD + ":" + temp.substring(1,temp.length());
                    }else{
                        q+=" AND " + ResourceCells.SEARCH_FIELD + ":" + temp.substring(1,temp.length());
                if (StringUtils.isNotBlank(substring)){
                    if (StringUtils.isBlank(q)){
                        q += ResourceCells.SEARCH_FIELD + ":" + temp.substring(1,temp.length());
                    } else {
                        q +=" AND " + ResourceCells.SEARCH_FIELD + ":" + temp.substring(1,temp.length());
                    }
                }
            }else{
                if(StringUtils.isNotBlank(temp)){
            } else {
                if (StringUtils.isNotBlank(temp)){
                    temp = ClientUtils.escapeQueryChars(temp);
                    if(StringUtils.isBlank(q)){
                        q+= ResourceCells.SEARCH_FIELD + ":" + temp;
                    }else{
                        q+=" AND " + ResourceCells.SEARCH_FIELD + ":" + temp;
                    if (StringUtils.isBlank(q)){
                        q += ResourceCells.SEARCH_FIELD + ":" + temp;
                    } else {
                        q += " AND " + ResourceCells.SEARCH_FIELD + ":" + temp;
                    }
                }
            }
@ -298,35 +303,35 @@ public class TextSearchEndPoint extends EnvelopRestEndPoint {
        return q;
    }
    private String builderOrQuery(String temp,String q){
        if(StringUtils.isNotBlank(temp)){
            if(temp.startsWith("-")){
    private String builderOrQuery(String temp, String q){
        if (StringUtils.isNotBlank(temp)){
            if (temp.startsWith("-")){
                String substring = temp.substring(1, temp.length());
                substring = ClientUtils.escapeQueryChars(substring);
                if(StringUtils.isNotBlank(substring)){
                    if(StringUtils.isBlank(q)){
                        q+="-" + ResourceCells.SEARCH_FIELD + ":" + substring;
                    }else{
                        q+=" OR -" + ResourceCells.SEARCH_FIELD + ":" + substring;
                if (StringUtils.isNotBlank(substring)){
                    if (StringUtils.isBlank(q)){
                        q += "-" + ResourceCells.SEARCH_FIELD + ":" + substring;
                    } else {
                        q += " OR -" + ResourceCells.SEARCH_FIELD + ":" + substring;
                    }
                }
            }else if(temp.startsWith("+")){
            } else if (temp.startsWith("+")){
                String substring = temp.substring(1, temp.length());
                substring = ClientUtils.escapeQueryChars(substring);
                if(StringUtils.isNotBlank(substring)){
                    if(StringUtils.isBlank(q)){
                        q+= ResourceCells.SEARCH_FIELD + ":" + substring;
                    }else{
                        q+=" OR " + ResourceCells.SEARCH_FIELD + ":" + substring;
                if (StringUtils.isNotBlank(substring)){
                    if (StringUtils.isBlank(q)){
                        q += ResourceCells.SEARCH_FIELD + ":" + substring;
                    } else {
                        q += " OR " + ResourceCells.SEARCH_FIELD + ":" + substring;
                    }
                }
            }else{
                if(StringUtils.isNotBlank(temp)) {
            } else {
                if (StringUtils.isNotBlank(temp)) {
                    temp = ClientUtils.escapeQueryChars(temp);
                    if(StringUtils.isBlank(q)){
                        q+= ResourceCells.SEARCH_FIELD + ":" + temp;
                    }else{
                        q+=" OR " + ResourceCells.SEARCH_FIELD + ":" + temp;
                    if (StringUtils.isBlank(q)){
                        q += ResourceCells.SEARCH_FIELD + ":" + temp;
                    } else {
                        q += " OR " + ResourceCells.SEARCH_FIELD + ":" + temp;
                    }
                }
            }