搜索中心-法规月报-查询优化
This commit is contained in:
+72
-49
@@ -77,35 +77,27 @@ public class LawsMonthlyReportSearchServiceImpl implements ILawsMonthlyReportSea
|
||||
Map<String,Object> mapHighlight1 = new HashMap<>();
|
||||
//封装首次的条件
|
||||
if (StringUtils.isNotBlank(selectValue)) {
|
||||
selectValue = selectValue.toLowerCase();
|
||||
for (String field : fieldList) {
|
||||
Map<String, Object> map2 = new HashMap<>();
|
||||
Map<String, Object> map3 = new HashMap<>();
|
||||
Map<String, Object> 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<String, Object> map25 = new HashMap<>();
|
||||
Map<String, Object> 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<String, Object> queryMap = new HashMap<>();
|
||||
Map<String, Object> map2 = new HashMap<>();
|
||||
Map<String, Object> 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<String, Object> map25 = new HashMap<>();
|
||||
Map<String, Object> 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("<text class='highlight-class'>")){
|
||||
mapSource.put("file_text","");
|
||||
}
|
||||
}
|
||||
mapList.add(mapSource);
|
||||
}
|
||||
@@ -258,10 +234,57 @@ public class LawsMonthlyReportSearchServiceImpl implements ILawsMonthlyReportSea
|
||||
list1.add("</text>");
|
||||
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<String, Object> map21 = new HashMap<>();
|
||||
Map<String, Object> map31 = new HashMap<>();
|
||||
Map<String, Object> 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<String, Object> map22 = new HashMap<>();
|
||||
Map<String, Object> map32 = new HashMap<>();
|
||||
Map<String, Object> 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<String, Object> map23 = new HashMap<>();
|
||||
Map<String, Object> map43 = new HashMap<>();
|
||||
map23.put(field, selectValue);
|
||||
map43.put("match", map23);
|
||||
queryMapJson.add(map43);
|
||||
|
||||
// wildcard模糊查询
|
||||
//情况举例:用户只记得中间那段字
|
||||
Map<String, Object> map24 = new HashMap<>();
|
||||
Map<String, Object> map44 = new HashMap<>();
|
||||
map24.put(field, "*" + selectValue + "*");
|
||||
map44.put("wildcard",map24);
|
||||
queryMapJson.add(map44);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user