EsService.java 34 KB

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