EsService.java 43 KB

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