EsService.java 55 KB

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