EsService.java 54 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375
  1. package cn.cslg.pas.service.business.es;
  2. import cn.cslg.pas.common.core.base.IfConstant;
  3. import cn.cslg.pas.common.dto.GetUnselectedDTO;
  4. import cn.cslg.pas.common.dto.PatentColumnDTO;
  5. import cn.cslg.pas.common.dto.PatentDTO;
  6. //import cn.cslg.pas.common.dto.business.EsPatentFamilyDTO;
  7. import cn.cslg.pas.common.dto.PatentStarListDTO;
  8. import cn.cslg.pas.common.dto.business.ContentDetailDTO;
  9. import cn.cslg.pas.common.dto.business.EsPatentFamilyDTO;
  10. import cn.cslg.pas.common.dto.business.SelectClaimDTO;
  11. import cn.cslg.pas.common.dto.es.EsCustomFieldValueDTO;
  12. import cn.cslg.pas.common.model.request.MapRequest;
  13. import cn.cslg.pas.common.model.request.OrderDTO;
  14. import cn.cslg.pas.common.model.request.QueryRequest;
  15. import cn.cslg.pas.common.model.request.StringRequest;
  16. import cn.cslg.pas.common.utils.FormatUtil;
  17. import cn.cslg.pas.common.utils.parseQueryToTree.expressManager;
  18. import cn.cslg.pas.common.utils.parseQueryToTree.operateNode;
  19. import cn.cslg.pas.common.utils.parseQueryToTree.treeNode;
  20. import cn.cslg.pas.common.vo.*;
  21. import cn.cslg.pas.common.vo.es.PatentFamilyMessageWithId;
  22. import cn.cslg.pas.domain.es.*;
  23. import cn.cslg.pas.exception.XiaoShiException;
  24. import cn.cslg.pas.service.business.CommonService;
  25. import cn.cslg.pas.service.common.PatentStarApiService;
  26. import cn.cslg.pas.service.query.FormatQueryService;
  27. import co.elastic.clients.elasticsearch.ElasticsearchClient;
  28. import co.elastic.clients.elasticsearch._types.*;
  29. import co.elastic.clients.elasticsearch._types.aggregations.*;
  30. import co.elastic.clients.elasticsearch._types.query_dsl.*;
  31. import co.elastic.clients.elasticsearch.core.*;
  32. import co.elastic.clients.elasticsearch.core.search.Hit;
  33. import co.elastic.clients.json.JsonData;
  34. import com.alibaba.fastjson.JSON;
  35. import com.alibaba.fastjson.JSONArray;
  36. import com.alibaba.fastjson2.JSONObject;
  37. import lombok.RequiredArgsConstructor;
  38. import org.apache.commons.lang3.ObjectUtils;
  39. import org.apache.commons.lang3.StringUtils;
  40. import org.elasticsearch.client.RequestOptions;
  41. import org.springframework.beans.BeanUtils;
  42. import org.springframework.beans.factory.annotation.Autowired;
  43. import org.springframework.context.annotation.Lazy;
  44. import org.springframework.stereotype.Service;
  45. import org.springframework.util.CollectionUtils;
  46. import java.io.IOException;
  47. import java.text.SimpleDateFormat;
  48. import java.util.*;
  49. import java.util.stream.Collectors;
  50. @Service
  51. @RequiredArgsConstructor(onConstructor_ = {@Lazy})
  52. public class EsService {
  53. public final List<String> dateList = Arrays.asList("AD", "PD", "GD", "EXD", "PAD", "PED", "PPD", "EPD");
  54. private final ElasticsearchClient client;
  55. @Autowired
  56. private FormatQueryService formatQueryService;
  57. @Autowired
  58. private PatentStarApiService patentStarApiService;
  59. @Autowired
  60. private EsPatentService esPatentService;
  61. @Lazy
  62. @Autowired
  63. private EsCountService esCountService;
  64. /**
  65. * @param patent
  66. * @throws Exception
  67. */
  68. public String addPatent(Patent patent) throws Exception {
  69. IndexResponse indexResponse = client.index(i -> i
  70. .index("patent")
  71. //传入user对象
  72. .document(patent)
  73. );
  74. return indexResponse.id();
  75. }
  76. /**
  77. * @param patent
  78. * @throws Exception
  79. */
  80. public String addChildPatent(Patent patent, String id) throws Exception {
  81. IndexResponse indexResponse = client.index(i -> i
  82. .index("patent")
  83. .routing(id)
  84. //传入user对象
  85. .document(patent).refresh(Refresh.True).waitForActiveShards(WaitForActiveShards.of(t -> t.count(1)))
  86. );
  87. return indexResponse.id();
  88. }
  89. /**
  90. * 根据专利号获取专利id
  91. *
  92. * @param patentNo
  93. * @return
  94. * @throws Exception
  95. */
  96. public PatentWithIdVO getIdByPatentNo(String patentNo) throws Exception {
  97. SearchRequest.Builder builder = new SearchRequest.Builder();
  98. //设置查询索引
  99. builder.index("patent");
  100. PatentWithIdVO patentWithIdVO = null;
  101. String id = null;
  102. Query q1 = QueryBuilders.term(t -> t.field("app_no.keyword").value(patentNo));
  103. //公开号
  104. Query q2 = QueryBuilders.term(t -> t.field("public_no.keyword").value(patentNo));
  105. //授权号
  106. Query q3 = QueryBuilders.term(t -> t.field("grant_no.keyword").value(patentNo));
  107. Query query = QueryBuilders.bool(i -> i.should(q1, q2, q3));
  108. builder.query(query);
  109. SearchResponse<Patent> response = client.search(builder.build(), Patent.class);
  110. List<Hit<Patent>> hits = response.hits().hits();
  111. if (hits != null && hits.size() > 0) {
  112. id = hits.get(0).id();
  113. Patent patent = hits.get(0).source();
  114. patentWithIdVO = new PatentWithIdVO();
  115. patentWithIdVO.setPatent(patent);
  116. patentWithIdVO.setId(id);
  117. }
  118. return patentWithIdVO;
  119. }
  120. /**
  121. * Es检索
  122. *
  123. * @param queryRequest 检索条件
  124. * @return
  125. */
  126. public PatentDTO esSearch(QueryRequest queryRequest) throws Exception {
  127. PatentDTO dto = new PatentDTO();
  128. Integer taskId = queryRequest.getTaskId();
  129. String searchCondition = "";
  130. Integer projectId = queryRequest.getProjectId();
  131. Integer productId = queryRequest.getProductId();
  132. String productFrom = queryRequest.getFrom();
  133. Long current = queryRequest.getCurrent();
  134. Long size = queryRequest.getSize();
  135. String groupField = queryRequest.getGroupField();
  136. //判断表达式
  137. if (queryRequest instanceof StringRequest) {
  138. searchCondition = ((StringRequest) queryRequest).getSearchQuery();
  139. } else if (queryRequest instanceof MapRequest) {
  140. Map<String, Object> map = ((MapRequest) queryRequest).getSearchQuery();
  141. StringBuilder stringBuilder = new StringBuilder();
  142. for (String key : map.keySet()) {
  143. Object value = map.get(key);
  144. if (!"".contentEquals(stringBuilder)) {
  145. stringBuilder = stringBuilder.append(" AND ").append(key).append("=").append(value);
  146. } else {
  147. stringBuilder = stringBuilder.append(key).append("=").append(value);
  148. }
  149. }
  150. searchCondition = stringBuilder.toString();
  151. }
  152. List<EsCustomFieldValueDTO> customFields = queryRequest.getCustomFields();
  153. if (!CollectionUtils.isEmpty(customFields)) {
  154. searchCondition = this.parseCustomField(customFields,projectId,taskId);
  155. }
  156. String condition = this.appendIdsCondition(searchCondition, taskId, productFrom, projectId, productId);
  157. SearchRequest.Builder builder = new SearchRequest.Builder();
  158. //设置查询索引
  159. builder.index("patent");
  160. //1. 解析检索条件
  161. treeNode tree = expressManager.getInstance().Parse(condition, false);
  162. //3. 从es中检索数据
  163. Query q = formatQueryService.EsQueryToQuery((operateNode) tree, "patent",projectId);
  164. builder.query(q);
  165. //判断同族分组
  166. String str = "";
  167. if (StringUtils.isNotEmpty(groupField)) {
  168. switch (groupField) {
  169. case "simpleFamilyId":
  170. str = "simple_family_id";
  171. break;
  172. case "inpadocFamilyId":
  173. str = "inpadoc_family_id";
  174. break;
  175. case "patsnapFamilyId":
  176. str = "patsnap_family_id";
  177. break;
  178. }
  179. }
  180. String esField = str;
  181. if (StringUtils.isNotEmpty(esField)) {
  182. List<SortOptions> options = new ArrayList<>();
  183. SortOptions appDate = SortOptions.of(i -> i.field(j -> j.field("app_date").order(SortOrder.Asc)));
  184. options.add(appDate);
  185. Aggregation bucketSort = AggregationBuilders.bucketSort(i -> i.from((current.intValue() - 1) * size.intValue()).size(size.intValue()));
  186. Aggregation aggregation = new Aggregation.Builder().terms(new TermsAggregation.Builder()
  187. .field(esField).size(100000).build())
  188. .aggregations(new HashMap() {{
  189. put("hitAgg", bucketSort);
  190. }}).build();
  191. builder.aggregations("Agg", aggregation);
  192. //对聚合结果统计出总数
  193. Aggregation terms = AggregationBuilders.terms(i -> i.field(esField).size(100000));
  194. builder.aggregations("termAgg", terms);
  195. BucketsPath bucketsPath = BucketsPath.of(i -> i.single("termAgg>_count"));
  196. Aggregation statsBucket = AggregationBuilders.statsBucket(i -> i.bucketsPath(bucketsPath));
  197. builder.aggregations("statsBucket", statsBucket);
  198. }
  199. //排序
  200. List<SortOptions> optionsList = new ArrayList<>();
  201. List<OrderDTO> dtoList = queryRequest.getOrderDTOList();
  202. if (!CollectionUtils.isEmpty(dtoList)) {
  203. String json = CommonService.readJsonFile("patent.json");
  204. List<EsConfigVO> esConfigVOS = JSON.parseArray(json, EsConfigVO.class);
  205. for (OrderDTO orderDTO : dtoList) {
  206. EsConfigVO configVO = esConfigVOS.stream().filter(item -> item.getField().equals(orderDTO.getOrderBy())).findFirst().orElse(null);
  207. if (configVO != null) {
  208. if (orderDTO.getOrderType().equals(IfConstant.NO)) {
  209. SortOptions sortOptions = SortOptions.of(i -> i.field(j -> j.field(configVO.getEsField()).order(SortOrder.Asc).missing(-1)));
  210. optionsList.add(sortOptions);
  211. } else {
  212. SortOptions sortOptions = SortOptions.of(i -> i.field(j -> j.field(configVO.getEsField()).order(SortOrder.Desc).missing(-1)));
  213. optionsList.add(sortOptions);
  214. }
  215. }
  216. }
  217. } else {
  218. SortOptions sortOptions = SortOptions.of(i -> i.field(j -> j.field("patent_no.keyword").order(SortOrder.Desc).missing(-1)));
  219. optionsList.add(sortOptions);
  220. }
  221. builder.sort(optionsList);
  222. //分页
  223. if (current != null && size != null && current > 0 && size > 0) {
  224. builder.from((current.intValue() - 1) * size.intValue()).size(size.intValue());
  225. } else {
  226. builder.from(0).size(99999);
  227. }
  228. //解除最大条数限制
  229. builder.trackTotalHits(i -> i.enabled(true));
  230. SearchResponse<Patent> response = client.search(builder.build(), Patent.class);
  231. List<PatentColumnDTO> list = new ArrayList<>();
  232. long total = 0L;
  233. if (StringUtils.isNotEmpty(esField)) {
  234. Aggregate statsAgg = response.aggregations().get("statsBucket");
  235. total = statsAgg.statsBucket().count();
  236. Aggregate agg = response.aggregations().get("Agg");
  237. List<StringTermsBucket> termsBuckets = agg.sterms().buckets().array();
  238. for (StringTermsBucket termsBucket : termsBuckets) {
  239. String key = termsBucket.key().stringValue();
  240. PatentColumnDTO columnDTO = this.getPatentColumnDTO(key, projectId,esField);
  241. list.add(columnDTO);
  242. }
  243. } else {
  244. List<Hit<Patent>> hits = response.hits().hits();
  245. total = response.hits().total().value();
  246. for (Hit<Patent> hit : hits) {
  247. String id = hit.id();
  248. Patent esMess = hit.source();
  249. PatentColumnDTO columnDTO = this.getPatentColumnDTO(esMess, projectId, id);
  250. list.add(columnDTO);
  251. }
  252. }
  253. this.loadCoulumnDTO(list);
  254. dto.setTotal(total);
  255. dto.setPatents(list);
  256. dto.setPageNum(current);
  257. dto.setPageSize(size);
  258. return dto;
  259. }
  260. //封装专利清单的同族分组
  261. public PatentColumnDTO getPatentColumnDTO(String key, Integer projectId, String field) throws Exception {
  262. PatentColumnDTO dto = new PatentColumnDTO();
  263. SearchRequest.Builder builder = new SearchRequest.Builder();
  264. //设置查询索引
  265. builder.index("patent");
  266. Query query = QueryBuilders.term(i -> i.field(field).value(key));
  267. builder.query(query);
  268. SearchResponse<Patent> response = client.search(builder.build(), Patent.class);
  269. List<Hit<Patent>> hits = response.hits().hits();
  270. if (!CollectionUtils.isEmpty(hits)) {
  271. String id = hits.get(0).id();
  272. Patent patent = hits.get(0).source();
  273. dto = this.getPatentColumnDTO(patent, projectId, id);
  274. }
  275. return dto;
  276. }
  277. //装载专利清单列表的数据
  278. public PatentColumnDTO getPatentColumnDTO(Patent patent, Integer projectId, String id) {
  279. PatentColumnDTO columnDTO = new PatentColumnDTO();
  280. BeanUtils.copyProperties(patent, columnDTO);
  281. if (projectId != null) {
  282. try {
  283. columnDTO.setMergeApplicant(esPatentService.getMergeApp(projectId, id));
  284. } catch (IOException e) {
  285. columnDTO.setMergeApplicant(new ArrayList<>());
  286. }
  287. try {
  288. columnDTO.setMergeRightHolder(esPatentService.getMergeRight(projectId, id));
  289. } catch (IOException e) {
  290. columnDTO.setMergeRightHolder(new ArrayList<>());
  291. }
  292. try {
  293. columnDTO.setMergeInventor(esPatentService.getMergeInventor(projectId, id));
  294. } catch (IOException e) {
  295. columnDTO.setMergeInventor(new ArrayList<>());
  296. }
  297. }
  298. if (ObjectUtils.isNotEmpty(patent.getApplicantAddr())) {
  299. PersonAddress applicantAddr = patent.getApplicantAddr();
  300. columnDTO.setAppAddress(applicantAddr.getAddress());
  301. columnDTO.setApplicantCountry(applicantAddr.getCountry());
  302. columnDTO.setAppProvince(applicantAddr.getProvince());
  303. columnDTO.setAppCity(applicantAddr.getCity());
  304. columnDTO.setAppDistrict(applicantAddr.getDistrict());
  305. }
  306. if (ObjectUtils.isNotEmpty(patent.getRightHolderAddr())) {
  307. PersonAddress rightAddr = patent.getRightHolderAddr();
  308. columnDTO.setRightAddress(rightAddr.getAddress());
  309. columnDTO.setRightCountry(rightAddr.getCountry());
  310. columnDTO.setRightProvince(rightAddr.getProvince());
  311. columnDTO.setRightCity(rightAddr.getCity());
  312. columnDTO.setRightDistrict(rightAddr.getDistrict());
  313. }
  314. columnDTO.setApplicant(esPatentService.loadName(patent.getApplicant()));
  315. columnDTO.setRightHolder(esPatentService.loadName(patent.getRightHolder()));
  316. columnDTO.setInventor(esPatentService.loadName(patent.getInventor()));
  317. return columnDTO;
  318. }
  319. //拼接专题库id或其他id条件
  320. public String appendIdsCondition(String searchCondition,Integer taskId,String productFrom,Integer projectId,Integer productId) {
  321. String condition = "";
  322. if (taskId != null) {
  323. if (searchCondition != null && !"".equals(searchCondition.trim())) {
  324. searchCondition = "taskId = " + taskId + " AND " + searchCondition;
  325. } else {
  326. searchCondition = "taskId = " + taskId;
  327. }
  328. } else {
  329. if (StringUtils.isNotEmpty(productFrom)) {
  330. if (productId != null) {
  331. if (searchCondition != null && !"".equals(searchCondition.trim())) {
  332. searchCondition = "productId = " + productId + " AND " + searchCondition;
  333. } else {
  334. searchCondition = "productId = " + productId;
  335. }
  336. }
  337. } else {
  338. if (projectId != null) {
  339. if (searchCondition != null && !"".equals(searchCondition.trim())) {
  340. searchCondition = "projectId = " + projectId + " AND " + searchCondition;
  341. } else {
  342. searchCondition = "projectId = " + projectId;
  343. }
  344. }
  345. }
  346. }
  347. condition = searchCondition;
  348. return condition;
  349. }
  350. /**
  351. * 解析自定义栏位和值
  352. *
  353. * @param customFields
  354. * @return
  355. */
  356. public String parseCustomField(List<EsCustomFieldValueDTO> customFields, Integer projectId, Integer taskId) throws Exception {
  357. int m = 1;
  358. int n = 0;
  359. StringBuilder builder = new StringBuilder();
  360. long start = System.currentTimeMillis();
  361. if (customFields.size() > m) {
  362. for (int i = 0; i < customFields.size(); i++) {
  363. EsCustomFieldValueDTO customField = customFields.get(i);
  364. if (i != n) {
  365. builder.append(" ").append("and").append(" ").append("(");
  366. this.appendStr(customField, builder, m, customField.getIfHaveChild(), projectId, taskId);
  367. } else {
  368. builder.append("(");
  369. this.appendStr(customField, builder, m, customField.getIfHaveChild(), projectId, taskId);
  370. }
  371. }
  372. } else {
  373. for (int i = 0; i < customFields.size(); i++) {
  374. EsCustomFieldValueDTO customField = customFields.get(i);
  375. if (i != n) {
  376. builder.append(" ").append("and").append(" ");
  377. this.appendStr(customField, builder, m, customField.getIfHaveChild(), projectId, taskId);
  378. } else {
  379. builder.append("(");
  380. this.appendStr(customField, builder, m, customField.getIfHaveChild(), projectId, taskId);
  381. }
  382. }
  383. }
  384. long end = System.currentTimeMillis();
  385. System.out.println("耗时" + (end - start));
  386. return builder.toString();
  387. }
  388. public void appendStr(EsCustomFieldValueDTO customField, StringBuilder builder, int m, boolean ifHaveChild,
  389. Integer projectId, Integer taskId) throws Exception {
  390. builder.append("field").append("=").append(customField.getFieldId());
  391. List<String> values = customField.getFieldValue();
  392. if (values.contains("未选择")) {
  393. values.removeIf(value -> value.equals("未选择"));
  394. values.add("未选择");
  395. }
  396. if (!CollectionUtils.isEmpty(values)) {
  397. builder.append(" ").append("and").append(" ");
  398. if (ifHaveChild) {
  399. builder.append("statsValue").append("=");
  400. } else {
  401. builder.append("fieldValue").append("=");
  402. }
  403. if (values.size() > m) {
  404. builder.append("(");
  405. for (int j = 0; j < values.size(); j++) {
  406. String s = values.get(j);
  407. if (j != values.size() - m) {
  408. builder.append(s).append(" ").append("or").append(" ");
  409. } else {
  410. if (s.equals("未选择")) {
  411. GetUnselectedDTO unselectedDTO = esCountService.getUnselectedCustomNum(projectId, taskId);
  412. List<String> childIds = unselectedDTO.getBeinglessChildIds();
  413. List<String> nos = this.getPatentNos(childIds);
  414. String noCondition = this.appendPatentNo(nos);
  415. builder.append(s).append(")").append(" ").append("or")
  416. .append(" ").append("(").append(noCondition).append(")").append(")");
  417. } else {
  418. builder.append(s).append(")").append(")");
  419. }
  420. }
  421. }
  422. } else {
  423. for (String value : values) {
  424. if (value.equals("未选择")) {
  425. GetUnselectedDTO unselectedDTO = esCountService.getUnselectedCustomNum(projectId, taskId);
  426. List<String> childIds = unselectedDTO.getBeinglessChildIds();
  427. List<String> nos = this.getPatentNos(childIds);
  428. String noCondition = this.appendPatentNo(nos);
  429. builder.append(value).append(" ").append("or").append(" ").append(noCondition).append(")");
  430. } else {
  431. builder.append(value).append(")");
  432. }
  433. }
  434. }
  435. } else {
  436. builder.append(")");
  437. }
  438. }
  439. //拼接专利号
  440. public String appendPatentNo(List<String> nos) {
  441. String str = "NO = ";
  442. if (nos.size() > 1) {
  443. str = str + "(";
  444. for (int i = 0; i < nos.size(); i++) {
  445. String s = nos.get(i);
  446. if (i != nos.size() - 1) {
  447. str = str + s + " " + "OR" + " ";
  448. } else {
  449. str = str + s + ")";
  450. }
  451. }
  452. } else {
  453. for (String no : nos) {
  454. str = str + no;
  455. }
  456. }
  457. return str;
  458. }
  459. //更新patent
  460. public Integer updatePatent(Patent patent, String id) {
  461. UpdateRequest<Patent, Patent> req;
  462. req = UpdateRequest.of(
  463. b -> b.index("patent").id(id)
  464. .doc(patent)
  465. );
  466. try {
  467. client.update(req, Patent.class);
  468. return 1;
  469. } catch (IOException e) {
  470. return -1;
  471. }
  472. }
  473. //更新patent
  474. public Integer updatePatentShouldWait(Patent patent, String id) {
  475. UpdateRequest<Patent, Patent> req;
  476. req = UpdateRequest.of(
  477. b -> b.index("patent").id(id)
  478. .doc(patent).refresh(Refresh.True).waitForActiveShards(WaitForActiveShards.of(i -> i.count(1)))
  479. );
  480. try {
  481. client.update(req, Patent.class);
  482. return 1;
  483. } catch (IOException e) {
  484. return -1;
  485. }
  486. }
  487. //更新patent
  488. public Integer updatePatentById(Patent patent, String id) {
  489. UpdateRequest<Patent, Patent> req;
  490. req = UpdateRequest.of(
  491. b -> b.index("patent").id(id)
  492. .doc(patent));
  493. try {
  494. client.update(req, Patent.class);
  495. return 1;
  496. } catch (IOException e) {
  497. return -1;
  498. }
  499. }
  500. /*POST /patent/_update_by_query
  501. {
  502. "script": {
  503. "source": "ctx._source.project_id = 1",
  504. "lang": "painless"
  505. },
  506. "query": {
  507. "term": {
  508. "_id": "yYVNzowBmzIo81_44OnV"
  509. }
  510. }
  511. }*/
  512. //更新子文档
  513. public Integer updateByQuery(Patent patent, String id) throws IOException {
  514. ESCustomField customField = patent.getESCustomField();
  515. String valueField = "[";
  516. List<String> fieldValueList = customField.getFieldValue();
  517. if (!CollectionUtils.isEmpty(fieldValueList)) {
  518. int lastSum = fieldValueList.size() - 1;
  519. for (int i = 0; i < fieldValueList.size(); i++) {
  520. String s = fieldValueList.get(i);
  521. if (i == lastSum) {
  522. valueField = valueField + "\"" + s + "\"";
  523. } else {
  524. valueField = valueField + "\"" + s + "\"" + ",";
  525. }
  526. }
  527. }
  528. valueField = valueField + "]";
  529. String valueStats = "[";
  530. List<String> statsValueList = customField.getStatsValue();
  531. if (!CollectionUtils.isEmpty(statsValueList)) {
  532. int lastSum = statsValueList.size() - 1;
  533. for (int i = 0; i < statsValueList.size(); i++) {
  534. String s = statsValueList.get(i);
  535. if (i == lastSum) {
  536. valueStats = valueStats + "\"" + s + "\"";
  537. } else {
  538. valueStats = valueStats + "\"" + s + "\"" + ",";
  539. }
  540. }
  541. }
  542. valueStats = valueStats + "]";
  543. SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
  544. String s = format.format(customField.getCreateTime());
  545. String dateStr = "\'" + s + "\'";
  546. String projectId = "ctx._source.custom_field.project_id = " + customField.getProjectId() + ";";
  547. String field = "ctx._source.custom_field.field=" + customField.getField() + ";";
  548. String fieldType = "ctx._source.custom_field.field_type = " + customField.getFieldType() + ";";
  549. String personId = "ctx._source.custom_field.person_id = " + customField.getPersonId() + ";";
  550. String createTime = "ctx._source.custom_field.create_time = " + dateStr + ";";
  551. String fieldValue = "ctx._source.custom_field.field_value = " + valueField + ";";
  552. String statsValue = "ctx._source.custom_field.stats_value = " + valueStats;
  553. String source = projectId + field + fieldType + personId + createTime + fieldValue + statsValue;
  554. InlineScript inlineScript = InlineScript.of(i -> i.lang("painless").source(source));
  555. Script script = Script.of(i -> i.inline(inlineScript));
  556. Query query = QueryBuilders.term(i -> i.field("_id").value(id));
  557. //.waitForActiveShards(WaitForActiveShards.of(t -> t.count(1)))
  558. UpdateByQueryRequest request = UpdateByQueryRequest.of(i -> i.index("patent").script(script).refresh(true).query(query));
  559. try {
  560. client.updateByQuery(request);
  561. return 1;
  562. } catch (IOException e) {
  563. return -1;
  564. }
  565. }
  566. /**
  567. * @param key
  568. * @param page
  569. * @param limit
  570. * @return
  571. * @throws IOException
  572. */
  573. public List<Patent> searchChild(String key, Integer page, Integer limit) throws IOException {
  574. SearchRequest.Builder builder = new SearchRequest.Builder();
  575. //设置查询索引
  576. builder.index("patent");
  577. //组装查询条件
  578. HasChildQuery.Builder hasChildQuery = new HasChildQuery.Builder();
  579. hasChildQuery.type("project");
  580. hasChildQuery.query(q -> q.match(m -> m
  581. .query(key)
  582. //字段名
  583. .field("project_id")
  584. ));
  585. builder.query(q -> q.hasChild(hasChildQuery.build()));
  586. SearchResponse<Patent> response = client.search(builder.build(), Patent.class);
  587. List<Patent> list = new ArrayList<>();
  588. List<Hit<Patent>> hits = response.hits().hits();
  589. for (Hit<Patent> hit : hits) {
  590. Patent esMess = hit.source();
  591. list.add(esMess);
  592. }
  593. return list;
  594. }
  595. /**
  596. * 查询是否存在专利
  597. *
  598. * @param parentId
  599. * @param projectId
  600. * @return
  601. */
  602. public Boolean searchPatent(String parentId, Integer projectId) throws IOException {
  603. boolean flag = false;
  604. SearchRequest.Builder builder = new SearchRequest.Builder();
  605. //设置查询索引
  606. builder.index("patent");
  607. Query q1 = QueryBuilders.term(t -> t.field("project_id").value(projectId));
  608. Query q2 = QueryBuilders.parentId(parent -> parent.type("project").id(parentId));
  609. Query bool = QueryBuilders.bool(i -> i.must(q1, q2));
  610. builder.query(bool);
  611. SearchResponse<Patent> response = client.search(builder.build(), Patent.class);
  612. long total = response.hits().total().value();
  613. if (total > 0) {
  614. flag = true;
  615. }
  616. return flag;
  617. }
  618. /**
  619. * 查询是否存在专利
  620. *
  621. * @param parentId
  622. * @param projectId
  623. * @return
  624. */
  625. public Boolean ifInTask(String parentId, Integer projectId, Integer taskId) throws IOException {
  626. boolean flag = false;
  627. SearchRequest.Builder builder = new SearchRequest.Builder();
  628. //设置查询索引
  629. builder.index("patent");
  630. Query q1 = QueryBuilders.term(t -> t.field("import_task.project_id").value(projectId));
  631. Query q3 = QueryBuilders.term(t -> t.field("import_task.task_id").value(projectId));
  632. Query q2 = QueryBuilders.parentId(parent -> parent.type("import_task").id(parentId));
  633. Query bool = QueryBuilders.bool(i -> i.must(q1, q2, q3));
  634. builder.query(bool);
  635. SearchResponse<Patent> response = client.search(builder.build(), Patent.class);
  636. long total = response.hits().total().value();
  637. if (total > 0) {
  638. flag = true;
  639. }
  640. return flag;
  641. }
  642. /**
  643. * 查询同族
  644. *
  645. * @param nos
  646. * @param type
  647. * @return
  648. * @throws IOException
  649. */
  650. public EsPatentFamilyDTO addEsPatentFamily(Patent patent, List<String> nos, String type) throws Exception {
  651. EsPatentFamilyDTO esDTO = new EsPatentFamilyDTO();
  652. List<String> absentList = new ArrayList<>();
  653. PatentFamilyMessage patentFamilyMessage = null;
  654. String id = "";
  655. PatentFamilyMessageWithId patentFamilyMessageWithId = queryPatentFamily(patent, type);
  656. if (patentFamilyMessageWithId != null) {
  657. patentFamilyMessage = patentFamilyMessageWithId.getPatentFamilyMessage();
  658. id = patentFamilyMessageWithId.getId();
  659. } else {
  660. //遍历,根据专利号查询同族
  661. for (String no : nos) {
  662. SearchRequest.Builder builder = new SearchRequest.Builder();
  663. //设置查询索引
  664. builder.index("patent_family");
  665. //申请号
  666. Query q1 = QueryBuilders.term(t -> t.field("patent.app_no").value(no));
  667. //公开号
  668. Query q2 = QueryBuilders.term(t -> t.field("patent.public_no").value(no));
  669. //授权号
  670. Query q3 = QueryBuilders.term(t -> t.field("patent.grant_no").value(no));
  671. //类型
  672. Query q4 = QueryBuilders.term(t -> t.field("family_type").value(type));
  673. Query p = QueryBuilders.bool(i -> i.should(q1, q2, q3));
  674. Query bool = QueryBuilders.bool(i -> i.must(q4, p));
  675. builder.query(bool);
  676. SearchResponse<PatentFamilyMessage> response = client.search(builder.build(), PatentFamilyMessage.class);
  677. long total = response.hits().total().value();
  678. if (total > 0) {
  679. List<FamilyPatent> list = new ArrayList<>();
  680. List<Hit<PatentFamilyMessage>> hits = response.hits().hits();
  681. Hit<PatentFamilyMessage> hit = hits.get(0);
  682. id = hit.id();
  683. patentFamilyMessage = hit.source();
  684. break;
  685. }
  686. }
  687. }
  688. List<String> notInNos = new ArrayList<>();
  689. //当查询到同族时
  690. if (patentFamilyMessage != null) {
  691. List<FamilyPatent> familyPatents = patentFamilyMessage.getPatent();
  692. for (String no : nos) {
  693. FamilyPatent familyPatent = familyPatents.stream()
  694. .filter(item -> item.getAppNo() != null && item.getAppNo().equals(no) ||
  695. item.getPublicNo() != null && item.getPublicNo().equals(no) ||
  696. item.getGrantNo() != null && item.getGrantNo().equals(no))
  697. .findFirst().orElse(null);
  698. if (familyPatent == null) {
  699. notInNos.add(no);
  700. }
  701. }
  702. }
  703. //当未查询到同族时
  704. else {
  705. patentFamilyMessage = new PatentFamilyMessage();
  706. patentFamilyMessage.setFamilyType(type);
  707. patentFamilyMessage.setPatent(new ArrayList<>());
  708. notInNos.addAll(nos);
  709. }
  710. List<FamilyPatent> familyPatents = patentFamilyMessage.getPatent();
  711. String appNo = "";
  712. String publicNo = "";
  713. String grantNo = "";
  714. if (patent.getAppNo() != null) {
  715. appNo = patent.getAppNo();
  716. }
  717. if (patent.getPublicNo() != null) {
  718. publicNo = patent.getPublicNo();
  719. }
  720. if (patent.getGrantNo() != null) {
  721. grantNo = patent.getGrantNo();
  722. }
  723. String appNo1 = appNo;
  724. String publicNo1 = publicNo;
  725. String grantNo1 = grantNo;
  726. FamilyPatent patent1 = familyPatents.stream().filter(item ->
  727. publicNo1.equals(item.getPublicNo())
  728. || publicNo1.equals(item.getGrantNo())
  729. || grantNo1.equals(item.getPublicNo())
  730. || grantNo1.equals(item.getGrantNo())
  731. ).findFirst().orElse(null);
  732. if (patent1 == null) {
  733. patent1 = new FamilyPatent();
  734. patent1.setGrantNo(patent.getGrantNo());
  735. patent1.setAppNo(patent.getAppNo());
  736. patent1.setPublicNo(patent.getPublicNo());
  737. familyPatents.add(patent1);
  738. } else {
  739. patent1.setGrantNo(patent.getGrantNo());
  740. patent1.setAppNo(patent.getAppNo());
  741. patent1.setPublicNo(patent.getPublicNo());
  742. }
  743. if (notInNos.size() > 0) {
  744. //所有专利号的专利详情
  745. List<FamilyPatent> patents = new ArrayList<>();
  746. notInNos.forEach(item -> {
  747. FamilyPatent familyPatent = new FamilyPatent();
  748. familyPatent.setPublicNo(item);
  749. patents.add(familyPatent);
  750. });
  751. patentFamilyMessage.getPatent().addAll(patents);
  752. }
  753. //当id不为null时
  754. if (!id.equals("")) {
  755. this.updatePatentFamily(patentFamilyMessage, id);
  756. } else {
  757. try {
  758. id = this.addPatentFamily(patentFamilyMessage);
  759. } catch (Exception e) {
  760. }
  761. }
  762. esDTO.setPatentFamilyId(id);
  763. esDTO.setFamilyNum(patentFamilyMessage.getPatent().size());
  764. return esDTO;
  765. }
  766. /**
  767. * 添加同族
  768. *
  769. * @param message
  770. * @return
  771. * @throws Exception
  772. */
  773. public String addPatentFamily(PatentFamilyMessage message) throws Exception {
  774. IndexResponse indexResponse = client.index(i -> i
  775. .index("patent_family")
  776. .document(message)
  777. );
  778. return indexResponse.id();
  779. }
  780. /**
  781. * 更新同族
  782. *
  783. * @param message
  784. * @param id
  785. * @return
  786. */
  787. public Integer updatePatentFamily(PatentFamilyMessage message, String id) {
  788. UpdateRequest<PatentFamilyMessage, PatentFamilyMessage> req;
  789. req = UpdateRequest.of(
  790. b -> b.index("patent_family")
  791. .id(id)
  792. .doc(message)
  793. );
  794. try {
  795. client.update(req, PatentFamilyMessage.class);
  796. return 1;
  797. } catch (IOException e) {
  798. return -1;
  799. }
  800. }
  801. /**
  802. * 更新专利事务
  803. *
  804. * @param
  805. * @param id
  806. * @return
  807. */
  808. public Integer updateLegalEvent(LegalEvent legalEvent, String id) {
  809. UpdateRequest<LegalEvent, LegalEvent> req;
  810. req = UpdateRequest.of(
  811. b -> b.index("legal_event")
  812. .id(id)
  813. .doc(legalEvent).refresh(Refresh.True).waitForActiveShards(WaitForActiveShards.of(i -> i.count(1)))
  814. );
  815. try {
  816. client.update(req, LegalEvent.class);
  817. return 1;
  818. } catch (IOException e) {
  819. return -1;
  820. }
  821. }
  822. /**
  823. * 添加法律事务
  824. *
  825. * @param
  826. * @return
  827. * @throws Exception
  828. */
  829. public String addLegalEvent(LegalEvent legalEvent) throws Exception {
  830. IndexResponse indexResponse = client.index(i -> i
  831. .index("legal_event")
  832. .document(legalEvent).refresh(Refresh.True).waitForActiveShards(WaitForActiveShards.of(t -> t.count(1)))
  833. );
  834. return indexResponse.id();
  835. }
  836. /**
  837. * 更新专利事务
  838. *
  839. * @param
  840. * @param id
  841. * @return
  842. */
  843. public Integer updateQuotePatent(PatentQuoteMessage patentQuoteMessage, String id) {
  844. UpdateRequest<PatentQuoteMessage, PatentQuoteMessage> req;
  845. req = UpdateRequest.of(
  846. b -> b.index("quote_patent")
  847. .id(id)
  848. .doc(patentQuoteMessage)
  849. );
  850. try {
  851. client.update(req, PatentQuoteMessage.class);
  852. return 1;
  853. } catch (IOException e) {
  854. return -1;
  855. }
  856. }
  857. /**
  858. * 添加法律事务
  859. *
  860. * @param
  861. * @return
  862. * @throws Exception
  863. */
  864. public String addQuotePatent(PatentQuoteMessage patentQuoteMessage) throws Exception {
  865. IndexResponse indexResponse = client.index(i -> i
  866. .index("quote_patent")
  867. .document(patentQuoteMessage)
  868. );
  869. return indexResponse.id();
  870. }
  871. public String addEsQuotePatent(StarPatentVO starPatentVO, List<String> nos) throws Exception {
  872. if (starPatentVO == null) {
  873. return null;
  874. }
  875. QuotePatent quotePatent = this.reQuotePatent(starPatentVO);
  876. PatentQuoteMessage patentQuoteMessage = null;
  877. //根据申请号和
  878. String id = "";
  879. //根据专利号查询是否有引用信息
  880. SearchRequest.Builder builder = new SearchRequest.Builder();
  881. //设置查询索引
  882. builder.index("quote_patent");
  883. //申请号
  884. Query q1 = QueryBuilders.term(t -> t.field("patent.app_no").value(starPatentVO.getApplicationNo()));
  885. Query bool = QueryBuilders.bool(i -> i.must(q1));
  886. builder.query(bool);
  887. SearchResponse<PatentQuoteMessage> response = client.search(builder.build(), PatentQuoteMessage.class);
  888. long total = response.hits().total().value();
  889. if (total > 0) {
  890. patentQuoteMessage = response.hits().hits().get(0).source();
  891. id = response.hits().hits().get(0).id();
  892. }
  893. List<QuotePatent> quotePatents = new ArrayList<>();
  894. if (patentQuoteMessage != null && patentQuoteMessage.getQuotedPatents() != null) {
  895. quotePatents = patentQuoteMessage.getQuotedPatents();
  896. }
  897. if (nos != null && nos.size() > 0) {
  898. List<Patent> patents = esPatentService.getPatentsByNo(nos, true, null, null);
  899. for (String patentNo : nos) {
  900. QuotePatent quotePatent1 = null;
  901. if (quotePatents.size() > 0) {
  902. quotePatent1 = quotePatents.stream()
  903. .filter(item -> patentNo.equals(item.getAppNo()) || patentNo.equals(item.getPublicNo()) || patentNo.equals(item.getGrantNo())).findFirst().orElse(null);
  904. }
  905. Patent patent = patents.stream().filter(item -> patentNo.equals(item.getAppNo())).findFirst().orElse(null);
  906. if (quotePatent1 == null) {
  907. quotePatent1 = this.reQuotePatent(patent, patentNo);
  908. quotePatents.add(quotePatent1);
  909. } else {
  910. quotePatent1 = this.reQuotePatent(patent, patentNo);
  911. }
  912. }
  913. }
  914. if (patentQuoteMessage == null) {
  915. if (quotePatents.size() != 0) {
  916. patentQuoteMessage = new PatentQuoteMessage();
  917. patentQuoteMessage.setPatent(quotePatent);
  918. patentQuoteMessage.setQuotedPatents(quotePatents);
  919. this.addQuotePatent(patentQuoteMessage);
  920. }
  921. } else {
  922. patentQuoteMessage.getQuotedPatents().addAll(quotePatents);
  923. this.updateQuotePatent(patentQuoteMessage, id);
  924. }
  925. return "";
  926. }
  927. private QuotePatent reQuotePatent(StarPatentVO starPatentVO) {
  928. QuotePatent quotePatent = new QuotePatent();
  929. //装载申请人
  930. if (starPatentVO.getApplicantStr() != null && !starPatentVO.getApplicantStr().equals("")) {
  931. List<String> names = Arrays.asList(starPatentVO.getApplicantStr().split(";"));
  932. List<PatentPerson> patentPeople = new ArrayList<>();
  933. for (int i = 0; i < names.size(); i++) {
  934. PatentPerson patentPerson = new PatentPerson();
  935. patentPerson.setOrder(i + 1);
  936. patentPerson.setType("1");
  937. patentPerson.setName(names.get(i));
  938. patentPeople.add(patentPerson);
  939. }
  940. quotePatent.setApplicant(patentPeople);
  941. }
  942. //装载权利人
  943. if (starPatentVO.getCurrentApplicantStr() != null && !starPatentVO.getCurrentApplicantStr().equals("")) {
  944. List<String> names = Arrays.asList(starPatentVO.getCurrentApplicantStr().split(";"));
  945. List<PatentPerson> patentPeople = new ArrayList<>();
  946. for (int i = 0; i < names.size(); i++) {
  947. PatentPerson patentPerson = new PatentPerson();
  948. patentPerson.setOrder(0);
  949. patentPerson.setType("1");
  950. patentPerson.setName(names.get(i));
  951. patentPeople.add(patentPerson);
  952. }
  953. quotePatent.setRightHolder(patentPeople);
  954. }
  955. quotePatent.setAppNo(starPatentVO.getApplicationNo());
  956. quotePatent.setGrantNo(starPatentVO.getPublicAccreditNo());
  957. quotePatent.setPublicNo(starPatentVO.getPublicNo());
  958. return quotePatent;
  959. }
  960. private QuotePatent reQuotePatent(Patent patent, String no) {
  961. QuotePatent quotePatent = new QuotePatent();
  962. if (patent == null) {
  963. quotePatent.setPublicNo(no);
  964. return quotePatent;
  965. }
  966. //装载申请人
  967. quotePatent.setApplicant(patent.getApplicant());
  968. quotePatent.setRightHolder(patent.getRightHolder());
  969. quotePatent.setAppNo(patent.getAppNo());
  970. quotePatent.setGrantNo(patent.getGrantNo());
  971. quotePatent.setPublicNo(patent.getPublicNo());
  972. return quotePatent;
  973. }
  974. public String addEsLegalEvent(StarPatentVO starPatentVO) {
  975. LegalEvent legalEvent = null;
  976. String id = "";
  977. //根据专利号查询
  978. if (starPatentVO == null) {
  979. return null;
  980. }
  981. String cnLegalApiStr = patentStarApiService.getCnLegalApi(starPatentVO.getRowApplicationNo());
  982. if (cnLegalApiStr != null && !cnLegalApiStr.equals("")) {
  983. List<LegalEvent> legalEvents = new ArrayList<>();
  984. //根据专利号查询是否有引用信息
  985. SearchRequest.Builder builder = new SearchRequest.Builder();
  986. //设置查询索引
  987. builder.index("legal_event");
  988. //申请号
  989. List<Query> queries = new ArrayList<>();
  990. if (starPatentVO.getApplicationNo() != null) {
  991. Query q1 = QueryBuilders.term(t -> t.field("app_no").value(starPatentVO.getApplicationNo()));
  992. queries.add(q1);
  993. }
  994. if (starPatentVO.getPublicAccreditNo() != null) {
  995. Query q2 = QueryBuilders.term((t -> t.field("grant_no").value(starPatentVO.getPublicAccreditNo())));
  996. queries.add(q2);
  997. }
  998. if (starPatentVO.getPublicNo() != null) {
  999. Query q3 = QueryBuilders.term((t -> t.field("public_no").value(starPatentVO.getPublicNo())));
  1000. queries.add(q3);
  1001. }
  1002. Query bool = QueryBuilders.bool(i -> i.should(queries));
  1003. builder.query(bool);
  1004. SearchResponse<LegalEvent> response = null;
  1005. try {
  1006. response = client.search(builder.build(), LegalEvent.class);
  1007. } catch (IOException e) {
  1008. e.printStackTrace();
  1009. }
  1010. long total = response.hits().total().value();
  1011. if (total > 0) {
  1012. response.hits().hits().forEach(item -> {
  1013. legalEvents.add(item.source());
  1014. });
  1015. }
  1016. if(!cnLegalApiStr.contains("408")) {
  1017. List<ChinaLeagalStatus> chinaLeagalStatuses = JSON.parseArray(cnLegalApiStr, ChinaLeagalStatus.class);
  1018. chinaLeagalStatuses.forEach(item -> {
  1019. LegalEvent tem = legalEvents.stream().filter(em -> em.getEventDate() != null && em.getEventDate().compareTo(item.getLegalDate()) == 0).findFirst().orElse(null);
  1020. if (tem == null) {
  1021. LegalEvent legalEvent1 = new LegalEvent();
  1022. legalEvent1.setEventDate(item.getLegalDate());
  1023. legalEvent1.setCode(item.getLegalCode());
  1024. legalEvent1.setAppNo(starPatentVO.getApplicationNo());
  1025. legalEvent1.setGrantNo(starPatentVO.getPublicAccreditNo());
  1026. legalEvent1.setPublicNo(starPatentVO.getPublicNo());
  1027. legalEvent1.setDescription(item.getLegalStatusInfo());
  1028. legalEvent1.setName(item.getLegalStatus());
  1029. try {
  1030. String reId = this.addLegalEvent(legalEvent1);
  1031. } catch (Exception e) {
  1032. throw new XiaoShiException(e.getMessage());
  1033. }
  1034. }
  1035. });
  1036. }
  1037. }
  1038. return "";
  1039. }
  1040. /**
  1041. * 查询权利要求
  1042. *
  1043. * @param patentNo
  1044. * @return
  1045. */
  1046. public SelectClaimDTO selectClaim(String patentNo) throws IOException {
  1047. SearchRequest.Builder builder = new SearchRequest.Builder();
  1048. //设置查询索引
  1049. builder.index("patent");
  1050. //原始数据
  1051. Query q = QueryBuilders.term(t -> t.field("claim.if_origin").value(true));
  1052. //申请号
  1053. Query q1 = QueryBuilders.term(t -> t.field("app_no.keyword").value(patentNo));
  1054. //公开号
  1055. Query q2 = QueryBuilders.term(t -> t.field("public_no.keyword").value(patentNo));
  1056. //授权号
  1057. Query q3 = QueryBuilders.term(t -> t.field("grant_no.keyword").value(patentNo));
  1058. Query query = QueryBuilders.bool(i -> i.should(q1, q2, q3));
  1059. Query bool = QueryBuilders.bool(i -> i.must(q, query));
  1060. builder.query(bool);
  1061. SearchResponse<Patent> response = client.search(builder.build(), Patent.class);
  1062. SelectClaimDTO dto = new SelectClaimDTO();
  1063. List<Hit<Patent>> hits = response.hits().hits();
  1064. for (Hit<Patent> hit : hits) {
  1065. Patent esMess = hit.source();
  1066. if (esMess != null) {
  1067. dto.setClaimContent(esMess.getClaim());
  1068. dto.setClaim(esMess.getClaim().get(0).getTextContent());
  1069. }
  1070. }
  1071. return dto;
  1072. }
  1073. /**
  1074. * 装载摘要附图
  1075. *
  1076. * @param patentColumnDTOS
  1077. * @return
  1078. */
  1079. public List<PatentColumnDTO> loadCoulumnDTO(List<PatentColumnDTO> patentColumnDTOS) {
  1080. patentColumnDTOS.forEach(item -> {
  1081. item.setPictureGuid(FormatUtil.getPictureFormat(item.getAppNo()));
  1082. });
  1083. return patentColumnDTOS;
  1084. }
  1085. /**
  1086. * 根据专利号查询出其他专利号
  1087. *
  1088. * @param patentNos
  1089. * @return
  1090. * @throws IOException
  1091. */
  1092. public SelectClaimDTO selectPatentNo(List<String> patentNos) throws IOException {
  1093. SearchRequest.Builder builder = new SearchRequest.Builder();
  1094. //设置查询索引
  1095. builder.index("patent");
  1096. List<Query> queryList = new ArrayList<>();
  1097. for (String patentNo : patentNos) {
  1098. Query q1 = QueryBuilders.term(t -> t.field("patent_no.keyword").value(patentNo));
  1099. queryList.add(q1);
  1100. }
  1101. //申请号
  1102. Query query = QueryBuilders.bool(i -> i.mustNot(queryList));
  1103. builder.query(query);
  1104. SearchResponse<Patent> response = client.search(builder.build(), Patent.class);
  1105. SelectClaimDTO dto = new SelectClaimDTO();
  1106. List<Hit<Patent>> hits = response.hits().hits();
  1107. long value = response.hits().total().value();
  1108. if (value > 1) {
  1109. System.out.println("====================" + value);
  1110. }
  1111. return dto;
  1112. }
  1113. /**
  1114. * 更新合并申请人/权利人/发明人
  1115. *
  1116. * @param patent
  1117. * @param id
  1118. * @return
  1119. */
  1120. public Integer updateMergePerson(Patent patent, String id) {
  1121. UpdateRequest<Patent, Patent> req;
  1122. req = UpdateRequest.of(
  1123. b -> b.index("patent")
  1124. .id(id)
  1125. .doc(patent)
  1126. .docAsUpsert(true)
  1127. );
  1128. try {
  1129. client.update(req, Patent.class);
  1130. return 1;
  1131. } catch (IOException e) {
  1132. return -1;
  1133. }
  1134. }
  1135. /**
  1136. * 删除申请人/权利人/发明人合并名称
  1137. *
  1138. * @param patent
  1139. * @param id
  1140. * @return
  1141. */
  1142. public Integer delMergePerson(Patent patent, String id, Integer type, String name) {
  1143. String source = "";
  1144. if (type == 0) {
  1145. source = "if (ctx._source.merge_applicant != null) { ctx._source.merge_applicant.removeIf(item -> item.name == params.name); } if (ctx._source.merge_right_holder != null) { ctx._source.merge_right_holder .removeIf(item -> item.name == params.name); }";
  1146. } else {
  1147. source = "if (ctx._source.merge_inventor != null) { ctx._source.merge_inventor.removeIf(item -> item.name == params.name); }";
  1148. }
  1149. String finalSource = source;
  1150. InlineScript inlineScript = InlineScript.of(i -> i.lang("painless").params("name", JsonData.of(name)).source(finalSource));
  1151. Script script = Script.of(i -> i.inline(inlineScript));
  1152. Query query = QueryBuilders.term(i -> i.field("_id").value(id));
  1153. UpdateByQueryRequest request = UpdateByQueryRequest.of(i -> i.index("patent").script(script).query(query));
  1154. try {
  1155. client.updateByQuery(request);
  1156. return 1;
  1157. } catch (IOException e) {
  1158. return -1;
  1159. }
  1160. }
  1161. public PatentFamilyMessageWithId queryPatentFamily(Patent patent, String type) throws Exception {
  1162. //查询该专利是否已有同族
  1163. SearchRequest.Builder builderPatent = new SearchRequest.Builder();
  1164. //设置查询索引
  1165. builderPatent.index("patent_family");
  1166. //申请号
  1167. List<Query> queries = new ArrayList<>();
  1168. if (patent.getAppNo() != null) {
  1169. Query q1 = QueryBuilders.term(t -> t.field("patent.app_no").value(patent.getAppNo()));
  1170. //公开号
  1171. Query q2 = QueryBuilders.term(t -> t.field("patent.public_no").value(patent.getAppNo()));
  1172. //授权号
  1173. Query q3 = QueryBuilders.term(t -> t.field("patent.grant_no").value(patent.getAppNo()));
  1174. //类型
  1175. queries.add(q1);
  1176. queries.add(q2);
  1177. queries.add(q3);
  1178. }
  1179. if (patent.getPublicNo() != null) {
  1180. Query q1 = QueryBuilders.term(t -> t.field("patent.app_no").value(patent.getPublicNo()));
  1181. //公开号
  1182. Query q2 = QueryBuilders.term(t -> t.field("patent.public_no").value(patent.getPublicNo()));
  1183. //授权号
  1184. Query q3 = QueryBuilders.term(t -> t.field("patent.grant_no").value(patent.getPublicNo()));
  1185. queries.add(q1);
  1186. queries.add(q2);
  1187. queries.add(q3);
  1188. }
  1189. if (patent.getGrantNo() != null) {
  1190. Query q1 = QueryBuilders.term(t -> t.field("patent.app_no").value(patent.getGrantNo()));
  1191. //公开号
  1192. Query q2 = QueryBuilders.term(t -> t.field("patent.public_no").value(patent.getGrantNo()));
  1193. //授权号
  1194. Query q3 = QueryBuilders.term(t -> t.field("patent.grant_no").value(patent.getGrantNo()));
  1195. queries.add(q1);
  1196. queries.add(q2);
  1197. queries.add(q3);
  1198. }
  1199. Query q4 = QueryBuilders.term(t -> t.field("family_type").value(type));
  1200. Query p = QueryBuilders.bool(i -> i.should(queries));
  1201. Query bool = QueryBuilders.bool(i -> i.must(q4, p));
  1202. builderPatent.query(bool);
  1203. SearchResponse<PatentFamilyMessage> response = client.search(builderPatent.build(), PatentFamilyMessage.class);
  1204. long total = response.hits().total().value();
  1205. if (total > 0) {
  1206. List<FamilyPatent> list = new ArrayList<>();
  1207. List<Hit<PatentFamilyMessage>> hits = response.hits().hits();
  1208. Hit<PatentFamilyMessage> hit = hits.get(0);
  1209. String id = hit.id();
  1210. PatentFamilyMessageWithId patentFamilyMessageWithId = new PatentFamilyMessageWithId();
  1211. patentFamilyMessageWithId.setId(id);
  1212. patentFamilyMessageWithId.setPatentFamilyMessage(hit.source());
  1213. return patentFamilyMessageWithId;
  1214. }
  1215. return null;
  1216. }
  1217. public List<String> getPatentNos(List<String> ids) throws IOException {
  1218. SearchRequest.Builder builder = new SearchRequest.Builder();
  1219. //设置查询索引
  1220. builder.index("patent");
  1221. Query q = QueryBuilders.ids(i -> i.values(ids));
  1222. builder.size(9999);
  1223. builder.query(q);
  1224. SearchResponse<Patent> response = client.search(builder.build(), Patent.class);
  1225. List<Hit<Patent>> hits = response.hits().hits();
  1226. List<String> list = new ArrayList<>();
  1227. for (Hit<Patent> hit : hits) {
  1228. Patent patent = hit.source();
  1229. list.add(patent.getPatentNo());
  1230. }
  1231. return list;
  1232. }
  1233. public Integer getPatent(String patentNo, Integer projectId) throws IOException {
  1234. SearchRequest.Builder builder = new SearchRequest.Builder();
  1235. //设置查询索引
  1236. builder.index("patent");
  1237. Query query = QueryBuilders.term(i -> i.field("project_id").value(projectId));
  1238. Query q = QueryBuilders.term(i -> i.field("patent_no.keyword").value(patentNo));
  1239. Query query1 = QueryBuilders.hasParent(i -> i.parentType("patent").query(q));
  1240. Query bool = QueryBuilders.bool(i -> i.must(query, query1));
  1241. builder.query(bool);
  1242. //解除最大条数限制
  1243. builder.trackTotalHits(i -> i.enabled(true));
  1244. SearchResponse<Patent> response = client.search(builder.build(), Patent.class);
  1245. List<Hit<Patent>> hits = response.hits().hits();
  1246. List<String> list = new ArrayList<>();
  1247. for (Hit<Patent> hit : hits) {
  1248. String id = hit.id();
  1249. list.add(id);
  1250. }
  1251. return this.deleteByIds(list);
  1252. }
  1253. public Integer deleteByIds(List<String> ids) {
  1254. Query query = QueryBuilders.ids(n -> n.values(ids));
  1255. DeleteByQueryRequest request = DeleteByQueryRequest.of(i -> i.index("patent").query(query));
  1256. try {
  1257. client.deleteByQuery(request);
  1258. return 1;
  1259. } catch (IOException e) {
  1260. throw new XiaoShiException("删除失败");
  1261. }
  1262. }
  1263. }