EsService.java 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. package com.example.xiaoshiweixinback.service.importPatent;
  2. import co.elastic.clients.elasticsearch.ElasticsearchClient;
  3. import co.elastic.clients.elasticsearch._types.*;
  4. import co.elastic.clients.elasticsearch._types.aggregations.*;
  5. import co.elastic.clients.elasticsearch._types.query_dsl.HasChildQuery;
  6. import co.elastic.clients.elasticsearch._types.query_dsl.Query;
  7. import co.elastic.clients.elasticsearch._types.query_dsl.QueryBuilders;
  8. import co.elastic.clients.elasticsearch.core.*;
  9. import co.elastic.clients.elasticsearch.core.search.Hit;
  10. import co.elastic.clients.json.JsonData;
  11. import com.alibaba.fastjson.JSON;
  12. import com.example.xiaoshiweixinback.domain.es.Patent;
  13. import com.example.xiaoshiweixinback.domain.es.PatentVector;
  14. import com.example.xiaoshiweixinback.entity.vo.patent.PatentWithIdVO;
  15. import lombok.RequiredArgsConstructor;
  16. import org.apache.commons.lang3.ObjectUtils;
  17. import org.apache.commons.lang3.StringUtils;
  18. import org.springframework.beans.BeanUtils;
  19. import org.springframework.beans.factory.annotation.Autowired;
  20. import org.springframework.context.annotation.Lazy;
  21. import org.springframework.stereotype.Service;
  22. import org.springframework.transaction.annotation.Propagation;
  23. import org.springframework.transaction.annotation.Transactional;
  24. import org.springframework.util.CollectionUtils;
  25. import java.io.IOException;
  26. import java.text.SimpleDateFormat;
  27. import java.util.*;
  28. @Service
  29. @RequiredArgsConstructor(onConstructor_ = {@Lazy})
  30. public class EsService {
  31. public final List<String> dateList = Arrays.asList("AD", "PD", "GD", "EXD", "PAD", "PED", "PPD", "EPD");
  32. private final ElasticsearchClient client;
  33. @Autowired
  34. private PatentStarApiService patentStarApiService;
  35. /**
  36. * @param patent
  37. * @throws Exception
  38. */
  39. public String addPatent(Patent patent) throws Exception {
  40. IndexResponse indexResponse = client.index(i -> i
  41. .index("wxpatent")
  42. //传入user对象
  43. .document(patent)
  44. );
  45. return indexResponse.id();
  46. }
  47. public String addPatentVector(PatentVector patentVector) throws Exception {
  48. IndexResponse indexResponse = client.index(i -> i
  49. .index("patent_vector")
  50. //传入user对象
  51. .document(patentVector)
  52. );
  53. return indexResponse.id();
  54. }
  55. /**
  56. * 根据专利号获取专利id
  57. *
  58. * @param patentNo
  59. * @return
  60. * @throws Exception
  61. */
  62. public PatentWithIdVO getIdByPatentNo(String patentNo) throws Exception {
  63. SearchRequest.Builder builder = new SearchRequest.Builder();
  64. //设置查询索引
  65. builder.index("wxpatent");
  66. PatentWithIdVO patentWithIdVO = null;
  67. String id = null;
  68. Query q1 = QueryBuilders.term(t -> t.field("app_no.keyword").value(patentNo));
  69. //公开号
  70. Query q2 = QueryBuilders.term(t -> t.field("public_no.keyword").value(patentNo));
  71. //授权号
  72. Query q3 = QueryBuilders.term(t -> t.field("grant_no.keyword").value(patentNo));
  73. Query query = QueryBuilders.bool(i -> i.should(q1, q2, q3));
  74. builder.query(query);
  75. SearchResponse<Patent> response = client.search(builder.build(), Patent.class);
  76. List<Hit<Patent>> hits = response.hits().hits();
  77. if (hits != null && hits.size() > 0) {
  78. id = hits.get(0).id();
  79. Patent patent = hits.get(0).source();
  80. patentWithIdVO = new PatentWithIdVO();
  81. patentWithIdVO.setPatent(patent);
  82. patentWithIdVO.setId(id);
  83. }
  84. return patentWithIdVO;
  85. }
  86. //拼接专题库id或其他id条件
  87. public String appendIdsCondition(String searchCondition,Integer taskId,String productFrom,Integer projectId,Integer productId) {
  88. String condition = "";
  89. if (taskId != null) {
  90. if (searchCondition != null && !"".equals(searchCondition.trim())) {
  91. searchCondition = "taskId = " + taskId + " AND " + searchCondition;
  92. } else {
  93. searchCondition = "taskId = " + taskId;
  94. }
  95. } else {
  96. if (StringUtils.isNotEmpty(productFrom)) {
  97. if (productId != null) {
  98. if (searchCondition != null && !"".equals(searchCondition.trim())) {
  99. searchCondition = "productId = " + productId + " AND " + searchCondition;
  100. } else {
  101. searchCondition = "productId = " + productId;
  102. }
  103. }
  104. } else {
  105. if (projectId != null) {
  106. if (searchCondition != null && !"".equals(searchCondition.trim())) {
  107. searchCondition = "projectId = " + projectId + " AND " + searchCondition;
  108. } else {
  109. searchCondition = "projectId = " + projectId;
  110. }
  111. }
  112. }
  113. }
  114. condition = searchCondition;
  115. return condition;
  116. }
  117. //拼接专利号
  118. public String appendPatentNo(List<String> nos) {
  119. String str = "NO = ";
  120. if (nos.size() > 1) {
  121. str = str + "(";
  122. for (int i = 0; i < nos.size(); i++) {
  123. String s = nos.get(i);
  124. if (i != nos.size() - 1) {
  125. str = str + s + " " + "OR" + " ";
  126. } else {
  127. str = str + s + ")";
  128. }
  129. }
  130. } else {
  131. for (String no : nos) {
  132. str = str + no;
  133. }
  134. }
  135. return str;
  136. }
  137. //更新patent
  138. public Integer updatePatent(Patent patent, String id) {
  139. UpdateRequest<Patent, Patent> req;
  140. req = UpdateRequest.of(
  141. b -> b.index("wxpatent").id(id)
  142. .doc(patent)
  143. );
  144. try {
  145. client.update(req, Patent.class);
  146. return 1;
  147. } catch (IOException e) {
  148. return -1;
  149. }
  150. }
  151. //更新patent
  152. public Integer updatePatentShouldWait(Patent patent, String id) {
  153. UpdateRequest<Patent, Patent> req;
  154. req = UpdateRequest.of(
  155. b -> b.index("patent").id(id)
  156. .doc(patent).refresh(Refresh.True).waitForActiveShards(WaitForActiveShards.of(i -> i.count(1)))
  157. );
  158. try {
  159. client.update(req, Patent.class);
  160. return 1;
  161. } catch (IOException e) {
  162. return -1;
  163. }
  164. }
  165. }