|
@@ -1,30 +1,27 @@
|
|
|
package cn.cslg.pas.service.business.es;
|
|
|
|
|
|
import cn.cslg.pas.common.vo.PatentWithIdVO;
|
|
|
-import cn.cslg.pas.domain.School;
|
|
|
import cn.cslg.pas.domain.es.Patent;
|
|
|
import co.elastic.clients.elasticsearch.ElasticsearchClient;
|
|
|
import co.elastic.clients.elasticsearch._types.SortOrder;
|
|
|
import co.elastic.clients.elasticsearch._types.query_dsl.BoolQuery;
|
|
|
+import co.elastic.clients.elasticsearch._types.query_dsl.HasChildQuery;
|
|
|
import co.elastic.clients.elasticsearch.core.*;
|
|
|
import co.elastic.clients.elasticsearch.core.search.Hit;
|
|
|
-import co.elastic.clients.elasticsearch.core.search.TotalHits;
|
|
|
-import com.github.pagehelper.util.StringUtil;
|
|
|
import lombok.RequiredArgsConstructor;
|
|
|
import org.springframework.context.annotation.Lazy;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
import java.io.IOException;
|
|
|
import java.util.ArrayList;
|
|
|
-import java.util.HashMap;
|
|
|
import java.util.List;
|
|
|
-import java.util.Map;
|
|
|
|
|
|
|
|
|
@Service
|
|
|
@RequiredArgsConstructor(onConstructor_ = {@Lazy})
|
|
|
public class EsService {
|
|
|
private final ElasticsearchClient client;
|
|
|
+
|
|
|
/**
|
|
|
* @param patent
|
|
|
* @throws Exception
|
|
@@ -33,12 +30,26 @@ public class EsService {
|
|
|
IndexResponse indexResponse = client.index(i -> i
|
|
|
.index("patent")
|
|
|
//传入user对象
|
|
|
- .document(patent));
|
|
|
-
|
|
|
+ .document(patent)
|
|
|
+ );
|
|
|
return indexResponse.id();
|
|
|
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * @param patent
|
|
|
+ * @throws Exception
|
|
|
+ */
|
|
|
+ public String addChildPatent(Patent patent, String id) throws Exception {
|
|
|
+ IndexResponse indexResponse = client.index(i -> i
|
|
|
+ .index("patent")
|
|
|
+ .routing(id)
|
|
|
+ //传入user对象
|
|
|
+ .document(patent)
|
|
|
+ );
|
|
|
+ return indexResponse.id();
|
|
|
+
|
|
|
+ }
|
|
|
|
|
|
/**
|
|
|
* 根据专利号获取专利id
|
|
@@ -48,7 +59,7 @@ public class EsService {
|
|
|
* @throws Exception
|
|
|
*/
|
|
|
public PatentWithIdVO getIdByPatentNo(String patentNo) throws Exception {
|
|
|
- PatentWithIdVO patentWithIdVO =null;
|
|
|
+ PatentWithIdVO patentWithIdVO = null;
|
|
|
String id = null;
|
|
|
SearchResponse<Patent> response = client.search(
|
|
|
s -> s.index("patent")
|
|
@@ -58,42 +69,50 @@ public class EsService {
|
|
|
.query(patentNo)
|
|
|
)
|
|
|
)
|
|
|
- ,Patent.class
|
|
|
+ , 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();
|
|
|
+ Patent patent = hits.get(0).source();
|
|
|
+ patentWithIdVO = new PatentWithIdVO();
|
|
|
patentWithIdVO.setPatent(patent);
|
|
|
patentWithIdVO.setId(id);
|
|
|
}
|
|
|
return patentWithIdVO;
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * @param key
|
|
|
+ * @param page
|
|
|
+ * @param limit
|
|
|
+ * @return
|
|
|
+ * @throws IOException
|
|
|
+ */
|
|
|
+ public List<Patent> search(String key, Integer page, Integer limit) throws IOException {
|
|
|
|
|
|
- public List<Patent> search(String key,Integer page,Integer limit) throws IOException {
|
|
|
SearchRequest.Builder builder = new SearchRequest.Builder();
|
|
|
//设置查询索引
|
|
|
builder.index("patent");
|
|
|
//组装查询条件
|
|
|
BoolQuery.Builder boolQuery = new BoolQuery.Builder();
|
|
|
|
|
|
- boolQuery.should(q -> q.matchPhrasePrefix(m -> m
|
|
|
- .query(key)
|
|
|
- //字段名
|
|
|
- .field("title")
|
|
|
- ));
|
|
|
- //多字段匹配
|
|
|
- boolQuery.should(q->q.matchPhrasePrefix(m->m.query(key).field("content")));
|
|
|
+ boolQuery.should(q -> q.matchPhrasePrefix(m -> m
|
|
|
+ .query(key)
|
|
|
+ //字段名
|
|
|
+ .field("title")
|
|
|
+ ));
|
|
|
|
|
|
+ //多字段匹配
|
|
|
+ boolQuery.should(q -> q.matchPhrasePrefix(m -> m.query(key).field("content")));
|
|
|
|
|
|
builder.query(q -> q.bool(boolQuery.build()));
|
|
|
//分页
|
|
|
- if(page!=null && limit!=null){
|
|
|
+ if (page != null && limit != null) {
|
|
|
builder.from(page).size(limit);
|
|
|
}
|
|
|
+
|
|
|
//排序
|
|
|
builder.sort(sortOptionsBuilder -> sortOptionsBuilder
|
|
|
.field(fieldSortBuilder -> fieldSortBuilder
|
|
@@ -110,19 +129,54 @@ public class EsService {
|
|
|
}
|
|
|
|
|
|
|
|
|
- public Integer updateDocument(Patent patent,String id){
|
|
|
- UpdateRequest<Patent,Patent> req;
|
|
|
+ public Integer updateDocument(Patent patent, String id) {
|
|
|
+ UpdateRequest<Patent, Patent> req;
|
|
|
req = UpdateRequest.of(
|
|
|
- b-> b.index("patent").id(id)
|
|
|
+ b -> b.index("patent").id(id)
|
|
|
.doc(patent)
|
|
|
);
|
|
|
try {
|
|
|
- client.update(req,Patent.class);
|
|
|
- return 1;
|
|
|
+ client.update(req, Patent.class);
|
|
|
+ return 1;
|
|
|
} catch (IOException e) {
|
|
|
return -1;
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @param key
|
|
|
+ * @param page
|
|
|
+ * @param limit
|
|
|
+ * @return
|
|
|
+ * @throws IOException
|
|
|
+ */
|
|
|
+ public List<Patent> searchChild(String key, Integer page, Integer limit) throws IOException {
|
|
|
+
|
|
|
+ SearchRequest.Builder builder = new SearchRequest.Builder();
|
|
|
+ //设置查询索引
|
|
|
+ builder.index("patent");
|
|
|
+ //组装查询条件
|
|
|
+ HasChildQuery.Builder hasChildQuery = new HasChildQuery.Builder();
|
|
|
+ hasChildQuery.type("project");
|
|
|
+ hasChildQuery.query(q -> q.match(m -> m
|
|
|
+ .query(key)
|
|
|
+ //字段名
|
|
|
+ .field("project_id")
|
|
|
+ ));
|
|
|
+
|
|
|
+
|
|
|
+ builder.query(q -> q.hasChild(hasChildQuery.build()));
|
|
|
+ SearchResponse<Patent> response = client.search(builder.build(), Patent.class);
|
|
|
+ List<Patent> list = new ArrayList<>();
|
|
|
+ List<Hit<Patent>> hits = response.hits().hits();
|
|
|
+ for (Hit<Patent> hit : hits) {
|
|
|
+ Patent esMess = hit.source();
|
|
|
+ list.add(esMess);
|
|
|
+ }
|
|
|
+
|
|
|
+ return list;
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
|