EsService.java 36 KB

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