EsService.java 56 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406
  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",projectId);
  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. //.waitForActiveShards(WaitForActiveShards.of(t -> t.count(1)))
  594. UpdateByQueryRequest request = UpdateByQueryRequest.of(i -> i.index("patent").script(script).refresh(true).query(query));
  595. try {
  596. client.updateByQuery(request);
  597. return 1;
  598. } catch (IOException e) {
  599. return -1;
  600. }
  601. }
  602. /**
  603. * @param key
  604. * @param page
  605. * @param limit
  606. * @return
  607. * @throws IOException
  608. */
  609. public List<Patent> searchChild(String key, Integer page, Integer limit) throws IOException {
  610. SearchRequest.Builder builder = new SearchRequest.Builder();
  611. //设置查询索引
  612. builder.index("patent");
  613. //组装查询条件
  614. HasChildQuery.Builder hasChildQuery = new HasChildQuery.Builder();
  615. hasChildQuery.type("project");
  616. hasChildQuery.query(q -> q.match(m -> m
  617. .query(key)
  618. //字段名
  619. .field("project_id")
  620. ));
  621. builder.query(q -> q.hasChild(hasChildQuery.build()));
  622. SearchResponse<Patent> response = client.search(builder.build(), Patent.class);
  623. List<Patent> list = new ArrayList<>();
  624. List<Hit<Patent>> hits = response.hits().hits();
  625. for (Hit<Patent> hit : hits) {
  626. Patent esMess = hit.source();
  627. list.add(esMess);
  628. }
  629. return list;
  630. }
  631. /**
  632. * 查询是否存在专利
  633. *
  634. * @param parentId
  635. * @param projectId
  636. * @return
  637. */
  638. public Boolean searchPatent(String parentId, Integer projectId) throws IOException {
  639. boolean flag = false;
  640. SearchRequest.Builder builder = new SearchRequest.Builder();
  641. //设置查询索引
  642. builder.index("patent");
  643. Query q1 = QueryBuilders.term(t -> t.field("project_id").value(projectId));
  644. Query q2 = QueryBuilders.parentId(parent -> parent.type("project").id(parentId));
  645. Query bool = QueryBuilders.bool(i -> i.must(q1, q2));
  646. builder.query(bool);
  647. SearchResponse<Patent> response = client.search(builder.build(), Patent.class);
  648. long total = response.hits().total().value();
  649. if (total > 0) {
  650. flag = true;
  651. }
  652. return flag;
  653. }
  654. /**
  655. * 查询是否存在专利
  656. *
  657. * @param parentId
  658. * @param projectId
  659. * @return
  660. */
  661. public Boolean ifInTask(String parentId, Integer projectId, Integer taskId) throws IOException {
  662. boolean flag = false;
  663. SearchRequest.Builder builder = new SearchRequest.Builder();
  664. //设置查询索引
  665. builder.index("patent");
  666. Query q1 = QueryBuilders.term(t -> t.field("import_task.project_id").value(projectId));
  667. Query q3 = QueryBuilders.term(t -> t.field("import_task.task_id").value(projectId));
  668. Query q2 = QueryBuilders.parentId(parent -> parent.type("import_task").id(parentId));
  669. Query bool = QueryBuilders.bool(i -> i.must(q1, q2, q3));
  670. builder.query(bool);
  671. SearchResponse<Patent> response = client.search(builder.build(), Patent.class);
  672. long total = response.hits().total().value();
  673. if (total > 0) {
  674. flag = true;
  675. }
  676. return flag;
  677. }
  678. /**
  679. * 查询同族
  680. *
  681. * @param nos
  682. * @param type
  683. * @return
  684. * @throws IOException
  685. */
  686. public EsPatentFamilyDTO addEsPatentFamily(Patent patent, List<String> nos, String type) throws Exception {
  687. EsPatentFamilyDTO esDTO = new EsPatentFamilyDTO();
  688. List<String> absentList = new ArrayList<>();
  689. PatentFamilyMessage patentFamilyMessage = null;
  690. String id = "";
  691. PatentFamilyMessageWithId patentFamilyMessageWithId = queryPatentFamily(patent, type);
  692. if (patentFamilyMessageWithId != null) {
  693. patentFamilyMessage = patentFamilyMessageWithId.getPatentFamilyMessage();
  694. id = patentFamilyMessageWithId.getId();
  695. } else {
  696. //遍历,根据专利号查询同族
  697. for (String no : nos) {
  698. SearchRequest.Builder builder = new SearchRequest.Builder();
  699. //设置查询索引
  700. builder.index("patent_family");
  701. //申请号
  702. Query q1 = QueryBuilders.term(t -> t.field("patent.app_no").value(no));
  703. //公开号
  704. Query q2 = QueryBuilders.term(t -> t.field("patent.public_no").value(no));
  705. //授权号
  706. Query q3 = QueryBuilders.term(t -> t.field("patent.grant_no").value(no));
  707. //类型
  708. Query q4 = QueryBuilders.term(t -> t.field("family_type").value(type));
  709. Query p = QueryBuilders.bool(i -> i.should(q1, q2, q3));
  710. Query bool = QueryBuilders.bool(i -> i.must(q4, p));
  711. builder.query(bool);
  712. SearchResponse<PatentFamilyMessage> response = client.search(builder.build(), PatentFamilyMessage.class);
  713. long total = response.hits().total().value();
  714. if (total > 0) {
  715. List<FamilyPatent> list = new ArrayList<>();
  716. List<Hit<PatentFamilyMessage>> hits = response.hits().hits();
  717. Hit<PatentFamilyMessage> hit = hits.get(0);
  718. id = hit.id();
  719. patentFamilyMessage = hit.source();
  720. break;
  721. }
  722. }
  723. }
  724. List<String> notInNos = new ArrayList<>();
  725. //当查询到同族时
  726. if (patentFamilyMessage != null) {
  727. List<FamilyPatent> familyPatents = patentFamilyMessage.getPatent();
  728. for (String no : nos) {
  729. FamilyPatent familyPatent = familyPatents.stream()
  730. .filter(item -> item.getAppNo() != null && item.getAppNo().equals(no) ||
  731. item.getPublicNo() != null && item.getPublicNo().equals(no) ||
  732. item.getGrantNo() != null && item.getGrantNo().equals(no))
  733. .findFirst().orElse(null);
  734. if (familyPatent == null) {
  735. notInNos.add(no);
  736. }
  737. }
  738. }
  739. //当未查询到同族时
  740. else {
  741. patentFamilyMessage = new PatentFamilyMessage();
  742. patentFamilyMessage.setFamilyType(type);
  743. patentFamilyMessage.setPatent(new ArrayList<>());
  744. notInNos.addAll(nos);
  745. }
  746. List<FamilyPatent> familyPatents = patentFamilyMessage.getPatent();
  747. String appNo = "";
  748. String publicNo = "";
  749. String grantNo = "";
  750. if (patent.getAppNo() != null) {
  751. appNo = patent.getAppNo();
  752. }
  753. if (patent.getPublicNo() != null) {
  754. publicNo = patent.getPublicNo();
  755. }
  756. if (patent.getGrantNo() != null) {
  757. grantNo = patent.getGrantNo();
  758. }
  759. String appNo1 = appNo;
  760. String publicNo1 = publicNo;
  761. String grantNo1 = grantNo;
  762. FamilyPatent patent1 = familyPatents.stream().filter(item ->
  763. publicNo1.equals(item.getPublicNo())
  764. || publicNo1.equals(item.getGrantNo())
  765. || grantNo1.equals(item.getPublicNo())
  766. || grantNo1.equals(item.getGrantNo())
  767. ).findFirst().orElse(null);
  768. if (patent1 == null) {
  769. patent1 = new FamilyPatent();
  770. patent1.setGrantNo(patent.getGrantNo());
  771. patent1.setAppNo(patent.getAppNo());
  772. patent1.setPublicNo(patent.getPublicNo());
  773. familyPatents.add(patent1);
  774. } else {
  775. patent1.setGrantNo(patent.getGrantNo());
  776. patent1.setAppNo(patent.getAppNo());
  777. patent1.setPublicNo(patent.getPublicNo());
  778. }
  779. if (notInNos.size() > 0) {
  780. //所有专利号的专利详情
  781. List<FamilyPatent> patents = new ArrayList<>();
  782. notInNos.forEach(item -> {
  783. FamilyPatent familyPatent = new FamilyPatent();
  784. familyPatent.setPublicNo(item);
  785. patents.add(familyPatent);
  786. });
  787. patentFamilyMessage.getPatent().addAll(patents);
  788. }
  789. //当id不为null时
  790. if (!id.equals("")) {
  791. this.updatePatentFamily(patentFamilyMessage, id);
  792. } else {
  793. try {
  794. id = this.addPatentFamily(patentFamilyMessage);
  795. } catch (Exception e) {
  796. }
  797. }
  798. esDTO.setPatentFamilyId(id);
  799. esDTO.setFamilyNum(patentFamilyMessage.getPatent().size());
  800. return esDTO;
  801. }
  802. /**
  803. * 添加同族
  804. *
  805. * @param message
  806. * @return
  807. * @throws Exception
  808. */
  809. public String addPatentFamily(PatentFamilyMessage message) throws Exception {
  810. IndexResponse indexResponse = client.index(i -> i
  811. .index("patent_family")
  812. .document(message)
  813. );
  814. return indexResponse.id();
  815. }
  816. /**
  817. * 更新同族
  818. *
  819. * @param message
  820. * @param id
  821. * @return
  822. */
  823. public Integer updatePatentFamily(PatentFamilyMessage message, String id) {
  824. UpdateRequest<PatentFamilyMessage, PatentFamilyMessage> req;
  825. req = UpdateRequest.of(
  826. b -> b.index("patent_family")
  827. .id(id)
  828. .doc(message)
  829. );
  830. try {
  831. client.update(req, PatentFamilyMessage.class);
  832. return 1;
  833. } catch (IOException e) {
  834. return -1;
  835. }
  836. }
  837. /**
  838. * 更新专利事务
  839. *
  840. * @param
  841. * @param id
  842. * @return
  843. */
  844. public Integer updateLegalEvent(LegalEvent legalEvent, String id) {
  845. UpdateRequest<LegalEvent, LegalEvent> req;
  846. req = UpdateRequest.of(
  847. b -> b.index("legal_event")
  848. .id(id)
  849. .doc(legalEvent).refresh(Refresh.True).waitForActiveShards(WaitForActiveShards.of(i -> i.count(1)))
  850. );
  851. try {
  852. client.update(req, LegalEvent.class);
  853. return 1;
  854. } catch (IOException e) {
  855. return -1;
  856. }
  857. }
  858. /**
  859. * 添加法律事务
  860. *
  861. * @param
  862. * @return
  863. * @throws Exception
  864. */
  865. public String addLegalEvent(LegalEvent legalEvent) throws Exception {
  866. IndexResponse indexResponse = client.index(i -> i
  867. .index("legal_event")
  868. .document(legalEvent).refresh(Refresh.True).waitForActiveShards(WaitForActiveShards.of(t -> t.count(1)))
  869. );
  870. return indexResponse.id();
  871. }
  872. /**
  873. * 更新专利事务
  874. *
  875. * @param
  876. * @param id
  877. * @return
  878. */
  879. public Integer updateQuotePatent(PatentQuoteMessage patentQuoteMessage, String id) {
  880. UpdateRequest<PatentQuoteMessage, PatentQuoteMessage> req;
  881. req = UpdateRequest.of(
  882. b -> b.index("quote_patent")
  883. .id(id)
  884. .doc(patentQuoteMessage)
  885. );
  886. try {
  887. client.update(req, PatentQuoteMessage.class);
  888. return 1;
  889. } catch (IOException e) {
  890. return -1;
  891. }
  892. }
  893. /**
  894. * 添加法律事务
  895. *
  896. * @param
  897. * @return
  898. * @throws Exception
  899. */
  900. public String addQuotePatent(PatentQuoteMessage patentQuoteMessage) throws Exception {
  901. IndexResponse indexResponse = client.index(i -> i
  902. .index("quote_patent")
  903. .document(patentQuoteMessage)
  904. );
  905. return indexResponse.id();
  906. }
  907. public String addEsQuotePatent(StarPatentVO starPatentVO, List<String> nos) throws Exception {
  908. if (starPatentVO == null) {
  909. return null;
  910. }
  911. QuotePatent quotePatent = this.reQuotePatent(starPatentVO);
  912. PatentQuoteMessage patentQuoteMessage = null;
  913. //根据申请号和
  914. String id = "";
  915. //根据专利号查询是否有引用信息
  916. SearchRequest.Builder builder = new SearchRequest.Builder();
  917. //设置查询索引
  918. builder.index("quote_patent");
  919. //申请号
  920. Query q1 = QueryBuilders.term(t -> t.field("patent.app_no").value(starPatentVO.getApplicationNo()));
  921. Query bool = QueryBuilders.bool(i -> i.must(q1));
  922. builder.query(bool);
  923. SearchResponse<PatentQuoteMessage> response = client.search(builder.build(), PatentQuoteMessage.class);
  924. long total = response.hits().total().value();
  925. if (total > 0) {
  926. patentQuoteMessage = response.hits().hits().get(0).source();
  927. id = response.hits().hits().get(0).id();
  928. }
  929. List<QuotePatent> quotePatents = new ArrayList<>();
  930. if (patentQuoteMessage != null && patentQuoteMessage.getQuotedPatents() != null) {
  931. quotePatents = patentQuoteMessage.getQuotedPatents();
  932. }
  933. if (nos != null && nos.size() > 0) {
  934. List<Patent> patents = esPatentService.getPatentsByNo(nos, true, null, null);
  935. for (String patentNo : nos) {
  936. QuotePatent quotePatent1 = null;
  937. if (quotePatents.size() > 0) {
  938. quotePatent1 = quotePatents.stream()
  939. .filter(item -> patentNo.equals(item.getAppNo()) || patentNo.equals(item.getPublicNo()) || patentNo.equals(item.getGrantNo())).findFirst().orElse(null);
  940. }
  941. Patent patent = patents.stream().filter(item -> patentNo.equals(item.getAppNo())).findFirst().orElse(null);
  942. if (quotePatent1 == null) {
  943. quotePatent1 = this.reQuotePatent(patent, patentNo);
  944. quotePatents.add(quotePatent1);
  945. } else {
  946. quotePatent1 = this.reQuotePatent(patent, patentNo);
  947. }
  948. }
  949. }
  950. if (patentQuoteMessage == null) {
  951. if (quotePatents.size() != 0) {
  952. patentQuoteMessage = new PatentQuoteMessage();
  953. patentQuoteMessage.setPatent(quotePatent);
  954. patentQuoteMessage.setQuotedPatents(quotePatents);
  955. this.addQuotePatent(patentQuoteMessage);
  956. }
  957. } else {
  958. patentQuoteMessage.getQuotedPatents().addAll(quotePatents);
  959. this.updateQuotePatent(patentQuoteMessage, id);
  960. }
  961. return "";
  962. }
  963. private QuotePatent reQuotePatent(StarPatentVO starPatentVO) {
  964. QuotePatent quotePatent = new QuotePatent();
  965. //装载申请人
  966. if (starPatentVO.getApplicantStr() != null && !starPatentVO.getApplicantStr().equals("")) {
  967. List<String> names = Arrays.asList(starPatentVO.getApplicantStr().split(";"));
  968. List<PatentPerson> patentPeople = new ArrayList<>();
  969. for (int i = 0; i < names.size(); i++) {
  970. PatentPerson patentPerson = new PatentPerson();
  971. patentPerson.setOrder(i + 1);
  972. patentPerson.setType("1");
  973. patentPerson.setName(names.get(i));
  974. patentPeople.add(patentPerson);
  975. }
  976. quotePatent.setApplicant(patentPeople);
  977. }
  978. //装载权利人
  979. if (starPatentVO.getCurrentApplicantStr() != null && !starPatentVO.getCurrentApplicantStr().equals("")) {
  980. List<String> names = Arrays.asList(starPatentVO.getCurrentApplicantStr().split(";"));
  981. List<PatentPerson> patentPeople = new ArrayList<>();
  982. for (int i = 0; i < names.size(); i++) {
  983. PatentPerson patentPerson = new PatentPerson();
  984. patentPerson.setOrder(0);
  985. patentPerson.setType("1");
  986. patentPerson.setName(names.get(i));
  987. patentPeople.add(patentPerson);
  988. }
  989. quotePatent.setRightHolder(patentPeople);
  990. }
  991. quotePatent.setAppNo(starPatentVO.getApplicationNo());
  992. quotePatent.setGrantNo(starPatentVO.getPublicAccreditNo());
  993. quotePatent.setPublicNo(starPatentVO.getPublicNo());
  994. return quotePatent;
  995. }
  996. private QuotePatent reQuotePatent(Patent patent, String no) {
  997. QuotePatent quotePatent = new QuotePatent();
  998. if (patent == null) {
  999. quotePatent.setPublicNo(no);
  1000. return quotePatent;
  1001. }
  1002. //装载申请人
  1003. quotePatent.setApplicant(patent.getApplicant());
  1004. quotePatent.setRightHolder(patent.getRightHolder());
  1005. quotePatent.setAppNo(patent.getAppNo());
  1006. quotePatent.setGrantNo(patent.getGrantNo());
  1007. quotePatent.setPublicNo(patent.getPublicNo());
  1008. return quotePatent;
  1009. }
  1010. public String addEsLegalEvent(StarPatentVO starPatentVO) {
  1011. LegalEvent legalEvent = null;
  1012. String id = "";
  1013. //根据专利号查询
  1014. if (starPatentVO == null) {
  1015. return null;
  1016. }
  1017. String cnLegalApiStr = patentStarApiService.getCnLegalApi(starPatentVO.getRowApplicationNo());
  1018. if (cnLegalApiStr != null && !cnLegalApiStr.equals("")) {
  1019. List<LegalEvent> legalEvents = new ArrayList<>();
  1020. //根据专利号查询是否有引用信息
  1021. SearchRequest.Builder builder = new SearchRequest.Builder();
  1022. //设置查询索引
  1023. builder.index("legal_event");
  1024. //申请号
  1025. List<Query> queries = new ArrayList<>();
  1026. if (starPatentVO.getApplicationNo() != null) {
  1027. Query q1 = QueryBuilders.term(t -> t.field("app_no").value(starPatentVO.getApplicationNo()));
  1028. queries.add(q1);
  1029. }
  1030. if (starPatentVO.getPublicAccreditNo() != null) {
  1031. Query q2 = QueryBuilders.term((t -> t.field("grant_no").value(starPatentVO.getPublicAccreditNo())));
  1032. queries.add(q2);
  1033. }
  1034. if (starPatentVO.getPublicNo() != null) {
  1035. Query q3 = QueryBuilders.term((t -> t.field("public_no").value(starPatentVO.getPublicNo())));
  1036. queries.add(q3);
  1037. }
  1038. Query bool = QueryBuilders.bool(i -> i.should(queries));
  1039. builder.query(bool);
  1040. SearchResponse<LegalEvent> response = null;
  1041. try {
  1042. response = client.search(builder.build(), LegalEvent.class);
  1043. } catch (IOException e) {
  1044. e.printStackTrace();
  1045. }
  1046. long total = response.hits().total().value();
  1047. if (total > 0) {
  1048. response.hits().hits().forEach(item -> {
  1049. legalEvents.add(item.source());
  1050. });
  1051. }
  1052. if(!cnLegalApiStr.contains("408")) {
  1053. List<ChinaLeagalStatus> chinaLeagalStatuses = JSON.parseArray(cnLegalApiStr, ChinaLeagalStatus.class);
  1054. chinaLeagalStatuses.forEach(item -> {
  1055. LegalEvent tem = legalEvents.stream().filter(em -> em.getEventDate() != null && em.getEventDate().compareTo(item.getLegalDate()) == 0).findFirst().orElse(null);
  1056. if (tem == null) {
  1057. LegalEvent legalEvent1 = new LegalEvent();
  1058. legalEvent1.setEventDate(item.getLegalDate());
  1059. legalEvent1.setCode(item.getLegalCode());
  1060. legalEvent1.setAppNo(starPatentVO.getApplicationNo());
  1061. legalEvent1.setGrantNo(starPatentVO.getPublicAccreditNo());
  1062. legalEvent1.setPublicNo(starPatentVO.getPublicNo());
  1063. legalEvent1.setDescription(item.getLegalStatusInfo());
  1064. legalEvent1.setName(item.getLegalStatus());
  1065. try {
  1066. String reId = this.addLegalEvent(legalEvent1);
  1067. } catch (Exception e) {
  1068. throw new XiaoShiException(e.getMessage());
  1069. }
  1070. }
  1071. });
  1072. }
  1073. }
  1074. return "";
  1075. }
  1076. /**
  1077. * 查询权利要求
  1078. *
  1079. * @param patentNo
  1080. * @return
  1081. */
  1082. public SelectClaimDTO selectClaim(String patentNo) throws IOException {
  1083. SearchRequest.Builder builder = new SearchRequest.Builder();
  1084. //设置查询索引
  1085. builder.index("patent");
  1086. //原始数据
  1087. Query q = QueryBuilders.term(t -> t.field("claim.if_origin").value(true));
  1088. //申请号
  1089. Query q1 = QueryBuilders.term(t -> t.field("app_no.keyword").value(patentNo));
  1090. //公开号
  1091. Query q2 = QueryBuilders.term(t -> t.field("public_no.keyword").value(patentNo));
  1092. //授权号
  1093. Query q3 = QueryBuilders.term(t -> t.field("grant_no.keyword").value(patentNo));
  1094. Query query = QueryBuilders.bool(i -> i.should(q1, q2, q3));
  1095. Query bool = QueryBuilders.bool(i -> i.must(q, query));
  1096. builder.query(bool);
  1097. SearchResponse<Patent> response = client.search(builder.build(), Patent.class);
  1098. SelectClaimDTO dto = new SelectClaimDTO();
  1099. List<Hit<Patent>> hits = response.hits().hits();
  1100. for (Hit<Patent> hit : hits) {
  1101. Patent esMess = hit.source();
  1102. if (esMess != null) {
  1103. dto.setClaimContent(esMess.getClaim());
  1104. String claim = StringUtils.strip(JSON.toJSONString(esMess.getClaim()), "[]");
  1105. ContentDetailDTO contentDetailDTO = JSONObject.parseObject(claim, ContentDetailDTO.class);
  1106. if (contentDetailDTO.getIfOrigin()) {
  1107. dto.setClaim(contentDetailDTO.getTextContent().replaceAll("\t", ""));
  1108. }
  1109. }
  1110. }
  1111. return dto;
  1112. }
  1113. /**
  1114. * 装载摘要附图
  1115. *
  1116. * @param patentColumnDTOS
  1117. * @return
  1118. */
  1119. public List<PatentColumnDTO> loadCoulumnDTO(List<PatentColumnDTO> patentColumnDTOS) {
  1120. patentColumnDTOS.forEach(item -> {
  1121. item.setPictureGuid(FormatUtil.getPictureFormat(item.getAppNo()));
  1122. });
  1123. return patentColumnDTOS;
  1124. }
  1125. /**
  1126. * 根据专利号查询出其他专利号
  1127. *
  1128. * @param patentNos
  1129. * @return
  1130. * @throws IOException
  1131. */
  1132. public SelectClaimDTO selectPatentNo(List<String> patentNos) throws IOException {
  1133. SearchRequest.Builder builder = new SearchRequest.Builder();
  1134. //设置查询索引
  1135. builder.index("patent");
  1136. List<Query> queryList = new ArrayList<>();
  1137. for (String patentNo : patentNos) {
  1138. Query q1 = QueryBuilders.term(t -> t.field("patent_no.keyword").value(patentNo));
  1139. queryList.add(q1);
  1140. }
  1141. //申请号
  1142. Query query = QueryBuilders.bool(i -> i.mustNot(queryList));
  1143. builder.query(query);
  1144. SearchResponse<Patent> response = client.search(builder.build(), Patent.class);
  1145. SelectClaimDTO dto = new SelectClaimDTO();
  1146. List<Hit<Patent>> hits = response.hits().hits();
  1147. long value = response.hits().total().value();
  1148. if (value > 1) {
  1149. System.out.println("====================" + value);
  1150. }
  1151. return dto;
  1152. }
  1153. /**
  1154. * 更新合并申请人/权利人/发明人
  1155. *
  1156. * @param patent
  1157. * @param id
  1158. * @return
  1159. */
  1160. public Integer updateMergePerson(Patent patent, String id) {
  1161. UpdateRequest<Patent, Patent> req;
  1162. req = UpdateRequest.of(
  1163. b -> b.index("patent")
  1164. .id(id)
  1165. .doc(patent)
  1166. .docAsUpsert(true)
  1167. );
  1168. try {
  1169. client.update(req, Patent.class);
  1170. return 1;
  1171. } catch (IOException e) {
  1172. return -1;
  1173. }
  1174. }
  1175. /**
  1176. * 删除申请人/权利人/发明人合并名称
  1177. *
  1178. * @param patent
  1179. * @param id
  1180. * @return
  1181. */
  1182. public Integer delMergePerson(Patent patent, String id, Integer type, String name) {
  1183. String source = "";
  1184. if (type == 0) {
  1185. 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); }";
  1186. } else {
  1187. source = "if (ctx._source.merge_inventor != null) { ctx._source.merge_inventor.removeIf(item -> item.name == params.name); }";
  1188. }
  1189. String finalSource = source;
  1190. InlineScript inlineScript = InlineScript.of(i -> i.lang("painless").params("name", JsonData.of(name)).source(finalSource));
  1191. Script script = Script.of(i -> i.inline(inlineScript));
  1192. Query query = QueryBuilders.term(i -> i.field("_id").value(id));
  1193. UpdateByQueryRequest request = UpdateByQueryRequest.of(i -> i.index("patent").script(script).query(query));
  1194. try {
  1195. client.updateByQuery(request);
  1196. return 1;
  1197. } catch (IOException e) {
  1198. return -1;
  1199. }
  1200. }
  1201. public PatentFamilyMessageWithId queryPatentFamily(Patent patent, String type) throws Exception {
  1202. //查询该专利是否已有同族
  1203. SearchRequest.Builder builderPatent = new SearchRequest.Builder();
  1204. //设置查询索引
  1205. builderPatent.index("patent_family");
  1206. //申请号
  1207. List<Query> queries = new ArrayList<>();
  1208. if (patent.getAppNo() != null) {
  1209. Query q1 = QueryBuilders.term(t -> t.field("patent.app_no").value(patent.getAppNo()));
  1210. //公开号
  1211. Query q2 = QueryBuilders.term(t -> t.field("patent.public_no").value(patent.getAppNo()));
  1212. //授权号
  1213. Query q3 = QueryBuilders.term(t -> t.field("patent.grant_no").value(patent.getAppNo()));
  1214. //类型
  1215. queries.add(q1);
  1216. queries.add(q2);
  1217. queries.add(q3);
  1218. }
  1219. if (patent.getPublicNo() != null) {
  1220. Query q1 = QueryBuilders.term(t -> t.field("patent.app_no").value(patent.getPublicNo()));
  1221. //公开号
  1222. Query q2 = QueryBuilders.term(t -> t.field("patent.public_no").value(patent.getPublicNo()));
  1223. //授权号
  1224. Query q3 = QueryBuilders.term(t -> t.field("patent.grant_no").value(patent.getPublicNo()));
  1225. queries.add(q1);
  1226. queries.add(q2);
  1227. queries.add(q3);
  1228. }
  1229. if (patent.getGrantNo() != null) {
  1230. Query q1 = QueryBuilders.term(t -> t.field("patent.app_no").value(patent.getGrantNo()));
  1231. //公开号
  1232. Query q2 = QueryBuilders.term(t -> t.field("patent.public_no").value(patent.getGrantNo()));
  1233. //授权号
  1234. Query q3 = QueryBuilders.term(t -> t.field("patent.grant_no").value(patent.getGrantNo()));
  1235. queries.add(q1);
  1236. queries.add(q2);
  1237. queries.add(q3);
  1238. }
  1239. Query q4 = QueryBuilders.term(t -> t.field("family_type").value(type));
  1240. Query p = QueryBuilders.bool(i -> i.should(queries));
  1241. Query bool = QueryBuilders.bool(i -> i.must(q4, p));
  1242. builderPatent.query(bool);
  1243. SearchResponse<PatentFamilyMessage> response = client.search(builderPatent.build(), PatentFamilyMessage.class);
  1244. long total = response.hits().total().value();
  1245. if (total > 0) {
  1246. List<FamilyPatent> list = new ArrayList<>();
  1247. List<Hit<PatentFamilyMessage>> hits = response.hits().hits();
  1248. Hit<PatentFamilyMessage> hit = hits.get(0);
  1249. String id = hit.id();
  1250. PatentFamilyMessageWithId patentFamilyMessageWithId = new PatentFamilyMessageWithId();
  1251. patentFamilyMessageWithId.setId(id);
  1252. patentFamilyMessageWithId.setPatentFamilyMessage(hit.source());
  1253. return patentFamilyMessageWithId;
  1254. }
  1255. return null;
  1256. }
  1257. public List<String> getPatentNos(List<String> ids) throws IOException {
  1258. SearchRequest.Builder builder = new SearchRequest.Builder();
  1259. //设置查询索引
  1260. builder.index("patent");
  1261. Query q = QueryBuilders.ids(i -> i.values(ids));
  1262. builder.size(9999);
  1263. builder.query(q);
  1264. SearchResponse<Patent> response = client.search(builder.build(), Patent.class);
  1265. List<Hit<Patent>> hits = response.hits().hits();
  1266. List<String> list = new ArrayList<>();
  1267. for (Hit<Patent> hit : hits) {
  1268. Patent patent = hit.source();
  1269. list.add(patent.getPatentNo());
  1270. }
  1271. return list;
  1272. }
  1273. public String appendPatentNo(List<String> nos) {
  1274. String str = "NO = ";
  1275. if (nos.size() > 1) {
  1276. str = str + "(";
  1277. for (int i = 0; i < nos.size(); i++) {
  1278. String s = nos.get(i);
  1279. if (i != nos.size() - 1) {
  1280. str = str + s + " " + "OR" + " ";
  1281. } else {
  1282. str = str + s + ")";
  1283. }
  1284. }
  1285. } else {
  1286. for (String no : nos) {
  1287. str = str + no;
  1288. }
  1289. }
  1290. return str;
  1291. }
  1292. }