123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192 |
- package com.example.xiaoshiweixinback.service.importPatent;
- import co.elastic.clients.elasticsearch.ElasticsearchClient;
- import co.elastic.clients.elasticsearch._types.*;
- import co.elastic.clients.elasticsearch._types.aggregations.*;
- import co.elastic.clients.elasticsearch._types.query_dsl.HasChildQuery;
- import co.elastic.clients.elasticsearch._types.query_dsl.Query;
- import co.elastic.clients.elasticsearch._types.query_dsl.QueryBuilders;
- import co.elastic.clients.elasticsearch.core.*;
- import co.elastic.clients.elasticsearch.core.search.Hit;
- import co.elastic.clients.json.JsonData;
- import com.alibaba.fastjson.JSON;
- import com.example.xiaoshiweixinback.domain.es.Patent;
- import com.example.xiaoshiweixinback.domain.es.PatentVector;
- import com.example.xiaoshiweixinback.entity.vo.patent.PatentWithIdVO;
- import lombok.RequiredArgsConstructor;
- import org.apache.commons.lang3.ObjectUtils;
- import org.apache.commons.lang3.StringUtils;
- import org.springframework.beans.BeanUtils;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.context.annotation.Lazy;
- import org.springframework.stereotype.Service;
- import org.springframework.transaction.annotation.Propagation;
- import org.springframework.transaction.annotation.Transactional;
- import org.springframework.util.CollectionUtils;
- import java.io.IOException;
- import java.text.SimpleDateFormat;
- import java.util.*;
- @Service
- @RequiredArgsConstructor(onConstructor_ = {@Lazy})
- public class EsService {
- public final List<String> dateList = Arrays.asList("AD", "PD", "GD", "EXD", "PAD", "PED", "PPD", "EPD");
- private final ElasticsearchClient client;
- @Autowired
- private PatentStarApiService patentStarApiService;
- /**
- * @param patent
- * @throws Exception
- */
- public String addPatent(Patent patent) throws Exception {
- IndexResponse indexResponse = client.index(i -> i
- .index("wxpatent")
- //传入user对象
- .document(patent)
- );
- return indexResponse.id();
- }
- public String addPatentVector(PatentVector patentVector) throws Exception {
- IndexResponse indexResponse = client.index(i -> i
- .index("patent_vector")
- //传入user对象
- .document(patentVector)
- );
- return indexResponse.id();
- }
- /**
- * 根据专利号获取专利id
- *
- * @param patentNo
- * @return
- * @throws Exception
- */
- public PatentWithIdVO getIdByPatentNo(String patentNo) throws Exception {
- SearchRequest.Builder builder = new SearchRequest.Builder();
- //设置查询索引
- builder.index("wxpatent");
- PatentWithIdVO patentWithIdVO = null;
- String id = null;
- Query q1 = QueryBuilders.term(t -> t.field("app_no.keyword").value(patentNo));
- //公开号
- Query q2 = QueryBuilders.term(t -> t.field("public_no.keyword").value(patentNo));
- //授权号
- Query q3 = QueryBuilders.term(t -> t.field("grant_no.keyword").value(patentNo));
- Query query = QueryBuilders.bool(i -> i.should(q1, q2, q3));
- builder.query(query);
- SearchResponse<Patent> response = client.search(builder.build(), Patent.class);
- List<Hit<Patent>> hits = response.hits().hits();
- if (hits != null && hits.size() > 0) {
- id = hits.get(0).id();
- Patent patent = hits.get(0).source();
- patentWithIdVO = new PatentWithIdVO();
- patentWithIdVO.setPatent(patent);
- patentWithIdVO.setId(id);
- }
- return patentWithIdVO;
- }
- //拼接专题库id或其他id条件
- public String appendIdsCondition(String searchCondition,Integer taskId,String productFrom,Integer projectId,Integer productId) {
- String condition = "";
- if (taskId != null) {
- if (searchCondition != null && !"".equals(searchCondition.trim())) {
- searchCondition = "taskId = " + taskId + " AND " + searchCondition;
- } else {
- searchCondition = "taskId = " + taskId;
- }
- } else {
- if (StringUtils.isNotEmpty(productFrom)) {
- if (productId != null) {
- if (searchCondition != null && !"".equals(searchCondition.trim())) {
- searchCondition = "productId = " + productId + " AND " + searchCondition;
- } else {
- searchCondition = "productId = " + productId;
- }
- }
- } else {
- if (projectId != null) {
- if (searchCondition != null && !"".equals(searchCondition.trim())) {
- searchCondition = "projectId = " + projectId + " AND " + searchCondition;
- } else {
- searchCondition = "projectId = " + projectId;
- }
- }
- }
- }
- condition = searchCondition;
- return condition;
- }
- //拼接专利号
- public String appendPatentNo(List<String> nos) {
- String str = "NO = ";
- if (nos.size() > 1) {
- str = str + "(";
- for (int i = 0; i < nos.size(); i++) {
- String s = nos.get(i);
- if (i != nos.size() - 1) {
- str = str + s + " " + "OR" + " ";
- } else {
- str = str + s + ")";
- }
- }
- } else {
- for (String no : nos) {
- str = str + no;
- }
- }
- return str;
- }
- //更新patent
- public Integer updatePatent(Patent patent, String id) {
- UpdateRequest<Patent, Patent> req;
- req = UpdateRequest.of(
- b -> b.index("wxpatent").id(id)
- .doc(patent)
- );
- try {
- client.update(req, Patent.class);
- return 1;
- } catch (IOException e) {
- return -1;
- }
- }
- //更新patent
- public Integer updatePatentShouldWait(Patent patent, String id) {
- UpdateRequest<Patent, Patent> req;
- req = UpdateRequest.of(
- b -> b.index("patent").id(id)
- .doc(patent).refresh(Refresh.True).waitForActiveShards(WaitForActiveShards.of(i -> i.count(1)))
- );
- try {
- client.update(req, Patent.class);
- return 1;
- } catch (IOException e) {
- return -1;
- }
- }
- }
|