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 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 response = client.search(builder.build(), Patent.class); List> 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 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 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 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; } } }