123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184 |
- package cn.cslg.pas.service.business.es;
- import cn.cslg.pas.common.vo.PatentWithIdVO;
- import cn.cslg.pas.domain.es.Patent;
- import cn.cslg.pas.service.query.Query;
- 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._types.query_dsl.QueryBuilders;
- import co.elastic.clients.elasticsearch.core.*;
- import co.elastic.clients.elasticsearch.core.search.Hit;
- 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.List;
- @Service
- @RequiredArgsConstructor(onConstructor_ = {@Lazy})
- public class EsService {
- private final ElasticsearchClient client;
- /**
- * @param patent
- * @throws Exception
- */
- public String addPatent(Patent patent) throws Exception {
- IndexResponse indexResponse = client.index(i -> i
- .index("patent")
- //传入user对象
- .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
- *
- * @param patentNo
- * @return
- * @throws Exception
- */
- public PatentWithIdVO getIdByPatentNo(String patentNo) throws Exception {
- PatentWithIdVO patentWithIdVO = null;
- String id = null;
- SearchResponse<Patent> response = client.search(
- s -> s.index("patent")
- .query(
- q -> q.match(
- t -> t.field("patent_no")
- .query(patentNo)
- )
- )
- , 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;
- }
- /**
- * @param key
- * @param page
- * @param limit
- * @return
- * @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")));
- builder.query(q -> q.bool(boolQuery.build()));
- //分页
- if (page != null && limit != null) {
- builder.from(page).size(limit);
- }
- //排序
- // builder.sort(sortOptionsBuilder -> sortOptionsBuilder
- // .field(fieldSortBuilder -> fieldSortBuilder
- // .field("createTime").order(SortOrder.Desc)));
- 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;
- }
- public Integer updateDocument(Patent patent, String id) {
- UpdateRequest<Patent, Patent> req;
- req = UpdateRequest.of(
- b -> b.index("patent").id(id)
- .doc(patent)
- );
- try {
- 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;
- }
- }
|