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 response = client.search( s -> s.index("patent") .query( q -> q.match( t -> t.field("patent_no") .query(patentNo) ) ) , 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; } /** * @param key * @param page * @param limit * @return * @throws IOException */ public List 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 response = client.search(builder.build(), Patent.class); List list = new ArrayList<>(); List> hits = response.hits().hits(); for (Hit hit : hits) { Patent esMess = hit.source(); list.add(esMess); } return list; } public Integer updateDocument(Patent patent, String id) { UpdateRequest 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 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 response = client.search(builder.build(), Patent.class); List list = new ArrayList<>(); List> hits = response.hits().hits(); for (Hit hit : hits) { Patent esMess = hit.source(); list.add(esMess); } return list; } }