123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571 |
- package com.example.xiaoshiweixinback.service.importPatent;
- import co.elastic.clients.elasticsearch.ElasticsearchClient;
- import co.elastic.clients.elasticsearch._types.InlineScript;
- import co.elastic.clients.elasticsearch._types.Script;
- import co.elastic.clients.elasticsearch._types.SortOptions;
- import co.elastic.clients.elasticsearch._types.SortOrder;
- import co.elastic.clients.elasticsearch._types.aggregations.*;
- 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 co.elastic.clients.elasticsearch.core.search.FieldCollapse;
- import co.elastic.clients.elasticsearch.core.search.Hit;
- import co.elastic.clients.json.JsonData;
- import com.alibaba.fastjson.JSON;
- import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
- import com.example.xiaoshiweixinback.business.common.base.Records;
- import com.example.xiaoshiweixinback.business.exception.BusinessException;
- import com.example.xiaoshiweixinback.business.exception.ExceptionEnum;
- import com.example.xiaoshiweixinback.business.utils.BeanUtil;
- import com.example.xiaoshiweixinback.business.utils.CacheUtil;
- import com.example.xiaoshiweixinback.business.utils.LoginUtils;
- import com.example.xiaoshiweixinback.business.utils.ToolUtil;
- import com.example.xiaoshiweixinback.business.utils.parseQueryToTree.expressManager;
- import com.example.xiaoshiweixinback.business.utils.parseQueryToTree.operateNode;
- import com.example.xiaoshiweixinback.business.utils.parseQueryToTree.treeNode;
- import com.example.xiaoshiweixinback.domain.AssoPersonProduct;
- import com.example.xiaoshiweixinback.domain.es.*;
- import com.example.xiaoshiweixinback.entity.dto.esPicture.EsPictureNoDTO;
- import com.example.xiaoshiweixinback.entity.dto.patent.*;
- import com.example.xiaoshiweixinback.entity.patent.MapRequest;
- import com.example.xiaoshiweixinback.entity.patent.QueryRequest;
- import com.example.xiaoshiweixinback.entity.patent.StringRequest;
- import com.example.xiaoshiweixinback.entity.vo.PersonnelVO;
- import com.example.xiaoshiweixinback.entity.vo.esPicture.EsPatentVectorVo;
- import com.example.xiaoshiweixinback.entity.vo.esPicture.EsPictureNoVo;
- import com.example.xiaoshiweixinback.mapper.AssoPersonProductMapper;
- import com.example.xiaoshiweixinback.service.common.FileManagerService;
- import com.example.xiaoshiweixinback.service.common.GetVectorService;
- 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.beans.factory.annotation.Value;
- import org.springframework.context.annotation.Lazy;
- import org.springframework.stereotype.Service;
- import org.springframework.util.CollectionUtils;
- import java.io.File;
- import java.io.IOException;
- import java.util.*;
- import java.util.stream.Collectors;
- @Service
- @RequiredArgsConstructor(onConstructor_ = {@Lazy})
- public class EsPatentService {
- private final ElasticsearchClient client;
- @Autowired
- private EsService esService;
- @Autowired
- private CacheUtil cacheUtils;
- @Autowired
- private AssoPersonProductMapper assoPersonProductMapper;
- @Autowired
- private EsDenseVectorService esDenseVectorService;
- @Autowired
- private FileManagerService fileManagerService;
- @Autowired
- private GetVectorService getVectorService;
- @Autowired
- private FormatQueryService formatQueryService;
- @Value("${ES.patentVector}")
- private String patentVectorName;
- @Value("${ES.patent}")
- private String patentMapName;
- /**
- * 收藏专利
- *
- * @param patentDTO
- * @return
- */
- public boolean collectPatent(CollectPatentDTO patentDTO) throws IOException {
- boolean flag = false;
- List<String> patentNos = patentDTO.getPatentNos();
- Integer productId = patentDTO.getProductId();
- //获取用户
- PersonnelVO personnelVO = cacheUtils.getLoginUser(LoginUtils.getToken());
- if (ToolUtil.isEmpty(personnelVO)) {
- throw new BusinessException(ExceptionEnum.THE_GET_INFORMATION_TOKEN_INVALID);
- }
- String uuid = personnelVO.getUuid();
- //获取人员产品关联表id
- AssoPersonProduct assoPersonProduct = assoPersonProductMapper.selectOne(new LambdaQueryWrapper<AssoPersonProduct>()
- .eq(AssoPersonProduct::getProductId, productId)
- .eq(AssoPersonProduct::getPersonUuid, uuid));
- Integer assoPersonProductId = assoPersonProduct.getId();
- //判断该专利是否已经被收藏
- List<String> patentNoList = this.selectPatentByProductId(assoPersonProductId);
- patentNos.removeAll(patentNoList);
- //根据专利号获取专利id
- List<String> list = new ArrayList<>();
- if (!CollectionUtils.isEmpty(patentNos)) {
- for (String patentNo : patentNos) {
- String patentId = this.selectPatentByPatentNo(patentNo);
- Patent patent = new Patent();
- patent.setProductId(assoPersonProductId);
- PatentJoin patentJoin = new PatentJoin();
- patentJoin.setParent(patentId);
- patentJoin.setName("product");
- patent.setPatentJoin(patentJoin);
- try {
- String id = esService.addChildPatent(patent, patentId);
- list.add(id);
- } catch (Exception e) {
- }
- }
- }
- if (ToolUtil.equals(list.size(), patentNos.size())) {
- flag = true;
- }
- return flag;
- }
- /**
- * 查询收藏专利
- *
- * @param dto
- * @return
- * @throws IOException
- */
- public Records selectCollectPatent(SelectCollectPatentDTO dto) throws IOException {
- Integer assoPersonProductId = dto.getAssoPersonProductId();
- Long pageNum = dto.getCurrent();
- Long pageSize = dto.getSize();
- List<PatentColumnDTO> list = new ArrayList<>();
- SearchRequest.Builder builder = new SearchRequest.Builder();
- //设置查询索引
- builder.index(patentMapName);
- Query q = QueryBuilders.term(i -> i.field("product_id").value(assoPersonProductId));
- Query query = QueryBuilders.hasChild(i -> i.type("product").query(q));
- builder.query(query);
- //分页
- if (pageNum != null && pageSize != null && pageNum > 0 && pageSize > 0) {
- builder.from((pageNum.intValue() - 1) * pageSize.intValue()).size(pageSize.intValue());
- }
- //解除最大条数限制
- builder.trackTotalHits(i -> i.enabled(true));
- SearchResponse<Patent> response = client.search(builder.build(), Patent.class);
- List<Hit<Patent>> hits = response.hits().hits();
- long total = response.hits().total().value();
- for (Hit<Patent> hit : hits) {
- Patent patent = hit.source();
- PatentColumnDTO columnDTO = this.getPatentColumnDTO(patent, null, null);
- list.add(columnDTO);
- }
- Records records = new Records();
- records.setCurrent(pageNum);
- records.setSize(pageSize);
- records.setData(list);
- records.setTotal(total);
- return records;
- }
- /**
- * 删除收藏专利
- *
- * @param patentDTO
- * @return
- * @throws IOException
- */
- public boolean delCollectPatent(DelCollectPatentDTO patentDTO) throws IOException {
- Integer assoPersonProductId = patentDTO.getAssoPersonProductId();
- List<String> patentNos = patentDTO.getPatentNos();
- List<String> patentIds = new ArrayList<>();
- for (String patentNo : patentNos) {
- List<String> list = this.selectChildPatentId(patentNo, assoPersonProductId);
- patentIds.addAll(list);
- }
- if (!CollectionUtils.isEmpty(patentIds)) {
- Integer num = esService.deleteWxPatent(patentIds);
- if (num < 1) {
- throw new BusinessException(ExceptionEnum.THE_FAIL_TO_DELETE);
- }
- } else {
- return false;
- }
- return true;
- }
- //根据专利号、assoPersonProductId查询相关专利id
- public List<String> selectChildPatentId(String patentNo, Integer assoPersonProductId) throws IOException {
- List<String> list = new ArrayList<>();
- SearchRequest.Builder builder = new SearchRequest.Builder();
- //设置查询索引
- builder.index(patentMapName);
- Query q = QueryBuilders.term(i -> i.field("product_id").value(assoPersonProductId));
- Query query = QueryBuilders.term(i -> i.field("patent_no.keyword").value(patentNo));
- Query q1 = QueryBuilders.hasParent(i -> i.parentType("patent").query(query));
- Query bool = QueryBuilders.bool(i -> i.must(q, q1));
- builder.query(bool);
- //解除最大条数限制
- builder.trackTotalHits(i -> i.enabled(true));
- SearchResponse<Patent> response = client.search(builder.build(), Patent.class);
- List<Hit<Patent>> hits = response.hits().hits();
- for (Hit<Patent> hit : hits) {
- String id = hit.id();
- list.add(id);
- }
- return list;
- }
- //装载专利清单列表的数据
- public PatentColumnDTO getPatentColumnDTO(Patent patent, Integer projectId, String id) {
- PatentColumnDTO columnDTO = new PatentColumnDTO();
- BeanUtil.copy(patent, columnDTO);
- if (ToolUtil.isNotEmpty(patent.getApplicantAddr())) {
- PersonAddress applicantAddr = patent.getApplicantAddr();
- columnDTO.setAppAddress(applicantAddr.getAddress());
- columnDTO.setApplicantCountry(applicantAddr.getCountry());
- columnDTO.setAppProvince(applicantAddr.getProvince());
- columnDTO.setAppCity(applicantAddr.getCity());
- columnDTO.setAppDistrict(applicantAddr.getDistrict());
- }
- if (ToolUtil.isNotEmpty(patent.getRightHolderAddr())) {
- PersonAddress rightAddr = patent.getRightHolderAddr();
- columnDTO.setRightAddress(rightAddr.getAddress());
- columnDTO.setRightCountry(rightAddr.getCountry());
- columnDTO.setRightProvince(rightAddr.getProvince());
- columnDTO.setRightCity(rightAddr.getCity());
- columnDTO.setRightDistrict(rightAddr.getDistrict());
- }
- columnDTO.setApplicant(this.loadName(patent.getApplicant()));
- columnDTO.setRightHolder(this.loadName(patent.getRightHolder()));
- columnDTO.setInventor(this.loadName(patent.getInventor()));
- return columnDTO;
- }
- //通用返回申请人、权利人、发明人
- public List<String> loadName(List<PatentPerson> list) {
- List<String> collect = new ArrayList<>();
- if (!CollectionUtils.isEmpty(list)) {
- collect = list.stream().filter(i -> StringUtils.isNotEmpty(i.getName())).map(PatentPerson::getName).collect(Collectors.toList());
- }
- return collect;
- }
- /**
- * 根据专利号获取专利id
- *
- * @param patentNo
- * @return
- * @throws IOException
- */
- public String selectPatentByPatentNo(String patentNo) throws IOException {
- String id = "";
- SearchRequest.Builder builder = new SearchRequest.Builder();
- //设置查询索引
- builder.index(patentMapName);
- Query q = QueryBuilders.term(i -> i.field("patent_no.keyword").value(patentNo));
- builder.query(q);
- builder.size(10);
- SearchResponse<Patent> response = client.search(builder.build(), Patent.class);
- List<Hit<Patent>> hits = response.hits().hits();
- for (Hit<Patent> hit : hits) {
- id = hit.id();
- }
- return id;
- }
- /**
- * 根据专利号获取专利id
- *
- * @param patentNo
- * @return
- * @throws IOException
- */
- public PatentColumnDTO selectPatentInfoByPatentNo(String patentNo) throws IOException {
- PatentColumnDTO columnDTO = new PatentColumnDTO();
- SearchRequest.Builder builder = new SearchRequest.Builder();
- //设置查询索引
- builder.index(patentMapName);
- Query q = QueryBuilders.term(i -> i.field("patent_no.keyword").value(patentNo));
- builder.query(q);
- builder.size(10);
- SearchResponse<Patent> response = client.search(builder.build(), Patent.class);
- List<Hit<Patent>> hits = response.hits().hits();
- for (Hit<Patent> hit : hits) {
- Patent patent = hit.source();
- columnDTO = this.getPatentColumnDTO(patent, null, null);
- }
- return columnDTO;
- }
- /**
- * 判断该专利是否已经被收藏
- *
- * @param personProductId
- * @return
- * @throws IOException
- */
- public List<String> selectPatentByProductId(Integer personProductId) throws IOException {
- List<String> list = new ArrayList<>();
- SearchRequest.Builder builder = new SearchRequest.Builder();
- //设置查询索引
- builder.index(patentMapName);
- Query q = QueryBuilders.term(i -> i.field("product_id").value(personProductId));
- Query query = QueryBuilders.hasChild(i -> i.type("product").query(q));
- builder.query(query);
- builder.size(10);
- SearchResponse<Patent> response = client.search(builder.build(), Patent.class);
- List<Hit<Patent>> hits = response.hits().hits();
- for (Hit<Patent> hit : hits) {
- Patent patent = hit.source();
- list.add(patent.getPatentNo());
- }
- return list;
- }
- //-------------------查询相关专利信息-----------
- /**
- * 查询专利(用于导出专利数据)
- *
- * @param patentDTO
- * @param file
- * @return
- * @throws Exception
- */
- public List<PatentColumnDTO> selectPatent(SelectPatentDTO patentDTO, File file) throws Exception {
- List<PatentColumnDTO> columnDTOS = new ArrayList<>();
- if (!CollectionUtils.isEmpty(patentDTO.getAppNos())) {
- for (String appNo : patentDTO.getAppNos()) {
- //获取专利的多张摘要附图
- EsPictureNoDTO esPictureNoDTO = new EsPictureNoDTO();
- esPictureNoDTO.setAppNo(appNo);
- List<EsPictureNoVo> noVos = esDenseVectorService.getPictureByNo(esPictureNoDTO);
- List<String> guids = noVos.stream().map(EsPictureNoVo::getGuid).collect(Collectors.toList());
- //根据专利申请号appNo获取专利信息
- PatentColumnDTO columnDTO = this.selectPatentByAppNo(appNo);
- columnDTO.setPictureGuids(guids);
- columnDTOS.add(columnDTO);
- }
- } else {
- //根据条件获取图片专利里的数据信息,如appNo
- List<EsPatentVectorVo> vectorVos = this.selectPicturePatent(patentDTO, file);
- if (!CollectionUtils.isEmpty(vectorVos)) {
- for (EsPatentVectorVo vectorVo : vectorVos) {
- String appNo = vectorVo.getAppNo();
- //获取专利的多张摘要附图
- EsPictureNoDTO esPictureNoDTO = new EsPictureNoDTO();
- esPictureNoDTO.setAppNo(appNo);
- List<EsPictureNoVo> noVos = esDenseVectorService.getPictureByNo(esPictureNoDTO);
- List<String> guids = noVos.stream().map(EsPictureNoVo::getGuid).collect(Collectors.toList());
- //根据专利申请号appNo获取专利信息
- PatentColumnDTO columnDTO = this.selectPatentByAppNo(appNo);
- columnDTO.setPictureGuids(guids);
- columnDTOS.add(columnDTO);
- }
- }
- }
- return columnDTOS;
- }
- /**
- * 获取图片专利里的信息
- *
- * @param patentDTO
- * @param file
- * @return
- * @throws Exception
- */
- public List<EsPatentVectorVo> selectPicturePatent(SelectPatentDTO patentDTO, File file) throws Exception {
- Boolean ifAllExport = patentDTO.getIfAllExport();
- Integer limitNum = patentDTO.getLimitNum();
- SearchRequest.Builder builder = new SearchRequest.Builder();
- //设置查询索引
- builder.index(patentMapName);
- Query q = null;
- String condition = esDenseVectorService.appendCondition(patentDTO.getProductId(), patentDTO.getKey(), patentDTO.getAppCountry(), patentDTO.getCompanyName());
- if (StringUtils.isNotEmpty(condition)) {
- //1. 解析检索条件
- treeNode tree = expressManager.getInstance().Parse(condition, false);
- //2. 从es中检索数据
- q = formatQueryService.EsQueryToQuery((operateNode) tree, "patentVector", null);
- }
- //获取图片向量
- List<Float> imageList = new ArrayList<>();
- List<String> stringList = new ArrayList<>();
- if (file != null && file.exists() && file.length() != 0) {
- stringList = getVectorService.getVectorByFile(file);
- } else if (StringUtils.isNotEmpty(patentDTO.getDescription())) {
- stringList = getVectorService.getVectorByText(patentDTO.getDescription());
- }
- stringList.forEach(item -> {
- Float a = Float.parseFloat(item);
- imageList.add(a);
- });
- if (!CollectionUtils.isEmpty(imageList)) {
- String source = "cosineSimilarity(params.queryVector, 'my_vector') + 1.0";
- InlineScript inlineScript = InlineScript.of(i -> i.lang("painless").params("queryVector", JsonData.of(imageList)).source(source));
- Script script = Script.of(i -> i.inline(inlineScript));
- Query query = null;
- if (q != null) {
- Query finalQ = q;
- query = QueryBuilders.scriptScore(i -> i.script(script)
- .query(finalQ));
- } else {
- query = QueryBuilders.scriptScore(i -> i.script(script)
- .query(org.springframework.data.elasticsearch.client.elc.QueryBuilders.matchAllQueryAsQuery()));
- }
- builder.query(query);
- } else {
- builder.query(q);
- }
- //根据申请号去重
- FieldCollapse collapse = FieldCollapse.of(i -> i.field("app_no"));
- builder.collapse(collapse);
- //分页
- if (Boolean.TRUE.equals(ifAllExport)) {
- builder.from(0).size(999999);
- } else {
- builder.size(limitNum);
- }
- //解除最大条数限制
- builder.trackTotalHits(i -> i.enabled(true));
- SearchResponse<PatentVector> response = client.search(builder.build(), PatentVector.class);
- List<Hit<PatentVector>> hits = response.hits().hits();
- List<EsPatentVectorVo> vectorVos = new ArrayList<>();
- Double fixedScore = 1.7d;
- for (Hit<PatentVector> hit : hits) {
- Double score = hit.score();
- if (score > fixedScore) {
- PatentVector vector = hit.source();
- EsPatentVectorVo vectorVo = new EsPatentVectorVo();
- BeanUtil.copy(vector, vectorVo);
- vectorVos.add(vectorVo);
- }
- }
- return vectorVos;
- }
- /**
- * 根据申请号查询专利信息
- *
- * @param appNo
- * @return
- * @throws IOException
- */
- public PatentColumnDTO selectPatentByAppNo(String appNo) throws IOException {
- PatentColumnDTO columnDTO = new PatentColumnDTO();
- SearchRequest.Builder builder = new SearchRequest.Builder();
- //设置查询索引
- builder.index(patentMapName);
- Query q = QueryBuilders.term(i -> i.field("app_no.keyword").value(appNo));
- builder.query(q);
- builder.size(10);
- SearchResponse<Patent> response = client.search(builder.build(), Patent.class);
- List<Hit<Patent>> hits = response.hits().hits();
- for (Hit<Patent> hit : hits) {
- Patent patent = hit.source();
- columnDTO = this.getPatentColumnDTO(patent, null, null);
- }
- return columnDTO;
- }
- /**
- * Es检索
- *
- * @param queryRequest 检索条件
- * @return
- */
- public PatentDTO esSearch(QueryRequest queryRequest) throws Exception {
- PatentDTO dto = new PatentDTO();
- String searchCondition = "";
- Integer projectId = queryRequest.getProjectId();
- Long current = queryRequest.getCurrent();
- Long size = queryRequest.getSize();
- //判断表达式
- if (queryRequest instanceof StringRequest) {
- searchCondition = ((StringRequest) queryRequest).getSearchQuery();
- } else if (queryRequest instanceof MapRequest) {
- Map<String, Object> map = ((MapRequest) queryRequest).getSearchQuery();
- StringBuilder stringBuilder = new StringBuilder();
- for (String key : map.keySet()) {
- Object value = map.get(key);
- if (!"".contentEquals(stringBuilder)) {
- stringBuilder = stringBuilder.append(" AND ").append(key).append("=").append(value);
- } else {
- stringBuilder = stringBuilder.append(key).append("=").append(value);
- }
- }
- searchCondition = stringBuilder.toString();
- }
- String condition = searchCondition;
- SearchRequest.Builder builder = new SearchRequest.Builder();
- //设置查询索引
- builder.index("wxpatent");
- //1. 解析检索条件
- treeNode tree = expressManager.getInstance().Parse(condition, false);
- //3. 从es中检索数据
- Query q = formatQueryService.EsQueryToQuery((operateNode) tree, "patent", projectId);
- builder.query(q);
- //排序
- // List<SortOptions> optionsList = new ArrayList<>();
- //
- // SortOptions sortOptions = SortOptions.of(i -> i.field(j -> j.field("public_date").order(SortOrder.Desc).missing(-1)));
- // optionsList.add(sortOptions);
- //
- // builder.sort(optionsList);
- // //分页
- // if (current != null && size != null && current > 0 && size > 0) {
- // builder.from((current.intValue() - 1) * size.intValue()).size(size.intValue());
- // } else {
- // builder.from(1).size(99999);
- // }
- //解除最大条数限制
- builder.trackTotalHits(i -> i.enabled(true));
- SearchResponse<Patent> response = client.search(builder.build(), Patent.class);
- List<PatentColumnDTO> list = new ArrayList<>();
- long total = 0L;
- List<Hit<Patent>> hits = response.hits().hits();
- total = response.hits().total().value();
- for (Hit<Patent> hit : hits) {
- // String id = hit.id();
- // Patent esMess = hit.source();
- // PatentColumnDTO columnDTO = this.getPatentColumnDTO(esMess, projectId, id);
- // list.add(columnDTO);
- }
- dto.setTotal(total);
- dto.setPatents(list);
- dto.setPageNum(current);
- dto.setPageSize(size);
- return dto;
- }
- }
|