From 6ac2aea0137bcc24ea68a612b0adae486598c225 Mon Sep 17 00:00:00 2001 From: liyawei Date: Fri, 26 Aug 2022 14:28:07 +0800 Subject: [PATCH] =?UTF-8?q?=E6=90=9C=E7=B4=A2=E4=B8=AD=E5=BF=83-=E6=B3=95?= =?UTF-8?q?=E8=A7=84=E6=9C=88=E6=8A=A5-=E6=9F=A5=E8=AF=A2=E4=BC=98?= =?UTF-8?q?=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../LawsMonthlyReportSearchServiceImpl.java | 121 +++++++++++------- 1 file changed, 72 insertions(+), 49 deletions(-) diff --git a/jero-boot/jero-boot-modules/src/main/java/com/jero/modules/searchcenter/service/impl/LawsMonthlyReportSearchServiceImpl.java b/jero-boot/jero-boot-modules/src/main/java/com/jero/modules/searchcenter/service/impl/LawsMonthlyReportSearchServiceImpl.java index f046e4f02..13a854a87 100644 --- a/jero-boot/jero-boot-modules/src/main/java/com/jero/modules/searchcenter/service/impl/LawsMonthlyReportSearchServiceImpl.java +++ b/jero-boot/jero-boot-modules/src/main/java/com/jero/modules/searchcenter/service/impl/LawsMonthlyReportSearchServiceImpl.java @@ -77,35 +77,27 @@ public class LawsMonthlyReportSearchServiceImpl implements ILawsMonthlyReportSea Map mapHighlight1 = new HashMap<>(); //封装首次的条件 if (StringUtils.isNotBlank(selectValue)) { + selectValue = selectValue.toLowerCase(); for (String field : fieldList) { - Map map2 = new HashMap<>(); - Map map3 = new HashMap<>(); - Map map4 = new HashMap<>(); - if("title".equals(field)){ - map3.put("boost",1); - }else if("content".equals(field)){ - map3.put("boost",0.01); - } - // 标题字段 通配符模糊匹配 - if (field.equals("title")){ - // map2放最内层 - map2.put(field, "*" + selectValue + "*"); - - // map4放query - map4.put("wildcard",map2); - } else { - map3.put("query", selectValue); - map2.put(field, map3); - map4.put("match", map2); - } - queryMapJson.add(map4); + getQueryMapJson(queryMapJson, selectValue, field); //高亮 if(!"flag".equals(field)){ highlight(mapHighlight, field); + } else { + // query_string查询 + Map map25 = new HashMap<>(); + Map map45 = new HashMap<>(); + map25.put("query", selectValue); + map25.put("allow_leading_wildcard", false); //禁用了前置通配符 + map45.put("query_string", map25); + queryMapJson.add(map45); + } + } mapHighlight1.put("fields",mapHighlight); + } if(CollectionUtils.isNotEmpty(queryMapJson)){ JSONObject jsonObject = jeroElasticsearchTemplate.buildBoolQuery(null, null, queryMapJson); @@ -113,33 +105,22 @@ public class LawsMonthlyReportSearchServiceImpl implements ILawsMonthlyReportSea } //封装二次的条件 if (StringUtils.isNotBlank(selectValueTwo)) { + selectValueTwo = selectValueTwo.toLowerCase(); for (String field : fieldList) { - Map queryMap = new HashMap<>(); - Map map2 = new HashMap<>(); - Map map3 = new HashMap<>(); - if("title".equals(field)){ - map3.put("boost",1); - }else if("content".equals(field)){ - map3.put("boost",0.01); - } + getQueryMapJson(queryMapJsonTwo, selectValueTwo, field); - // 标题字段 通配符模糊匹配 - if (field.equals("title")){ - // map2放最内层 - map2.put(field, "*" + selectValueTwo + "*"); - // map4放query - queryMap.put("wildcard",map2); - } else { - map3.put("query", selectValueTwo); -// map3.put("minimum_should_match",2); - map2.put(field, map3); - queryMap.put("match", map2); - } - - queryMapJsonTwo.add(queryMap); //高亮 if(!"flag".equals(field)){ highlight(mapHighlight, field); + } else { + // query_string查询 + Map map25 = new HashMap<>(); + Map map45 = new HashMap<>(); + map25.put("query", selectValue); + map25.put("allow_leading_wildcard", false); + map45.put("query_string", map25); + queryMapJsonTwo.add(map45); + } } mapHighlight1.put("fields",mapHighlight); @@ -227,11 +208,6 @@ public class LawsMonthlyReportSearchServiceImpl implements ILawsMonthlyReportSea } mapSource.put(key,fieldConyent); } - //如果这一条数据中有高亮字段值,但是file_text中没有高亮值, 则赋空值,否则列表中会展示所有的文件内容, 赋空后,则展示基础数据 - String fileText = (String) mapSource.get("file_text"); - if(StringUtils.isNotBlank(fileText) && !fileText.contains("")){ - mapSource.put("file_text",""); - } } mapList.add(mapSource); } @@ -258,10 +234,57 @@ public class LawsMonthlyReportSearchServiceImpl implements ILawsMonthlyReportSea list1.add(""); mapTemp.put("pre_tags", list); mapTemp.put("post_tags", list1); - mapTemp.put("fragment_size", 550);//高亮字段内容长度,去除html样式标签,统计字数,设置为200 + mapTemp.put("fragment_size", 450);//高亮字段内容长度,去除html样式标签,统计字数,设置为200 mapTemp.put("number_of_fragments", 1);//高亮内容默认分为5段, 此处设置为一段 mapTemp.put("type", "plain"); mapHighlight.put(key, mapTemp); } + private void getQueryMapJson(JSONArray queryMapJson, String selectValue, String field) { + // 前缀匹配 缺点是前缀一定不能断开 + //情况举例:用户只记得前面那段字 + Map map21 = new HashMap<>(); + Map map31 = new HashMap<>(); + Map map41 = new HashMap<>(); + if("title".equals(field)){ + map31.put("boost",1); + }else if("content".equals(field)){ + map31.put("boost",0.01); + } + map31.put("value", selectValue); + map21.put(field + ".keyword", map31); + map41.put("prefix", map21); + queryMapJson.add(map41); + + // match_phrase_prefix 词组匹配查询,允许最后词组与文中的任意分词前缀匹配 + Map map22 = new HashMap<>(); + Map map32 = new HashMap<>(); + Map map42 = new HashMap<>(); + if("title".equals(field)){ + map32.put("boost",1); + }else if("content".equals(field)){ + map32.put("boost",0.01); + } + map32.put("query", selectValue); + map22.put(field, map32); + map42.put("match_phrase_prefix", map22); + queryMapJson.add(map42); + + // match分词匹配查询 + // 情况举例:用户可能他知道开头的前缀几个字,知道中间的几个字 + Map map23 = new HashMap<>(); + Map map43 = new HashMap<>(); + map23.put(field, selectValue); + map43.put("match", map23); + queryMapJson.add(map43); + + // wildcard模糊查询 + //情况举例:用户只记得中间那段字 + Map map24 = new HashMap<>(); + Map map44 = new HashMap<>(); + map24.put(field, "*" + selectValue + "*"); + map44.put("wildcard",map24); + queryMapJson.add(map44); + } + }