EsService.java 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. package cn.cslg.pas.service.business.es;
  2. import cn.cslg.pas.common.vo.PatentWithIdVO;
  3. import cn.cslg.pas.domain.es.Patent;
  4. import cn.cslg.pas.service.query.Query;
  5. import co.elastic.clients.elasticsearch.ElasticsearchClient;
  6. import co.elastic.clients.elasticsearch._types.SortOrder;
  7. import co.elastic.clients.elasticsearch._types.query_dsl.BoolQuery;
  8. import co.elastic.clients.elasticsearch._types.query_dsl.HasChildQuery;
  9. import co.elastic.clients.elasticsearch._types.query_dsl.QueryBuilders;
  10. import co.elastic.clients.elasticsearch.core.*;
  11. import co.elastic.clients.elasticsearch.core.search.Hit;
  12. import lombok.RequiredArgsConstructor;
  13. import org.springframework.context.annotation.Lazy;
  14. import org.springframework.stereotype.Service;
  15. import java.io.IOException;
  16. import java.util.ArrayList;
  17. import java.util.List;
  18. @Service
  19. @RequiredArgsConstructor(onConstructor_ = {@Lazy})
  20. public class EsService {
  21. private final ElasticsearchClient client;
  22. /**
  23. * @param patent
  24. * @throws Exception
  25. */
  26. public String addPatent(Patent patent) throws Exception {
  27. IndexResponse indexResponse = client.index(i -> i
  28. .index("patent")
  29. //传入user对象
  30. .document(patent)
  31. );
  32. return indexResponse.id();
  33. }
  34. /**
  35. * @param patent
  36. * @throws Exception
  37. */
  38. public String addChildPatent(Patent patent, String id) throws Exception {
  39. IndexResponse indexResponse = client.index(i -> i
  40. .index("patent")
  41. .routing(id)
  42. //传入user对象
  43. .document(patent)
  44. );
  45. return indexResponse.id();
  46. }
  47. /**
  48. * 根据专利号获取专利id
  49. *
  50. * @param patentNo
  51. * @return
  52. * @throws Exception
  53. */
  54. public PatentWithIdVO getIdByPatentNo(String patentNo) throws Exception {
  55. PatentWithIdVO patentWithIdVO = null;
  56. String id = null;
  57. SearchResponse<Patent> response = client.search(
  58. s -> s.index("patent")
  59. .query(
  60. q -> q.match(
  61. t -> t.field("patent_no")
  62. .query(patentNo)
  63. )
  64. )
  65. , Patent.class
  66. );
  67. List<Hit<Patent>> hits = response.hits().hits();
  68. if (hits != null && hits.size() > 0) {
  69. id = hits.get(0).id();
  70. Patent patent = hits.get(0).source();
  71. patentWithIdVO = new PatentWithIdVO();
  72. patentWithIdVO.setPatent(patent);
  73. patentWithIdVO.setId(id);
  74. }
  75. return patentWithIdVO;
  76. }
  77. /**
  78. * @param key
  79. * @param page
  80. * @param limit
  81. * @return
  82. * @throws IOException
  83. */
  84. public List<Patent> search(String key, Integer page, Integer limit) throws IOException {
  85. SearchRequest.Builder builder = new SearchRequest.Builder();
  86. //设置查询索引
  87. builder.index("patent");
  88. //组装查询条件
  89. BoolQuery.Builder boolQuery = new BoolQuery.Builder();
  90. boolQuery.should(q -> q.matchPhrasePrefix(m -> m
  91. .query(key)
  92. //字段名
  93. .field("title")
  94. ));
  95. //多字段匹配
  96. boolQuery.should(q -> q.matchPhrasePrefix(m -> m.query(key).field("content")));
  97. builder.query(q -> q.bool(boolQuery.build()));
  98. //分页
  99. if (page != null && limit != null) {
  100. builder.from(page).size(limit);
  101. }
  102. //排序
  103. // builder.sort(sortOptionsBuilder -> sortOptionsBuilder
  104. // .field(fieldSortBuilder -> fieldSortBuilder
  105. // .field("createTime").order(SortOrder.Desc)));
  106. SearchResponse<Patent> response = client.search(builder.build(), Patent.class);
  107. List<Patent> list = new ArrayList<>();
  108. List<Hit<Patent>> hits = response.hits().hits();
  109. for (Hit<Patent> hit : hits) {
  110. Patent esMess = hit.source();
  111. list.add(esMess);
  112. }
  113. return list;
  114. }
  115. public Integer updateDocument(Patent patent, String id) {
  116. UpdateRequest<Patent, Patent> req;
  117. req = UpdateRequest.of(
  118. b -> b.index("patent").id(id)
  119. .doc(patent)
  120. );
  121. try {
  122. client.update(req, Patent.class);
  123. return 1;
  124. } catch (IOException e) {
  125. return -1;
  126. }
  127. }
  128. /**
  129. * @param key
  130. * @param page
  131. * @param limit
  132. * @return
  133. * @throws IOException
  134. */
  135. public List<Patent> searchChild(String key, Integer page, Integer limit) throws IOException {
  136. SearchRequest.Builder builder = new SearchRequest.Builder();
  137. //设置查询索引
  138. builder.index("patent");
  139. //组装查询条件
  140. HasChildQuery.Builder hasChildQuery = new HasChildQuery.Builder();
  141. hasChildQuery.type("project");
  142. hasChildQuery.query(q -> q.match(m -> m
  143. .query(key)
  144. //字段名
  145. .field("project_id")
  146. ));
  147. builder.query(q -> q.hasChild(hasChildQuery.build()));
  148. SearchResponse<Patent> response = client.search(builder.build(), Patent.class);
  149. List<Patent> list = new ArrayList<>();
  150. List<Hit<Patent>> hits = response.hits().hits();
  151. for (Hit<Patent> hit : hits) {
  152. Patent esMess = hit.source();
  153. list.add(esMess);
  154. }
  155. return list;
  156. }
  157. }