feat: There is no way the, Es

This commit is contained in:
super_liu
2022-06-07 17:46:27 +08:00
parent 531344edf1
commit 2c248b2c23
@@ -58,6 +58,12 @@ public class SearchCenterServiceImpl implements SearchCenterService {
private TransportClient client;
// 函数式接口,获取next数组
@FunctionalInterface
public interface NextArray {
int[] getNext(String target);
}
@Autowired
private DicTypeEODao dicTypeEODao;
@@ -1346,22 +1352,27 @@ public class SearchCenterServiceImpl implements SearchCenterService {
QueryBuilder multiQueryBuilder1;
QueryBuilder queryBuilder1 = QueryBuilders.multiMatchQuery("");
if(StringUtils.isNotBlank(searchInfoEO.getSelectValue())){
if(searchInfoEO.getSelectValue().length() > 1){
String str0 = searchInfoEO.getSelectValue().substring(0,1);
String Str1 = searchInfoEO.getSelectValue().substring(1,2);
String reg = ".*[0-9].*";
Pattern pattern = Pattern.compile("^[a-zA-Z\\/]+$");
if (pattern.matcher(str0).matches() && Str1.matches(reg)) {
String value = searchInfoEO.getSelectValue().replace(str0,str0+" ");
searchInfoEO.setSelectValue(value);
}
}
queryBuilder1 = QueryBuilders.multiMatchQuery(searchInfoEO.getSelectValue(),
"title"
).minimumShouldMatch("100%").field("title",1000f);
boolQueryBuilder.must(queryBuilder1);
// if(searchInfoEO.getSelectValue().length() > 1){
// String str0 = searchInfoEO.getSelectValue().substring(0,1);
// String Str1 = searchInfoEO.getSelectValue().substring(1,2);
// String reg = ".*[0-9].*";
// Pattern pattern = Pattern.compile("^[a-zA-Z\\/]+$");
// if (pattern.matcher(str0).matches() && Str1.matches(reg)) {
// String value = searchInfoEO.getSelectValue().replace(str0,str0+" ");
// searchInfoEO.setSelectValue(value);
// }
// }
boolQueryShould
.should(QueryBuilders.wildcardQuery("sortTitle.keyword", "*" + searchInfoEO.getSelectValue().toUpperCase() + "*")).boost(1005f)
.should(QueryBuilders.wildcardQuery("numberTitle.keyword", "*" + searchInfoEO.getSelectValue().toUpperCase() + "*")).boost(1004f)
.should(QueryBuilders.wildcardQuery("yearOrder.keyword", "*" + searchInfoEO.getSelectValue().toUpperCase() + "*")).boost(1003f)
.should(QueryBuilders.wildcardQuery("nameTitle.keyword", "*" + searchInfoEO.getSelectValue().toUpperCase() + "*")).boost(1002f)
.should(QueryBuilders.wildcardQuery("title.keyword", "*" + searchInfoEO.getSelectValue().toUpperCase() + "*").boost(1000f));
// queryBuilder1 = QueryBuilders.wildcardQuery("title.keyword","*" + searchInfoEO.getSelectValue() + "*");
// queryBuilder1 = QueryBuilders.multiMatchQuery(searchInfoEO.getSelectValue(),
// "title"
// ).minimumShouldMatch("100%").field("title",1000f);
boolQueryBuilder.must(boolQueryShould);
}
@@ -1384,7 +1395,7 @@ public class SearchCenterServiceImpl implements SearchCenterService {
//在结果中检索
HighlightBuilder hiBuilder=new HighlightBuilder();
HighlightBuilder.Field highlightTitle = new HighlightBuilder.Field("title");
HighlightBuilder.Field highlightTitle = new HighlightBuilder.Field("title.keyword");
hiBuilder.field(highlightTitle);
hiBuilder.preTags("<span class='highlight-Es-class'>");
hiBuilder.postTags("</span>");
@@ -1409,37 +1420,124 @@ public class SearchCenterServiceImpl implements SearchCenterService {
for (SearchHit searchHit : response.getHits().getHits()) {
searchHit.getSourceAsMap().put("id", searchHit.getId());
Map<String, Object> sourceAsMap = searchHit.getSourceAsMap();
// String textContent = sourceAsMap.getOrDefault("textContent","").toString();
String textStatusColor = "";
if(StringUtils.isNotBlank(sourceAsMap.getOrDefault("textStatusFull","").toString())){
String textStatus = sourceAsMap.getOrDefault("textStatusFull","").toString();
if("废止".equals(textStatus) || "被代替".equals(textStatus) || "作废".equals(textStatus) ){
textStatusColor = "redClass";
}else if("参考".equals(textStatus)){
textStatusColor = "yellowClass";
// if(StringUtils.isNotBlank(textContent) && StringUtils.isNotBlank(searchInfoEO.getSelectValue())){
// String[] standStr = new String[]{"废止","被代替","作废","参考"};
// for (String textStatus : standStr) {
// int i = indexOfToKMP(textContent, "文本状态:"+textStatus, 0, getNextValInstance());
// if(i > 0 && ("废止".equals(textStatus) || "被代替".equals(textStatus) || "作废".equals(textStatus) )){
// textStatusColor = "redClass";
// break;
// }else if("参考".equals(textStatus)){
// textStatusColor = "yellowClass";
// break;
// }
// }
//
// }
if(StringUtils.isNotBlank(searchInfoEO.getSelectValue())){
if(StringUtils.isNotBlank(sourceAsMap.getOrDefault("textStatusFull","").toString())){
String textStatus = sourceAsMap.getOrDefault("textStatusFull","").toString();
if("废止".equals(textStatus) || "被代替".equals(textStatus) || "作废".equals(textStatus) ){
textStatusColor = "redClass";
}else if("参考".equals(textStatus)){
textStatusColor = "yellowClass";
}
}
//高亮标题覆盖原标题
String titleStr = sourceAsMap.get("title").toString();
titleStr = titleStr.replace(searchInfoEO.getSelectValue(),"<span class='highlight-Es-class'>"+searchInfoEO.getSelectValue()+"</span>");
if(StringUtils.isNotBlank(textStatusColor)){
sourceAsMap.put("title", "<span class='"+textStatusColor+"'>"+titleStr+"</span>");
}else {
sourceAsMap.put("title", titleStr);
}
}
//解析高亮字段
Map<String, HighlightField> highlightFields = searchHit.getHighlightFields();
HighlightField field= highlightFields.get("title");
if(field!= null){
Text[] fragments = field.fragments();
StringBuilder n_field = new StringBuilder();
for (Text fragment : fragments) {
n_field.append(fragment);
}
//高亮标题覆盖原标题
if(StringUtils.isNotBlank(textStatusColor)){
sourceAsMap.put("title", "<span class='"+textStatusColor+"'>"+n_field.toString()+"</span>");
}else {
sourceAsMap.put("title", n_field.toString());
}
}
// Map<String, HighlightField> highlightFields = searchHit.getHighlightFields();
// HighlightField field= highlightFields.get("title.keyword");
// if(field!= null){
// Text[] fragments = field.fragments();
// StringBuilder n_field = new StringBuilder();
// for (Text fragment : fragments) {
// n_field.append(fragment);
// }
// //高亮标题覆盖原标题
// if(StringUtils.isNotBlank(textStatusColor)){
// sourceAsMap.put("title", "<span class='"+textStatusColor+"'>"+n_field.toString()+"</span>");
// }else {
// sourceAsMap.put("title", n_field.toString());
// }
// }
result.add(sourceAsMap);
}
searchInfoEO.getPager().setRowCount((int) response.getHits().getTotalHits().value);
return result;
}
// 改进版的nextVal数组
public NextArray getNextValInstance() {
return target -> {
// 初始化数组大小
int[] nextVal = new int[target.length()];
// 0 位置默认为-1
nextVal[0] = -1;
// 遍历字符串每一位,计算每个位置上的值
for (int i = 0, k = -1; i < target.length() - 1; ) {
if (k == -1 || target.charAt(i) == target.charAt(k)) {
i++;
k++;
// 判断当前字符和求的k位置的字符是否相等
if (target.charAt(i) == target.charAt(k)) {
// 若相等,则继承k位置的值
nextVal[i] = nextVal[k];
} else {
// 不等,则还是使用原来next数组的值
nextVal[i] = k;
}
} else {
k = nextVal[k];
}
}
return nextVal;
};
}
/**
* 采用KMP算法,查找字符串子串的位置
*
* @param source 源字符串
* @param target 目标(子)字符串
* @param pos 源字符串的起始索引
* @param nextArray 函数式接口,传入获取next数组的方法(因为next数组上面定义了两种获取方式)
* @return 目标字符串在源字符串中第一次出现的索引位置
*/
public int indexOfToKMP(String source, String target, int pos, NextArray nextArray) {
int i, j;
// 调用接口中的方法,获取next数组
int[] next = nextArray.getNext(target);
// 初始化遍历
i = pos;
j = 0;
// 判断i和j的索引都不能大于等于字符串长度,否则说明比较完成
while (i < source.length() && j < target.length()) {
// 如果j==-1,说明需要从头开始比较;或者由公共元素相等,继续比较
if (j == -1 || source.charAt(i) == target.charAt(j)) {
i++;
j++;
} else {
// 需要按照数组回溯j
j = next[j];
}
}
// 判断子字符的索引是否大于等于子字符串,如果为true说明查找成功,返回源字符串当前位置-子字符串长度
return j >= target.length() ? i - target.length() : -1;
}
@Override
public List<Map<String, Object>> searchSarBykeyAndHighLightNumber(SeniorSearchInfoEO searchInfoEO) throws Exception {
String real = searchInfoEO.getSelectValue();