EsService.java 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908
  1. package cn.cslg.pas.service.business.es;
  2. import cn.cslg.pas.common.core.base.IfConstant;
  3. import cn.cslg.pas.common.dto.PatentColumnDTO;
  4. import cn.cslg.pas.common.dto.PatentDTO;
  5. //import cn.cslg.pas.common.dto.business.EsPatentFamilyDTO;
  6. import cn.cslg.pas.common.dto.PatentStarListDTO;
  7. import cn.cslg.pas.common.dto.business.ContentDetailDTO;
  8. import cn.cslg.pas.common.dto.business.EsPatentFamilyDTO;
  9. import cn.cslg.pas.common.dto.business.SelectClaimDTO;
  10. import cn.cslg.pas.common.dto.es.EsCustomFieldValueDTO;
  11. import cn.cslg.pas.common.model.request.MapRequest;
  12. import cn.cslg.pas.common.model.request.OrderDTO;
  13. import cn.cslg.pas.common.model.request.QueryRequest;
  14. import cn.cslg.pas.common.model.request.StringRequest;
  15. import cn.cslg.pas.common.utils.FormatUtil;
  16. import cn.cslg.pas.common.utils.parseQueryToTree.expressManager;
  17. import cn.cslg.pas.common.utils.parseQueryToTree.operateNode;
  18. import cn.cslg.pas.common.utils.parseQueryToTree.treeNode;
  19. import cn.cslg.pas.common.vo.*;
  20. import cn.cslg.pas.domain.es.*;
  21. import cn.cslg.pas.service.business.CommonService;
  22. import cn.cslg.pas.service.common.PatentStarApiService;
  23. import cn.cslg.pas.service.query.FormatQueryService;
  24. import co.elastic.clients.elasticsearch.ElasticsearchClient;
  25. import co.elastic.clients.elasticsearch._types.SortOrder;
  26. import co.elastic.clients.elasticsearch._types.query_dsl.*;
  27. import co.elastic.clients.elasticsearch.core.*;
  28. import co.elastic.clients.elasticsearch.core.search.Hit;
  29. import com.alibaba.fastjson.JSON;
  30. import com.alibaba.fastjson2.JSONObject;
  31. import lombok.RequiredArgsConstructor;
  32. import org.apache.commons.lang3.StringUtils;
  33. import org.springframework.beans.BeanUtils;
  34. import org.springframework.beans.factory.annotation.Autowired;
  35. import org.springframework.context.annotation.Lazy;
  36. import org.springframework.stereotype.Service;
  37. import org.springframework.util.CollectionUtils;
  38. import java.io.IOException;
  39. import java.util.*;
  40. @Service
  41. @RequiredArgsConstructor(onConstructor_ = {@Lazy})
  42. public class EsService {
  43. public final List<String> dateList = Arrays.asList("AD", "PD", "GD", "EXD", "PAD", "PED", "PPD", "EPD");
  44. private final ElasticsearchClient client;
  45. @Autowired
  46. private FormatQueryService formatQueryService;
  47. @Autowired
  48. private PatentStarApiService patentStarApiService;
  49. /**
  50. * @param patent
  51. * @throws Exception
  52. */
  53. public String addPatent(Patent patent) throws Exception {
  54. IndexResponse indexResponse = client.index(i -> i
  55. .index("patent")
  56. //传入user对象
  57. .document(patent)
  58. );
  59. return indexResponse.id();
  60. }
  61. /**
  62. * @param patent
  63. * @throws Exception
  64. */
  65. public String addChildPatent(Patent patent, String id) throws Exception {
  66. IndexResponse indexResponse = client.index(i -> i
  67. .index("patent")
  68. .routing(id)
  69. //传入user对象
  70. .document(patent)
  71. );
  72. return indexResponse.id();
  73. }
  74. /**
  75. * 根据专利号获取专利id
  76. *
  77. * @param patentNo
  78. * @return
  79. * @throws Exception
  80. */
  81. public PatentWithIdVO getIdByPatentNo(String patentNo) throws Exception {
  82. SearchRequest.Builder builder = new SearchRequest.Builder();
  83. //设置查询索引
  84. builder.index("patent");
  85. PatentWithIdVO patentWithIdVO = null;
  86. String id = null;
  87. Query q1 = QueryBuilders.term(t -> t.field("app_no.keyword").value(patentNo));
  88. //公开号
  89. Query q2 = QueryBuilders.term(t -> t.field("public_no.keyword").value(patentNo));
  90. //授权号
  91. Query q3 = QueryBuilders.term(t -> t.field("grant_no.keyword").value(patentNo));
  92. Query query = QueryBuilders.bool(i -> i.should(q1, q2, q3));
  93. builder.query(query);
  94. SearchResponse<Patent> response = client.search(builder.build(), Patent.class);
  95. List<Hit<Patent>> hits = response.hits().hits();
  96. if (hits != null && hits.size() > 0) {
  97. id = hits.get(0).id();
  98. Patent patent = hits.get(0).source();
  99. patentWithIdVO = new PatentWithIdVO();
  100. patentWithIdVO.setPatent(patent);
  101. patentWithIdVO.setId(id);
  102. }
  103. return patentWithIdVO;
  104. }
  105. /**
  106. * Es检索
  107. *
  108. * @param queryRequest 检索条件
  109. * @return
  110. */
  111. public PatentDTO esSearch(QueryRequest queryRequest) throws Exception {
  112. PatentDTO dto = new PatentDTO();
  113. Integer taskId = queryRequest.getTaskId();
  114. String searchCondition = "";
  115. Integer projectId = queryRequest.getProjectId();
  116. Long current = queryRequest.getCurrent();
  117. Long size = queryRequest.getSize();
  118. //判断表达式
  119. if (queryRequest instanceof StringRequest) {
  120. searchCondition = ((StringRequest) queryRequest).getSearchQuery();
  121. } else if (queryRequest instanceof MapRequest) {
  122. Map<String, Object> map = ((MapRequest) queryRequest).getSearchQuery();
  123. StringBuilder stringBuilder = new StringBuilder();
  124. for (String key : map.keySet()) {
  125. Object value = map.get(key);
  126. if (!"".contentEquals(stringBuilder)) {
  127. stringBuilder = stringBuilder.append(" AND ").append(key).append("=").append(value);
  128. } else {
  129. stringBuilder = stringBuilder.append(key).append("=").append(value);
  130. }
  131. }
  132. searchCondition = stringBuilder.toString();
  133. }
  134. List<EsCustomFieldValueDTO> customFields = queryRequest.getCustomFields();
  135. if (!CollectionUtils.isEmpty(customFields)) {
  136. searchCondition = this.parseCustomField(customFields);
  137. }
  138. if (taskId != null) {
  139. if (searchCondition != null && !"".equals(searchCondition.trim())) {
  140. searchCondition = "taskId = " + taskId + " AND " + searchCondition;
  141. } else {
  142. searchCondition = "taskId = " + taskId;
  143. }
  144. } else {
  145. if (projectId != null) {
  146. if (searchCondition != null && !"".equals(searchCondition.trim())) {
  147. searchCondition = "projectId = " + projectId + " AND " + searchCondition;
  148. } else {
  149. searchCondition = "projectId = " + projectId;
  150. }
  151. }
  152. }
  153. SearchRequest.Builder builder = new SearchRequest.Builder();
  154. //设置查询索引
  155. builder.index("patent");
  156. //1. 解析检索条件
  157. treeNode tree = expressManager.getInstance().Parse(searchCondition, false);
  158. //格式化检索式
  159. //3. 从es中检索数据
  160. Query q = formatQueryService.EsQueryToQuery((operateNode) tree, "patent");
  161. //4. 返回数据
  162. builder.query(q);
  163. //排序
  164. List<OrderDTO> dtoList = queryRequest.getOrderDTOList();
  165. if (!CollectionUtils.isEmpty(dtoList)) {
  166. String json = CommonService.readJsonFile("patent.json");
  167. List<EsConfigVO> esConfigVOS = JSON.parseArray(json, EsConfigVO.class);
  168. for (OrderDTO orderDTO : dtoList) {
  169. EsConfigVO configVO = esConfigVOS.stream().filter(item -> item.getField().equals(orderDTO.getOrderBy())).findFirst().orElse(null);
  170. if (configVO != null) {
  171. if (orderDTO.getOrderType().equals(IfConstant.NO)) {
  172. builder.sort(sortOptionsBuilder -> sortOptionsBuilder
  173. .field(fieldSortBuilder -> fieldSortBuilder
  174. .field(configVO.getEsField()).order(SortOrder.Asc)));
  175. } else {
  176. builder.sort(sortOptionsBuilder -> sortOptionsBuilder
  177. .field(fieldSortBuilder -> fieldSortBuilder
  178. .field(configVO.getEsField()).order(SortOrder.Desc)));
  179. }
  180. }
  181. }
  182. } else {
  183. builder.sort(sortOptionsBuilder -> sortOptionsBuilder
  184. .field(fieldSortBuilder -> fieldSortBuilder
  185. .field("patent_no.keyword").order(SortOrder.Desc)));
  186. }
  187. //分页
  188. if (current > 0 && size > 0) {
  189. builder.from((current.intValue() - 1) * size.intValue()).size(size.intValue());
  190. }
  191. SearchResponse<Patent> response = client.search(builder.build(), Patent.class);
  192. List<PatentColumnDTO> list = new ArrayList<>();
  193. List<Hit<Patent>> hits = response.hits().hits();
  194. long total = response.hits().total().value();
  195. for (Hit<Patent> hit : hits) {
  196. Patent esMess = hit.source();
  197. PatentColumnDTO columnDTO = new PatentColumnDTO();
  198. // columnDTO.setPatentNo(esMess.getPatentNo());
  199. BeanUtils.copyProperties(esMess, columnDTO);
  200. list.add(columnDTO);
  201. }
  202. this.loadCoulumnDTO(list);
  203. dto.setTotal(total);
  204. dto.setPatents(list);
  205. dto.setPageNum(current);
  206. dto.setPageSize(size);
  207. return dto;
  208. }
  209. /**
  210. * 解析自定义栏位和值
  211. *
  212. * @param customFields
  213. * @return
  214. */
  215. public String parseCustomField(List<EsCustomFieldValueDTO> customFields) {
  216. int m = 1;
  217. int n = 0;
  218. StringBuilder builder = new StringBuilder();
  219. long start = System.currentTimeMillis();
  220. if (customFields.size() > m) {
  221. for (int i = 0; i < customFields.size(); i++) {
  222. EsCustomFieldValueDTO customField = customFields.get(i);
  223. if (i != n) {
  224. builder.append(" ").append("and").append(" ").append("(");
  225. this.appendStr(customField, builder, m, customField.getIfHaveChild());
  226. } else {
  227. builder.append("(");
  228. this.appendStr(customField, builder, m, customField.getIfHaveChild());
  229. }
  230. }
  231. } else {
  232. for (int i = 0; i < customFields.size(); i++) {
  233. EsCustomFieldValueDTO customField = customFields.get(i);
  234. if (i != n) {
  235. builder.append(" ").append("and").append(" ");
  236. this.appendStr(customField, builder, m, customField.getIfHaveChild());
  237. } else {
  238. builder.append("(");
  239. this.appendStr(customField, builder, m, customField.getIfHaveChild());
  240. }
  241. }
  242. }
  243. long end = System.currentTimeMillis();
  244. System.out.println("耗时" + (end - start));
  245. return builder.toString();
  246. }
  247. public void appendStr(EsCustomFieldValueDTO customField, StringBuilder builder, int m, boolean ifHaveChild) {
  248. builder.append("field").append("=").append(customField.getFieldId());
  249. List<String> values = customField.getFieldValue();
  250. if (!CollectionUtils.isEmpty(values)) {
  251. builder.append(" ").append("and").append(" ");
  252. if (ifHaveChild) {
  253. builder.append("statsValue").append("=");
  254. } else {
  255. builder.append("fieldValue").append("=");
  256. }
  257. if (values.size() > m) {
  258. builder.append("(");
  259. for (int j = 0; j < values.size(); j++) {
  260. String s = values.get(j);
  261. if (j != values.size() - m) {
  262. builder.append(s).append(" ").append("or").append(" ");
  263. } else {
  264. builder.append(s).append(")").append(")");
  265. }
  266. }
  267. } else {
  268. for (String value : values) {
  269. builder.append(value).append(")");
  270. }
  271. }
  272. } else {
  273. builder.append(")");
  274. }
  275. }
  276. /**
  277. * @param key
  278. * @param page
  279. * @param limit
  280. * @return
  281. */
  282. public List<Patent> search(String key, Integer page, Integer limit) throws IOException {
  283. SearchRequest.Builder builder = new SearchRequest.Builder();
  284. //设置查询索引
  285. builder.index("patent");
  286. //组装查询条件
  287. BoolQuery.Builder boolQuery = new BoolQuery.Builder();
  288. boolQuery.should(q -> q.match(v -> v
  289. .query(key)
  290. //字段名
  291. .field("patent_no")
  292. ));
  293. //多字段匹配
  294. // boolQuery.should(q -> q.matchPhrasePrefix(m -> m.query(key).field("content")));
  295. builder.query(q -> q.bool(boolQuery.build()));
  296. //分页
  297. if (page != null && limit != null) {
  298. builder.from(page).size(limit);
  299. }
  300. //排序
  301. // builder.sort(sortOptionsBuilder -> sortOptionsBuilder
  302. // .field(fieldSortBuilder -> fieldSortBuilder
  303. // .field("createTime").order(SortOrder.Desc)));
  304. SearchResponse<Patent> response = client.search(builder.build(), Patent.class);
  305. List<Patent> list = new ArrayList<>();
  306. List<Hit<Patent>> hits = response.hits().hits();
  307. for (Hit<Patent> hit : hits) {
  308. Patent esMess = hit.source();
  309. list.add(esMess);
  310. }
  311. return list;
  312. }
  313. public Integer updatePatent(Patent patent, String id) {
  314. UpdateRequest<Patent, Patent> req;
  315. req = UpdateRequest.of(
  316. b -> b.index("patent").id(id)
  317. .doc(patent)
  318. );
  319. try {
  320. client.update(req, Patent.class);
  321. return 1;
  322. } catch (IOException e) {
  323. return -1;
  324. }
  325. }
  326. /**
  327. * @param key
  328. * @param page
  329. * @param limit
  330. * @return
  331. * @throws IOException
  332. */
  333. public List<Patent> searchChild(String key, Integer page, Integer limit) throws IOException {
  334. SearchRequest.Builder builder = new SearchRequest.Builder();
  335. //设置查询索引
  336. builder.index("patent");
  337. //组装查询条件
  338. HasChildQuery.Builder hasChildQuery = new HasChildQuery.Builder();
  339. hasChildQuery.type("project");
  340. hasChildQuery.query(q -> q.match(m -> m
  341. .query(key)
  342. //字段名
  343. .field("project_id")
  344. ));
  345. builder.query(q -> q.hasChild(hasChildQuery.build()));
  346. SearchResponse<Patent> response = client.search(builder.build(), Patent.class);
  347. List<Patent> list = new ArrayList<>();
  348. List<Hit<Patent>> hits = response.hits().hits();
  349. for (Hit<Patent> hit : hits) {
  350. Patent esMess = hit.source();
  351. list.add(esMess);
  352. }
  353. return list;
  354. }
  355. /**
  356. * 查询是否存在专利
  357. *
  358. * @param parentId
  359. * @param projectId
  360. * @return
  361. */
  362. public Boolean searchPatent(String parentId, Integer projectId) throws IOException {
  363. boolean flag = false;
  364. SearchRequest.Builder builder = new SearchRequest.Builder();
  365. //设置查询索引
  366. builder.index("patent");
  367. Query q1 = QueryBuilders.term(t -> t.field("project_id").value(projectId));
  368. Query q2 = QueryBuilders.parentId(parent -> parent.type("project").id(parentId));
  369. Query bool = QueryBuilders.bool(i -> i.must(q1, q2));
  370. builder.query(bool);
  371. SearchResponse<Patent> response = client.search(builder.build(), Patent.class);
  372. long total = response.hits().total().value();
  373. if (total > 0) {
  374. flag = true;
  375. }
  376. return flag;
  377. }
  378. /**
  379. * 查询是否存在专利
  380. *
  381. * @param parentId
  382. * @param projectId
  383. * @return
  384. */
  385. public Boolean ifInTask(String parentId, Integer projectId, Integer taskId) throws IOException {
  386. boolean flag = false;
  387. SearchRequest.Builder builder = new SearchRequest.Builder();
  388. //设置查询索引
  389. builder.index("patent");
  390. Query q1 = QueryBuilders.term(t -> t.field("import_task.project_id").value(projectId));
  391. Query q3 = QueryBuilders.term(t -> t.field("import_task.task_id").value(projectId));
  392. Query q2 = QueryBuilders.parentId(parent -> parent.type("import_task").id(parentId));
  393. Query bool = QueryBuilders.bool(i -> i.must(q1, q2, q3));
  394. builder.query(bool);
  395. SearchResponse<Patent> response = client.search(builder.build(), Patent.class);
  396. long total = response.hits().total().value();
  397. if (total > 0) {
  398. flag = true;
  399. }
  400. return flag;
  401. }
  402. /**
  403. * 查询同族
  404. *
  405. * @param nos
  406. * @param type
  407. * @return
  408. * @throws IOException
  409. */
  410. public EsPatentFamilyDTO addEsPatentFamily(List<String> nos, String type) throws IOException {
  411. EsPatentFamilyDTO esDTO = new EsPatentFamilyDTO();
  412. List<String> absentList = new ArrayList<>();
  413. PatentFamilyMessage patentFamilyMessage = null;
  414. String id = "";
  415. //遍历,根据专利号查询同族
  416. for (String no : nos) {
  417. SearchRequest.Builder builder = new SearchRequest.Builder();
  418. //设置查询索引
  419. builder.index("patent_family");
  420. //申请号
  421. Query q1 = QueryBuilders.term(t -> t.field("app_no").value(no));
  422. //公开号
  423. Query q2 = QueryBuilders.term(t -> t.field("public_no").value(no));
  424. //授权号
  425. Query q3 = QueryBuilders.term(t -> t.field("grant_no").value(no));
  426. //类型
  427. Query q4 = QueryBuilders.term(t -> t.field("family_type").value(type));
  428. Query bool = QueryBuilders.bool(i -> i.must(q4).should(q1, q2, q3));
  429. builder.query(bool);
  430. SearchResponse<PatentFamilyMessage> response = client.search(builder.build(), PatentFamilyMessage.class);
  431. long total = response.hits().total().value();
  432. if (total > 0) {
  433. List<FamilyPatent> list = new ArrayList<>();
  434. List<Hit<PatentFamilyMessage>> hits = response.hits().hits();
  435. Hit<PatentFamilyMessage> hit = hits.get(0);
  436. id = hit.id();
  437. patentFamilyMessage = hit.source();
  438. break;
  439. }
  440. }
  441. List<String> notInNos = new ArrayList<>();
  442. //当查询到同族时
  443. if (patentFamilyMessage != null) {
  444. List<FamilyPatent> familyPatents = patentFamilyMessage.getPatent();
  445. for (String no : nos) {
  446. FamilyPatent familyPatent = familyPatents.stream()
  447. .filter(item -> item.getAppNo() != null && item.getAppNo().equals(no) ||
  448. item.getPublicNo() != null && item.getPublicNo().equals(no) ||
  449. item.getGrantNo() != null && item.getGrantNo().equals(no))
  450. .findFirst().orElse(null);
  451. if (familyPatent == null) {
  452. notInNos.add(no);
  453. }
  454. }
  455. }
  456. //当未查询到同族时
  457. else {
  458. patentFamilyMessage = new PatentFamilyMessage();
  459. patentFamilyMessage.setFamilyType(type);
  460. patentFamilyMessage.setPatent(new ArrayList<>());
  461. notInNos.addAll(nos);
  462. }
  463. if (notInNos.size() > 0) {
  464. //所有专利号的专利详情
  465. List<StarPatentVO> starPatents = new ArrayList<>();
  466. PatentStarListDTO patentStarListDTO = new PatentStarListDTO();
  467. List<PatentStarListDTO> patentStarListDtos = patentStarApiService.splitPatentNoByType(notInNos, patentStarListDTO);
  468. while (patentStarListDtos.size() > 0) {
  469. PatentStarListDTO patentStarListDTO1 = patentStarListDtos.remove(0);
  470. patentStarListDTO1.setPageNum(1);
  471. patentStarListDTO1.setRowCount(50);
  472. Map<String, Object> resultMap = patentStarApiService.patentStarSearchApi(patentStarListDTO1);
  473. if (resultMap == null || (Integer) resultMap.get("total") == 0) {
  474. continue;
  475. }
  476. starPatents.addAll((List<StarPatentVO>) resultMap.get("records"));
  477. }
  478. List<FamilyPatent> patents = new ArrayList<>();
  479. starPatents.forEach(item -> {
  480. FamilyPatent familyPatent = new FamilyPatent();
  481. familyPatent.setAppNo(item.getApplicationNo());
  482. familyPatent.setPublicNo(item.getPublicNo());
  483. familyPatent.setGrantNo(item.getPublicAccreditNo());
  484. patents.add(familyPatent);
  485. });
  486. patentFamilyMessage.getPatent().addAll(patents);
  487. //当id不为null时
  488. if (!id.equals("")) {
  489. this.updatePatentFamily(patentFamilyMessage, id);
  490. } else {
  491. try {
  492. id = this.addPatentFamily(patentFamilyMessage);
  493. } catch (Exception e) {
  494. }
  495. }
  496. }
  497. esDTO.setPatentFamilyId(id);
  498. esDTO.setFamilyNum(patentFamilyMessage.getPatent().size());
  499. return esDTO;
  500. }
  501. /**
  502. * 添加同族
  503. *
  504. * @param message
  505. * @return
  506. * @throws Exception
  507. */
  508. public String addPatentFamily(PatentFamilyMessage message) throws Exception {
  509. IndexResponse indexResponse = client.index(i -> i
  510. .index("patent_family")
  511. .document(message)
  512. );
  513. return indexResponse.id();
  514. }
  515. /**
  516. * 更新同族
  517. *
  518. * @param message
  519. * @param id
  520. * @return
  521. */
  522. public Integer updatePatentFamily(PatentFamilyMessage message, String id) {
  523. UpdateRequest<PatentFamilyMessage, PatentFamilyMessage> req;
  524. req = UpdateRequest.of(
  525. b -> b.index("patent_family")
  526. .id(id)
  527. .doc(message)
  528. );
  529. try {
  530. client.update(req, PatentFamilyMessage.class);
  531. return 1;
  532. } catch (IOException e) {
  533. return -1;
  534. }
  535. }
  536. /**
  537. * 更新专利事务
  538. *
  539. * @param
  540. * @param id
  541. * @return
  542. */
  543. public Integer updateLegalEvent(LegalEvent legalEvent, String id) {
  544. UpdateRequest<LegalEvent, LegalEvent> req;
  545. req = UpdateRequest.of(
  546. b -> b.index("legal_event")
  547. .id(id)
  548. .doc(legalEvent)
  549. );
  550. try {
  551. client.update(req, LegalEvent.class);
  552. return 1;
  553. } catch (IOException e) {
  554. return -1;
  555. }
  556. }
  557. /**
  558. * 添加法律事务
  559. *
  560. * @param
  561. * @return
  562. * @throws Exception
  563. */
  564. public String addLegalEvent(LegalEvent legalEvent) throws Exception {
  565. IndexResponse indexResponse = client.index(i -> i
  566. .index("legal_event")
  567. .document(legalEvent)
  568. );
  569. return indexResponse.id();
  570. }
  571. /**
  572. * 更新专利事务
  573. *
  574. * @param
  575. * @param id
  576. * @return
  577. */
  578. public Integer updateQuotePatent(PatentQuoteMessage patentQuoteMessage, String id) {
  579. UpdateRequest<PatentQuoteMessage, PatentQuoteMessage> req;
  580. req = UpdateRequest.of(
  581. b -> b.index("quote_patent")
  582. .id(id)
  583. .doc(patentQuoteMessage)
  584. );
  585. try {
  586. client.update(req, PatentQuoteMessage.class);
  587. return 1;
  588. } catch (IOException e) {
  589. return -1;
  590. }
  591. }
  592. /**
  593. * 添加法律事务
  594. *
  595. * @param
  596. * @return
  597. * @throws Exception
  598. */
  599. public String addQuotePatent(PatentQuoteMessage patentQuoteMessage) throws Exception {
  600. IndexResponse indexResponse = client.index(i -> i
  601. .index("quote_patent")
  602. .document(patentQuoteMessage)
  603. );
  604. return indexResponse.id();
  605. }
  606. public String addEsQuotePatent(String no, List<String> nos) throws Exception {
  607. StarPatentVO starPatentVO = patentStarApiService.getPatentByNo(no);
  608. if (starPatentVO == null) {
  609. return null;
  610. }
  611. QuotePatent quotePatent = this.reQuotePatent(starPatentVO);
  612. PatentQuoteMessage patentQuoteMessage = null;
  613. //根据申请号和
  614. String id = "";
  615. //根据专利号查询是否有引用信息
  616. SearchRequest.Builder builder = new SearchRequest.Builder();
  617. //设置查询索引
  618. builder.index("quote_patent");
  619. //申请号
  620. Query q1 = QueryBuilders.term(t -> t.field("patent.app_no").value(starPatentVO.getApplicationNo()));
  621. Query bool = QueryBuilders.bool(i -> i.must(q1));
  622. builder.query(bool);
  623. SearchResponse<PatentQuoteMessage> response = client.search(builder.build(), PatentQuoteMessage.class);
  624. long total = response.hits().total().value();
  625. if (total > 0) {
  626. patentQuoteMessage = response.hits().hits().get(0).source();
  627. id = response.hits().hits().get(0).id();
  628. }
  629. List<QuotePatent> quotePatents = new ArrayList<>();
  630. if (patentQuoteMessage != null && patentQuoteMessage.getQuotedPatents() != null) {
  631. quotePatents = patentQuoteMessage.getQuotedPatents();
  632. }
  633. for (String patentNo : nos) {
  634. StarPatentVO starPatentVO1 = patentStarApiService.getPatentByNo(patentNo);
  635. QuotePatent quotePatent1 = null;
  636. if (quotePatents.size() > 0) {
  637. quotePatent1 = quotePatents.stream()
  638. .filter(item -> item.getAppNo().equals(starPatentVO1.getApplicationNo())).findFirst().orElse(null);
  639. }
  640. if (quotePatent1 == null) {
  641. quotePatent1 = this.reQuotePatent(starPatentVO1);
  642. quotePatents.add(quotePatent1);
  643. }
  644. }
  645. if (patentQuoteMessage == null) {
  646. if (quotePatents.size() != 0) {
  647. patentQuoteMessage = new PatentQuoteMessage();
  648. patentQuoteMessage.setPatent(quotePatent);
  649. patentQuoteMessage.setQuotedPatents(quotePatents);
  650. this.addQuotePatent(patentQuoteMessage);
  651. }
  652. } else {
  653. patentQuoteMessage.getQuotedPatents().addAll(quotePatents);
  654. this.updateQuotePatent(patentQuoteMessage, id);
  655. }
  656. return "";
  657. }
  658. private QuotePatent reQuotePatent(StarPatentVO starPatentVO) {
  659. QuotePatent quotePatent = new QuotePatent();
  660. //装载申请人
  661. if (starPatentVO.getApplicantStr() != null && !starPatentVO.getApplicantStr().equals("")) {
  662. List<String> names = Arrays.asList(starPatentVO.getApplicantStr().split(";"));
  663. List<PatentPerson> patentPeople = new ArrayList<>();
  664. for (int i = 0; i < names.size(); i++) {
  665. PatentPerson patentPerson = new PatentPerson();
  666. patentPerson.setOrder(i + 1);
  667. patentPerson.setType("1");
  668. patentPerson.setName(names.get(i));
  669. patentPeople.add(patentPerson);
  670. }
  671. quotePatent.setApplicant(patentPeople);
  672. }
  673. //装载权利人
  674. if (starPatentVO.getCurrentApplicantStr() != null && !starPatentVO.getCurrentApplicantStr().equals("")) {
  675. List<String> names = Arrays.asList(starPatentVO.getCurrentApplicantStr().split(";"));
  676. List<PatentPerson> patentPeople = new ArrayList<>();
  677. for (int i = 0; i < names.size(); i++) {
  678. PatentPerson patentPerson = new PatentPerson();
  679. patentPerson.setOrder(0);
  680. patentPerson.setType("1");
  681. patentPerson.setName(names.get(i));
  682. patentPeople.add(patentPerson);
  683. }
  684. quotePatent.setRightHolder(patentPeople);
  685. }
  686. quotePatent.setAppNo(starPatentVO.getApplicationNo());
  687. quotePatent.setGrantNo(starPatentVO.getPublicAccreditNo());
  688. quotePatent.setPublicNo(starPatentVO.getPublicNo());
  689. return quotePatent;
  690. }
  691. public String addEsLegalEvent(String patentNo) {
  692. LegalEvent legalEvent = null;
  693. String id = "";
  694. //根据专利号查询
  695. StarPatentVO starPatentVO = patentStarApiService.getPatentByNo(patentNo);
  696. if (starPatentVO == null) {
  697. return null;
  698. }
  699. String cnLegalApiStr = patentStarApiService.getCnLegalApi(starPatentVO.getRowApplicationNo());
  700. if (cnLegalApiStr != null && !cnLegalApiStr.equals("")) {
  701. List<LegalEvent> legalEvents = new ArrayList<>();
  702. //根据专利号查询是否有引用信息
  703. SearchRequest.Builder builder = new SearchRequest.Builder();
  704. //设置查询索引
  705. builder.index("legal_event");
  706. //申请号
  707. Query q1 = QueryBuilders.term(t -> t.field("app_no").value(starPatentVO.getApplicationNo()));
  708. Query q2 = QueryBuilders.term((t -> t.field("grant_no").value(starPatentVO.getPublicAccreditNo())));
  709. Query q3 = QueryBuilders.term((t -> t.field("public_no").value(starPatentVO.getPublicNo())));
  710. Query bool = QueryBuilders.bool(i -> i.should(q1, q2, q3));
  711. builder.query(bool);
  712. SearchResponse<LegalEvent> response = null;
  713. try {
  714. response = client.search(builder.build(), LegalEvent.class);
  715. } catch (IOException e) {
  716. e.printStackTrace();
  717. }
  718. long total = response.hits().total().value();
  719. if (total > 0) {
  720. response.hits().hits().forEach(item -> {
  721. legalEvents.add(item.source());
  722. });
  723. }
  724. List<ChinaLeagalStatus> chinaLeagalStatuses = JSON.parseArray(cnLegalApiStr, ChinaLeagalStatus.class);
  725. chinaLeagalStatuses.forEach(item -> {
  726. LegalEvent tem = legalEvents.stream().filter(em -> em.getEventDate() != null && em.getEventDate().compareTo(item.getLegalDate()) == 0).findFirst().orElse(null);
  727. if (tem == null) {
  728. LegalEvent legalEvent1 = new LegalEvent();
  729. legalEvent1.setEventDate(item.getLegalDate());
  730. legalEvent1.setCode(item.getLegalCode());
  731. legalEvent1.setAppNo(starPatentVO.getApplicationNo());
  732. legalEvent1.setGrantNo(starPatentVO.getPublicAccreditNo());
  733. legalEvent1.setPublicNo(starPatentVO.getPublicNo());
  734. legalEvent1.setDescription(item.getLegalStatusInfo());
  735. legalEvent1.setName(item.getLegalStatus());
  736. try {
  737. this.addLegalEvent(legalEvent1);
  738. } catch (Exception e) {
  739. }
  740. }
  741. });
  742. }
  743. return "";
  744. }
  745. /**
  746. * 查询权利要求
  747. *
  748. * @param patentNo
  749. * @return
  750. */
  751. public SelectClaimDTO selectClaim(String patentNo) throws IOException {
  752. SearchRequest.Builder builder = new SearchRequest.Builder();
  753. //设置查询索引
  754. builder.index("patent");
  755. //原始数据
  756. Query q = QueryBuilders.term(t -> t.field("claim.if_origin").value(true));
  757. //申请号
  758. Query q1 = QueryBuilders.term(t -> t.field("app_no.keyword").value(patentNo));
  759. //公开号
  760. Query q2 = QueryBuilders.term(t -> t.field("public_no.keyword").value(patentNo));
  761. //授权号
  762. Query q3 = QueryBuilders.term(t -> t.field("grant_no.keyword").value(patentNo));
  763. Query query = QueryBuilders.bool(i -> i.should(q1, q2, q3));
  764. Query bool = QueryBuilders.bool(i -> i.must(q, query));
  765. builder.query(bool);
  766. SearchResponse<Patent> response = client.search(builder.build(), Patent.class);
  767. SelectClaimDTO dto = new SelectClaimDTO();
  768. List<Hit<Patent>> hits = response.hits().hits();
  769. for (Hit<Patent> hit : hits) {
  770. Patent esMess = hit.source();
  771. if (esMess != null) {
  772. String claim = StringUtils.strip(JSON.toJSONString(esMess.getClaim()), "[]");
  773. ContentDetailDTO contentDetailDTO = JSONObject.parseObject(claim, ContentDetailDTO.class);
  774. if (contentDetailDTO.getIfOrigin()) {
  775. dto.setClaim(contentDetailDTO.getTextContent().replaceAll("\t", ""));
  776. }
  777. }
  778. }
  779. return dto;
  780. }
  781. public List<PatentColumnDTO> loadCoulumnDTO(List<PatentColumnDTO> patentColumnDTOS) {
  782. patentColumnDTOS.forEach(item -> {
  783. item.setPictureGuid(FormatUtil.getPictureFormat(item.getAppNo()));
  784. });
  785. return patentColumnDTOS;
  786. }
  787. /**
  788. * 根据专利号查询出其他专利号
  789. *
  790. * @param patentNos
  791. * @return
  792. * @throws IOException
  793. */
  794. public SelectClaimDTO selectPatentNo(List<String> patentNos) throws IOException {
  795. SearchRequest.Builder builder = new SearchRequest.Builder();
  796. //设置查询索引
  797. builder.index("patent");
  798. List<Query> queryList = new ArrayList<>();
  799. for (String patentNo : patentNos) {
  800. Query q1 = QueryBuilders.term(t -> t.field("patent_no.keyword").value(patentNo));
  801. queryList.add(q1);
  802. }
  803. //申请号
  804. Query query = QueryBuilders.bool(i -> i.mustNot(queryList));
  805. builder.query(query);
  806. SearchResponse<Patent> response = client.search(builder.build(), Patent.class);
  807. SelectClaimDTO dto = new SelectClaimDTO();
  808. List<Hit<Patent>> hits = response.hits().hits();
  809. long value = response.hits().total().value();
  810. if (value > 1) {
  811. System.out.println("====================" + value);
  812. }
  813. return dto;
  814. }
  815. }