EsService.java 67 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666
  1. package cn.cslg.pas.service.business.es;
  2. import cn.cslg.pas.common.core.base.IfConstant;
  3. import cn.cslg.pas.common.dto.GetUnselectedDTO;
  4. import cn.cslg.pas.common.dto.PatentColumnDTO;
  5. import cn.cslg.pas.common.dto.PatentDTO;
  6. import cn.cslg.pas.common.dto.business.EsPatentFamilyDTO;
  7. import cn.cslg.pas.common.dto.business.SelectClaimDTO;
  8. import cn.cslg.pas.common.dto.es.EsCustomFieldValueDTO;
  9. import cn.cslg.pas.common.model.es.GetVectorVO;
  10. import cn.cslg.pas.common.model.request.MapRequest;
  11. import cn.cslg.pas.common.model.request.OrderDTO;
  12. import cn.cslg.pas.common.model.request.QueryRequest;
  13. import cn.cslg.pas.common.model.request.StringRequest;
  14. import cn.cslg.pas.common.utils.FormatUtil;
  15. import cn.cslg.pas.common.vo.*;
  16. import cn.cslg.pas.common.vo.es.EsCustomFieldBatchVO;
  17. import cn.cslg.pas.common.vo.es.EsPatentCommonVO;
  18. import cn.cslg.pas.common.vo.es.PatentFamilyMessageWithId;
  19. import cn.cslg.pas.domain.es.*;
  20. import cn.cslg.pas.exception.ExceptionEnum;
  21. import cn.cslg.pas.exception.XiaoShiException;
  22. import cn.cslg.pas.service.business.AssoProjectPersonService;
  23. import cn.cslg.pas.service.business.CommonService;
  24. import cn.cslg.pas.service.common.PatentStarApiService;
  25. import cn.cslg.pas.service.common.PythonApiService;
  26. import cn.cslg.pas.service.importPatent.WebVOTransformService;
  27. import cn.cslg.pas.service.query.FormatQueryService;
  28. import co.elastic.clients.elasticsearch.ElasticsearchClient;
  29. import co.elastic.clients.elasticsearch._types.*;
  30. import co.elastic.clients.elasticsearch._types.aggregations.*;
  31. import co.elastic.clients.elasticsearch._types.query_dsl.*;
  32. import co.elastic.clients.elasticsearch.core.*;
  33. import co.elastic.clients.elasticsearch.core.search.FieldCollapse;
  34. import co.elastic.clients.elasticsearch.core.search.Hit;
  35. import co.elastic.clients.elasticsearch.core.search.TrackHits;
  36. import co.elastic.clients.json.JsonData;
  37. import com.alibaba.fastjson.JSON;
  38. import lombok.RequiredArgsConstructor;
  39. import org.apache.commons.lang3.ObjectUtils;
  40. import org.apache.commons.lang3.StringUtils;
  41. import org.springframework.beans.BeanUtils;
  42. import org.springframework.beans.factory.annotation.Autowired;
  43. import org.springframework.context.annotation.Lazy;
  44. import org.springframework.stereotype.Service;
  45. import org.springframework.transaction.annotation.Propagation;
  46. import org.springframework.transaction.annotation.Transactional;
  47. import org.springframework.util.CollectionUtils;
  48. import java.io.IOException;
  49. import java.text.SimpleDateFormat;
  50. import java.util.*;
  51. import java.util.stream.Collectors;
  52. import static org.springframework.data.elasticsearch.annotations.TermVector.no;
  53. @Service
  54. @RequiredArgsConstructor(onConstructor_ = {@Lazy})
  55. public class EsService {
  56. public final List<String> dateList = Arrays.asList("AD", "PD", "GD", "EXD", "PAD", "PED", "PPD", "EPD");
  57. private final ElasticsearchClient client;
  58. private final PythonApiService pythonApiService;
  59. @Autowired
  60. private EsFamilyService esFamilyService;
  61. @Autowired
  62. private PatentStarApiService patentStarApiService;
  63. @Autowired
  64. private EsPatentService esPatentService;
  65. @Lazy
  66. @Autowired
  67. private EsCountService esCountService;
  68. @Autowired
  69. private EsCustomFieldService esCustomFieldService;
  70. @Autowired
  71. private AssoProjectPersonService assoProjectPersonService;
  72. /**
  73. * @param patent
  74. * @throws Exception
  75. */
  76. public String addPatent(Patent patent) throws Exception {
  77. IndexResponse indexResponse = client.index(i -> i
  78. .index("patent")
  79. //传入user对象
  80. .document(patent).refresh(Refresh.True)
  81. );
  82. return indexResponse.id();
  83. }
  84. /**
  85. * @param patent
  86. * @throws Exception
  87. */
  88. public String addChildPatent(Patent patent, String id) throws Exception {
  89. IndexResponse indexResponse = client.index(i -> i
  90. .index("patent")
  91. .routing(id)
  92. //传入user对象
  93. .document(patent).refresh(Refresh.True).waitForActiveShards(WaitForActiveShards.of(t -> t.count(1)))
  94. );
  95. return indexResponse.id();
  96. }
  97. /**
  98. * 根据专利号获取专利id
  99. *
  100. * @param patentNo
  101. * @return
  102. * @throws Exception
  103. */
  104. public PatentWithIdVO getIdByPatentNo(String patentNo) throws Exception {
  105. String patentNo1 = patentNo.replace(" ", "");
  106. SearchRequest.Builder builder = new SearchRequest.Builder();
  107. //设置查询索引
  108. builder.index("patent");
  109. PatentWithIdVO patentWithIdVO = null;
  110. String id = null;
  111. // Query q1 = QueryBuilders.term(t -> t.field("app_no.keyword").value(patentNo));
  112. // //公开号
  113. // Query q2 = QueryBuilders.term(t -> t.field("public_no.keyword").value(patentNo));
  114. // //授权号
  115. // Query q3 = QueryBuilders.term(t -> t.field("grant_no.keyword").value(patentNo));
  116. // Query query = QueryBuilders.bool(i -> i.should(q1, q2, q3));
  117. Query query = QueryBuilders.term(t -> t.field("patent_no.keyword").value(patentNo1));
  118. builder.query(query);
  119. SearchResponse<Patent> response = null;
  120. try {
  121. response = client.search(builder.build(), Patent.class);
  122. } catch (IOException e) {
  123. throw new XiaoShiException(ExceptionEnum.BUSINESS_ERROR, "ES检索失败");
  124. }
  125. List<Hit<Patent>> hits = response.hits().hits();
  126. if (hits != null && hits.size() > 0) {
  127. id = hits.get(0).id();
  128. Patent patent = hits.get(0).source();
  129. patentWithIdVO = new PatentWithIdVO();
  130. patentWithIdVO.setPatent(patent);
  131. patentWithIdVO.setId(id);
  132. }
  133. return patentWithIdVO;
  134. }
  135. /**
  136. * 根据专利号获取专利id
  137. *
  138. * @param patentNo
  139. * @return
  140. * @throws Exception
  141. */
  142. public PatentWithIdVO importPatentQueryByPatentNo(String patentNo) throws Exception {
  143. SearchRequest.Builder builder = new SearchRequest.Builder();
  144. //设置查询索引
  145. builder.index("patent");
  146. PatentWithIdVO patentWithIdVO = null;
  147. String id = null;
  148. Query query = QueryBuilders.term(t -> t.field("patent_no.keyword").value(patentNo));
  149. builder.query(query);
  150. SearchResponse<Patent> response = null;
  151. try {
  152. response = client.search(builder.build(), Patent.class);
  153. } catch (Exception e) {
  154. throw new XiaoShiException(ExceptionEnum.BUSINESS_ERROR, "ES检索失败");
  155. }
  156. List<Hit<Patent>> hits = response.hits().hits();
  157. if (hits != null && hits.size() > 0) {
  158. id = hits.get(0).id();
  159. Patent patent = hits.get(0).source();
  160. patentWithIdVO = new PatentWithIdVO();
  161. patentWithIdVO.setPatent(patent);
  162. patentWithIdVO.setId(id);
  163. }
  164. return patentWithIdVO;
  165. }
  166. /**
  167. * Es检索
  168. *
  169. * @param queryRequest 检索条件
  170. * @return
  171. */
  172. public PatentDTO esSearch(QueryRequest queryRequest) throws Exception {
  173. //this.judgeQueryPatentRoot(queryRequest);
  174. PatentDTO dto = new PatentDTO();
  175. Integer taskId = queryRequest.getTaskId();
  176. String searchCondition = "";
  177. Integer projectId = queryRequest.getProjectId();
  178. Integer productId = queryRequest.getProductId();
  179. String productFrom = queryRequest.getFrom();
  180. Long current = queryRequest.getCurrent();
  181. Long size = queryRequest.getSize();
  182. String groupField = queryRequest.getGroupField();
  183. //判断表达式
  184. if (queryRequest instanceof StringRequest) {
  185. searchCondition = ((StringRequest) queryRequest).getSearchQuery();
  186. if (searchCondition == null) {
  187. searchCondition = "";
  188. }
  189. } else if (queryRequest instanceof MapRequest) {
  190. Map<String, Object> map = ((MapRequest) queryRequest).getSearchQuery();
  191. StringBuilder stringBuilder = new StringBuilder();
  192. for (String key : map.keySet()) {
  193. Object value = map.get(key);
  194. if (!"".contentEquals(stringBuilder)) {
  195. stringBuilder = stringBuilder.append(" AND ").append(key).append("=").append(value);
  196. } else {
  197. stringBuilder = stringBuilder.append(key).append("=").append(value);
  198. }
  199. }
  200. searchCondition = stringBuilder.toString();
  201. }
  202. //判断同族分组
  203. String esField = "";
  204. if (StringUtils.isNotEmpty(groupField) && !groupField.equals("0")) {
  205. esField = this.getGroupField(groupField);
  206. }
  207. EsPatentCommonVO commonVO = new EsPatentCommonVO();
  208. commonVO.setTaskId(taskId);
  209. commonVO.setProjectId(projectId);
  210. commonVO.setProductId(productId);
  211. if (StringUtils.isNotEmpty(searchCondition) && searchCondition.contains("罒")) {
  212. String replace = searchCondition.replace("罒", " OR ");
  213. commonVO.setSearchCondition(replace);
  214. } else {
  215. commonVO.setSearchCondition(searchCondition);
  216. }
  217. commonVO.setProductFrom(productFrom);
  218. commonVO.setCurrent(current);
  219. commonVO.setSize(size);
  220. commonVO.setEsField(esField);
  221. commonVO.setCustomFields(queryRequest.getCustomFields());
  222. commonVO.setOrderDTOList(queryRequest.getOrderDTOList());
  223. commonVO.setSemanteme(queryRequest.getSemanteme());
  224. SearchRequest.Builder builder = this.getCommonPatent(commonVO);
  225. //解除最大条数限制
  226. builder.trackTotalHits(i -> i.enabled(true));
  227. SearchResponse<Patent> response = null;
  228. try {
  229. response = client.search(builder.build(), Patent.class);
  230. } catch (Exception e) {
  231. throw new XiaoShiException(ExceptionEnum.BUSINESS_ERROR, "ES检索失败");
  232. }
  233. List<PatentColumnDTO> list = new ArrayList<>();
  234. List<Hit<Patent>> hits = response.hits().hits();
  235. long total = 0L;
  236. if (StringUtils.isNotEmpty(esField)) {
  237. Aggregate aggregate = response.aggregations().get("count");
  238. total = aggregate.cardinality().value();
  239. } else {
  240. total = response.hits().total().value();
  241. }
  242. for (Hit<Patent> hit : hits) {
  243. Double score= hit.score();
  244. String str = String.format("%.4f",score);
  245. double four = Double.parseDouble(str);
  246. String id = hit.id();
  247. Patent esMess = hit.source();
  248. PatentColumnDTO columnDTO = this.getPatentColumnDTO(esMess, projectId, id, "");
  249. columnDTO.setScore(four);
  250. list.add(columnDTO);
  251. }
  252. this.loadCoulumnDTO(list);
  253. dto.setTotal(total);
  254. dto.setPatents(list);
  255. dto.setPageNum(current);
  256. dto.setPageSize(size);
  257. return dto;
  258. }
  259. //获取分组字段栏位
  260. public String getGroupField(String groupField) {
  261. String esField = "";
  262. switch (groupField) {
  263. case "simpleFamilyId":
  264. esField = "simple_family_id";
  265. break;
  266. case "inpadocFamilyId":
  267. esField = "inpadoc_family_id";
  268. break;
  269. case "patsnapFamilyId":
  270. esField = "patsnap_family_id";
  271. break;
  272. default:
  273. esField = "0";
  274. break;
  275. }
  276. return esField;
  277. }
  278. //获取通用检索专利方法--服务检索专利清单
  279. public SearchRequest.Builder getCommonPatent(EsPatentCommonVO commonVO) throws Exception {
  280. String esField = commonVO.getEsField();
  281. //计算分页信息
  282. Long current = commonVO.getCurrent();
  283. Long size = commonVO.getSize();
  284. Long startNum = commonVO.getStartNum();
  285. Long endNum = commonVO.getEndNum();
  286. String semanteme = commonVO.getSemanteme();
  287. Integer temPageFrom = 0;
  288. Integer temPageSize = 100000;
  289. if (current != null && size != null) {
  290. temPageFrom = (current.intValue() - 1) * size.intValue();
  291. temPageSize = size.intValue();
  292. } else if (startNum != null && endNum != null) {
  293. temPageFrom = startNum.intValue() - 1;
  294. temPageSize = (endNum.intValue() - startNum.intValue()) + 1;
  295. }
  296. Integer pageFrom = temPageFrom;
  297. Integer pageSize = temPageSize;
  298. SearchRequest.Builder builder = new SearchRequest.Builder();
  299. //设置查询索引
  300. builder.index("patent");
  301. Query reTmQuery = esPatentService.getEntireNotInQuery(commonVO);
  302. Query reQuery = this.getMeanSearch(reTmQuery, semanteme);
  303. //判断同族分组
  304. if (StringUtils.isNotEmpty(esField)) {
  305. Query existQ = QueryBuilders.exists(i -> i.field(esField));
  306. Query bool = QueryBuilders.bool(i -> i.must(reQuery, existQ));
  307. builder.query(bool);
  308. FieldCollapse collapse = FieldCollapse.of(i -> i.field(esField));
  309. builder.collapse(collapse);
  310. //统计总数
  311. Aggregation aggregation = AggregationBuilders.cardinality(i -> i.field(esField));
  312. builder.aggregations("count", aggregation);
  313. } else {
  314. builder.query(reQuery);
  315. }
  316. builder.from(pageFrom).size(pageSize);
  317. //排序
  318. if (semanteme == null || semanteme.trim().equals("")) {
  319. List<OrderDTO> dtoList = commonVO.getOrderDTOList();
  320. List<SortOptions> optionsList = this.getCommonSortMethod(dtoList);
  321. builder.sort(optionsList);
  322. }
  323. return builder;
  324. }
  325. //获取通用检索专利方法--服务于批量删除、标引、查询价值曲线、导出excel、pdf首页
  326. public SearchRequest.Builder getCommonPatentByGroup(EsPatentCommonVO commonVO) throws Exception {
  327. String esField = commonVO.getEsField();
  328. Query reQuery = esPatentService.getEntireNotInQuery(commonVO);
  329. SearchRequest.Builder builder = new SearchRequest.Builder();
  330. //设置查询索引
  331. builder.index("patent");
  332. //判断同族分组
  333. if (StringUtils.isNotEmpty(esField)) {
  334. Query existQ = QueryBuilders.exists(i -> i.field(esField));
  335. Query bool = QueryBuilders.bool(i -> i.must(reQuery, existQ));
  336. builder.query(bool);
  337. FieldCollapse collapse = FieldCollapse.of(i -> i.field(esField));
  338. builder.collapse(collapse);
  339. } else {
  340. builder.query(reQuery);
  341. }
  342. //排序
  343. List<OrderDTO> dtoList = commonVO.getOrderDTOList();
  344. List<SortOptions> optionsList = this.getCommonSortMethod(dtoList);
  345. builder.sort(optionsList);
  346. return builder;
  347. }
  348. public PatentWithIdVO getPatentColumnDTOByGroup2(String key, Integer projectId, String field) throws Exception {
  349. PatentWithIdVO dto = new PatentWithIdVO();
  350. SearchRequest.Builder builder = new SearchRequest.Builder();
  351. //设置查询索引
  352. builder.index("patent");
  353. Query query = QueryBuilders.term(i -> i.field("project_id").value(projectId));
  354. Query q1 = QueryBuilders.hasChild(i -> i.type("project").query(query));
  355. Query q2 = QueryBuilders.term(i -> i.field(field).value(key));
  356. Query bool = QueryBuilders.bool(i -> i.must(q1, q2));
  357. builder.query(bool);
  358. SearchResponse<Patent> response = null;
  359. try {
  360. response = client.search(builder.build(), Patent.class);
  361. } catch (Exception e) {
  362. throw new XiaoShiException(ExceptionEnum.BUSINESS_ERROR, "ES检索失败");
  363. }
  364. List<Hit<Patent>> hits = response.hits().hits();
  365. if (!CollectionUtils.isEmpty(hits)) {
  366. String id = hits.get(0).id();
  367. Patent patent = hits.get(0).source();
  368. dto.setPatent(patent);
  369. dto.setId(id);
  370. }
  371. return dto;
  372. }
  373. /**
  374. * 获取公共排序方法
  375. *
  376. * @param dtoList
  377. * @return
  378. */
  379. public List<SortOptions> getCommonSortMethod(List<OrderDTO> dtoList) {
  380. List<SortOptions> optionsList = new ArrayList<>();
  381. if (!CollectionUtils.isEmpty(dtoList)) {
  382. String json = CommonService.readJsonFile("patent.json");
  383. List<EsConfigVO> esConfigVOS = JSON.parseArray(json, EsConfigVO.class);
  384. for (OrderDTO orderDTO : dtoList) {
  385. EsConfigVO configVO = esConfigVOS.stream().filter(item -> item.getField().equals(orderDTO.getOrderBy())).findFirst().orElse(null);
  386. if (configVO != null) {
  387. if (orderDTO.getOrderType().equals(IfConstant.NO)) {
  388. SortOptions sortOptions = SortOptions.of(i -> i.field(j -> j.field(configVO.getEsField()).order(SortOrder.Asc).missing(-1)));
  389. optionsList.add(sortOptions);
  390. } else {
  391. SortOptions sortOptions = SortOptions.of(i -> i.field(j -> j.field(configVO.getEsField()).order(SortOrder.Desc).missing(-1)));
  392. optionsList.add(sortOptions);
  393. }
  394. }
  395. }
  396. } else {
  397. SortOptions sortOptions = SortOptions.of(i -> i.field(j -> j.field("patent_no.keyword").order(SortOrder.Desc).missing(-1)));
  398. optionsList.add(sortOptions);
  399. }
  400. return optionsList;
  401. }
  402. //装载专利清单列表的数据
  403. public PatentColumnDTO getPatentColumnDTO(Patent patent, Integer projectId, String id, String typeStr) {
  404. PatentColumnDTO columnDTO = new PatentColumnDTO();
  405. columnDTO.setPatentId(id);
  406. BeanUtils.copyProperties(patent, columnDTO);
  407. if (!CollectionUtils.isEmpty(patent.getIpc())) {
  408. boolean b = patent.getIpc().stream().allMatch(Objects::isNull);
  409. if (b) {
  410. columnDTO.setIpc(new ArrayList<>());
  411. }
  412. }
  413. if (projectId != null) {
  414. try {
  415. columnDTO.setMergeApplicant(esPatentService.getMergeApp(projectId, id));
  416. } catch (IOException e) {
  417. columnDTO.setMergeApplicant(new ArrayList<>());
  418. }
  419. try {
  420. columnDTO.setMergeRightHolder(esPatentService.getMergeRight(projectId, id));
  421. } catch (IOException e) {
  422. columnDTO.setMergeRightHolder(new ArrayList<>());
  423. }
  424. try {
  425. columnDTO.setMergeInventor(esPatentService.getMergeInventor(projectId, id));
  426. } catch (IOException e) {
  427. columnDTO.setMergeInventor(new ArrayList<>());
  428. }
  429. }
  430. if (ObjectUtils.isNotEmpty(patent.getApplicantAddr())) {
  431. PersonAddress applicantAddr = patent.getApplicantAddr();
  432. columnDTO.setAppAddress(applicantAddr.getAddress());
  433. columnDTO.setApplicantCountry(applicantAddr.getCountry());
  434. columnDTO.setAppProvince(applicantAddr.getProvince());
  435. columnDTO.setAppCity(applicantAddr.getCity());
  436. columnDTO.setAppDistrict(applicantAddr.getDistrict());
  437. }
  438. if (ObjectUtils.isNotEmpty(patent.getRightHolderAddr())) {
  439. PersonAddress rightAddr = patent.getRightHolderAddr();
  440. columnDTO.setRightAddress(rightAddr.getAddress());
  441. columnDTO.setRightCountry(rightAddr.getCountry());
  442. columnDTO.setRightProvince(rightAddr.getProvince());
  443. columnDTO.setRightCity(rightAddr.getCity());
  444. columnDTO.setRightDistrict(rightAddr.getDistrict());
  445. }
  446. if (!CollectionUtils.isEmpty(patent.getPriorities())) {
  447. List<Priorities> priorities = patent.getPriorities();
  448. List<String> priorityCountrys = priorities.stream().map(Priorities::getPriorityCountry).distinct().collect(Collectors.toList());
  449. List<String> priorityNos = priorities.stream().map(Priorities::getPriorityNo).distinct().collect(Collectors.toList());
  450. List<String> priorityDates = priorities.stream().map(Priorities::getPriorityDate).distinct().collect(Collectors.toList());
  451. if (StringUtils.isNotEmpty(typeStr)) {
  452. String priorityCountry = String.join(",", priorityCountrys);
  453. columnDTO.setPriorityCountry(priorityCountry);
  454. String priorityNo = String.join(",", priorityNos);
  455. columnDTO.setPriorityNo(priorityNo);
  456. String priorityDate = String.join(",", priorityDates);
  457. if (priorityDate.contains("null")) {
  458. columnDTO.setPriorityDate("");
  459. } else {
  460. columnDTO.setPriorityDate(priorityDate);
  461. }
  462. } else {
  463. String priorityCountry = String.join("<br>", priorityCountrys);
  464. columnDTO.setPriorityCountry(priorityCountry);
  465. String priorityNo = String.join("<br>", priorityNos);
  466. columnDTO.setPriorityNo(priorityNo);
  467. String priorityDate = String.join("<br>", priorityDates);
  468. if (priorityDate.contains("null")) {
  469. columnDTO.setPriorityDate("");
  470. } else {
  471. columnDTO.setPriorityDate(priorityDate);
  472. }
  473. }
  474. }
  475. columnDTO.setApplicant(esPatentService.loadName(patent.getApplicant()));
  476. columnDTO.setRightHolder(esPatentService.loadName(patent.getRightHolder()));
  477. columnDTO.setInventor(esPatentService.loadName(patent.getInventor()));
  478. return columnDTO;
  479. }
  480. //拼接专题库id或其他id条件
  481. public String appendIdsCondition(String searchCondition, Integer taskId, String productFrom, Integer projectId, Integer productId) {
  482. String condition = "";
  483. if (taskId != null) {
  484. if (searchCondition != null && !"".equals(searchCondition.trim())) {
  485. searchCondition = "taskId = " + taskId + " AND " + searchCondition;
  486. } else {
  487. searchCondition = "taskId = " + taskId;
  488. }
  489. } else {
  490. if (StringUtils.isNotEmpty(productFrom)) {
  491. if (productId != null) {
  492. if (searchCondition != null && !"".equals(searchCondition.trim())) {
  493. searchCondition = "productId = " + productId + " AND " + searchCondition;
  494. } else {
  495. searchCondition = "productId = " + productId;
  496. }
  497. }
  498. } else {
  499. if (projectId != null) {
  500. if (searchCondition != null && !"".equals(searchCondition.trim())) {
  501. searchCondition = "projectId = " + projectId + " AND " + searchCondition;
  502. } else {
  503. searchCondition = "projectId = " + projectId;
  504. }
  505. }
  506. }
  507. }
  508. condition = searchCondition;
  509. return condition;
  510. }
  511. /**
  512. * 解析自定义栏位和值
  513. *
  514. * @param customFields
  515. * @return
  516. */
  517. public String parseCustomField(List<EsCustomFieldValueDTO> customFields, Integer projectId, Integer taskId) throws Exception {
  518. int m = 1;
  519. int n = 0;
  520. StringBuilder builder = new StringBuilder();
  521. if (customFields.size() > m) {
  522. for (int i = 0; i < customFields.size(); i++) {
  523. EsCustomFieldValueDTO customField = customFields.get(i);
  524. if (i != n) {
  525. builder.append(" ").append("and").append(" ").append("(");
  526. this.appendStr(customField, builder, m, customField.getIfHaveChild(), projectId, taskId);
  527. } else {
  528. builder.append("(");
  529. this.appendStr(customField, builder, m, customField.getIfHaveChild(), projectId, taskId);
  530. }
  531. }
  532. } else {
  533. for (int i = 0; i < customFields.size(); i++) {
  534. EsCustomFieldValueDTO customField = customFields.get(i);
  535. if (i != n) {
  536. builder.append(" ").append("and").append(" ");
  537. this.appendStr(customField, builder, m, customField.getIfHaveChild(), projectId, taskId);
  538. } else {
  539. builder.append("(");
  540. this.appendStr(customField, builder, m, customField.getIfHaveChild(), projectId, taskId);
  541. }
  542. }
  543. }
  544. return builder.toString();
  545. }
  546. //拼接自定义栏位
  547. public void appendStr(EsCustomFieldValueDTO customField, StringBuilder builder, int m, boolean ifHaveChild,
  548. Integer projectId, Integer taskId) throws Exception {
  549. builder.append("field").append("=").append(customField.getFieldId());
  550. List<String> values = customField.getFieldValue();
  551. if (!CollectionUtils.isEmpty(values)) {
  552. builder.append(" ").append("and").append(" ");
  553. if (ifHaveChild) {
  554. builder.append("statsValue").append("=");
  555. } else {
  556. builder.append("fieldValue").append("=");
  557. }
  558. if (values.size() > m) {
  559. builder.append("(");
  560. for (int j = 0; j < values.size(); j++) {
  561. String s = values.get(j);
  562. if (j != values.size() - m) {
  563. builder.append(s).append(" ").append("or").append(" ");
  564. } else {
  565. if (s.equals("未选择")) {
  566. String noCondition = "(field = (NOT " + customField.getFieldId() + ")" + " OR " + "(field = " + customField.getFieldId() + " AND " + "fieldValue1 = 1))";
  567. builder.append(s).append(")").append(" ").append("OR")
  568. .append(" ").append("(").append(noCondition).append(")").append(")");
  569. } else {
  570. builder.append(s).append(")").append(")");
  571. }
  572. }
  573. }
  574. } else {
  575. for (String value : values) {
  576. if (value.equals("未选择")) {
  577. String noCondition = "(field = (NOT " + customField.getFieldId() + ")" + " OR " + "(field = " + customField.getFieldId() + " AND " + "fieldValue1 = 1))";
  578. builder.append(value).append(" ").append("OR").append(" ").append(noCondition).append(")");
  579. } else {
  580. builder.append(value).append(")");
  581. }
  582. }
  583. }
  584. } else {
  585. builder.append(")");
  586. }
  587. }
  588. //拼接专利ids
  589. public String appendIds(List<String> nos) {
  590. String str = "ids = ";
  591. if (nos.size() > 1) {
  592. str = str + "\"";
  593. for (int i = 0; i < nos.size(); i++) {
  594. String s = nos.get(i);
  595. if (i != nos.size() - 1) {
  596. str = str + s + ",";
  597. } else {
  598. str = str + s + "\"";
  599. }
  600. }
  601. } else {
  602. for (String no : nos) {
  603. str = str + no;
  604. }
  605. }
  606. return str;
  607. }
  608. //拼接专利号NOS
  609. public String appendNOS(List<String> nos) {
  610. String str = "NO = ";
  611. if (nos.size() > 1) {
  612. str = str + "(";
  613. for (int i = 0; i < nos.size(); i++) {
  614. String s = nos.get(i);
  615. if (i != nos.size() - 1) {
  616. str = str + s + " " + "OR" + " ";
  617. } else {
  618. str = str + s + ")";
  619. }
  620. }
  621. } else {
  622. for (String no : nos) {
  623. str = str + no;
  624. }
  625. }
  626. return str;
  627. }
  628. //更新patent
  629. public Integer updatePatent(Patent patent, String id) {
  630. UpdateRequest<Patent, Patent> req;
  631. req = UpdateRequest.of(
  632. b -> b.index("patent").id(id)
  633. .doc(patent).refresh(Refresh.True)
  634. );
  635. try {
  636. client.update(req, Patent.class);
  637. return 1;
  638. } catch (IOException e) {
  639. return -1;
  640. }
  641. }
  642. //更新patent
  643. public Integer updatePatentShouldWait(Patent patent, String id) {
  644. UpdateRequest<Patent, Patent> req;
  645. req = UpdateRequest.of(
  646. b -> b.index("patent").id(id)
  647. .doc(patent).refresh(Refresh.True)
  648. );
  649. try {
  650. client.update(req, Patent.class);
  651. return 1;
  652. } catch (IOException e) {
  653. return -1;
  654. }
  655. }
  656. //更新patent
  657. public Integer updatePatentById(Patent patent, String id) {
  658. UpdateRequest<Patent, Patent> req;
  659. req = UpdateRequest.of(
  660. b -> b.index("patent").id(id)
  661. .doc(patent));
  662. try {
  663. client.update(req, Patent.class);
  664. return 1;
  665. } catch (IOException e) {
  666. return -1;
  667. }
  668. }
  669. //获取父文档相关的所有自定义子文档,按照时间按倒序,首个改为最新数据,其他if——new改为0:不是最新
  670. public Integer getIds(String parentId, Integer projectId, Integer taskId, String fieldId) throws Exception {
  671. int ifNew = 0;
  672. SearchRequest.Builder builder = new SearchRequest.Builder();
  673. //设置查询索引
  674. builder.index("patent");
  675. Query q1 = QueryBuilders.hasParent(i -> i.parentType("patent").query(j -> j.ids(IdsQuery.of(k -> k.values(parentId)))));
  676. Query q2 = QueryBuilders.exists(i -> i.field("custom_field"));
  677. Query q3 = QueryBuilders.term(i -> i.field("custom_field.project_id").value(projectId));
  678. Query q4 = QueryBuilders.term(i -> i.field("custom_field.field").value(fieldId));
  679. Query bool = QueryBuilders.bool(i -> i.must(q1, q2, q3, q4));
  680. builder.query(bool);
  681. List<SortOptions> optionsList = new ArrayList<>();
  682. SortOptions sortOptions = SortOptions.of(i -> i.field(j -> j.field("custom_field.create_time").order(SortOrder.Desc).missing(-1)));
  683. optionsList.add(sortOptions);
  684. builder.sort(optionsList);
  685. SearchResponse<Patent> response = null;
  686. try {
  687. response = client.search(builder.build(), Patent.class);
  688. } catch (Exception e) {
  689. throw new XiaoShiException(ExceptionEnum.BUSINESS_ERROR, "ES检索失败");
  690. }
  691. List<Hit<Patent>> hits = response.hits().hits();
  692. Map<String, ESCustomField> map = new HashMap<>();
  693. for (Hit<Patent> hit : hits) {
  694. String id = hit.id();
  695. Patent patent = hit.source();
  696. map.put(id, patent.getESCustomField());
  697. }
  698. if (!CollectionUtils.isEmpty(map)) {
  699. for (String id : map.keySet()) {
  700. ESCustomField esCustomField = map.get(id);
  701. esCustomField.setIfNew(0);
  702. Patent patent = new Patent();
  703. patent.setESCustomField(esCustomField);
  704. Integer aNew = this.updateCustomFieldIfNew(patent, id);
  705. if (aNew > 0) {
  706. ifNew++;
  707. } else {
  708. ifNew = 0;
  709. break;
  710. }
  711. }
  712. }
  713. return ifNew;
  714. }
  715. //更新自定义文档是否是最新数据
  716. public Integer updateCustomFieldIfNew(Patent patent, String id) throws IOException {
  717. ESCustomField customField = patent.getESCustomField();
  718. String source = "ctx._source.custom_field.if_new = " + customField.getIfNew();
  719. InlineScript inlineScript = InlineScript.of(i -> i.lang("painless").source(source));
  720. Script script = Script.of(i -> i.inline(inlineScript));
  721. Query query = QueryBuilders.term(i -> i.field("_id").value(id));
  722. UpdateByQueryRequest request = UpdateByQueryRequest.of(i -> i.index("patent").script(script).refresh(true).query(query));
  723. try {
  724. client.updateByQuery(request);
  725. return 1;
  726. } catch (IOException e) {
  727. return -1;
  728. }
  729. }
  730. //更新子文档
  731. public Integer updateByQuery(Patent patent, String id) throws Exception {
  732. ESCustomField customField = patent.getESCustomField();
  733. String parentId = patent.getPatentJoin().getParent();
  734. Integer num = this.getIds(parentId, customField.getProjectId(), customField.getTaskId(), customField.getField());
  735. if (num < 1) {
  736. throw new XiaoShiException(ExceptionEnum.BUSINESS_ERROR, "子文档是否最新更新失败");
  737. }
  738. String valueField = "[";
  739. List<String> fieldValueList = customField.getFieldValue();
  740. if (!CollectionUtils.isEmpty(fieldValueList)) {
  741. int lastSum = fieldValueList.size() - 1;
  742. for (int i = 0; i < fieldValueList.size(); i++) {
  743. String s = fieldValueList.get(i);
  744. if (i == lastSum) {
  745. valueField = valueField + "\"" + s + "\"";
  746. } else {
  747. valueField = valueField + "\"" + s + "\"" + ",";
  748. }
  749. }
  750. }
  751. valueField = valueField + "]";
  752. String valueStats = "[";
  753. List<String> statsValueList = customField.getStatsValue();
  754. if (!CollectionUtils.isEmpty(statsValueList)) {
  755. int lastSum = statsValueList.size() - 1;
  756. for (int i = 0; i < statsValueList.size(); i++) {
  757. String s = statsValueList.get(i);
  758. if (i == lastSum) {
  759. valueStats = valueStats + "\"" + s + "\"";
  760. } else {
  761. valueStats = valueStats + "\"" + s + "\"" + ",";
  762. }
  763. }
  764. }
  765. valueStats = valueStats + "]";
  766. SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
  767. String s = format.format(customField.getCreateTime());
  768. String dateStr = "\'" + s + "\'";
  769. String projectId = "ctx._source.custom_field.project_id = " + customField.getProjectId() + ";";
  770. String taskId = "ctx._source.custom_field.task_id = " + customField.getTaskId() + ";";
  771. String ifNew = "ctx._source.custom_field.if_new = " + 1 + ";";
  772. String field = "ctx._source.custom_field.field=" + customField.getField() + ";";
  773. String fieldType = "ctx._source.custom_field.field_type = " + customField.getFieldType() + ";";
  774. String personId = "ctx._source.custom_field.person_id = " + customField.getPersonId() + ";";
  775. String createTime = "ctx._source.custom_field.create_time = " + dateStr + ";";
  776. String fieldValue = "ctx._source.custom_field.field_value = " + valueField + ";";
  777. String statsValue = "ctx._source.custom_field.stats_value = " + valueStats;
  778. String source = projectId + taskId + ifNew + field + fieldType + personId + createTime + fieldValue + statsValue;
  779. InlineScript inlineScript = InlineScript.of(i -> i.lang("painless").source(source));
  780. Script script = Script.of(i -> i.inline(inlineScript));
  781. Query query = QueryBuilders.term(i -> i.field("_id").value(id));
  782. UpdateByQueryRequest request = UpdateByQueryRequest.of(i -> i.index("patent").script(script).refresh(true).query(query));
  783. try {
  784. client.updateByQuery(request);
  785. return 1;
  786. } catch (IOException e) {
  787. return -1;
  788. }
  789. }
  790. /**
  791. * @param key
  792. * @param page
  793. * @param limit
  794. * @return
  795. * @throws IOException
  796. */
  797. public List<Patent> searchChild(String key, Integer page, Integer limit) throws IOException {
  798. SearchRequest.Builder builder = new SearchRequest.Builder();
  799. //设置查询索引
  800. builder.index("patent");
  801. //组装查询条件
  802. HasChildQuery.Builder hasChildQuery = new HasChildQuery.Builder();
  803. hasChildQuery.type("project");
  804. hasChildQuery.query(q -> q.match(m -> m
  805. .query(key)
  806. //字段名
  807. .field("project_id")
  808. ));
  809. builder.query(q -> q.hasChild(hasChildQuery.build()));
  810. SearchResponse<Patent> response = null;
  811. try {
  812. response = client.search(builder.build(), Patent.class);
  813. } catch (Exception e) {
  814. throw new XiaoShiException(ExceptionEnum.BUSINESS_ERROR, "ES检索失败");
  815. }
  816. List<Patent> list = new ArrayList<>();
  817. List<Hit<Patent>> hits = response.hits().hits();
  818. for (Hit<Patent> hit : hits) {
  819. Patent esMess = hit.source();
  820. list.add(esMess);
  821. }
  822. return list;
  823. }
  824. /**
  825. * 查询是否存在专利
  826. *
  827. * @param parentId
  828. * @param projectId
  829. * @return
  830. */
  831. public Boolean searchPatent(String parentId, Integer projectId) throws IOException {
  832. boolean flag = false;
  833. SearchRequest.Builder builder = new SearchRequest.Builder();
  834. //设置查询索引
  835. builder.index("patent");
  836. Query q1 = QueryBuilders.term(t -> t.field("project_id").value(projectId));
  837. Query q2 = QueryBuilders.parentId(parent -> parent.type("project").id(parentId));
  838. Query bool = QueryBuilders.bool(i -> i.must(q1, q2));
  839. builder.query(bool);
  840. SearchResponse<Patent> response = null;
  841. try {
  842. response = client.search(builder.build(), Patent.class);
  843. } catch (Exception e) {
  844. throw new XiaoShiException(ExceptionEnum.BUSINESS_ERROR, "ES检索失败");
  845. }
  846. long total = response.hits().total().value();
  847. if (total > 0) {
  848. flag = true;
  849. }
  850. return flag;
  851. }
  852. /**
  853. * 查询是否存在专利
  854. *
  855. * @param parentId
  856. * @param projectId
  857. * @return
  858. */
  859. public Boolean ifInTask(String parentId, Integer projectId, Integer taskId) throws IOException {
  860. boolean flag = false;
  861. SearchRequest.Builder builder = new SearchRequest.Builder();
  862. //设置查询索引
  863. builder.index("patent");
  864. Query q1 = QueryBuilders.term(t -> t.field("import_task.project_id").value(projectId));
  865. Query q3 = QueryBuilders.term(t -> t.field("import_task.task_id").value(projectId));
  866. Query q2 = QueryBuilders.parentId(parent -> parent.type("import_task").id(parentId));
  867. Query bool = QueryBuilders.bool(i -> i.must(q1, q2, q3));
  868. builder.query(bool);
  869. SearchResponse<Patent> response = null;
  870. try {
  871. response = client.search(builder.build(), Patent.class);
  872. } catch (Exception e) {
  873. throw new XiaoShiException(ExceptionEnum.BUSINESS_ERROR, "ES检索失败");
  874. }
  875. long total = response.hits().total().value();
  876. if (total > 0) {
  877. flag = true;
  878. }
  879. return flag;
  880. }
  881. /**
  882. * 查询同族
  883. *
  884. * @param nos
  885. * @param type
  886. * @return
  887. * @throws IOException
  888. */
  889. public EsPatentFamilyDTO addEsPatentFamily(Patent patent, List<String> nos, String type) throws Exception {
  890. EsPatentFamilyDTO esDTO = new EsPatentFamilyDTO();
  891. List<String> newNos = new ArrayList<>();
  892. nos.forEach(item -> newNos.add(item.trim()));
  893. nos = newNos;
  894. PatentFamilyMessage patentFamilyMessage = null;
  895. List<FamilyPatent> familyPatents = new ArrayList<>();
  896. String id = "";
  897. PatentFamilyMessageWithId patentFamilyMessageWithId = queryPatentFamily(patent, type);
  898. if (patentFamilyMessageWithId != null) {
  899. patentFamilyMessage = patentFamilyMessageWithId.getPatentFamilyMessage();
  900. id = patentFamilyMessageWithId.getId();
  901. } else {
  902. //遍历,根据专利号查询同族
  903. for (String no : nos) {
  904. SearchRequest.Builder builder = new SearchRequest.Builder();
  905. //设置查询索引
  906. builder.index("patent_family");
  907. //申请号
  908. Query q1 = QueryBuilders.term(t -> t.field("patent.app_no").value(no));
  909. //公开号
  910. Query q2 = QueryBuilders.term(t -> t.field("patent.public_no").value(no));
  911. //授权号
  912. Query q3 = QueryBuilders.term(t -> t.field("patent.grant_no").value(no));
  913. //类型
  914. Query q4 = QueryBuilders.term(t -> t.field("family_type").value(type));
  915. Query p = QueryBuilders.bool(i -> i.should(q1, q2, q3));
  916. Query bool = QueryBuilders.bool(i -> i.must(q4, p));
  917. builder.query(bool);
  918. SearchResponse<PatentFamilyMessage> response = null;
  919. try {
  920. response = client.search(builder.build(), PatentFamilyMessage.class);
  921. } catch (Exception e) {
  922. throw new XiaoShiException(ExceptionEnum.BUSINESS_ERROR, "ES检索失败");
  923. }
  924. long total = response.hits().total().value();
  925. if (total > 0) {
  926. List<FamilyPatent> list = new ArrayList<>();
  927. List<Hit<PatentFamilyMessage>> hits = response.hits().hits();
  928. Hit<PatentFamilyMessage> hit = hits.get(0);
  929. id = hit.id();
  930. patentFamilyMessage = hit.source();
  931. break;
  932. }
  933. }
  934. }
  935. if (patentFamilyMessage == null) {
  936. patentFamilyMessage = new PatentFamilyMessage();
  937. patentFamilyMessage.setFamilyType(type);
  938. } else {
  939. familyPatents = patentFamilyMessage.getPatent();
  940. familyPatents = this.getDistFamilyPatents(familyPatents);
  941. }
  942. //装载专利
  943. String appNo = "";
  944. String publicNo = "";
  945. String grantNo = "";
  946. if (patent.getAppNo() != null) {
  947. appNo = patent.getAppNo();
  948. }
  949. if (patent.getPublicNo() != null) {
  950. publicNo = patent.getPublicNo();
  951. }
  952. if (patent.getGrantNo() != null) {
  953. grantNo = patent.getGrantNo();
  954. }
  955. String appNo1 = appNo;
  956. String publicNo1 = publicNo;
  957. String grantNo1 = grantNo;
  958. FamilyPatent patent1 = familyPatents.stream().filter(item ->
  959. publicNo1.equals(item.getPublicNo()) || publicNo1.equals(item.getGrantNo()) || publicNo1.equals(item.getAppNo()) || grantNo1.equals(item.getPublicNo())
  960. || grantNo1.equals(item.getGrantNo()) || grantNo1.equals(item.getAppNo()) || appNo1.equals(item.getAppNo()) || appNo1.equals(item.getPublicNo()) || appNo1.equals(item.getGrantNo())
  961. ).findFirst().orElse(null);
  962. if (patent1 == null) {
  963. patent1 = new FamilyPatent();
  964. patent1.setGrantNo(patent.getGrantNo());
  965. patent1.setAppNo(patent.getAppNo());
  966. patent1.setPublicNo(patent.getPublicNo());
  967. familyPatents.add(patent1);
  968. } else {
  969. patent1.setGrantNo(patent.getGrantNo());
  970. patent1.setAppNo(patent.getAppNo());
  971. patent1.setPublicNo(patent.getPublicNo());
  972. }
  973. //装载专利号
  974. familyPatents = this.getDistFamilyPatentsWithNos(familyPatents, nos);
  975. patentFamilyMessage.setPatent(familyPatents);
  976. //当id不为null时
  977. if (!id.equals("")) {
  978. this.updatePatentFamily(patentFamilyMessage, id);
  979. } else {
  980. try {
  981. id = this.addPatentFamily(patentFamilyMessage);
  982. } catch (Exception e) {
  983. }
  984. }
  985. if (familyPatents != null && familyPatents.size() > 0) {
  986. List<String> temNos = new ArrayList<>();
  987. familyPatents.forEach(item -> {
  988. if (item.getGrantNo() != null) {
  989. temNos.add(item.getGrantNo());
  990. } else if (item.getPublicNo() != null) {
  991. temNos.add(item.getPublicNo());
  992. } else if (item.getAppNo() != null) {
  993. temNos.add(item.getAppNo());
  994. }
  995. });
  996. esFamilyService.updatePatentFamilyByQuery(type, id, familyPatents.size(), temNos);
  997. }
  998. esDTO.setPatentFamilyId(id);
  999. esDTO.setFamilyNum(patentFamilyMessage.getPatent().size());
  1000. return esDTO;
  1001. }
  1002. private List<FamilyPatent> getDistFamilyPatents(List<FamilyPatent> familyPatents) {
  1003. List<FamilyPatent> familyPatentsNew = new ArrayList<>();
  1004. if (familyPatents == null || familyPatents.size() == 0) {
  1005. return familyPatentsNew;
  1006. }
  1007. for (int i = 0; i < familyPatents.size(); i++) {
  1008. FamilyPatent familyPatent = familyPatents.get(i);
  1009. FamilyPatent familyPatentNew = familyPatentsNew.stream().filter(item -> (item.getPublicNo() != null && (item.getPublicNo().equals(familyPatent.getPublicNo()) || item.getPublicNo().equals(familyPatent.getAppNo()) || item.getPublicNo().equals(familyPatent.getGrantNo())))
  1010. || (item.getGrantNo() != null && (item.getGrantNo().equals(familyPatent.getGrantNo()) || item.getGrantNo().equals(familyPatent.getPublicNo()) || item.getGrantNo().equals(familyPatent.getAppNo())) || (item.getAppNo() != null && (item.getAppNo().equals(familyPatent.getGrantNo()) || item.getAppNo().equals(familyPatent.getPublicNo()) || item.getAppNo().equals(familyPatent.getAppNo()))))).findFirst().orElse(null);
  1011. if (familyPatentNew == null) {
  1012. familyPatentsNew.add(familyPatent);
  1013. } else {
  1014. if (familyPatentNew.getAppNo() == null && familyPatent.getGrantNo() == null) {
  1015. familyPatentsNew.remove(familyPatentNew);
  1016. familyPatentsNew.add(familyPatent);
  1017. }
  1018. }
  1019. }
  1020. return familyPatentsNew;
  1021. }
  1022. private List<FamilyPatent> getDistFamilyPatentsWithNos(List<FamilyPatent> familyPatents, List<String> nos) {
  1023. List<FamilyPatent> familyPatentsNew = new ArrayList<>();
  1024. if (nos.size() == 0 || nos == null) {
  1025. return familyPatents;
  1026. }
  1027. if (familyPatents == null) {
  1028. familyPatents = new ArrayList<>();
  1029. }
  1030. for (int i = 0; i < nos.size(); i++) {
  1031. FamilyPatent familyPatentNew = familyPatents.stream().filter(item -> (item.getPublicNo() != null && item.getPublicNo().equals(no))
  1032. || (item.getGrantNo() != null && item.getGrantNo().equals(no) || (item.getAppNo() != null && item.getAppNo().equals(no)))).findFirst().orElse(null);
  1033. if (familyPatentNew == null) {
  1034. familyPatentNew = new FamilyPatent();
  1035. familyPatentNew.setPublicNo(nos.get(i));
  1036. familyPatents.add(familyPatentNew);
  1037. }
  1038. }
  1039. return familyPatents;
  1040. }
  1041. /**
  1042. * 添加同族
  1043. *
  1044. * @param message
  1045. * @return
  1046. * @throws Exception
  1047. */
  1048. public String addPatentFamily(PatentFamilyMessage message) throws Exception {
  1049. IndexResponse indexResponse = client.index(i -> i
  1050. .index("patent_family")
  1051. .document(message)
  1052. );
  1053. return indexResponse.id();
  1054. }
  1055. /**
  1056. * 更新同族
  1057. *
  1058. * @param message
  1059. * @param id
  1060. * @return
  1061. */
  1062. public Integer updatePatentFamily(PatentFamilyMessage message, String id) {
  1063. UpdateRequest<PatentFamilyMessage, PatentFamilyMessage> req;
  1064. req = UpdateRequest.of(
  1065. b -> b.index("patent_family")
  1066. .id(id)
  1067. .doc(message)
  1068. );
  1069. try {
  1070. client.update(req, PatentFamilyMessage.class);
  1071. return 1;
  1072. } catch (IOException e) {
  1073. return -1;
  1074. }
  1075. }
  1076. /**
  1077. * 更新专利事务
  1078. *
  1079. * @param
  1080. * @param id
  1081. * @return
  1082. */
  1083. public Integer updateLegalEvent(LegalEvent legalEvent, String id) {
  1084. UpdateRequest<LegalEvent, LegalEvent> req;
  1085. req = UpdateRequest.of(
  1086. b -> b.index("legal_event")
  1087. .id(id)
  1088. .doc(legalEvent).refresh(Refresh.True).waitForActiveShards(WaitForActiveShards.of(i -> i.count(1)))
  1089. );
  1090. try {
  1091. client.update(req, LegalEvent.class);
  1092. return 1;
  1093. } catch (IOException e) {
  1094. return -1;
  1095. }
  1096. }
  1097. /**
  1098. * 更新专利事务
  1099. *
  1100. * @param
  1101. * @param id
  1102. * @return
  1103. */
  1104. public Integer updateQuotePatent(PatentQuoteMessage patentQuoteMessage, String id) {
  1105. UpdateRequest<PatentQuoteMessage, PatentQuoteMessage> req;
  1106. req = UpdateRequest.of(
  1107. b -> b.index("quote_patent")
  1108. .id(id)
  1109. .doc(patentQuoteMessage)
  1110. );
  1111. try {
  1112. client.update(req, PatentQuoteMessage.class);
  1113. return 1;
  1114. } catch (IOException e) {
  1115. return -1;
  1116. }
  1117. }
  1118. /**
  1119. * 添加法律事务
  1120. *
  1121. * @param
  1122. * @return
  1123. * @throws Exception
  1124. */
  1125. public String addQuotePatent(PatentQuoteMessage patentQuoteMessage) throws Exception {
  1126. IndexResponse indexResponse = client.index(i -> i
  1127. .index("quote_patent")
  1128. .document(patentQuoteMessage)
  1129. );
  1130. return indexResponse.id();
  1131. }
  1132. public PatentQuoteMessage addEsQuotePatent(Patent patent, List<String> nos) throws Exception {
  1133. if (patent == null) {
  1134. return null;
  1135. }
  1136. QuotePatent quotePatent = this.reQuotePatent(patent);
  1137. PatentQuoteMessage patentQuoteMessage = null;
  1138. //根据申请号和
  1139. String id = "";
  1140. //根据专利号查询是否有引用信息
  1141. SearchRequest.Builder builder = new SearchRequest.Builder();
  1142. //设置查询索引
  1143. builder.index("quote_patent");
  1144. //申请号
  1145. Query q1 = QueryBuilders.term(t -> t.field("patent.app_no").value(patent.getAppNo()));
  1146. Query bool = QueryBuilders.bool(i -> i.must(q1));
  1147. builder.query(bool);
  1148. SearchResponse<PatentQuoteMessage> response = null;
  1149. try {
  1150. response = client.search(builder.build(), PatentQuoteMessage.class);
  1151. } catch (Exception e) {
  1152. throw new XiaoShiException(ExceptionEnum.BUSINESS_ERROR, "ES检索失败");
  1153. }
  1154. long total = response.hits().total().value();
  1155. if (total > 0) {
  1156. patentQuoteMessage = response.hits().hits().get(0).source();
  1157. id = response.hits().hits().get(0).id();
  1158. }
  1159. List<QuotePatent> quotePatents = new ArrayList<>();
  1160. if (patentQuoteMessage != null && patentQuoteMessage.getQuotedPatents() != null) {
  1161. quotePatents = patentQuoteMessage.getQuotedPatents();
  1162. }
  1163. if (nos != null && nos.size() > 0) {
  1164. List<Patent> patents = esPatentService.getPatentsByNo(nos, true, null, null);
  1165. for (String patentNo : nos) {
  1166. QuotePatent quotePatent1 = null;
  1167. if (quotePatents.size() > 0) {
  1168. quotePatent1 = quotePatents.stream()
  1169. .filter(item -> patentNo.equals(item.getAppNo()) || patentNo.equals(item.getPublicNo()) || patentNo.equals(item.getGrantNo())).findFirst().orElse(null);
  1170. }
  1171. Patent patent1 = patents.stream().filter(item -> patentNo.equals(item.getAppNo())).findFirst().orElse(null);
  1172. quotePatent1 = this.reQuotePatent(patent1, patentNo);
  1173. quotePatents.add(quotePatent1);
  1174. }
  1175. }
  1176. if (patentQuoteMessage == null) {
  1177. if (quotePatents.size() != 0) {
  1178. patentQuoteMessage = new PatentQuoteMessage();
  1179. patentQuoteMessage.setPatent(quotePatent);
  1180. patentQuoteMessage.setQuotedPatents(quotePatents);
  1181. this.addQuotePatent(patentQuoteMessage);
  1182. }
  1183. } else {
  1184. patentQuoteMessage.getQuotedPatents().addAll(quotePatents);
  1185. this.updateQuotePatent(patentQuoteMessage, id);
  1186. }
  1187. return patentQuoteMessage;
  1188. }
  1189. private QuotePatent reQuotePatent(Patent patent) {
  1190. QuotePatent quotePatent = new QuotePatent();
  1191. //装载申请人
  1192. quotePatent.setApplicant(patent.getApplicant());
  1193. //装载权利人
  1194. quotePatent.setRightHolder(patent.getRightHolder());
  1195. quotePatent.setAppNo(patent.getAppNo());
  1196. quotePatent.setGrantNo(patent.getGrantNo());
  1197. quotePatent.setPublicNo(patent.getPublicNo());
  1198. return quotePatent;
  1199. }
  1200. private QuotePatent reQuotePatent(Patent patent, String no) {
  1201. QuotePatent quotePatent = new QuotePatent();
  1202. if (patent == null) {
  1203. quotePatent.setPublicNo(no);
  1204. return quotePatent;
  1205. }
  1206. //装载申请人
  1207. quotePatent.setApplicant(patent.getApplicant());
  1208. quotePatent.setRightHolder(patent.getRightHolder());
  1209. quotePatent.setAppNo(patent.getAppNo());
  1210. quotePatent.setGrantNo(patent.getGrantNo());
  1211. quotePatent.setPublicNo(patent.getPublicNo());
  1212. return quotePatent;
  1213. }
  1214. /**
  1215. * 查询权利要求
  1216. *
  1217. * @param patentNo
  1218. * @return
  1219. */
  1220. public SelectClaimDTO selectClaim(String patentNo) throws IOException {
  1221. SearchRequest.Builder builder = new SearchRequest.Builder();
  1222. //设置查询索引
  1223. builder.index("patent");
  1224. //原始数据
  1225. Query q = QueryBuilders.term(t -> t.field("claim.if_origin").value(true));
  1226. //申请号
  1227. Query q1 = QueryBuilders.term(t -> t.field("app_no.keyword").value(patentNo));
  1228. //公开号
  1229. Query q2 = QueryBuilders.term(t -> t.field("public_no.keyword").value(patentNo));
  1230. //授权号
  1231. Query q3 = QueryBuilders.term(t -> t.field("grant_no.keyword").value(patentNo));
  1232. Query query = QueryBuilders.bool(i -> i.should(q1, q2, q3));
  1233. Query bool = QueryBuilders.bool(i -> i.must(q, query));
  1234. builder.query(bool);
  1235. SearchResponse<Patent> response = null;
  1236. try {
  1237. response = client.search(builder.build(), Patent.class);
  1238. } catch (Exception e) {
  1239. throw new XiaoShiException(ExceptionEnum.BUSINESS_ERROR, "ES检索失败");
  1240. }
  1241. SelectClaimDTO dto = new SelectClaimDTO();
  1242. List<Hit<Patent>> hits = response.hits().hits();
  1243. for (Hit<Patent> hit : hits) {
  1244. Patent esMess = hit.source();
  1245. if (esMess != null) {
  1246. dto.setClaimContent(esMess.getClaim());
  1247. dto.setClaim(esMess.getClaim().get(0).getTextContent());
  1248. }
  1249. }
  1250. return dto;
  1251. }
  1252. /**
  1253. * 装载摘要附图
  1254. *
  1255. * @param patentColumnDTOS
  1256. * @return
  1257. */
  1258. public List<PatentColumnDTO> loadCoulumnDTO(List<PatentColumnDTO> patentColumnDTOS) {
  1259. patentColumnDTOS.forEach(item -> {
  1260. item.setPictureGuid(FormatUtil.getPictureFormat(item.getAppNo()));
  1261. });
  1262. return patentColumnDTOS;
  1263. }
  1264. /**
  1265. * 根据专利号查询出其他专利号
  1266. *
  1267. * @param patentNos
  1268. * @return
  1269. * @throws IOException
  1270. */
  1271. public SelectClaimDTO selectPatentNo(List<String> patentNos) throws IOException {
  1272. SearchRequest.Builder builder = new SearchRequest.Builder();
  1273. //设置查询索引
  1274. builder.index("patent");
  1275. List<Query> queryList = new ArrayList<>();
  1276. for (String patentNo : patentNos) {
  1277. Query q1 = QueryBuilders.term(t -> t.field("patent_no.keyword").value(patentNo));
  1278. queryList.add(q1);
  1279. }
  1280. //申请号
  1281. Query query = QueryBuilders.bool(i -> i.mustNot(queryList));
  1282. builder.query(query);
  1283. SearchResponse<Patent> response = null;
  1284. try {
  1285. response = client.search(builder.build(), Patent.class);
  1286. } catch (Exception e) {
  1287. throw new XiaoShiException(ExceptionEnum.BUSINESS_ERROR, "ES检索失败");
  1288. }
  1289. SelectClaimDTO dto = new SelectClaimDTO();
  1290. List<Hit<Patent>> hits = response.hits().hits();
  1291. long value = response.hits().total().value();
  1292. if (value > 1) {
  1293. System.out.println("====================" + value);
  1294. }
  1295. return dto;
  1296. }
  1297. public PatentFamilyMessageWithId queryPatentFamily(Patent patent, String type) throws Exception {
  1298. //查询该专利是否已有同族
  1299. SearchRequest.Builder builderPatent = new SearchRequest.Builder();
  1300. //设置查询索引
  1301. builderPatent.index("patent_family");
  1302. //申请号
  1303. List<Query> queries = new ArrayList<>();
  1304. if (patent.getAppNo() != null && !patent.getAppNo().equals("")) {
  1305. Query q1 = QueryBuilders.term(t -> t.field("patent.app_no").value(patent.getAppNo()));
  1306. //公开号
  1307. Query q2 = QueryBuilders.term(t -> t.field("patent.public_no").value(patent.getAppNo()));
  1308. //授权号
  1309. Query q3 = QueryBuilders.term(t -> t.field("patent.grant_no").value(patent.getAppNo()));
  1310. //类型
  1311. queries.add(q1);
  1312. queries.add(q2);
  1313. queries.add(q3);
  1314. }
  1315. if (patent.getPublicNo() != null && !patent.getPublicNo().equals("")) {
  1316. Query q1 = QueryBuilders.term(t -> t.field("patent.app_no").value(patent.getPublicNo()));
  1317. //公开号
  1318. Query q2 = QueryBuilders.term(t -> t.field("patent.public_no").value(patent.getPublicNo()));
  1319. //授权号
  1320. Query q3 = QueryBuilders.term(t -> t.field("patent.grant_no").value(patent.getPublicNo()));
  1321. queries.add(q1);
  1322. queries.add(q2);
  1323. queries.add(q3);
  1324. }
  1325. if (patent.getGrantNo() != null && !patent.getGrantNo().equals("")) {
  1326. Query q1 = QueryBuilders.term(t -> t.field("patent.app_no").value(patent.getGrantNo()));
  1327. //公开号
  1328. Query q2 = QueryBuilders.term(t -> t.field("patent.public_no").value(patent.getGrantNo()));
  1329. //授权号
  1330. Query q3 = QueryBuilders.term(t -> t.field("patent.grant_no").value(patent.getGrantNo()));
  1331. queries.add(q1);
  1332. queries.add(q2);
  1333. queries.add(q3);
  1334. }
  1335. Query q4 = QueryBuilders.term(t -> t.field("family_type").value(type));
  1336. Query p = QueryBuilders.bool(i -> i.should(queries));
  1337. Query bool = QueryBuilders.bool(i -> i.must(q4, p));
  1338. builderPatent.query(bool);
  1339. SearchResponse<PatentFamilyMessage> response = client.search(builderPatent.build(), PatentFamilyMessage.class);
  1340. long total = response.hits().total().value();
  1341. if (total > 0) {
  1342. List<FamilyPatent> list = new ArrayList<>();
  1343. List<Hit<PatentFamilyMessage>> hits = response.hits().hits();
  1344. Hit<PatentFamilyMessage> hit = hits.get(0);
  1345. String id = hit.id();
  1346. PatentFamilyMessageWithId patentFamilyMessageWithId = new PatentFamilyMessageWithId();
  1347. patentFamilyMessageWithId.setId(id);
  1348. patentFamilyMessageWithId.setPatentFamilyMessage(hit.source());
  1349. return patentFamilyMessageWithId;
  1350. }
  1351. return null;
  1352. }
  1353. //根据父id获取相应专利的专利号
  1354. public List<String> getPatentNos(List<String> ids) throws IOException {
  1355. SearchRequest.Builder builder = new SearchRequest.Builder();
  1356. //设置查询索引
  1357. builder.index("patent");
  1358. Query q = QueryBuilders.ids(i -> i.values(ids));
  1359. builder.size(9999);
  1360. builder.query(q);
  1361. SearchResponse<Patent> response = null;
  1362. try {
  1363. response = client.search(builder.build(), Patent.class);
  1364. } catch (Exception e) {
  1365. throw new XiaoShiException(ExceptionEnum.BUSINESS_ERROR, "ES检索失败");
  1366. }
  1367. List<Hit<Patent>> hits = response.hits().hits();
  1368. List<String> list = new ArrayList<>();
  1369. for (Hit<Patent> hit : hits) {
  1370. Patent patent = hit.source();
  1371. list.add(patent.getPatentNo());
  1372. }
  1373. return list;
  1374. }
  1375. @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Throwable.class)
  1376. public Boolean deletePatents(EsCustomFieldBatchVO vo) throws Exception {
  1377. Integer projectId = vo.getProjectId();
  1378. Integer startNum = vo.getStartNumber();
  1379. Integer endNum = vo.getEndNumber();
  1380. List<String> isAdd = vo.getIsAdd();
  1381. List<String> isDel = vo.getIsDelete();
  1382. if (!CollectionUtils.isEmpty(isAdd) && startNum <= 1 && endNum < 1) {
  1383. isAdd.removeAll(isDel);
  1384. for (String patentNo : isAdd) {
  1385. Integer number = this.getPatent(patentNo, projectId);
  1386. if (number < 1) {
  1387. throw new XiaoShiException(ExceptionEnum.BUSINESS_ERROR, "删除失败");
  1388. }
  1389. }
  1390. } else if (startNum >= 1 && endNum > 0) {
  1391. List<String> patentNos = esCustomFieldService.getPatentNos(vo);
  1392. for (String patentNo : patentNos) {
  1393. Integer number = this.getPatent(patentNo, projectId);
  1394. if (number < 1) {
  1395. throw new XiaoShiException(ExceptionEnum.BUSINESS_ERROR, "删除失败");
  1396. }
  1397. }
  1398. }
  1399. return true;
  1400. }
  1401. public Integer getPatent(String patentNo, Integer projectId) throws IOException {
  1402. SearchRequest.Builder builder = new SearchRequest.Builder();
  1403. //设置查询索引
  1404. builder.index("patent");
  1405. Query query = QueryBuilders.term(i -> i.field("project_id").value(projectId));
  1406. Query q = QueryBuilders.term(i -> i.field("patent_no.keyword").value(patentNo));
  1407. Query query1 = QueryBuilders.hasParent(i -> i.parentType("patent").query(q));
  1408. Query bool = QueryBuilders.bool(i -> i.must(query, query1));
  1409. builder.query(bool);
  1410. //解除最大条数限制
  1411. builder.trackTotalHits(i -> i.enabled(true));
  1412. SearchResponse<Patent> response = null;
  1413. try {
  1414. response = client.search(builder.build(), Patent.class);
  1415. } catch (Exception e) {
  1416. throw new XiaoShiException(ExceptionEnum.BUSINESS_ERROR, "ES检索失败");
  1417. }
  1418. List<Hit<Patent>> hits = response.hits().hits();
  1419. List<String> list = new ArrayList<>();
  1420. for (Hit<Patent> hit : hits) {
  1421. String id = hit.id();
  1422. list.add(id);
  1423. }
  1424. return this.deleteByIds(list);
  1425. }
  1426. public Integer deleteByIds(List<String> ids) {
  1427. Query query = QueryBuilders.ids(n -> n.values(ids));
  1428. DeleteByQueryRequest request = DeleteByQueryRequest.of(i -> i.index("patent").refresh(true).query(query)
  1429. .refresh(true));
  1430. try {
  1431. client.deleteByQuery(request);
  1432. return 1;
  1433. } catch (IOException e) {
  1434. throw new XiaoShiException(ExceptionEnum.BUSINESS_ERROR, "删除失败");
  1435. }
  1436. }
  1437. /**
  1438. * 装载条件
  1439. *
  1440. * @param queryRequest
  1441. * @return
  1442. */
  1443. public EsPatentCommonVO tranPatentRequestToComVO(QueryRequest queryRequest) {
  1444. StringRequest stringRequest = (StringRequest) queryRequest;
  1445. EsPatentCommonVO commonVO = new EsPatentCommonVO();
  1446. try {
  1447. Integer taskId = stringRequest.getTaskId();
  1448. Integer projectId = stringRequest.getProjectId();
  1449. Integer productId = stringRequest.getProductId();
  1450. Long current = stringRequest.getCurrent();
  1451. Long size = stringRequest.getSize();
  1452. String productFrom = stringRequest.getFrom();
  1453. String searchCondition = stringRequest.getSearchQuery();
  1454. String groupField = stringRequest.getGroupField();
  1455. //判断同族分组
  1456. String str = "";
  1457. if (StringUtils.isNotEmpty(groupField)) {
  1458. switch (groupField) {
  1459. case "simpleFamilyId":
  1460. str = "simple_family_id";
  1461. break;
  1462. case "inpadocFamilyId":
  1463. str = "inpadoc_family_id";
  1464. break;
  1465. case "patsnapFamilyId":
  1466. str = "patsnap_family_id";
  1467. break;
  1468. }
  1469. }
  1470. String esField = str;
  1471. commonVO.setTaskId(taskId);
  1472. commonVO.setProjectId(projectId);
  1473. commonVO.setProductId(productId);
  1474. if (StringUtils.isNotEmpty(searchCondition) && searchCondition.contains("罒")) {
  1475. String replace = searchCondition.replace("罒", " OR ");
  1476. commonVO.setSearchCondition(replace);
  1477. } else {
  1478. commonVO.setSearchCondition(searchCondition);
  1479. }
  1480. commonVO.setProductFrom(productFrom);
  1481. commonVO.setCurrent(current);
  1482. commonVO.setSize(size);
  1483. commonVO.setEsField(esField);
  1484. commonVO.setCustomFields(stringRequest.getCustomFields());
  1485. commonVO.setOrderDTOList(stringRequest.getOrderDTOList());
  1486. } catch (Exception e) {
  1487. }
  1488. return commonVO;
  1489. }
  1490. public void judgeQueryPatentRoot(QueryRequest queryRequest) {
  1491. Integer projectId = queryRequest.getProjectId();
  1492. Integer productId = queryRequest.getProductId();
  1493. String productFrom = queryRequest.getFrom();
  1494. if (projectId != null) {
  1495. List<Integer> ids = assoProjectPersonService.getRootProjectIds();
  1496. if (ids != null && (ids.size() == 0 || !ids.contains(projectId))) {
  1497. throw new XiaoShiException(ExceptionEnum.PERMISSION_ERROR, "无法查询此专题库专利");
  1498. }
  1499. }
  1500. }
  1501. public Query getMeanSearch(Query query, String mean) {
  1502. if (mean != null && !mean.trim().equals("")) {
  1503. GetVectorVO getVectorVO = new GetVectorVO();
  1504. getVectorVO.setText(mean);
  1505. List<Double> doubles = pythonApiService.getStrVector(getVectorVO);
  1506. String source = "if (!doc['abstract_vector'].isEmpty()) {return cosineSimilarity(params.queryVector,'abstract_vector');} else {return 0.1; }";
  1507. InlineScript inlineScript = InlineScript.of(i -> i.source(source).lang("painless").params("queryVector", JsonData.of(doubles)));
  1508. Script script = Script.of(i -> i.inline(inlineScript));
  1509. Query newQuery = QueryBuilders.scriptScore(i -> i.script(script)
  1510. .query(query));
  1511. String a = newQuery.toString();
  1512. System.out.println(a);
  1513. return newQuery;
  1514. }
  1515. return query;
  1516. }
  1517. public long getMergePersonCount(Integer projectId, String patentId) {
  1518. SearchRequest.Builder builder = new SearchRequest.Builder();
  1519. //设置查询索引
  1520. builder.index("patent");
  1521. //合并申请人
  1522. Query q = QueryBuilders.term(i -> i.field("merge_applicant.project_id").value(projectId));
  1523. Query query1 = QueryBuilders.nested(i -> i.path("merge_applicant").query(q));
  1524. //合并权利人
  1525. Query q1 = QueryBuilders.term(i -> i.field("merge_right_holder.project_id").value(projectId));
  1526. Query query2 = QueryBuilders.nested(i -> i.path("merge_right_holder").query(q1));
  1527. //合并发明人
  1528. Query q2 = QueryBuilders.term(i -> i.field("merge_inventor.project_id").value(projectId));
  1529. Query query3 = QueryBuilders.nested(i -> i.path("merge_inventor").query(q2));
  1530. Query query = QueryBuilders.bool(i -> i.should(query1, query2, query3));
  1531. Query idQ = QueryBuilders.ids(i -> i.values(Collections.singletonList(patentId)));
  1532. Query parentQ = QueryBuilders.hasParent(i -> i.parentType("patent").query(idQ));
  1533. Query bool = QueryBuilders.bool(i -> i.must(parentQ, query));
  1534. builder.query(bool);
  1535. builder.size(10);
  1536. SearchResponse<Patent> response = null;
  1537. try {
  1538. response = client.search(builder.build(), Patent.class);
  1539. } catch (Exception e) {
  1540. throw new XiaoShiException(ExceptionEnum.BUSINESS_ERROR, "ES检索失败");
  1541. }
  1542. return response.hits().total().value();
  1543. }
  1544. }