EsPatentService.java 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571
  1. package com.example.xiaoshiweixinback.service.importPatent;
  2. import co.elastic.clients.elasticsearch.ElasticsearchClient;
  3. import co.elastic.clients.elasticsearch._types.InlineScript;
  4. import co.elastic.clients.elasticsearch._types.Script;
  5. import co.elastic.clients.elasticsearch._types.SortOptions;
  6. import co.elastic.clients.elasticsearch._types.SortOrder;
  7. import co.elastic.clients.elasticsearch._types.aggregations.*;
  8. import co.elastic.clients.elasticsearch._types.query_dsl.Query;
  9. import co.elastic.clients.elasticsearch._types.query_dsl.QueryBuilders;
  10. import co.elastic.clients.elasticsearch.core.SearchRequest;
  11. import co.elastic.clients.elasticsearch.core.SearchResponse;
  12. import co.elastic.clients.elasticsearch.core.search.FieldCollapse;
  13. import co.elastic.clients.elasticsearch.core.search.Hit;
  14. import co.elastic.clients.json.JsonData;
  15. import com.alibaba.fastjson.JSON;
  16. import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
  17. import com.example.xiaoshiweixinback.business.common.base.Records;
  18. import com.example.xiaoshiweixinback.business.exception.BusinessException;
  19. import com.example.xiaoshiweixinback.business.exception.ExceptionEnum;
  20. import com.example.xiaoshiweixinback.business.utils.BeanUtil;
  21. import com.example.xiaoshiweixinback.business.utils.CacheUtil;
  22. import com.example.xiaoshiweixinback.business.utils.LoginUtils;
  23. import com.example.xiaoshiweixinback.business.utils.ToolUtil;
  24. import com.example.xiaoshiweixinback.business.utils.parseQueryToTree.expressManager;
  25. import com.example.xiaoshiweixinback.business.utils.parseQueryToTree.operateNode;
  26. import com.example.xiaoshiweixinback.business.utils.parseQueryToTree.treeNode;
  27. import com.example.xiaoshiweixinback.domain.AssoPersonProduct;
  28. import com.example.xiaoshiweixinback.domain.es.*;
  29. import com.example.xiaoshiweixinback.entity.dto.esPicture.EsPictureNoDTO;
  30. import com.example.xiaoshiweixinback.entity.dto.patent.*;
  31. import com.example.xiaoshiweixinback.entity.patent.MapRequest;
  32. import com.example.xiaoshiweixinback.entity.patent.QueryRequest;
  33. import com.example.xiaoshiweixinback.entity.patent.StringRequest;
  34. import com.example.xiaoshiweixinback.entity.vo.PersonnelVO;
  35. import com.example.xiaoshiweixinback.entity.vo.esPicture.EsPatentVectorVo;
  36. import com.example.xiaoshiweixinback.entity.vo.esPicture.EsPictureNoVo;
  37. import com.example.xiaoshiweixinback.mapper.AssoPersonProductMapper;
  38. import com.example.xiaoshiweixinback.service.common.FileManagerService;
  39. import com.example.xiaoshiweixinback.service.common.GetVectorService;
  40. import lombok.RequiredArgsConstructor;
  41. import org.apache.commons.lang3.ObjectUtils;
  42. import org.apache.commons.lang3.StringUtils;
  43. import org.springframework.beans.BeanUtils;
  44. import org.springframework.beans.factory.annotation.Autowired;
  45. import org.springframework.beans.factory.annotation.Value;
  46. import org.springframework.context.annotation.Lazy;
  47. import org.springframework.stereotype.Service;
  48. import org.springframework.util.CollectionUtils;
  49. import java.io.File;
  50. import java.io.IOException;
  51. import java.util.*;
  52. import java.util.stream.Collectors;
  53. @Service
  54. @RequiredArgsConstructor(onConstructor_ = {@Lazy})
  55. public class EsPatentService {
  56. private final ElasticsearchClient client;
  57. @Autowired
  58. private EsService esService;
  59. @Autowired
  60. private CacheUtil cacheUtils;
  61. @Autowired
  62. private AssoPersonProductMapper assoPersonProductMapper;
  63. @Autowired
  64. private EsDenseVectorService esDenseVectorService;
  65. @Autowired
  66. private FileManagerService fileManagerService;
  67. @Autowired
  68. private GetVectorService getVectorService;
  69. @Autowired
  70. private FormatQueryService formatQueryService;
  71. @Value("${ES.patentVector}")
  72. private String patentVectorName;
  73. @Value("${ES.patent}")
  74. private String patentMapName;
  75. /**
  76. * 收藏专利
  77. *
  78. * @param patentDTO
  79. * @return
  80. */
  81. public boolean collectPatent(CollectPatentDTO patentDTO) throws IOException {
  82. boolean flag = false;
  83. List<String> patentNos = patentDTO.getPatentNos();
  84. Integer productId = patentDTO.getProductId();
  85. //获取用户
  86. PersonnelVO personnelVO = cacheUtils.getLoginUser(LoginUtils.getToken());
  87. if (ToolUtil.isEmpty(personnelVO)) {
  88. throw new BusinessException(ExceptionEnum.THE_GET_INFORMATION_TOKEN_INVALID);
  89. }
  90. String uuid = personnelVO.getUuid();
  91. //获取人员产品关联表id
  92. AssoPersonProduct assoPersonProduct = assoPersonProductMapper.selectOne(new LambdaQueryWrapper<AssoPersonProduct>()
  93. .eq(AssoPersonProduct::getProductId, productId)
  94. .eq(AssoPersonProduct::getPersonUuid, uuid));
  95. Integer assoPersonProductId = assoPersonProduct.getId();
  96. //判断该专利是否已经被收藏
  97. List<String> patentNoList = this.selectPatentByProductId(assoPersonProductId);
  98. patentNos.removeAll(patentNoList);
  99. //根据专利号获取专利id
  100. List<String> list = new ArrayList<>();
  101. if (!CollectionUtils.isEmpty(patentNos)) {
  102. for (String patentNo : patentNos) {
  103. String patentId = this.selectPatentByPatentNo(patentNo);
  104. Patent patent = new Patent();
  105. patent.setProductId(assoPersonProductId);
  106. PatentJoin patentJoin = new PatentJoin();
  107. patentJoin.setParent(patentId);
  108. patentJoin.setName("product");
  109. patent.setPatentJoin(patentJoin);
  110. try {
  111. String id = esService.addChildPatent(patent, patentId);
  112. list.add(id);
  113. } catch (Exception e) {
  114. }
  115. }
  116. }
  117. if (ToolUtil.equals(list.size(), patentNos.size())) {
  118. flag = true;
  119. }
  120. return flag;
  121. }
  122. /**
  123. * 查询收藏专利
  124. *
  125. * @param dto
  126. * @return
  127. * @throws IOException
  128. */
  129. public Records selectCollectPatent(SelectCollectPatentDTO dto) throws IOException {
  130. Integer assoPersonProductId = dto.getAssoPersonProductId();
  131. Long pageNum = dto.getCurrent();
  132. Long pageSize = dto.getSize();
  133. List<PatentColumnDTO> list = new ArrayList<>();
  134. SearchRequest.Builder builder = new SearchRequest.Builder();
  135. //设置查询索引
  136. builder.index(patentMapName);
  137. Query q = QueryBuilders.term(i -> i.field("product_id").value(assoPersonProductId));
  138. Query query = QueryBuilders.hasChild(i -> i.type("product").query(q));
  139. builder.query(query);
  140. //分页
  141. if (pageNum != null && pageSize != null && pageNum > 0 && pageSize > 0) {
  142. builder.from((pageNum.intValue() - 1) * pageSize.intValue()).size(pageSize.intValue());
  143. }
  144. //解除最大条数限制
  145. builder.trackTotalHits(i -> i.enabled(true));
  146. SearchResponse<Patent> response = client.search(builder.build(), Patent.class);
  147. List<Hit<Patent>> hits = response.hits().hits();
  148. long total = response.hits().total().value();
  149. for (Hit<Patent> hit : hits) {
  150. Patent patent = hit.source();
  151. PatentColumnDTO columnDTO = this.getPatentColumnDTO(patent, null, null);
  152. list.add(columnDTO);
  153. }
  154. Records records = new Records();
  155. records.setCurrent(pageNum);
  156. records.setSize(pageSize);
  157. records.setData(list);
  158. records.setTotal(total);
  159. return records;
  160. }
  161. /**
  162. * 删除收藏专利
  163. *
  164. * @param patentDTO
  165. * @return
  166. * @throws IOException
  167. */
  168. public boolean delCollectPatent(DelCollectPatentDTO patentDTO) throws IOException {
  169. Integer assoPersonProductId = patentDTO.getAssoPersonProductId();
  170. List<String> patentNos = patentDTO.getPatentNos();
  171. List<String> patentIds = new ArrayList<>();
  172. for (String patentNo : patentNos) {
  173. List<String> list = this.selectChildPatentId(patentNo, assoPersonProductId);
  174. patentIds.addAll(list);
  175. }
  176. if (!CollectionUtils.isEmpty(patentIds)) {
  177. Integer num = esService.deleteWxPatent(patentIds);
  178. if (num < 1) {
  179. throw new BusinessException(ExceptionEnum.THE_FAIL_TO_DELETE);
  180. }
  181. } else {
  182. return false;
  183. }
  184. return true;
  185. }
  186. //根据专利号、assoPersonProductId查询相关专利id
  187. public List<String> selectChildPatentId(String patentNo, Integer assoPersonProductId) throws IOException {
  188. List<String> list = new ArrayList<>();
  189. SearchRequest.Builder builder = new SearchRequest.Builder();
  190. //设置查询索引
  191. builder.index(patentMapName);
  192. Query q = QueryBuilders.term(i -> i.field("product_id").value(assoPersonProductId));
  193. Query query = QueryBuilders.term(i -> i.field("patent_no.keyword").value(patentNo));
  194. Query q1 = QueryBuilders.hasParent(i -> i.parentType("patent").query(query));
  195. Query bool = QueryBuilders.bool(i -> i.must(q, q1));
  196. builder.query(bool);
  197. //解除最大条数限制
  198. builder.trackTotalHits(i -> i.enabled(true));
  199. SearchResponse<Patent> response = client.search(builder.build(), Patent.class);
  200. List<Hit<Patent>> hits = response.hits().hits();
  201. for (Hit<Patent> hit : hits) {
  202. String id = hit.id();
  203. list.add(id);
  204. }
  205. return list;
  206. }
  207. //装载专利清单列表的数据
  208. public PatentColumnDTO getPatentColumnDTO(Patent patent, Integer projectId, String id) {
  209. PatentColumnDTO columnDTO = new PatentColumnDTO();
  210. BeanUtil.copy(patent, columnDTO);
  211. if (ToolUtil.isNotEmpty(patent.getApplicantAddr())) {
  212. PersonAddress applicantAddr = patent.getApplicantAddr();
  213. columnDTO.setAppAddress(applicantAddr.getAddress());
  214. columnDTO.setApplicantCountry(applicantAddr.getCountry());
  215. columnDTO.setAppProvince(applicantAddr.getProvince());
  216. columnDTO.setAppCity(applicantAddr.getCity());
  217. columnDTO.setAppDistrict(applicantAddr.getDistrict());
  218. }
  219. if (ToolUtil.isNotEmpty(patent.getRightHolderAddr())) {
  220. PersonAddress rightAddr = patent.getRightHolderAddr();
  221. columnDTO.setRightAddress(rightAddr.getAddress());
  222. columnDTO.setRightCountry(rightAddr.getCountry());
  223. columnDTO.setRightProvince(rightAddr.getProvince());
  224. columnDTO.setRightCity(rightAddr.getCity());
  225. columnDTO.setRightDistrict(rightAddr.getDistrict());
  226. }
  227. columnDTO.setApplicant(this.loadName(patent.getApplicant()));
  228. columnDTO.setRightHolder(this.loadName(patent.getRightHolder()));
  229. columnDTO.setInventor(this.loadName(patent.getInventor()));
  230. return columnDTO;
  231. }
  232. //通用返回申请人、权利人、发明人
  233. public List<String> loadName(List<PatentPerson> list) {
  234. List<String> collect = new ArrayList<>();
  235. if (!CollectionUtils.isEmpty(list)) {
  236. collect = list.stream().filter(i -> StringUtils.isNotEmpty(i.getName())).map(PatentPerson::getName).collect(Collectors.toList());
  237. }
  238. return collect;
  239. }
  240. /**
  241. * 根据专利号获取专利id
  242. *
  243. * @param patentNo
  244. * @return
  245. * @throws IOException
  246. */
  247. public String selectPatentByPatentNo(String patentNo) throws IOException {
  248. String id = "";
  249. SearchRequest.Builder builder = new SearchRequest.Builder();
  250. //设置查询索引
  251. builder.index(patentMapName);
  252. Query q = QueryBuilders.term(i -> i.field("patent_no.keyword").value(patentNo));
  253. builder.query(q);
  254. builder.size(10);
  255. SearchResponse<Patent> response = client.search(builder.build(), Patent.class);
  256. List<Hit<Patent>> hits = response.hits().hits();
  257. for (Hit<Patent> hit : hits) {
  258. id = hit.id();
  259. }
  260. return id;
  261. }
  262. /**
  263. * 根据专利号获取专利id
  264. *
  265. * @param patentNo
  266. * @return
  267. * @throws IOException
  268. */
  269. public PatentColumnDTO selectPatentInfoByPatentNo(String patentNo) throws IOException {
  270. PatentColumnDTO columnDTO = new PatentColumnDTO();
  271. SearchRequest.Builder builder = new SearchRequest.Builder();
  272. //设置查询索引
  273. builder.index(patentMapName);
  274. Query q = QueryBuilders.term(i -> i.field("patent_no.keyword").value(patentNo));
  275. builder.query(q);
  276. builder.size(10);
  277. SearchResponse<Patent> response = client.search(builder.build(), Patent.class);
  278. List<Hit<Patent>> hits = response.hits().hits();
  279. for (Hit<Patent> hit : hits) {
  280. Patent patent = hit.source();
  281. columnDTO = this.getPatentColumnDTO(patent, null, null);
  282. }
  283. return columnDTO;
  284. }
  285. /**
  286. * 判断该专利是否已经被收藏
  287. *
  288. * @param personProductId
  289. * @return
  290. * @throws IOException
  291. */
  292. public List<String> selectPatentByProductId(Integer personProductId) throws IOException {
  293. List<String> list = new ArrayList<>();
  294. SearchRequest.Builder builder = new SearchRequest.Builder();
  295. //设置查询索引
  296. builder.index(patentMapName);
  297. Query q = QueryBuilders.term(i -> i.field("product_id").value(personProductId));
  298. Query query = QueryBuilders.hasChild(i -> i.type("product").query(q));
  299. builder.query(query);
  300. builder.size(10);
  301. SearchResponse<Patent> response = client.search(builder.build(), Patent.class);
  302. List<Hit<Patent>> hits = response.hits().hits();
  303. for (Hit<Patent> hit : hits) {
  304. Patent patent = hit.source();
  305. list.add(patent.getPatentNo());
  306. }
  307. return list;
  308. }
  309. //-------------------查询相关专利信息-----------
  310. /**
  311. * 查询专利(用于导出专利数据)
  312. *
  313. * @param patentDTO
  314. * @param file
  315. * @return
  316. * @throws Exception
  317. */
  318. public List<PatentColumnDTO> selectPatent(SelectPatentDTO patentDTO, File file) throws Exception {
  319. List<PatentColumnDTO> columnDTOS = new ArrayList<>();
  320. if (!CollectionUtils.isEmpty(patentDTO.getAppNos())) {
  321. for (String appNo : patentDTO.getAppNos()) {
  322. //获取专利的多张摘要附图
  323. EsPictureNoDTO esPictureNoDTO = new EsPictureNoDTO();
  324. esPictureNoDTO.setAppNo(appNo);
  325. List<EsPictureNoVo> noVos = esDenseVectorService.getPictureByNo(esPictureNoDTO);
  326. List<String> guids = noVos.stream().map(EsPictureNoVo::getGuid).collect(Collectors.toList());
  327. //根据专利申请号appNo获取专利信息
  328. PatentColumnDTO columnDTO = this.selectPatentByAppNo(appNo);
  329. columnDTO.setPictureGuids(guids);
  330. columnDTOS.add(columnDTO);
  331. }
  332. } else {
  333. //根据条件获取图片专利里的数据信息,如appNo
  334. List<EsPatentVectorVo> vectorVos = this.selectPicturePatent(patentDTO, file);
  335. if (!CollectionUtils.isEmpty(vectorVos)) {
  336. for (EsPatentVectorVo vectorVo : vectorVos) {
  337. String appNo = vectorVo.getAppNo();
  338. //获取专利的多张摘要附图
  339. EsPictureNoDTO esPictureNoDTO = new EsPictureNoDTO();
  340. esPictureNoDTO.setAppNo(appNo);
  341. List<EsPictureNoVo> noVos = esDenseVectorService.getPictureByNo(esPictureNoDTO);
  342. List<String> guids = noVos.stream().map(EsPictureNoVo::getGuid).collect(Collectors.toList());
  343. //根据专利申请号appNo获取专利信息
  344. PatentColumnDTO columnDTO = this.selectPatentByAppNo(appNo);
  345. columnDTO.setPictureGuids(guids);
  346. columnDTOS.add(columnDTO);
  347. }
  348. }
  349. }
  350. return columnDTOS;
  351. }
  352. /**
  353. * 获取图片专利里的信息
  354. *
  355. * @param patentDTO
  356. * @param file
  357. * @return
  358. * @throws Exception
  359. */
  360. public List<EsPatentVectorVo> selectPicturePatent(SelectPatentDTO patentDTO, File file) throws Exception {
  361. Boolean ifAllExport = patentDTO.getIfAllExport();
  362. Integer limitNum = patentDTO.getLimitNum();
  363. SearchRequest.Builder builder = new SearchRequest.Builder();
  364. //设置查询索引
  365. builder.index(patentMapName);
  366. Query q = null;
  367. String condition = esDenseVectorService.appendCondition(patentDTO.getProductId(), patentDTO.getKey(), patentDTO.getAppCountry(), patentDTO.getCompanyName());
  368. if (StringUtils.isNotEmpty(condition)) {
  369. //1. 解析检索条件
  370. treeNode tree = expressManager.getInstance().Parse(condition, false);
  371. //2. 从es中检索数据
  372. q = formatQueryService.EsQueryToQuery((operateNode) tree, "patentVector", null);
  373. }
  374. //获取图片向量
  375. List<Float> imageList = new ArrayList<>();
  376. List<String> stringList = new ArrayList<>();
  377. if (file != null && file.exists() && file.length() != 0) {
  378. stringList = getVectorService.getVectorByFile(file);
  379. } else if (StringUtils.isNotEmpty(patentDTO.getDescription())) {
  380. stringList = getVectorService.getVectorByText(patentDTO.getDescription());
  381. }
  382. stringList.forEach(item -> {
  383. Float a = Float.parseFloat(item);
  384. imageList.add(a);
  385. });
  386. if (!CollectionUtils.isEmpty(imageList)) {
  387. String source = "cosineSimilarity(params.queryVector, 'my_vector') + 1.0";
  388. InlineScript inlineScript = InlineScript.of(i -> i.lang("painless").params("queryVector", JsonData.of(imageList)).source(source));
  389. Script script = Script.of(i -> i.inline(inlineScript));
  390. Query query = null;
  391. if (q != null) {
  392. Query finalQ = q;
  393. query = QueryBuilders.scriptScore(i -> i.script(script)
  394. .query(finalQ));
  395. } else {
  396. query = QueryBuilders.scriptScore(i -> i.script(script)
  397. .query(org.springframework.data.elasticsearch.client.elc.QueryBuilders.matchAllQueryAsQuery()));
  398. }
  399. builder.query(query);
  400. } else {
  401. builder.query(q);
  402. }
  403. //根据申请号去重
  404. FieldCollapse collapse = FieldCollapse.of(i -> i.field("app_no"));
  405. builder.collapse(collapse);
  406. //分页
  407. if (Boolean.TRUE.equals(ifAllExport)) {
  408. builder.from(0).size(999999);
  409. } else {
  410. builder.size(limitNum);
  411. }
  412. //解除最大条数限制
  413. builder.trackTotalHits(i -> i.enabled(true));
  414. SearchResponse<PatentVector> response = client.search(builder.build(), PatentVector.class);
  415. List<Hit<PatentVector>> hits = response.hits().hits();
  416. List<EsPatentVectorVo> vectorVos = new ArrayList<>();
  417. Double fixedScore = 1.7d;
  418. for (Hit<PatentVector> hit : hits) {
  419. Double score = hit.score();
  420. if (score > fixedScore) {
  421. PatentVector vector = hit.source();
  422. EsPatentVectorVo vectorVo = new EsPatentVectorVo();
  423. BeanUtil.copy(vector, vectorVo);
  424. vectorVos.add(vectorVo);
  425. }
  426. }
  427. return vectorVos;
  428. }
  429. /**
  430. * 根据申请号查询专利信息
  431. *
  432. * @param appNo
  433. * @return
  434. * @throws IOException
  435. */
  436. public PatentColumnDTO selectPatentByAppNo(String appNo) throws IOException {
  437. PatentColumnDTO columnDTO = new PatentColumnDTO();
  438. SearchRequest.Builder builder = new SearchRequest.Builder();
  439. //设置查询索引
  440. builder.index(patentMapName);
  441. Query q = QueryBuilders.term(i -> i.field("app_no.keyword").value(appNo));
  442. builder.query(q);
  443. builder.size(10);
  444. SearchResponse<Patent> response = client.search(builder.build(), Patent.class);
  445. List<Hit<Patent>> hits = response.hits().hits();
  446. for (Hit<Patent> hit : hits) {
  447. Patent patent = hit.source();
  448. columnDTO = this.getPatentColumnDTO(patent, null, null);
  449. }
  450. return columnDTO;
  451. }
  452. /**
  453. * Es检索
  454. *
  455. * @param queryRequest 检索条件
  456. * @return
  457. */
  458. public PatentDTO esSearch(QueryRequest queryRequest) throws Exception {
  459. PatentDTO dto = new PatentDTO();
  460. String searchCondition = "";
  461. Integer projectId = queryRequest.getProjectId();
  462. Long current = queryRequest.getCurrent();
  463. Long size = queryRequest.getSize();
  464. //判断表达式
  465. if (queryRequest instanceof StringRequest) {
  466. searchCondition = ((StringRequest) queryRequest).getSearchQuery();
  467. } else if (queryRequest instanceof MapRequest) {
  468. Map<String, Object> map = ((MapRequest) queryRequest).getSearchQuery();
  469. StringBuilder stringBuilder = new StringBuilder();
  470. for (String key : map.keySet()) {
  471. Object value = map.get(key);
  472. if (!"".contentEquals(stringBuilder)) {
  473. stringBuilder = stringBuilder.append(" AND ").append(key).append("=").append(value);
  474. } else {
  475. stringBuilder = stringBuilder.append(key).append("=").append(value);
  476. }
  477. }
  478. searchCondition = stringBuilder.toString();
  479. }
  480. String condition = searchCondition;
  481. SearchRequest.Builder builder = new SearchRequest.Builder();
  482. //设置查询索引
  483. builder.index("wxpatent");
  484. //1. 解析检索条件
  485. treeNode tree = expressManager.getInstance().Parse(condition, false);
  486. //3. 从es中检索数据
  487. Query q = formatQueryService.EsQueryToQuery((operateNode) tree, "patent", projectId);
  488. builder.query(q);
  489. //排序
  490. List<SortOptions> optionsList = new ArrayList<>();
  491. SortOptions sortOptions = SortOptions.of(i -> i.field(j -> j.field("public_date").order(SortOrder.Desc).missing(-1)));
  492. optionsList.add(sortOptions);
  493. builder.sort(optionsList);
  494. // //分页
  495. // if (current != null && size != null && current > 0 && size > 0) {
  496. // builder.from((current.intValue() - 1) * size.intValue()).size(size.intValue());
  497. // } else {
  498. // builder.from(1).size(99999);
  499. // }
  500. //解除最大条数限制
  501. builder.trackTotalHits(i -> i.enabled(true));
  502. SearchResponse<Patent> response = client.search(builder.build(), Patent.class);
  503. List<PatentColumnDTO> list = new ArrayList<>();
  504. long total = 0L;
  505. List<Hit<Patent>> hits = response.hits().hits();
  506. total = response.hits().total().value();
  507. for (Hit<Patent> hit : hits) {
  508. String id = hit.id();
  509. Patent esMess = hit.source();
  510. PatentColumnDTO columnDTO = this.getPatentColumnDTO(esMess, projectId, id);
  511. list.add(columnDTO);
  512. }
  513. dto.setTotal(total);
  514. dto.setPatents(list);
  515. dto.setPageNum(current);
  516. dto.setPageSize(size);
  517. return dto;
  518. }
  519. }