|
@@ -1,10 +1,17 @@
|
|
|
package cn.cslg.pas.service.business.es;
|
|
|
|
|
|
+import cn.cslg.pas.common.dto.PatentColumnDTO;
|
|
|
import cn.cslg.pas.common.dto.QueryScratchWordsDTO;
|
|
|
+import cn.cslg.pas.common.model.cronModel.Personnel;
|
|
|
import cn.cslg.pas.common.model.cronModel.PersonnelVO;
|
|
|
import cn.cslg.pas.common.model.cronModel.Records;
|
|
|
+import cn.cslg.pas.common.model.request.StringRequest;
|
|
|
import cn.cslg.pas.common.utils.CacheUtils;
|
|
|
import cn.cslg.pas.common.utils.LoginUtils;
|
|
|
+import cn.cslg.pas.common.utils.parseQueryToTree.expressManager;
|
|
|
+import cn.cslg.pas.common.utils.parseQueryToTree.operateNode;
|
|
|
+import cn.cslg.pas.common.utils.parseQueryToTree.treeNode;
|
|
|
+import cn.cslg.pas.common.vo.EsConfigVO;
|
|
|
import cn.cslg.pas.common.vo.PatentWithIdVO;
|
|
|
import cn.cslg.pas.common.vo.ScratchWordsVO;
|
|
|
import cn.cslg.pas.domain.business.ScratchWords;
|
|
@@ -12,15 +19,19 @@ import cn.cslg.pas.domain.es.Marking;
|
|
|
import cn.cslg.pas.domain.es.Patent;
|
|
|
import cn.cslg.pas.domain.es.PatentJoin;
|
|
|
import cn.cslg.pas.exception.XiaoShiException;
|
|
|
+import cn.cslg.pas.service.business.CommonService;
|
|
|
import cn.cslg.pas.service.permissions.PermissionService;
|
|
|
+import cn.cslg.pas.service.query.FormatQueryService;
|
|
|
import co.elastic.clients.elasticsearch.ElasticsearchClient;
|
|
|
-import co.elastic.clients.elasticsearch._types.aggregations.AggregationBuilders;
|
|
|
+import co.elastic.clients.elasticsearch._types.aggregations.*;
|
|
|
import co.elastic.clients.elasticsearch._types.query_dsl.Query;
|
|
|
import co.elastic.clients.elasticsearch._types.query_dsl.QueryBuilders;
|
|
|
import co.elastic.clients.elasticsearch.core.*;
|
|
|
import co.elastic.clients.elasticsearch.core.search.Hit;
|
|
|
import co.elastic.clients.json.JsonData;
|
|
|
import com.alibaba.fastjson.JSON;
|
|
|
+import com.alibaba.fastjson.JSONObject;
|
|
|
+import org.apache.ibatis.javassist.bytecode.stackmap.MapMaker;
|
|
|
import org.springframework.beans.BeanUtils;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.stereotype.Service;
|
|
@@ -41,6 +52,8 @@ public class EsScratchWordsService {
|
|
|
private CacheUtils cacheUtils;
|
|
|
@Autowired
|
|
|
private LoginUtils loginUtils;
|
|
|
+ @Autowired
|
|
|
+ private FormatQueryService formatQueryService;
|
|
|
|
|
|
//添加划词标注
|
|
|
public String addScratchWords(ScratchWords scratchWords) {
|
|
@@ -51,7 +64,7 @@ public class EsScratchWordsService {
|
|
|
|
|
|
Marking marking = new Marking();
|
|
|
BeanUtils.copyProperties(scratchWords, marking);
|
|
|
-
|
|
|
+ marking.setMarkTime(new Date());
|
|
|
String id = null;
|
|
|
try {
|
|
|
id = this.saveToEs(marking);
|
|
@@ -77,86 +90,20 @@ public class EsScratchWordsService {
|
|
|
return id;
|
|
|
}
|
|
|
|
|
|
- public Records queryScratchWord(QueryScratchWordsDTO queryScratchWordsDTO) {
|
|
|
- Long current = queryScratchWordsDTO.getCurrent();
|
|
|
- Long size = queryScratchWordsDTO.getSize();
|
|
|
+ public Records queryScratchWord(StringRequest stringRequest) {
|
|
|
+ Long current = stringRequest.getCurrent();
|
|
|
+ Long size = stringRequest.getSize();
|
|
|
Records records = new Records();
|
|
|
List<ScratchWordsVO> scratchWordsVOS = new ArrayList<>();
|
|
|
- PersonnelVO personnelVO = cacheUtils.getLoginUser(loginUtils.getId());
|
|
|
- String createId = personnelVO.getId();
|
|
|
- Integer tenantId = personnelVO.getTenantId();
|
|
|
- String createName = queryScratchWordsDTO.getCreateName();
|
|
|
- String patentNo = queryScratchWordsDTO.getPatentNo();
|
|
|
- String content = queryScratchWordsDTO.getMarkSelectText();
|
|
|
- Date marketTime = queryScratchWordsDTO.getMarkTime();
|
|
|
- Integer projectId = queryScratchWordsDTO.getProjectId();
|
|
|
- //公开
|
|
|
- Query q1 = QueryBuilders.term(t -> t.field("is_public").value(true));
|
|
|
- //不公开
|
|
|
- Query q2 = QueryBuilders.term(t -> t.field("is_public").value(false));
|
|
|
- //创建人id
|
|
|
- Query q3 = QueryBuilders.term(t -> t.field("mark_user_id").value(createId));
|
|
|
- //租户id
|
|
|
- Query q4 = QueryBuilders.term(t -> t.field("public_tenant_id").value(tenantId));
|
|
|
- //公开范围为全部
|
|
|
- Query q5 = QueryBuilders.term(t -> t.field("public_scope").value(0));
|
|
|
- //公开范围为所在地
|
|
|
- Query q6 = QueryBuilders.term(t -> t.field("public_scope").value(1));
|
|
|
- //数据库id
|
|
|
- Query q7 = QueryBuilders.term(t -> t.field("public_project_id").value(projectId));
|
|
|
-
|
|
|
-
|
|
|
- //公开并且全部范围
|
|
|
- Query bool1 = QueryBuilders.bool(i -> i.must(q1, q5, q4));
|
|
|
- // 公开并且所在地
|
|
|
- List<Query> bool2Querys=new ArrayList<>();
|
|
|
- bool2Querys.add(q1);
|
|
|
- bool2Querys.add(q6);
|
|
|
- if(projectId!=null){
|
|
|
- bool2Querys.add(q7);
|
|
|
- }
|
|
|
- bool2Querys.add(q4);
|
|
|
- Query bool2 = QueryBuilders.bool(i -> i.must(bool2Querys));
|
|
|
-
|
|
|
- //不公开并且全部范围
|
|
|
- Query bool3 = QueryBuilders.bool(i -> i.must(q2, q5, q3));
|
|
|
- //不公开并且所在地
|
|
|
- List<Query> bool3Querys=new ArrayList<>();
|
|
|
- bool3Querys.add(q2);
|
|
|
- bool3Querys.add(q6);
|
|
|
- if(projectId!=null){
|
|
|
- bool3Querys.add(q7);
|
|
|
- }
|
|
|
- bool3Querys.add(q3);
|
|
|
- Query bool4 = QueryBuilders.bool(i -> i.must(bool3Querys));
|
|
|
- Query riBool = QueryBuilders.bool(i -> i.should(bool1, bool2, bool3, bool4));
|
|
|
- List<Query> queries = new ArrayList<>();
|
|
|
- queries.add(riBool);
|
|
|
- if (patentNo != null) {
|
|
|
- Query query = this.getPatentNoQuery(patentNo);
|
|
|
- queries.add(query);
|
|
|
- }
|
|
|
- if (createName != null && createName.trim() != "") {
|
|
|
- Query query = this.getCreateQuery(createName);
|
|
|
- queries.add(query);
|
|
|
- }
|
|
|
- if (content != null && content.trim() != "") {
|
|
|
- Query query = this.getContentQuery(content);
|
|
|
- queries.add(query);
|
|
|
- }
|
|
|
- if (marketTime != null) {
|
|
|
- Query query = this.getMarkTimeQuery(marketTime);
|
|
|
- queries.add(query);
|
|
|
- }
|
|
|
- Query searchBool = QueryBuilders.bool(i -> i.must(queries));
|
|
|
+ Query searchQuerys = this.getSearchQuerys(stringRequest);
|
|
|
SearchRequest.Builder builder = new SearchRequest.Builder();
|
|
|
builder.index("patent_marking");
|
|
|
- if (current == null || size == null || current <0 || size <0) {
|
|
|
- current =Long.parseLong(1+"");
|
|
|
- size=Long.parseLong(1000+"");
|
|
|
+ if (current == null || size == null || current < 0 || size < 0) {
|
|
|
+ current = Long.parseLong(1 + "");
|
|
|
+ size = Long.parseLong(1000 + "");
|
|
|
}
|
|
|
builder.from((current.intValue() - 1) * size.intValue()).size(size.intValue());
|
|
|
- builder.query(searchBool);
|
|
|
+ builder.query(searchQuerys);
|
|
|
|
|
|
long total = 0;
|
|
|
try {
|
|
@@ -178,6 +125,72 @@ public class EsScratchWordsService {
|
|
|
return records;
|
|
|
}
|
|
|
|
|
|
+ public Query getSearchQuerys(StringRequest stringRequest) {
|
|
|
+// PersonnelVO personnelVO = cacheUtils.getLoginUser(loginUtils.getId());
|
|
|
+ PersonnelVO personnelVO = new PersonnelVO();
|
|
|
+ personnelVO.setId("328");
|
|
|
+ personnelVO.setTenantId(1);
|
|
|
+ String createId = personnelVO.getId();
|
|
|
+ Integer tenantId = personnelVO.getTenantId();
|
|
|
+ String searchCondition = stringRequest.getSearchQuery();
|
|
|
+ //1. 解析检索条件
|
|
|
+ Query reQuery = null;
|
|
|
+ try {
|
|
|
+
|
|
|
+ Query q = this.getSearchQQuery(searchCondition);
|
|
|
+ Integer projectId = stringRequest.getProjectId();
|
|
|
+ //公开
|
|
|
+ Query q1 = QueryBuilders.term(t -> t.field("is_public").value(true));
|
|
|
+ //不公开
|
|
|
+ Query q2 = QueryBuilders.term(t -> t.field("is_public").value(false));
|
|
|
+ //创建人id
|
|
|
+ Query q3 = QueryBuilders.term(t -> t.field("mark_user_id").value(createId));
|
|
|
+ //租户id
|
|
|
+ Query q4 = QueryBuilders.term(t -> t.field("public_tenant_id").value(tenantId));
|
|
|
+ //公开范围为全部
|
|
|
+ Query q5 = QueryBuilders.term(t -> t.field("public_scope").value(0));
|
|
|
+ //公开范围为所在地
|
|
|
+ Query q6 = QueryBuilders.term(t -> t.field("public_scope").value(1));
|
|
|
+ //数据库id
|
|
|
+ Query q7 = null;
|
|
|
+ if (projectId != null) {
|
|
|
+ q7 = QueryBuilders.term(t -> t.field("public_project_id").value(projectId));
|
|
|
+ }
|
|
|
+
|
|
|
+ //公开并且全部范围
|
|
|
+ Query bool1 = QueryBuilders.bool(i -> i.must(q1, q5, q4));
|
|
|
+ // 公开并且所在地
|
|
|
+ List<Query> bool2Querys = new ArrayList<>();
|
|
|
+ bool2Querys.add(q1);
|
|
|
+ bool2Querys.add(q6);
|
|
|
+ if (projectId != null) {
|
|
|
+ bool2Querys.add(q7);
|
|
|
+ }
|
|
|
+ bool2Querys.add(q4);
|
|
|
+ Query bool2 = QueryBuilders.bool(i -> i.must(bool2Querys));
|
|
|
+
|
|
|
+ //不公开并且全部范围
|
|
|
+ Query bool3 = QueryBuilders.bool(i -> i.must(q2, q5, q3));
|
|
|
+ //不公开并且所在地
|
|
|
+ List<Query> bool3Querys = new ArrayList<>();
|
|
|
+ bool3Querys.add(q2);
|
|
|
+ bool3Querys.add(q6);
|
|
|
+ if (projectId != null) {
|
|
|
+ bool3Querys.add(q7);
|
|
|
+ }
|
|
|
+ bool3Querys.add(q3);
|
|
|
+ Query bool4 = QueryBuilders.bool(i -> i.must(bool3Querys));
|
|
|
+ Query riBool = QueryBuilders.bool(i -> i.should(bool1, bool2, bool3, bool4));
|
|
|
+ if (q == null) {
|
|
|
+ reQuery = riBool;
|
|
|
+ } else {
|
|
|
+ reQuery = QueryBuilders.bool(i -> i.must(riBool, q));
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+
|
|
|
+ }
|
|
|
+ return reQuery;
|
|
|
+ }
|
|
|
|
|
|
//查询栏位历史
|
|
|
public Marking getById(String wordsId) throws Exception {
|
|
@@ -203,43 +216,6 @@ public class EsScratchWordsService {
|
|
|
}
|
|
|
|
|
|
|
|
|
- //获得专利号检索式
|
|
|
- public Query getPatentNoQuery(String patentNo) {
|
|
|
- Query query = QueryBuilders.term(t -> t.field("patent_no").value(patentNo));
|
|
|
- return query;
|
|
|
- }
|
|
|
-
|
|
|
- //获得创建时间检索式
|
|
|
- public Query getMarkTimeQuery(Date markTime) {
|
|
|
- SimpleDateFormat format2 = new SimpleDateFormat("yyyy-MM-dd");
|
|
|
- String time = format2.format(markTime);
|
|
|
- Query query = QueryBuilders.term(t -> t.field("mark_time").value(time));
|
|
|
- return query;
|
|
|
- }
|
|
|
-
|
|
|
- //获得创建人检索式
|
|
|
- public Query getCreateQuery(String createName) {
|
|
|
- List<String> ids = new ArrayList<>();
|
|
|
- try {
|
|
|
- String json = permissionService.getPersonIdByNamePCS(createName, false);
|
|
|
- ids = JSON.parseArray(json, String.class);
|
|
|
- } catch (Exception e) {
|
|
|
- }
|
|
|
- if (ids == null || ids.size() == 0) {
|
|
|
- ids = new ArrayList<>();
|
|
|
- ids.add("0");
|
|
|
- }
|
|
|
- String strs = JsonData.of(ids).toString();
|
|
|
- Query query = QueryBuilders.term(t -> t.field("mark_user_id").value(strs));
|
|
|
- return query;
|
|
|
- }
|
|
|
-
|
|
|
- //获得内容检索式
|
|
|
- public Query getContentQuery(String content) {
|
|
|
- Query query = QueryBuilders.match(t -> t.field("mark_select_text").query(content));
|
|
|
- return query;
|
|
|
- }
|
|
|
-
|
|
|
//保存
|
|
|
public String saveToEs(Marking marking) throws Exception {
|
|
|
IndexResponse indexResponse = client.index(i -> i
|
|
@@ -265,9 +241,99 @@ public class EsScratchWordsService {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- public void GroupScratchWord(){
|
|
|
+ public Records groupScratchWord(StringRequest stringRequest) {
|
|
|
+ Records records = new Records();
|
|
|
+ Query searchQuerys = this.getSearchQuerys(stringRequest);
|
|
|
+ Long current = stringRequest.getCurrent();
|
|
|
+ Long size = stringRequest.getSize();
|
|
|
+ String groupField = stringRequest.getGroupField();
|
|
|
+ String esGroupFiled = this.getGroupField(groupField);
|
|
|
+ SearchRequest.Builder builder = new SearchRequest.Builder();
|
|
|
+
|
|
|
+ builder.index("patent_marking");
|
|
|
+ builder.query(searchQuerys);
|
|
|
+ Aggregation hits = AggregationBuilders.bucketSort(i -> i.size(size.intValue()).from((current.intValue() - 1) * size.intValue()));
|
|
|
+ Aggregation aggregation = new Aggregation.Builder().terms(new TermsAggregation.Builder()
|
|
|
+ .field(esGroupFiled).build()).aggregations(new HashMap() {{
|
|
|
+ put("pageAgg", hits);
|
|
|
+ }}).build();
|
|
|
+
|
|
|
+ builder.aggregations("agg", aggregation);
|
|
|
+ builder.size(0);
|
|
|
+
|
|
|
+ //查询总数聚合
|
|
|
+ Aggregation totalAggregation = AggregationBuilders.cardinality(i -> i.field(esGroupFiled));
|
|
|
+ builder.aggregations("total", totalAggregation);
|
|
|
+ try {
|
|
|
+ SearchResponse<Marking> response = client.search(builder.build(), Marking.class);
|
|
|
+ Aggregate agg = response.aggregations().get("agg");
|
|
|
+ List<StringTermsBucket> list = agg.sterms().buckets().array();
|
|
|
+ List<Map<String, Object>> maps = new ArrayList<>();
|
|
|
+ for (StringTermsBucket bucket : list) {
|
|
|
+ String key = bucket.key().stringValue();
|
|
|
+ Long count = bucket.docCount();
|
|
|
+ Map map = new HashMap();
|
|
|
+ map.put("key", key);
|
|
|
+ map.put("count", count);
|
|
|
+ maps.add(map);
|
|
|
+ }
|
|
|
+ maps =this.loadNameMap(maps,groupField);
|
|
|
+ records.setData(maps);
|
|
|
+
|
|
|
+ Aggregate totalAgg = response.aggregations().get("total");
|
|
|
+ long total = totalAgg.cardinality().value();
|
|
|
+ records.setTotal(total);
|
|
|
+ records.setCurrent(current);
|
|
|
+ records.setSize(size);
|
|
|
+ } catch (Exception e) {
|
|
|
+ }
|
|
|
+ return records;
|
|
|
+ }
|
|
|
+
|
|
|
+ public String getGroupField(String field) {
|
|
|
+ String json = CommonService.readJsonFile("marking" + ".json");
|
|
|
+ List<EsConfigVO> esConfigVOS = JSON.parseArray(json, EsConfigVO.class);
|
|
|
+ EsConfigVO esConfigVO = esConfigVOS.stream().filter(item -> item.getField().equals(field)).findFirst().orElse(null);
|
|
|
+
|
|
|
+ return esConfigVO.getEsField();
|
|
|
+ }
|
|
|
|
|
|
+ public Query getSearchQQuery(String searchCondition) throws Exception {
|
|
|
+ Query q = null;
|
|
|
+ if (searchCondition != null && searchCondition != "") {
|
|
|
+ treeNode tree = expressManager.getInstance().Parse(searchCondition, false);
|
|
|
+ //格式化检索式
|
|
|
+ //3. 从es中检索数据
|
|
|
+ q = formatQueryService.EsQueryToQuery((operateNode) tree, "marking");
|
|
|
|
|
|
+ }
|
|
|
+ return q;
|
|
|
+ }
|
|
|
+
|
|
|
+ public List<Map<String, Object>> loadNameMap(List<Map<String, Object>> maps, String groupField) {
|
|
|
+ if (maps.size() == 0) {
|
|
|
+ return maps;
|
|
|
+ }
|
|
|
+ if (groupField.equals("createName")) {
|
|
|
+ try {
|
|
|
+ List<String> ids = new ArrayList<>();
|
|
|
+ maps.forEach(item -> ids.add(item.get("key").toString()));
|
|
|
+ String res = permissionService.getPersonnelByIdsFromPCS(ids);
|
|
|
+ JSONObject jsonObject = JSONObject.parseObject(res);
|
|
|
+ List<Personnel> personnels = JSONObject.parseArray(jsonObject.getString("data"), Personnel.class);
|
|
|
+ maps.forEach(item ->
|
|
|
+ {
|
|
|
+ Personnel personnel = personnels.stream().filter(i -> i.getId().equals(item.get("key"))).findFirst().orElse(null);
|
|
|
+ if (personnel != null) {
|
|
|
+ item.put("key", personnel.getPersonnelName());
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ );
|
|
|
+ } catch (Exception e) {
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return maps;
|
|
|
|
|
|
}
|
|
|
}
|