|
@@ -0,0 +1,172 @@
|
|
|
|
+package com.bowintek.practice.services.impl;
|
|
|
|
+
|
|
|
|
+import co.elastic.clients.elasticsearch.ElasticsearchClient;
|
|
|
|
+import co.elastic.clients.elasticsearch._types.mapping.Property;
|
|
|
|
+import co.elastic.clients.elasticsearch._types.mapping.TextProperty;
|
|
|
|
+import co.elastic.clients.elasticsearch._types.query_dsl.*;
|
|
|
|
+import co.elastic.clients.elasticsearch.cat.IndicesResponse;
|
|
|
|
+import co.elastic.clients.elasticsearch.core.SearchRequest;
|
|
|
|
+import co.elastic.clients.elasticsearch.core.SearchResponse;
|
|
|
|
+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.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.fasterxml.jackson.core.type.TypeReference;
|
|
|
|
+import com.fasterxml.jackson.databind.ObjectMapper;
|
|
|
|
+import com.fasterxml.jackson.databind.node.ObjectNode;
|
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
+import org.springframework.stereotype.Component;
|
|
|
|
+
|
|
|
|
+import javax.naming.directory.SearchResult;
|
|
|
|
+import java.io.IOException;
|
|
|
|
+import java.util.*;
|
|
|
|
+
|
|
|
|
+@Component
|
|
|
|
+public class EsQueryServiceImpl implements EsQueryService {
|
|
|
|
+ @Autowired
|
|
|
|
+ private ElasticsearchClient esClient;
|
|
|
|
+ @Autowired
|
|
|
|
+ private AnalyzeService analyzeService;
|
|
|
|
+
|
|
|
|
+ @Override
|
|
|
|
+ public Map<String, Object> query(String text, int page, int limit) {
|
|
|
|
+ //[1]需要返回的结果map
|
|
|
|
+ Map<String, Object> result = new HashMap<>();
|
|
|
|
+ result.put("total", 0);
|
|
|
|
+ //[2]分析查询字符串,有的字符串需要变成条件查询
|
|
|
|
+ List<ComparisonResult> cmpList = analyzeService.analyzeJavas(text);
|
|
|
|
+ //[3]有查询条件分析内容,组装查询条件
|
|
|
|
+ List<Query> queryList = new ArrayList<>();
|
|
|
|
+ for(int i=0;i<cmpList.size();i++){
|
|
|
|
+ ComparisonResult cmp = cmpList.get(i);
|
|
|
|
+ if(cmp.getSearchType().equals("comparison")){
|
|
|
|
+ //对比搜索
|
|
|
|
+
|
|
|
|
+ }
|
|
|
|
+ else{
|
|
|
|
+ //query 文字搜索
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ return result;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public Map<String, Object> query1(String text, int page, int limit) {
|
|
|
|
+ Map<String, Object> result = new HashMap<>();
|
|
|
|
+ result.put("total", 0);
|
|
|
|
+
|
|
|
|
+ try {
|
|
|
|
+ IndicesResponse indicesResponse = esClient.cat().indices();
|
|
|
|
+ indicesResponse.valueBody().forEach(i -> {
|
|
|
|
+ System.out.println("get all index, health: " + i.health() + ", status: " + i.status() + ", index: " + i.index());
|
|
|
|
+ });
|
|
|
|
+
|
|
|
|
+ //建立查询参数分析
|
|
|
|
+ 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.size(limit);
|
|
|
|
+ searchRequest.from(page * limit);
|
|
|
|
+
|
|
|
|
+ //生成查询参数
|
|
|
|
+ if(!StringUtils.IsNullEmpty(text)) {
|
|
|
|
+ //非嵌套字段查询
|
|
|
|
+ String[] fields = new String[]{"well_common_name","testing_name"};
|
|
|
|
+ Query query1 = MultiMatchQuery.of(q -> q.fields(Arrays.asList(fields)).query(text).operator(Operator.Or))._toQuery();
|
|
|
|
+ //嵌套字段查询
|
|
|
|
+ String[] nestedFields = new String[]{"historys.testing_name"};
|
|
|
|
+ Query nestedQuery = MultiMatchQuery.of(q -> q.fields(Arrays.asList(nestedFields)).query(text).operator(Operator.Or))._toQuery();
|
|
|
|
+ Query query3 = NestedQuery.of(q-> q.path("historys").query(nestedQuery).ignoreUnmapped(true))._toQuery();
|
|
|
|
+ //对比类型查询
|
|
|
|
+ Query query2 = RangeQuery.of(q->q.field("authorized_md").gte(JsonData.of(500)))._toQuery();
|
|
|
|
+
|
|
|
|
+ Query[] arys = new Query[]{query1, query2, query3};
|
|
|
|
+ searchRequest.query(q->q.bool(b->b.should(Arrays.asList(arys))));
|
|
|
|
+ //高亮设置
|
|
|
|
+ searchRequest.highlight(h->h.fields("historys.testing_name", f->f.matchedFields("historys.testing_name")));
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ //Es发起查询
|
|
|
|
+ SearchRequest request = searchRequest.build();
|
|
|
|
+ SearchResponse response = esClient.search(request, ObjectNode.class);
|
|
|
|
+
|
|
|
|
+ //转换结果,可以对不同的index做出参数输出
|
|
|
|
+ List<Map<String, Object>> rows = searchResponse2List(response);
|
|
|
|
+ result.put("rows", rows);
|
|
|
|
+ result.put("total", response.hits().total().value());
|
|
|
|
+ System.out.println(response.hits().total()+" "+ request.toString());
|
|
|
|
+ }
|
|
|
|
+ catch (Exception ex){
|
|
|
|
+ ex.printStackTrace();
|
|
|
|
+ }
|
|
|
|
+ return result;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public void updateMappingTest() throws IOException {
|
|
|
|
+ Map<String, Property> maps = new HashMap<>();
|
|
|
|
+ maps.put("well_common_name", Property.of(p->p.text(TextProperty.of(t->t.index(true).analyzer("ik_max_word")))));
|
|
|
|
+ updateMappings("fact_dwr_well_basic_information", maps);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public void updateMappings(String index, Map<String, Property> maps) throws IOException {
|
|
|
|
+ PutMappingRequest putMappingRequest = PutMappingRequest.of(m -> m.index(index).properties(maps));
|
|
|
|
+ PutMappingResponse putMappingResponse = esClient.indices().putMapping(putMappingRequest);
|
|
|
|
+ boolean acknowledged = putMappingResponse.acknowledged();
|
|
|
|
+ System.out.println("update mappings ack: " + acknowledged);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public List<Map<String, Object>> searchResponse2List(SearchResponse<ObjectNode> searchResponse) {
|
|
|
|
+
|
|
|
|
+ if (searchResponse == null) {return new ArrayList<>(0);}
|
|
|
|
+ if (searchResponse.hits() == null) {return new ArrayList<>(0);}
|
|
|
|
+ //if (CommonUtils.isCollectionEmpty(searchResponse.hits().hits())) {return new ArrayList<>(0);}
|
|
|
|
+
|
|
|
|
+ List<Hit<ObjectNode>> hits = searchResponse.hits().hits();
|
|
|
|
+
|
|
|
|
+ List<Map<String, Object>> list = new ArrayList<>(hits.size());
|
|
|
|
+ for (Hit<ObjectNode> hit : hits) {
|
|
|
|
+ ObjectNode node = hit.source();
|
|
|
|
+ Map<String, Object> map = objectNode2Map(hit.index(), node);
|
|
|
|
+ map.put("index", hit.index());
|
|
|
|
+ map.put("highlight", hit.highlight());
|
|
|
|
+ list.add(map);
|
|
|
|
+
|
|
|
|
+ //输出结果日志
|
|
|
|
+ String line = "=>";
|
|
|
|
+ for(Map.Entry<String, Object> entry : map.entrySet()){
|
|
|
|
+ line += " "+entry.getKey()
|
|
|
|
+ +":"+ (entry.getValue()==null?"null":entry.getValue().toString());
|
|
|
|
+ }
|
|
|
|
+ System.out.println(line);
|
|
|
|
+ }
|
|
|
|
+ return list;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public String subFieldIndex(String index, String fieldName){
|
|
|
|
+ if(fieldName.startsWith(index) && fieldName.length()> index.length()+1) return fieldName.substring(index.length()+1);
|
|
|
|
+ return fieldName;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public Map<String, Object> objectNode2Map(String index, ObjectNode objectNode) {
|
|
|
|
+ if (null == objectNode) {return new HashMap<>(0);}
|
|
|
|
+ if (objectNode.isEmpty()) {return new HashMap<>(0);}
|
|
|
|
+ ObjectMapper objectMapper = new ObjectMapper();
|
|
|
|
+ Map<String, Object> map = objectMapper.convertValue(objectNode, new TypeReference<Map<String, Object>>() {});
|
|
|
|
+
|
|
|
|
+ for(Map.Entry<String, Object> entry : map.entrySet()){
|
|
|
|
+ String name = subFieldIndex(index, entry.getKey());
|
|
|
|
+ if(name.equals(entry.getKey())) continue;
|
|
|
|
+
|
|
|
|
+ map.remove(entry.getKey());
|
|
|
|
+ map.put(name, entry.getValue());
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ return map;
|
|
|
|
+ }
|
|
|
|
+}
|