|
@@ -0,0 +1,87 @@
|
|
|
+package cn.cslg.pas.service.business.es;
|
|
|
+
|
|
|
+import cn.cslg.pas.common.model.es.GetVectorVO;
|
|
|
+import cn.cslg.pas.domain.es.Patent;
|
|
|
+import cn.cslg.pas.domain.es.PatentJoin;
|
|
|
+import cn.cslg.pas.domain.es.Text;
|
|
|
+import cn.cslg.pas.service.common.PythonApiService;
|
|
|
+import co.elastic.clients.elasticsearch.ElasticsearchClient;
|
|
|
+import co.elastic.clients.elasticsearch._types.query_dsl.Query;
|
|
|
+import co.elastic.clients.elasticsearch._types.query_dsl.QueryBuilders;
|
|
|
+import co.elastic.clients.elasticsearch.core.SearchRequest;
|
|
|
+import co.elastic.clients.elasticsearch.core.SearchResponse;
|
|
|
+import lombok.RequiredArgsConstructor;
|
|
|
+import org.springframework.context.annotation.Bean;
|
|
|
+import org.springframework.context.annotation.Lazy;
|
|
|
+import org.springframework.scheduling.annotation.Async;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+
|
|
|
+import java.io.IOException;
|
|
|
+import java.util.List;
|
|
|
+
|
|
|
+@Service
|
|
|
+@RequiredArgsConstructor(onConstructor_ = {@Lazy})
|
|
|
+public class EsPatentVectorService {
|
|
|
+
|
|
|
+ private final ElasticsearchClient client;
|
|
|
+ private final PythonApiService pythonApiService;
|
|
|
+ private final EsService esService;
|
|
|
+
|
|
|
+ public void addPatentVector(Patent patent, String id) throws Exception {
|
|
|
+ Boolean ifHaveVector = this.searchIfHaveVector(id);
|
|
|
+ if (!ifHaveVector) {
|
|
|
+ this.addPatentVectorToEs(patent, id);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public Boolean searchIfHaveVector(String parentId) throws IOException {
|
|
|
+ boolean flag = false;
|
|
|
+ SearchRequest.Builder builder = new SearchRequest.Builder();
|
|
|
+ //设置查询索引
|
|
|
+ builder.index("patent");
|
|
|
+
|
|
|
+ Query q2 = QueryBuilders.parentId(parent -> parent.type("patent_vector").id(parentId));
|
|
|
+ Query bool = QueryBuilders.bool(i -> i.must(q2));
|
|
|
+ builder.query(bool);
|
|
|
+ SearchResponse<Patent> response = client.search(builder.build(), Patent.class);
|
|
|
+ long total = response.hits().total().value();
|
|
|
+ if (total > 0) {
|
|
|
+ flag = true;
|
|
|
+ }
|
|
|
+ return flag;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ public void addPatentVectorToEs(Patent patent, String patentId) throws Exception {
|
|
|
+
|
|
|
+ if (patent.getAbstractStr() != null && patent.getAbstractStr().size() > 0) {
|
|
|
+ Text text = patent.getAbstractStr().stream().filter(item -> item.getIfOrigin().equals(true)).findFirst().orElse(null);
|
|
|
+ if (text != null) {
|
|
|
+ String abStr = text.getTextContent();
|
|
|
+ GetVectorVO getVectorVO = new GetVectorVO();
|
|
|
+ getVectorVO.setText(abStr);
|
|
|
+ List<Float> floatList = pythonApiService.getStrVector(getVectorVO);
|
|
|
+ Patent patent1 = new Patent();
|
|
|
+ PatentJoin patentJoin = new PatentJoin();
|
|
|
+ patentJoin.setParent(patentId);
|
|
|
+ patentJoin.setName("patent_vector");
|
|
|
+ patent1.setPatentJoin(patentJoin);
|
|
|
+ patent1.setAbstractVector(floatList);
|
|
|
+ esService.addChildPatent(patent1, patentId);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ @Async("singleThreadAsyncTaskExecutor")
|
|
|
+ public void asyncAddPatentVector(Patent patent, String id) throws Exception {
|
|
|
+ this.addPatentVectorToEs(patent, id);
|
|
|
+ }
|
|
|
+
|
|
|
+ public void refreshPatentVector(Integer id) {
|
|
|
+ if (id != null) {
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ }
|
|
|
+}
|