|
@@ -11,10 +11,14 @@ import co.elastic.clients.elasticsearch.core.search.Hit;
|
|
|
import co.elastic.clients.elasticsearch.indices.PutMappingRequest;
|
|
|
import co.elastic.clients.elasticsearch.indices.PutMappingResponse;
|
|
|
import co.elastic.clients.json.JsonData;
|
|
|
+import com.bowintek.practice.mapper.cquery.EsIndexCquery;
|
|
|
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.EsIndexVo;
|
|
|
+import com.bowintek.practice.vo.EsIndexfieldVo;
|
|
|
+import com.bowintek.practice.vo.EsQueryLogVo;
|
|
|
import com.fasterxml.jackson.core.type.TypeReference;
|
|
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
|
|
import com.fasterxml.jackson.databind.node.ObjectNode;
|
|
@@ -24,6 +28,7 @@ import org.springframework.stereotype.Component;
|
|
|
import javax.naming.directory.SearchResult;
|
|
|
import java.io.IOException;
|
|
|
import java.util.*;
|
|
|
+import java.util.stream.Collectors;
|
|
|
|
|
|
@Component
|
|
|
public class EsQueryServiceImpl implements EsQueryService {
|
|
@@ -31,6 +36,43 @@ public class EsQueryServiceImpl implements EsQueryService {
|
|
|
private ElasticsearchClient esClient;
|
|
|
@Autowired
|
|
|
private AnalyzeService analyzeService;
|
|
|
+ @Autowired
|
|
|
+ private EsIndexCquery esIndexCquery;
|
|
|
+
|
|
|
+ private static Object lockObject = new Object();
|
|
|
+ private static List<String> indexCache = null;
|
|
|
+ private static List<String> pathCache = null;
|
|
|
+ private static long cacheTime = 0;
|
|
|
+
|
|
|
+ public void getCacheList(){
|
|
|
+ synchronized (lockObject) {
|
|
|
+ //从本地缓存加载数据
|
|
|
+ long timeSpan = (new Date()).getTime() - cacheTime;
|
|
|
+ if (timeSpan > 30 * 1000 || indexCache == null || pathCache==null) {
|
|
|
+ List<EsIndexVo> list1 = esIndexCquery.getList(null, null, null);
|
|
|
+ indexCache = list1.stream()
|
|
|
+ .map(m->m.getIndexCode())
|
|
|
+ .distinct().collect(Collectors.toList());
|
|
|
+
|
|
|
+ List<EsIndexfieldVo> list2 = esIndexCquery.getFieldList(null);
|
|
|
+ pathCache = list2.stream().filter(p->p.getDataType().equals("NESTED"))
|
|
|
+ .map(m->m.getFieldCode())
|
|
|
+ .distinct().collect(Collectors.toList());
|
|
|
+
|
|
|
+ cacheTime = (new Date()).getTime();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public List<String> getIndexCache(){
|
|
|
+ getCacheList();
|
|
|
+ return indexCache;
|
|
|
+ }
|
|
|
+
|
|
|
+ public List<String> getPathCache(){
|
|
|
+ getCacheList();
|
|
|
+ return pathCache;
|
|
|
+ }
|
|
|
|
|
|
@Override
|
|
|
public Map<String, Object> query(String text, int page, int limit) {
|
|
@@ -42,6 +84,7 @@ public class EsQueryServiceImpl implements EsQueryService {
|
|
|
//[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);
|
|
@@ -49,20 +92,23 @@ public class EsQueryServiceImpl implements EsQueryService {
|
|
|
//第一级,对比搜索
|
|
|
queryList.addAll(getRangeQueryByComparison(cmp));
|
|
|
//第二级,对比搜索
|
|
|
- queryList.addAll(getNestedRangeQueryByComparison(cmp, "historys"));
|
|
|
+ for(String path : pathList) {
|
|
|
+ queryList.addAll(getNestedRangeQueryByComparison(cmp, path));
|
|
|
+ }
|
|
|
} else {
|
|
|
//第一级,文字搜索
|
|
|
- queryList.add(getMultiMatchQuery(new String[]{"well_common_name","testing_name"}, cmp.getKeyString()));
|
|
|
+ queryList.add(getMultiMatchQuery(cmp.getKeyString()));
|
|
|
//第二级,嵌套类型,文字搜索
|
|
|
- queryList.add(getNestedMultiMatchQuery("historys", cmp.getKeyString()));
|
|
|
+ for(String path : pathList) {
|
|
|
+ queryList.add(getNestedMultiMatchQuery(path, cmp.getKeyString()));
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
|
|
|
//[4]建立查询参数分析
|
|
|
SearchRequest.Builder searchRequest = new SearchRequest.Builder();
|
|
|
- //==>要查询的索引列表
|
|
|
- String[] indexs = new String[]{"dws_basic_info_history", "dws_dm_test_history", "fact_dwr_well_basic_information"};
|
|
|
- searchRequest.index(Arrays.asList(indexs));
|
|
|
+ //==>要查询的索引列表,从数据库配置表中获取
|
|
|
+ searchRequest.index(getIndexCache());
|
|
|
//==>数据分页显示
|
|
|
searchRequest.size(limit);
|
|
|
searchRequest.from(page * limit);
|
|
@@ -81,6 +127,7 @@ 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());
|
|
|
System.out.println(response.hits().total() + " " + request.toString());
|
|
|
}
|
|
|
catch (Exception ex){
|