EsService.java 56 KB

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