|
@@ -16,10 +16,13 @@ import com.bowintek.practice.services.service.AnalyzeService;
|
|
|
import com.bowintek.practice.services.service.EsQueryService;
|
|
|
import com.bowintek.practice.util.StringUtils;
|
|
|
import com.bowintek.practice.vo.Analyze.ComparisonResult;
|
|
|
+import com.bowintek.practice.vo.Analyze.EsQueryText;
|
|
|
import com.bowintek.practice.vo.EsIndexVo;
|
|
|
import com.bowintek.practice.vo.EsIndexfieldVo;
|
|
|
import com.bowintek.practice.vo.EsQueryLogVo;
|
|
|
+import com.fasterxml.jackson.core.JsonProcessingException;
|
|
|
import com.fasterxml.jackson.core.type.TypeReference;
|
|
|
+import com.fasterxml.jackson.databind.JsonNode;
|
|
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
|
|
import com.fasterxml.jackson.databind.node.ObjectNode;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
@@ -74,34 +77,76 @@ public class EsQueryServiceImpl implements EsQueryService {
|
|
|
return pathCache;
|
|
|
}
|
|
|
|
|
|
+ public List<Query> getComparisonQueryList(EsQueryText queryText){
|
|
|
+ List<Query> queryList = new ArrayList<>();
|
|
|
+ //分析查询字符串,有的字符串需要变成条件查询
|
|
|
+ List<ComparisonResult> cmpList = analyzeService.analyzeJavas(queryText.getKeyString());
|
|
|
+ //二级查询的路径信息
|
|
|
+ List<String> pathList = getPathCache();
|
|
|
+
|
|
|
+ for (int i = 0; i < cmpList.size(); i++) {
|
|
|
+ ComparisonResult cmp = cmpList.get(i);
|
|
|
+ //检查查询、对比的值是否为空
|
|
|
+ if(StringUtils.IsNullEmpty(cmp.getKeyString())) continue;
|
|
|
+
|
|
|
+ if (cmp.getSearchType().equals("comparison")) { //对比查询,有字段,有对比符号,有值
|
|
|
+ //第一级,对比搜索
|
|
|
+ queryList.addAll(getRangeQueryByComparison(cmp));
|
|
|
+ //第二级,对比搜索
|
|
|
+ for(String path : pathList) {
|
|
|
+ queryList.addAll(getNestedRangeQueryByComparison(cmp, path));
|
|
|
+ }
|
|
|
+ } else if(StringUtils.IsNullEmpty(queryText.getField())){//全文查询,没有限制字段
|
|
|
+ //第一级,文字搜索
|
|
|
+ queryList.add(getMultiMatchQuery(cmp.getKeyString()));
|
|
|
+ //第二级,嵌套类型,文字搜索
|
|
|
+ for(String path : pathList) {
|
|
|
+ queryList.add(getNestedMultiMatchQuery(path, cmp.getKeyString()));
|
|
|
+ }
|
|
|
+ } else { //全文查询,限定了字段
|
|
|
+ //第一级,文字搜索
|
|
|
+ queryList.add(getMultiMatchQuery(new String[]{queryText.getField()} ,cmp.getKeyString()));
|
|
|
+ //第二级,嵌套类型,文字搜索
|
|
|
+ for(String path : pathList) {
|
|
|
+ queryList.add(getNestedMultiMatchQuery(path, new String[]{queryText.getField()}, cmp.getKeyString()));
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return queryList;
|
|
|
+ }
|
|
|
+
|
|
|
@Override
|
|
|
- public Map<String, Object> query(String text, int page, int limit) {
|
|
|
+ public Map<String, Object> query(List<EsQueryText> queryList, List<ComparisonResult> limiters, int page, int limit) {
|
|
|
//[1]需要返回的结果map
|
|
|
Map<String, Object> result = new HashMap<>();
|
|
|
result.put("total", 0);
|
|
|
|
|
|
try {
|
|
|
- //[2]分析查询字符串,有的字符串需要变成条件查询
|
|
|
- List<ComparisonResult> cmpList = analyzeService.analyzeJavas(text);
|
|
|
- //[3]有查询条件分析内容,组装查询条件
|
|
|
+ //二级查询的路径信息
|
|
|
List<String> pathList = getPathCache();
|
|
|
- List<Query> queryList = new ArrayList<>();
|
|
|
- for (int i = 0; i < cmpList.size(); i++) {
|
|
|
- ComparisonResult cmp = cmpList.get(i);
|
|
|
- if (cmp.getSearchType().equals("comparison")) {
|
|
|
- //第一级,对比搜索
|
|
|
- queryList.addAll(getRangeQueryByComparison(cmp));
|
|
|
- //第二级,对比搜索
|
|
|
- for(String path : pathList) {
|
|
|
- queryList.addAll(getNestedRangeQueryByComparison(cmp, path));
|
|
|
- }
|
|
|
- } else {
|
|
|
- //第一级,文字搜索
|
|
|
- queryList.add(getMultiMatchQuery(cmp.getKeyString()));
|
|
|
- //第二级,嵌套类型,文字搜索
|
|
|
- for(String path : pathList) {
|
|
|
- queryList.add(getNestedMultiMatchQuery(path, cmp.getKeyString()));
|
|
|
- }
|
|
|
+
|
|
|
+ List<Query> queryMustList = new ArrayList<>();
|
|
|
+ //[2]分析查询字符串,有的字符串需要变成条件查询
|
|
|
+ for(EsQueryText queryText : queryList){
|
|
|
+ List<Query> shouldQuerys = getComparisonQueryList(queryText);
|
|
|
+ if(shouldQuerys.size()>0){
|
|
|
+ queryMustList.add(Query.of(q->q.bool(b->b.should(shouldQuerys))));
|
|
|
+ }
|
|
|
+ }
|
|
|
+ //[3]限定查询条件,都是有条件的查询
|
|
|
+ for(ComparisonResult cmp:limiters){
|
|
|
+ List<Query> cmpQuerys = new ArrayList<>();
|
|
|
+ //检查查询、对比的值是否为空
|
|
|
+ if(StringUtils.IsNullEmpty(cmp.getValue())) continue;
|
|
|
+
|
|
|
+ //第一级,对比搜索
|
|
|
+ cmpQuerys.addAll(getRangeQueryByComparison(cmp));
|
|
|
+ //第二级,对比搜索
|
|
|
+ for(String path : pathList) {
|
|
|
+ cmpQuerys.addAll(getNestedRangeQueryByComparison(cmp, path));
|
|
|
+ }
|
|
|
+ if(cmpQuerys.size()>0){
|
|
|
+ queryMustList.add(Query.of(q->q.bool(b->b.should(cmpQuerys))));
|
|
|
}
|
|
|
}
|
|
|
|
|
@@ -113,8 +158,8 @@ public class EsQueryServiceImpl implements EsQueryService {
|
|
|
searchRequest.size(limit);
|
|
|
searchRequest.from(page * limit);
|
|
|
//==>设置查询条件
|
|
|
- if (queryList.size() > 0) {
|
|
|
- searchRequest.query(q -> q.bool(b -> b.should(queryList)));
|
|
|
+ if (queryMustList.size() > 0) {
|
|
|
+ searchRequest.query(q -> q.bool(b -> b.must(queryMustList)));
|
|
|
}
|
|
|
//==>高亮设置
|
|
|
searchRequest.highlight(h->h.fields("*", f->f.matchedFields("*")));
|
|
@@ -127,7 +172,11 @@ public class EsQueryServiceImpl implements EsQueryService {
|
|
|
List<Map<String, Object>> rows = searchResponse2List(response);
|
|
|
result.put("rows", rows);
|
|
|
result.put("total", response.hits().total().value());
|
|
|
- result.put("QueryString", request.toString());
|
|
|
+
|
|
|
+ //请求参数输出,方便调试
|
|
|
+ String[] jsonStrings = request.toString().split("typed_keys=true");
|
|
|
+ result.put("SearchUrl", jsonStrings[0]);
|
|
|
+ result.put("SearchRequest", stringToNodeJson(jsonStrings[1]));
|
|
|
System.out.println(response.hits().total() + " " + request.toString());
|
|
|
}
|
|
|
catch (Exception ex){
|
|
@@ -286,6 +335,12 @@ public class EsQueryServiceImpl implements EsQueryService {
|
|
|
System.out.println("update mappings ack: " + acknowledged);
|
|
|
}
|
|
|
|
|
|
+ public JsonNode stringToNodeJson(String jsonString) throws JsonProcessingException {
|
|
|
+ ObjectMapper objectMapper = new ObjectMapper();
|
|
|
+ JsonNode rootNode = objectMapper.readTree(jsonString);
|
|
|
+ return rootNode;
|
|
|
+ }
|
|
|
+
|
|
|
public List<Map<String, Object>> searchResponse2List(SearchResponse<ObjectNode> searchResponse) {
|
|
|
|
|
|
if (searchResponse == null) {return new ArrayList<>(0);}
|