Merge branch 'develop_3' into 'develop_master'

feat: 搜索中心 动态字段自动匹配es查询

See merge request !92
This commit is contained in:
super_liu
2021-08-04 10:07:20 +00:00
2 changed files with 20 additions and 6 deletions
@@ -19,7 +19,7 @@ public class SQLFilter {
for(int i$ = 0; i$ < len$; ++i$) { for(int i$ = 0; i$ < len$; ++i$) {
String keyword = arr$[i$]; String keyword = arr$[i$];
if (checkStr.indexOf(keyword) != -1) { if (checkStr.indexOf(keyword) != -1 && checkStr.indexOf("selectindex") == -1 && checkStr.indexOf("selectvalue") == -1) {
throw new AdcDaBaseException("包含非法字符"); throw new AdcDaBaseException("包含非法字符");
} }
} }
@@ -3,13 +3,14 @@ package com.adc.da.search.service.impl;
import com.adc.da.search.entity.*; import com.adc.da.search.entity.*;
import com.adc.da.search.service.SearchCenterService; import com.adc.da.search.service.SearchCenterService;
import com.adc.da.search.util.InitStandAttrSearchUtil; import com.adc.da.search.util.InitStandAttrSearchUtil;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import net.sf.json.JSONObject;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.elasticsearch.action.search.SearchRequestBuilder; import org.elasticsearch.action.search.SearchRequestBuilder;
import org.elasticsearch.action.search.SearchResponse; import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.action.search.SearchType; import org.elasticsearch.action.search.SearchType;
import org.elasticsearch.client.transport.TransportClient; import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.collect.HppcMaps;
import org.elasticsearch.common.text.Text; import org.elasticsearch.common.text.Text;
import org.elasticsearch.index.query.BoolQueryBuilder; import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilder; import org.elasticsearch.index.query.QueryBuilder;
@@ -22,6 +23,7 @@ import org.elasticsearch.search.aggregations.bucket.terms.StringTerms;
import org.elasticsearch.search.aggregations.bucket.terms.TermsAggregationBuilder; import org.elasticsearch.search.aggregations.bucket.terms.TermsAggregationBuilder;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder; import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightField; import org.elasticsearch.search.fetch.subphase.highlight.HighlightField;
import org.json.JSONTokener;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
@@ -145,10 +147,10 @@ public class SearchCenterServiceImpl implements SearchCenterService {
private BoolQueryBuilder querySeniorMsg(SeniorSearchInfoEO searchInfoEO, BoolQueryBuilder boolQueryBuilder) { private BoolQueryBuilder querySeniorMsg(SeniorSearchInfoEO searchInfoEO, BoolQueryBuilder boolQueryBuilder) {
BoolQueryBuilder boolQueryBuilderMust = new BoolQueryBuilder(); BoolQueryBuilder boolQueryBuilderMust = new BoolQueryBuilder();
String sarStandAttrEOStr = searchInfoEO.getSarStandAttrEOStr(); String sarStandAttrEOStr = searchInfoEO.getSarStandAttrEOStr();
Map<String, String> sarStandAttrMap = JSONObject.fromObject(sarStandAttrEOStr); Map<String, Object> sarStandAttrMap = net.sf.json.JSONObject.fromObject(sarStandAttrEOStr);
// MAP通过JAVA8新特性流过滤NULL值或对象 // MAP通过JAVA8新特性流过滤NULL值或对象
Map<String, String> collectAttrMap = sarStandAttrMap.entrySet().stream().filter((e) -> e.getValue() != null).collect(Collectors.toMap( Map<String, Object> collectAttrMap = sarStandAttrMap.entrySet().stream().filter((e) -> e.getValue() != null && StringUtils.isNotBlank(e.getValue().toString())).collect(Collectors.toMap(
Map.Entry::getKey, Map.Entry::getKey,
Map.Entry::getValue)); Map.Entry::getValue));
@@ -157,8 +159,20 @@ public class SearchCenterServiceImpl implements SearchCenterService {
// 动态存储属性字段 ---1 // 动态存储属性字段 ---1
for (String field : fieldInfoList) { for (String field : fieldInfoList) {
if(collectAttrMap.containsKey(field)){ if(collectAttrMap.containsKey(field.toLowerCase())){
boolQueryBuilderMust.must(QueryBuilders.fuzzyQuery(field, "*"+collectAttrMap.get(field)+"*")); System.out.println(collectAttrMap.get(field.toLowerCase())+"----------------------------");
List<String> fields = new ArrayList<>();
Object json= new JSONTokener(collectAttrMap.get(field.toLowerCase()).toString()).nextValue();
if(json instanceof org.json.JSONArray){
fields = JSONObject.parseArray(collectAttrMap.get(field.toLowerCase()).toString(),String.class);
}else {
fields.add(collectAttrMap.get(field.toLowerCase()).toString());
}
if(!fields.isEmpty()){
for (String str : fields) {
boolQueryBuilderMust.must(QueryBuilders.fuzzyQuery(field, "*"+str+"*"));
}
}
} }
} }