1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666 |
- package cn.cslg.pas.service.business.es;
- import cn.cslg.pas.common.core.base.IfConstant;
- import cn.cslg.pas.common.dto.GetUnselectedDTO;
- import cn.cslg.pas.common.dto.PatentColumnDTO;
- import cn.cslg.pas.common.dto.PatentDTO;
- import cn.cslg.pas.common.dto.business.EsPatentFamilyDTO;
- import cn.cslg.pas.common.dto.business.SelectClaimDTO;
- import cn.cslg.pas.common.dto.es.EsCustomFieldValueDTO;
- import cn.cslg.pas.common.model.es.GetVectorVO;
- import cn.cslg.pas.common.model.request.MapRequest;
- import cn.cslg.pas.common.model.request.OrderDTO;
- import cn.cslg.pas.common.model.request.QueryRequest;
- import cn.cslg.pas.common.model.request.StringRequest;
- import cn.cslg.pas.common.utils.FormatUtil;
- import cn.cslg.pas.common.vo.*;
- import cn.cslg.pas.common.vo.es.EsCustomFieldBatchVO;
- import cn.cslg.pas.common.vo.es.EsPatentCommonVO;
- import cn.cslg.pas.common.vo.es.PatentFamilyMessageWithId;
- import cn.cslg.pas.domain.es.*;
- import cn.cslg.pas.exception.ExceptionEnum;
- import cn.cslg.pas.exception.XiaoShiException;
- import cn.cslg.pas.service.business.AssoProjectPersonService;
- import cn.cslg.pas.service.business.CommonService;
- import cn.cslg.pas.service.common.PatentStarApiService;
- import cn.cslg.pas.service.common.PythonApiService;
- import cn.cslg.pas.service.importPatent.WebVOTransformService;
- import cn.cslg.pas.service.query.FormatQueryService;
- import co.elastic.clients.elasticsearch.ElasticsearchClient;
- import co.elastic.clients.elasticsearch._types.*;
- import co.elastic.clients.elasticsearch._types.aggregations.*;
- import co.elastic.clients.elasticsearch._types.query_dsl.*;
- import co.elastic.clients.elasticsearch.core.*;
- import co.elastic.clients.elasticsearch.core.search.FieldCollapse;
- import co.elastic.clients.elasticsearch.core.search.Hit;
- import co.elastic.clients.elasticsearch.core.search.TrackHits;
- import co.elastic.clients.json.JsonData;
- import com.alibaba.fastjson.JSON;
- 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.context.annotation.Lazy;
- import org.springframework.stereotype.Service;
- import org.springframework.transaction.annotation.Propagation;
- import org.springframework.transaction.annotation.Transactional;
- import org.springframework.util.CollectionUtils;
- import java.io.IOException;
- import java.text.SimpleDateFormat;
- import java.util.*;
- import java.util.stream.Collectors;
- import static org.springframework.data.elasticsearch.annotations.TermVector.no;
- @Service
- @RequiredArgsConstructor(onConstructor_ = {@Lazy})
- public class EsService {
- public final List<String> dateList = Arrays.asList("AD", "PD", "GD", "EXD", "PAD", "PED", "PPD", "EPD");
- private final ElasticsearchClient client;
- private final PythonApiService pythonApiService;
- @Autowired
- private EsFamilyService esFamilyService;
- @Autowired
- private PatentStarApiService patentStarApiService;
- @Autowired
- private EsPatentService esPatentService;
- @Lazy
- @Autowired
- private EsCountService esCountService;
- @Autowired
- private EsCustomFieldService esCustomFieldService;
- @Autowired
- private AssoProjectPersonService assoProjectPersonService;
- /**
- * @param patent
- * @throws Exception
- */
- public String addPatent(Patent patent) throws Exception {
- IndexResponse indexResponse = client.index(i -> i
- .index("patent")
- //传入user对象
- .document(patent).refresh(Refresh.True)
- );
- 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).refresh(Refresh.True).waitForActiveShards(WaitForActiveShards.of(t -> t.count(1)))
- );
- return indexResponse.id();
- }
- /**
- * 根据专利号获取专利id
- *
- * @param patentNo
- * @return
- * @throws Exception
- */
- public PatentWithIdVO getIdByPatentNo(String patentNo) throws Exception {
- String patentNo1 = patentNo.replace(" ", "");
- SearchRequest.Builder builder = new SearchRequest.Builder();
- //设置查询索引
- builder.index("patent");
- PatentWithIdVO patentWithIdVO = null;
- String id = null;
- // Query q1 = QueryBuilders.term(t -> t.field("app_no.keyword").value(patentNo));
- // //公开号
- // Query q2 = QueryBuilders.term(t -> t.field("public_no.keyword").value(patentNo));
- // //授权号
- // Query q3 = QueryBuilders.term(t -> t.field("grant_no.keyword").value(patentNo));
- // Query query = QueryBuilders.bool(i -> i.should(q1, q2, q3));
- Query query = QueryBuilders.term(t -> t.field("patent_no.keyword").value(patentNo1));
- builder.query(query);
- SearchResponse<Patent> response = null;
- try {
- response = client.search(builder.build(), Patent.class);
- } catch (IOException e) {
- throw new XiaoShiException(ExceptionEnum.BUSINESS_ERROR, "ES检索失败");
- }
- 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;
- }
- /**
- * 根据专利号获取专利id
- *
- * @param patentNo
- * @return
- * @throws Exception
- */
- public PatentWithIdVO importPatentQueryByPatentNo(String patentNo) throws Exception {
- SearchRequest.Builder builder = new SearchRequest.Builder();
- //设置查询索引
- builder.index("patent");
- PatentWithIdVO patentWithIdVO = null;
- String id = null;
- Query query = QueryBuilders.term(t -> t.field("patent_no.keyword").value(patentNo));
- builder.query(query);
- SearchResponse<Patent> response = null;
- try {
- response = client.search(builder.build(), Patent.class);
- } catch (Exception e) {
- throw new XiaoShiException(ExceptionEnum.BUSINESS_ERROR, "ES检索失败");
- }
- 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;
- }
- /**
- * Es检索
- *
- * @param queryRequest 检索条件
- * @return
- */
- public PatentDTO esSearch(QueryRequest queryRequest) throws Exception {
- //this.judgeQueryPatentRoot(queryRequest);
- PatentDTO dto = new PatentDTO();
- Integer taskId = queryRequest.getTaskId();
- String searchCondition = "";
- Integer projectId = queryRequest.getProjectId();
- Integer productId = queryRequest.getProductId();
- String productFrom = queryRequest.getFrom();
- Long current = queryRequest.getCurrent();
- Long size = queryRequest.getSize();
- String groupField = queryRequest.getGroupField();
- //判断表达式
- if (queryRequest instanceof StringRequest) {
- searchCondition = ((StringRequest) queryRequest).getSearchQuery();
- if (searchCondition == null) {
- searchCondition = "";
- }
- } 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 esField = "";
- if (StringUtils.isNotEmpty(groupField) && !groupField.equals("0")) {
- esField = this.getGroupField(groupField);
- }
- EsPatentCommonVO commonVO = new EsPatentCommonVO();
- commonVO.setTaskId(taskId);
- commonVO.setProjectId(projectId);
- commonVO.setProductId(productId);
- if (StringUtils.isNotEmpty(searchCondition) && searchCondition.contains("罒")) {
- String replace = searchCondition.replace("罒", " OR ");
- commonVO.setSearchCondition(replace);
- } else {
- commonVO.setSearchCondition(searchCondition);
- }
- commonVO.setProductFrom(productFrom);
- commonVO.setCurrent(current);
- commonVO.setSize(size);
- commonVO.setEsField(esField);
- commonVO.setCustomFields(queryRequest.getCustomFields());
- commonVO.setOrderDTOList(queryRequest.getOrderDTOList());
- commonVO.setSemanteme(queryRequest.getSemanteme());
- SearchRequest.Builder builder = this.getCommonPatent(commonVO);
- //解除最大条数限制
- builder.trackTotalHits(i -> i.enabled(true));
- SearchResponse<Patent> response = null;
- try {
- response = client.search(builder.build(), Patent.class);
- } catch (Exception e) {
- throw new XiaoShiException(ExceptionEnum.BUSINESS_ERROR, "ES检索失败");
- }
- List<PatentColumnDTO> list = new ArrayList<>();
- List<Hit<Patent>> hits = response.hits().hits();
- long total = 0L;
- if (StringUtils.isNotEmpty(esField)) {
- Aggregate aggregate = response.aggregations().get("count");
- total = aggregate.cardinality().value();
- } else {
- total = response.hits().total().value();
- }
- for (Hit<Patent> hit : hits) {
- Double score= hit.score();
- String str = String.format("%.4f",score);
- double four = Double.parseDouble(str);
- String id = hit.id();
- Patent esMess = hit.source();
- PatentColumnDTO columnDTO = this.getPatentColumnDTO(esMess, projectId, id, "");
- columnDTO.setScore(four);
- list.add(columnDTO);
- }
- this.loadCoulumnDTO(list);
- dto.setTotal(total);
- dto.setPatents(list);
- dto.setPageNum(current);
- dto.setPageSize(size);
- return dto;
- }
- //获取分组字段栏位
- public String getGroupField(String groupField) {
- String esField = "";
- switch (groupField) {
- case "simpleFamilyId":
- esField = "simple_family_id";
- break;
- case "inpadocFamilyId":
- esField = "inpadoc_family_id";
- break;
- case "patsnapFamilyId":
- esField = "patsnap_family_id";
- break;
- default:
- esField = "0";
- break;
- }
- return esField;
- }
- //获取通用检索专利方法--服务检索专利清单
- public SearchRequest.Builder getCommonPatent(EsPatentCommonVO commonVO) throws Exception {
- String esField = commonVO.getEsField();
- //计算分页信息
- Long current = commonVO.getCurrent();
- Long size = commonVO.getSize();
- Long startNum = commonVO.getStartNum();
- Long endNum = commonVO.getEndNum();
- String semanteme = commonVO.getSemanteme();
- Integer temPageFrom = 0;
- Integer temPageSize = 100000;
- if (current != null && size != null) {
- temPageFrom = (current.intValue() - 1) * size.intValue();
- temPageSize = size.intValue();
- } else if (startNum != null && endNum != null) {
- temPageFrom = startNum.intValue() - 1;
- temPageSize = (endNum.intValue() - startNum.intValue()) + 1;
- }
- Integer pageFrom = temPageFrom;
- Integer pageSize = temPageSize;
- SearchRequest.Builder builder = new SearchRequest.Builder();
- //设置查询索引
- builder.index("patent");
- Query reTmQuery = esPatentService.getEntireNotInQuery(commonVO);
- Query reQuery = this.getMeanSearch(reTmQuery, semanteme);
- //判断同族分组
- if (StringUtils.isNotEmpty(esField)) {
- Query existQ = QueryBuilders.exists(i -> i.field(esField));
- Query bool = QueryBuilders.bool(i -> i.must(reQuery, existQ));
- builder.query(bool);
- FieldCollapse collapse = FieldCollapse.of(i -> i.field(esField));
- builder.collapse(collapse);
- //统计总数
- Aggregation aggregation = AggregationBuilders.cardinality(i -> i.field(esField));
- builder.aggregations("count", aggregation);
- } else {
- builder.query(reQuery);
- }
- builder.from(pageFrom).size(pageSize);
- //排序
- if (semanteme == null || semanteme.trim().equals("")) {
- List<OrderDTO> dtoList = commonVO.getOrderDTOList();
- List<SortOptions> optionsList = this.getCommonSortMethod(dtoList);
- builder.sort(optionsList);
- }
- return builder;
- }
- //获取通用检索专利方法--服务于批量删除、标引、查询价值曲线、导出excel、pdf首页
- public SearchRequest.Builder getCommonPatentByGroup(EsPatentCommonVO commonVO) throws Exception {
- String esField = commonVO.getEsField();
- Query reQuery = esPatentService.getEntireNotInQuery(commonVO);
- SearchRequest.Builder builder = new SearchRequest.Builder();
- //设置查询索引
- builder.index("patent");
- //判断同族分组
- if (StringUtils.isNotEmpty(esField)) {
- Query existQ = QueryBuilders.exists(i -> i.field(esField));
- Query bool = QueryBuilders.bool(i -> i.must(reQuery, existQ));
- builder.query(bool);
- FieldCollapse collapse = FieldCollapse.of(i -> i.field(esField));
- builder.collapse(collapse);
- } else {
- builder.query(reQuery);
- }
- //排序
- List<OrderDTO> dtoList = commonVO.getOrderDTOList();
- List<SortOptions> optionsList = this.getCommonSortMethod(dtoList);
- builder.sort(optionsList);
- return builder;
- }
- public PatentWithIdVO getPatentColumnDTOByGroup2(String key, Integer projectId, String field) throws Exception {
- PatentWithIdVO dto = new PatentWithIdVO();
- SearchRequest.Builder builder = new SearchRequest.Builder();
- //设置查询索引
- builder.index("patent");
- Query query = QueryBuilders.term(i -> i.field("project_id").value(projectId));
- Query q1 = QueryBuilders.hasChild(i -> i.type("project").query(query));
- Query q2 = QueryBuilders.term(i -> i.field(field).value(key));
- Query bool = QueryBuilders.bool(i -> i.must(q1, q2));
- builder.query(bool);
- SearchResponse<Patent> response = null;
- try {
- response = client.search(builder.build(), Patent.class);
- } catch (Exception e) {
- throw new XiaoShiException(ExceptionEnum.BUSINESS_ERROR, "ES检索失败");
- }
- List<Hit<Patent>> hits = response.hits().hits();
- if (!CollectionUtils.isEmpty(hits)) {
- String id = hits.get(0).id();
- Patent patent = hits.get(0).source();
- dto.setPatent(patent);
- dto.setId(id);
- }
- return dto;
- }
- /**
- * 获取公共排序方法
- *
- * @param dtoList
- * @return
- */
- public List<SortOptions> getCommonSortMethod(List<OrderDTO> dtoList) {
- List<SortOptions> optionsList = new ArrayList<>();
- if (!CollectionUtils.isEmpty(dtoList)) {
- String json = CommonService.readJsonFile("patent.json");
- List<EsConfigVO> esConfigVOS = JSON.parseArray(json, EsConfigVO.class);
- for (OrderDTO orderDTO : dtoList) {
- EsConfigVO configVO = esConfigVOS.stream().filter(item -> item.getField().equals(orderDTO.getOrderBy())).findFirst().orElse(null);
- if (configVO != null) {
- if (orderDTO.getOrderType().equals(IfConstant.NO)) {
- SortOptions sortOptions = SortOptions.of(i -> i.field(j -> j.field(configVO.getEsField()).order(SortOrder.Asc).missing(-1)));
- optionsList.add(sortOptions);
- } else {
- SortOptions sortOptions = SortOptions.of(i -> i.field(j -> j.field(configVO.getEsField()).order(SortOrder.Desc).missing(-1)));
- optionsList.add(sortOptions);
- }
- }
- }
- } else {
- SortOptions sortOptions = SortOptions.of(i -> i.field(j -> j.field("patent_no.keyword").order(SortOrder.Desc).missing(-1)));
- optionsList.add(sortOptions);
- }
- return optionsList;
- }
- //装载专利清单列表的数据
- public PatentColumnDTO getPatentColumnDTO(Patent patent, Integer projectId, String id, String typeStr) {
- PatentColumnDTO columnDTO = new PatentColumnDTO();
- columnDTO.setPatentId(id);
- BeanUtils.copyProperties(patent, columnDTO);
- if (!CollectionUtils.isEmpty(patent.getIpc())) {
- boolean b = patent.getIpc().stream().allMatch(Objects::isNull);
- if (b) {
- columnDTO.setIpc(new ArrayList<>());
- }
- }
- if (projectId != null) {
- try {
- columnDTO.setMergeApplicant(esPatentService.getMergeApp(projectId, id));
- } catch (IOException e) {
- columnDTO.setMergeApplicant(new ArrayList<>());
- }
- try {
- columnDTO.setMergeRightHolder(esPatentService.getMergeRight(projectId, id));
- } catch (IOException e) {
- columnDTO.setMergeRightHolder(new ArrayList<>());
- }
- try {
- columnDTO.setMergeInventor(esPatentService.getMergeInventor(projectId, id));
- } catch (IOException e) {
- columnDTO.setMergeInventor(new ArrayList<>());
- }
- }
- if (ObjectUtils.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 (ObjectUtils.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());
- }
- if (!CollectionUtils.isEmpty(patent.getPriorities())) {
- List<Priorities> priorities = patent.getPriorities();
- List<String> priorityCountrys = priorities.stream().map(Priorities::getPriorityCountry).distinct().collect(Collectors.toList());
- List<String> priorityNos = priorities.stream().map(Priorities::getPriorityNo).distinct().collect(Collectors.toList());
- List<String> priorityDates = priorities.stream().map(Priorities::getPriorityDate).distinct().collect(Collectors.toList());
- if (StringUtils.isNotEmpty(typeStr)) {
- String priorityCountry = String.join(",", priorityCountrys);
- columnDTO.setPriorityCountry(priorityCountry);
- String priorityNo = String.join(",", priorityNos);
- columnDTO.setPriorityNo(priorityNo);
- String priorityDate = String.join(",", priorityDates);
- if (priorityDate.contains("null")) {
- columnDTO.setPriorityDate("");
- } else {
- columnDTO.setPriorityDate(priorityDate);
- }
- } else {
- String priorityCountry = String.join("<br>", priorityCountrys);
- columnDTO.setPriorityCountry(priorityCountry);
- String priorityNo = String.join("<br>", priorityNos);
- columnDTO.setPriorityNo(priorityNo);
- String priorityDate = String.join("<br>", priorityDates);
- if (priorityDate.contains("null")) {
- columnDTO.setPriorityDate("");
- } else {
- columnDTO.setPriorityDate(priorityDate);
- }
- }
- }
- columnDTO.setApplicant(esPatentService.loadName(patent.getApplicant()));
- columnDTO.setRightHolder(esPatentService.loadName(patent.getRightHolder()));
- columnDTO.setInventor(esPatentService.loadName(patent.getInventor()));
- return columnDTO;
- }
- //拼接专题库id或其他id条件
- public String appendIdsCondition(String searchCondition, Integer taskId, String productFrom, Integer projectId, Integer productId) {
- String condition = "";
- if (taskId != null) {
- if (searchCondition != null && !"".equals(searchCondition.trim())) {
- searchCondition = "taskId = " + taskId + " AND " + searchCondition;
- } else {
- searchCondition = "taskId = " + taskId;
- }
- } else {
- if (StringUtils.isNotEmpty(productFrom)) {
- if (productId != null) {
- if (searchCondition != null && !"".equals(searchCondition.trim())) {
- searchCondition = "productId = " + productId + " AND " + searchCondition;
- } else {
- searchCondition = "productId = " + productId;
- }
- }
- } else {
- if (projectId != null) {
- if (searchCondition != null && !"".equals(searchCondition.trim())) {
- searchCondition = "projectId = " + projectId + " AND " + searchCondition;
- } else {
- searchCondition = "projectId = " + projectId;
- }
- }
- }
- }
- condition = searchCondition;
- return condition;
- }
- /**
- * 解析自定义栏位和值
- *
- * @param customFields
- * @return
- */
- public String parseCustomField(List<EsCustomFieldValueDTO> customFields, Integer projectId, Integer taskId) throws Exception {
- int m = 1;
- int n = 0;
- StringBuilder builder = new StringBuilder();
- if (customFields.size() > m) {
- for (int i = 0; i < customFields.size(); i++) {
- EsCustomFieldValueDTO customField = customFields.get(i);
- if (i != n) {
- builder.append(" ").append("and").append(" ").append("(");
- this.appendStr(customField, builder, m, customField.getIfHaveChild(), projectId, taskId);
- } else {
- builder.append("(");
- this.appendStr(customField, builder, m, customField.getIfHaveChild(), projectId, taskId);
- }
- }
- } else {
- for (int i = 0; i < customFields.size(); i++) {
- EsCustomFieldValueDTO customField = customFields.get(i);
- if (i != n) {
- builder.append(" ").append("and").append(" ");
- this.appendStr(customField, builder, m, customField.getIfHaveChild(), projectId, taskId);
- } else {
- builder.append("(");
- this.appendStr(customField, builder, m, customField.getIfHaveChild(), projectId, taskId);
- }
- }
- }
- return builder.toString();
- }
- //拼接自定义栏位
- public void appendStr(EsCustomFieldValueDTO customField, StringBuilder builder, int m, boolean ifHaveChild,
- Integer projectId, Integer taskId) throws Exception {
- builder.append("field").append("=").append(customField.getFieldId());
- List<String> values = customField.getFieldValue();
- if (!CollectionUtils.isEmpty(values)) {
- builder.append(" ").append("and").append(" ");
- if (ifHaveChild) {
- builder.append("statsValue").append("=");
- } else {
- builder.append("fieldValue").append("=");
- }
- if (values.size() > m) {
- builder.append("(");
- for (int j = 0; j < values.size(); j++) {
- String s = values.get(j);
- if (j != values.size() - m) {
- builder.append(s).append(" ").append("or").append(" ");
- } else {
- if (s.equals("未选择")) {
- String noCondition = "(field = (NOT " + customField.getFieldId() + ")" + " OR " + "(field = " + customField.getFieldId() + " AND " + "fieldValue1 = 1))";
- builder.append(s).append(")").append(" ").append("OR")
- .append(" ").append("(").append(noCondition).append(")").append(")");
- } else {
- builder.append(s).append(")").append(")");
- }
- }
- }
- } else {
- for (String value : values) {
- if (value.equals("未选择")) {
- String noCondition = "(field = (NOT " + customField.getFieldId() + ")" + " OR " + "(field = " + customField.getFieldId() + " AND " + "fieldValue1 = 1))";
- builder.append(value).append(" ").append("OR").append(" ").append(noCondition).append(")");
- } else {
- builder.append(value).append(")");
- }
- }
- }
- } else {
- builder.append(")");
- }
- }
- //拼接专利ids
- public String appendIds(List<String> nos) {
- String str = "ids = ";
- if (nos.size() > 1) {
- str = str + "\"";
- for (int i = 0; i < nos.size(); i++) {
- String s = nos.get(i);
- if (i != nos.size() - 1) {
- str = str + s + ",";
- } else {
- str = str + s + "\"";
- }
- }
- } else {
- for (String no : nos) {
- str = str + no;
- }
- }
- return str;
- }
- //拼接专利号NOS
- public String appendNOS(List<String> nos) {
- String str = "NO = ";
- if (nos.size() > 1) {
- str = str + "(";
- for (int i = 0; i < nos.size(); i++) {
- String s = nos.get(i);
- if (i != nos.size() - 1) {
- str = str + s + " " + "OR" + " ";
- } else {
- str = str + s + ")";
- }
- }
- } else {
- for (String no : nos) {
- str = str + no;
- }
- }
- return str;
- }
- //更新patent
- public Integer updatePatent(Patent patent, String id) {
- UpdateRequest<Patent, Patent> req;
- req = UpdateRequest.of(
- b -> b.index("patent").id(id)
- .doc(patent).refresh(Refresh.True)
- );
- try {
- client.update(req, Patent.class);
- return 1;
- } catch (IOException e) {
- return -1;
- }
- }
- //更新patent
- public Integer updatePatentShouldWait(Patent patent, String id) {
- UpdateRequest<Patent, Patent> req;
- req = UpdateRequest.of(
- b -> b.index("patent").id(id)
- .doc(patent).refresh(Refresh.True)
- );
- try {
- client.update(req, Patent.class);
- return 1;
- } catch (IOException e) {
- return -1;
- }
- }
- //更新patent
- public Integer updatePatentById(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;
- }
- }
- //获取父文档相关的所有自定义子文档,按照时间按倒序,首个改为最新数据,其他if——new改为0:不是最新
- public Integer getIds(String parentId, Integer projectId, Integer taskId, String fieldId) throws Exception {
- int ifNew = 0;
- SearchRequest.Builder builder = new SearchRequest.Builder();
- //设置查询索引
- builder.index("patent");
- Query q1 = QueryBuilders.hasParent(i -> i.parentType("patent").query(j -> j.ids(IdsQuery.of(k -> k.values(parentId)))));
- Query q2 = QueryBuilders.exists(i -> i.field("custom_field"));
- Query q3 = QueryBuilders.term(i -> i.field("custom_field.project_id").value(projectId));
- Query q4 = QueryBuilders.term(i -> i.field("custom_field.field").value(fieldId));
- Query bool = QueryBuilders.bool(i -> i.must(q1, q2, q3, q4));
- builder.query(bool);
- List<SortOptions> optionsList = new ArrayList<>();
- SortOptions sortOptions = SortOptions.of(i -> i.field(j -> j.field("custom_field.create_time").order(SortOrder.Desc).missing(-1)));
- optionsList.add(sortOptions);
- builder.sort(optionsList);
- SearchResponse<Patent> response = null;
- try {
- response = client.search(builder.build(), Patent.class);
- } catch (Exception e) {
- throw new XiaoShiException(ExceptionEnum.BUSINESS_ERROR, "ES检索失败");
- }
- List<Hit<Patent>> hits = response.hits().hits();
- Map<String, ESCustomField> map = new HashMap<>();
- for (Hit<Patent> hit : hits) {
- String id = hit.id();
- Patent patent = hit.source();
- map.put(id, patent.getESCustomField());
- }
- if (!CollectionUtils.isEmpty(map)) {
- for (String id : map.keySet()) {
- ESCustomField esCustomField = map.get(id);
- esCustomField.setIfNew(0);
- Patent patent = new Patent();
- patent.setESCustomField(esCustomField);
- Integer aNew = this.updateCustomFieldIfNew(patent, id);
- if (aNew > 0) {
- ifNew++;
- } else {
- ifNew = 0;
- break;
- }
- }
- }
- return ifNew;
- }
- //更新自定义文档是否是最新数据
- public Integer updateCustomFieldIfNew(Patent patent, String id) throws IOException {
- ESCustomField customField = patent.getESCustomField();
- String source = "ctx._source.custom_field.if_new = " + customField.getIfNew();
- InlineScript inlineScript = InlineScript.of(i -> i.lang("painless").source(source));
- Script script = Script.of(i -> i.inline(inlineScript));
- Query query = QueryBuilders.term(i -> i.field("_id").value(id));
- UpdateByQueryRequest request = UpdateByQueryRequest.of(i -> i.index("patent").script(script).refresh(true).query(query));
- try {
- client.updateByQuery(request);
- return 1;
- } catch (IOException e) {
- return -1;
- }
- }
- //更新子文档
- public Integer updateByQuery(Patent patent, String id) throws Exception {
- ESCustomField customField = patent.getESCustomField();
- String parentId = patent.getPatentJoin().getParent();
- Integer num = this.getIds(parentId, customField.getProjectId(), customField.getTaskId(), customField.getField());
- if (num < 1) {
- throw new XiaoShiException(ExceptionEnum.BUSINESS_ERROR, "子文档是否最新更新失败");
- }
- String valueField = "[";
- List<String> fieldValueList = customField.getFieldValue();
- if (!CollectionUtils.isEmpty(fieldValueList)) {
- int lastSum = fieldValueList.size() - 1;
- for (int i = 0; i < fieldValueList.size(); i++) {
- String s = fieldValueList.get(i);
- if (i == lastSum) {
- valueField = valueField + "\"" + s + "\"";
- } else {
- valueField = valueField + "\"" + s + "\"" + ",";
- }
- }
- }
- valueField = valueField + "]";
- String valueStats = "[";
- List<String> statsValueList = customField.getStatsValue();
- if (!CollectionUtils.isEmpty(statsValueList)) {
- int lastSum = statsValueList.size() - 1;
- for (int i = 0; i < statsValueList.size(); i++) {
- String s = statsValueList.get(i);
- if (i == lastSum) {
- valueStats = valueStats + "\"" + s + "\"";
- } else {
- valueStats = valueStats + "\"" + s + "\"" + ",";
- }
- }
- }
- valueStats = valueStats + "]";
- SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
- String s = format.format(customField.getCreateTime());
- String dateStr = "\'" + s + "\'";
- String projectId = "ctx._source.custom_field.project_id = " + customField.getProjectId() + ";";
- String taskId = "ctx._source.custom_field.task_id = " + customField.getTaskId() + ";";
- String ifNew = "ctx._source.custom_field.if_new = " + 1 + ";";
- String field = "ctx._source.custom_field.field=" + customField.getField() + ";";
- String fieldType = "ctx._source.custom_field.field_type = " + customField.getFieldType() + ";";
- String personId = "ctx._source.custom_field.person_id = " + customField.getPersonId() + ";";
- String createTime = "ctx._source.custom_field.create_time = " + dateStr + ";";
- String fieldValue = "ctx._source.custom_field.field_value = " + valueField + ";";
- String statsValue = "ctx._source.custom_field.stats_value = " + valueStats;
- String source = projectId + taskId + ifNew + field + fieldType + personId + createTime + fieldValue + statsValue;
- InlineScript inlineScript = InlineScript.of(i -> i.lang("painless").source(source));
- Script script = Script.of(i -> i.inline(inlineScript));
- Query query = QueryBuilders.term(i -> i.field("_id").value(id));
- UpdateByQueryRequest request = UpdateByQueryRequest.of(i -> i.index("patent").script(script).refresh(true).query(query));
- try {
- client.updateByQuery(request);
- 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 = null;
- try {
- response = client.search(builder.build(), Patent.class);
- } catch (Exception e) {
- throw new XiaoShiException(ExceptionEnum.BUSINESS_ERROR, "ES检索失败");
- }
- 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;
- }
- /**
- * 查询是否存在专利
- *
- * @param parentId
- * @param projectId
- * @return
- */
- public Boolean searchPatent(String parentId, Integer projectId) throws IOException {
- boolean flag = false;
- SearchRequest.Builder builder = new SearchRequest.Builder();
- //设置查询索引
- builder.index("patent");
- Query q1 = QueryBuilders.term(t -> t.field("project_id").value(projectId));
- Query q2 = QueryBuilders.parentId(parent -> parent.type("project").id(parentId));
- Query bool = QueryBuilders.bool(i -> i.must(q1, q2));
- builder.query(bool);
- SearchResponse<Patent> response = null;
- try {
- response = client.search(builder.build(), Patent.class);
- } catch (Exception e) {
- throw new XiaoShiException(ExceptionEnum.BUSINESS_ERROR, "ES检索失败");
- }
- long total = response.hits().total().value();
- if (total > 0) {
- flag = true;
- }
- return flag;
- }
- /**
- * 查询是否存在专利
- *
- * @param parentId
- * @param projectId
- * @return
- */
- public Boolean ifInTask(String parentId, Integer projectId, Integer taskId) throws IOException {
- boolean flag = false;
- SearchRequest.Builder builder = new SearchRequest.Builder();
- //设置查询索引
- builder.index("patent");
- Query q1 = QueryBuilders.term(t -> t.field("import_task.project_id").value(projectId));
- Query q3 = QueryBuilders.term(t -> t.field("import_task.task_id").value(projectId));
- Query q2 = QueryBuilders.parentId(parent -> parent.type("import_task").id(parentId));
- Query bool = QueryBuilders.bool(i -> i.must(q1, q2, q3));
- builder.query(bool);
- SearchResponse<Patent> response = null;
- try {
- response = client.search(builder.build(), Patent.class);
- } catch (Exception e) {
- throw new XiaoShiException(ExceptionEnum.BUSINESS_ERROR, "ES检索失败");
- }
- long total = response.hits().total().value();
- if (total > 0) {
- flag = true;
- }
- return flag;
- }
- /**
- * 查询同族
- *
- * @param nos
- * @param type
- * @return
- * @throws IOException
- */
- public EsPatentFamilyDTO addEsPatentFamily(Patent patent, List<String> nos, String type) throws Exception {
- EsPatentFamilyDTO esDTO = new EsPatentFamilyDTO();
- List<String> newNos = new ArrayList<>();
- nos.forEach(item -> newNos.add(item.trim()));
- nos = newNos;
- PatentFamilyMessage patentFamilyMessage = null;
- List<FamilyPatent> familyPatents = new ArrayList<>();
- String id = "";
- PatentFamilyMessageWithId patentFamilyMessageWithId = queryPatentFamily(patent, type);
- if (patentFamilyMessageWithId != null) {
- patentFamilyMessage = patentFamilyMessageWithId.getPatentFamilyMessage();
- id = patentFamilyMessageWithId.getId();
- } else {
- //遍历,根据专利号查询同族
- for (String no : nos) {
- SearchRequest.Builder builder = new SearchRequest.Builder();
- //设置查询索引
- builder.index("patent_family");
- //申请号
- Query q1 = QueryBuilders.term(t -> t.field("patent.app_no").value(no));
- //公开号
- Query q2 = QueryBuilders.term(t -> t.field("patent.public_no").value(no));
- //授权号
- Query q3 = QueryBuilders.term(t -> t.field("patent.grant_no").value(no));
- //类型
- Query q4 = QueryBuilders.term(t -> t.field("family_type").value(type));
- Query p = QueryBuilders.bool(i -> i.should(q1, q2, q3));
- Query bool = QueryBuilders.bool(i -> i.must(q4, p));
- builder.query(bool);
- SearchResponse<PatentFamilyMessage> response = null;
- try {
- response = client.search(builder.build(), PatentFamilyMessage.class);
- } catch (Exception e) {
- throw new XiaoShiException(ExceptionEnum.BUSINESS_ERROR, "ES检索失败");
- }
- long total = response.hits().total().value();
- if (total > 0) {
- List<FamilyPatent> list = new ArrayList<>();
- List<Hit<PatentFamilyMessage>> hits = response.hits().hits();
- Hit<PatentFamilyMessage> hit = hits.get(0);
- id = hit.id();
- patentFamilyMessage = hit.source();
- break;
- }
- }
- }
- if (patentFamilyMessage == null) {
- patentFamilyMessage = new PatentFamilyMessage();
- patentFamilyMessage.setFamilyType(type);
- } else {
- familyPatents = patentFamilyMessage.getPatent();
- familyPatents = this.getDistFamilyPatents(familyPatents);
- }
- //装载专利
- String appNo = "";
- String publicNo = "";
- String grantNo = "";
- if (patent.getAppNo() != null) {
- appNo = patent.getAppNo();
- }
- if (patent.getPublicNo() != null) {
- publicNo = patent.getPublicNo();
- }
- if (patent.getGrantNo() != null) {
- grantNo = patent.getGrantNo();
- }
- String appNo1 = appNo;
- String publicNo1 = publicNo;
- String grantNo1 = grantNo;
- FamilyPatent patent1 = familyPatents.stream().filter(item ->
- publicNo1.equals(item.getPublicNo()) || publicNo1.equals(item.getGrantNo()) || publicNo1.equals(item.getAppNo()) || grantNo1.equals(item.getPublicNo())
- || grantNo1.equals(item.getGrantNo()) || grantNo1.equals(item.getAppNo()) || appNo1.equals(item.getAppNo()) || appNo1.equals(item.getPublicNo()) || appNo1.equals(item.getGrantNo())
- ).findFirst().orElse(null);
- if (patent1 == null) {
- patent1 = new FamilyPatent();
- patent1.setGrantNo(patent.getGrantNo());
- patent1.setAppNo(patent.getAppNo());
- patent1.setPublicNo(patent.getPublicNo());
- familyPatents.add(patent1);
- } else {
- patent1.setGrantNo(patent.getGrantNo());
- patent1.setAppNo(patent.getAppNo());
- patent1.setPublicNo(patent.getPublicNo());
- }
- //装载专利号
- familyPatents = this.getDistFamilyPatentsWithNos(familyPatents, nos);
- patentFamilyMessage.setPatent(familyPatents);
- //当id不为null时
- if (!id.equals("")) {
- this.updatePatentFamily(patentFamilyMessage, id);
- } else {
- try {
- id = this.addPatentFamily(patentFamilyMessage);
- } catch (Exception e) {
- }
- }
- if (familyPatents != null && familyPatents.size() > 0) {
- List<String> temNos = new ArrayList<>();
- familyPatents.forEach(item -> {
- if (item.getGrantNo() != null) {
- temNos.add(item.getGrantNo());
- } else if (item.getPublicNo() != null) {
- temNos.add(item.getPublicNo());
- } else if (item.getAppNo() != null) {
- temNos.add(item.getAppNo());
- }
- });
- esFamilyService.updatePatentFamilyByQuery(type, id, familyPatents.size(), temNos);
- }
- esDTO.setPatentFamilyId(id);
- esDTO.setFamilyNum(patentFamilyMessage.getPatent().size());
- return esDTO;
- }
- private List<FamilyPatent> getDistFamilyPatents(List<FamilyPatent> familyPatents) {
- List<FamilyPatent> familyPatentsNew = new ArrayList<>();
- if (familyPatents == null || familyPatents.size() == 0) {
- return familyPatentsNew;
- }
- for (int i = 0; i < familyPatents.size(); i++) {
- FamilyPatent familyPatent = familyPatents.get(i);
- FamilyPatent familyPatentNew = familyPatentsNew.stream().filter(item -> (item.getPublicNo() != null && (item.getPublicNo().equals(familyPatent.getPublicNo()) || item.getPublicNo().equals(familyPatent.getAppNo()) || item.getPublicNo().equals(familyPatent.getGrantNo())))
- || (item.getGrantNo() != null && (item.getGrantNo().equals(familyPatent.getGrantNo()) || item.getGrantNo().equals(familyPatent.getPublicNo()) || item.getGrantNo().equals(familyPatent.getAppNo())) || (item.getAppNo() != null && (item.getAppNo().equals(familyPatent.getGrantNo()) || item.getAppNo().equals(familyPatent.getPublicNo()) || item.getAppNo().equals(familyPatent.getAppNo()))))).findFirst().orElse(null);
- if (familyPatentNew == null) {
- familyPatentsNew.add(familyPatent);
- } else {
- if (familyPatentNew.getAppNo() == null && familyPatent.getGrantNo() == null) {
- familyPatentsNew.remove(familyPatentNew);
- familyPatentsNew.add(familyPatent);
- }
- }
- }
- return familyPatentsNew;
- }
- private List<FamilyPatent> getDistFamilyPatentsWithNos(List<FamilyPatent> familyPatents, List<String> nos) {
- List<FamilyPatent> familyPatentsNew = new ArrayList<>();
- if (nos.size() == 0 || nos == null) {
- return familyPatents;
- }
- if (familyPatents == null) {
- familyPatents = new ArrayList<>();
- }
- for (int i = 0; i < nos.size(); i++) {
- FamilyPatent familyPatentNew = familyPatents.stream().filter(item -> (item.getPublicNo() != null && item.getPublicNo().equals(no))
- || (item.getGrantNo() != null && item.getGrantNo().equals(no) || (item.getAppNo() != null && item.getAppNo().equals(no)))).findFirst().orElse(null);
- if (familyPatentNew == null) {
- familyPatentNew = new FamilyPatent();
- familyPatentNew.setPublicNo(nos.get(i));
- familyPatents.add(familyPatentNew);
- }
- }
- return familyPatents;
- }
- /**
- * 添加同族
- *
- * @param message
- * @return
- * @throws Exception
- */
- public String addPatentFamily(PatentFamilyMessage message) throws Exception {
- IndexResponse indexResponse = client.index(i -> i
- .index("patent_family")
- .document(message)
- );
- return indexResponse.id();
- }
- /**
- * 更新同族
- *
- * @param message
- * @param id
- * @return
- */
- public Integer updatePatentFamily(PatentFamilyMessage message, String id) {
- UpdateRequest<PatentFamilyMessage, PatentFamilyMessage> req;
- req = UpdateRequest.of(
- b -> b.index("patent_family")
- .id(id)
- .doc(message)
- );
- try {
- client.update(req, PatentFamilyMessage.class);
- return 1;
- } catch (IOException e) {
- return -1;
- }
- }
- /**
- * 更新专利事务
- *
- * @param
- * @param id
- * @return
- */
- public Integer updateLegalEvent(LegalEvent legalEvent, String id) {
- UpdateRequest<LegalEvent, LegalEvent> req;
- req = UpdateRequest.of(
- b -> b.index("legal_event")
- .id(id)
- .doc(legalEvent).refresh(Refresh.True).waitForActiveShards(WaitForActiveShards.of(i -> i.count(1)))
- );
- try {
- client.update(req, LegalEvent.class);
- return 1;
- } catch (IOException e) {
- return -1;
- }
- }
- /**
- * 更新专利事务
- *
- * @param
- * @param id
- * @return
- */
- public Integer updateQuotePatent(PatentQuoteMessage patentQuoteMessage, String id) {
- UpdateRequest<PatentQuoteMessage, PatentQuoteMessage> req;
- req = UpdateRequest.of(
- b -> b.index("quote_patent")
- .id(id)
- .doc(patentQuoteMessage)
- );
- try {
- client.update(req, PatentQuoteMessage.class);
- return 1;
- } catch (IOException e) {
- return -1;
- }
- }
- /**
- * 添加法律事务
- *
- * @param
- * @return
- * @throws Exception
- */
- public String addQuotePatent(PatentQuoteMessage patentQuoteMessage) throws Exception {
- IndexResponse indexResponse = client.index(i -> i
- .index("quote_patent")
- .document(patentQuoteMessage)
- );
- return indexResponse.id();
- }
- public PatentQuoteMessage addEsQuotePatent(Patent patent, List<String> nos) throws Exception {
- if (patent == null) {
- return null;
- }
- QuotePatent quotePatent = this.reQuotePatent(patent);
- PatentQuoteMessage patentQuoteMessage = null;
- //根据申请号和
- String id = "";
- //根据专利号查询是否有引用信息
- SearchRequest.Builder builder = new SearchRequest.Builder();
- //设置查询索引
- builder.index("quote_patent");
- //申请号
- Query q1 = QueryBuilders.term(t -> t.field("patent.app_no").value(patent.getAppNo()));
- Query bool = QueryBuilders.bool(i -> i.must(q1));
- builder.query(bool);
- SearchResponse<PatentQuoteMessage> response = null;
- try {
- response = client.search(builder.build(), PatentQuoteMessage.class);
- } catch (Exception e) {
- throw new XiaoShiException(ExceptionEnum.BUSINESS_ERROR, "ES检索失败");
- }
- long total = response.hits().total().value();
- if (total > 0) {
- patentQuoteMessage = response.hits().hits().get(0).source();
- id = response.hits().hits().get(0).id();
- }
- List<QuotePatent> quotePatents = new ArrayList<>();
- if (patentQuoteMessage != null && patentQuoteMessage.getQuotedPatents() != null) {
- quotePatents = patentQuoteMessage.getQuotedPatents();
- }
- if (nos != null && nos.size() > 0) {
- List<Patent> patents = esPatentService.getPatentsByNo(nos, true, null, null);
- for (String patentNo : nos) {
- QuotePatent quotePatent1 = null;
- if (quotePatents.size() > 0) {
- quotePatent1 = quotePatents.stream()
- .filter(item -> patentNo.equals(item.getAppNo()) || patentNo.equals(item.getPublicNo()) || patentNo.equals(item.getGrantNo())).findFirst().orElse(null);
- }
- Patent patent1 = patents.stream().filter(item -> patentNo.equals(item.getAppNo())).findFirst().orElse(null);
- quotePatent1 = this.reQuotePatent(patent1, patentNo);
- quotePatents.add(quotePatent1);
- }
- }
- if (patentQuoteMessage == null) {
- if (quotePatents.size() != 0) {
- patentQuoteMessage = new PatentQuoteMessage();
- patentQuoteMessage.setPatent(quotePatent);
- patentQuoteMessage.setQuotedPatents(quotePatents);
- this.addQuotePatent(patentQuoteMessage);
- }
- } else {
- patentQuoteMessage.getQuotedPatents().addAll(quotePatents);
- this.updateQuotePatent(patentQuoteMessage, id);
- }
- return patentQuoteMessage;
- }
- private QuotePatent reQuotePatent(Patent patent) {
- QuotePatent quotePatent = new QuotePatent();
- //装载申请人
- quotePatent.setApplicant(patent.getApplicant());
- //装载权利人
- quotePatent.setRightHolder(patent.getRightHolder());
- quotePatent.setAppNo(patent.getAppNo());
- quotePatent.setGrantNo(patent.getGrantNo());
- quotePatent.setPublicNo(patent.getPublicNo());
- return quotePatent;
- }
- private QuotePatent reQuotePatent(Patent patent, String no) {
- QuotePatent quotePatent = new QuotePatent();
- if (patent == null) {
- quotePatent.setPublicNo(no);
- return quotePatent;
- }
- //装载申请人
- quotePatent.setApplicant(patent.getApplicant());
- quotePatent.setRightHolder(patent.getRightHolder());
- quotePatent.setAppNo(patent.getAppNo());
- quotePatent.setGrantNo(patent.getGrantNo());
- quotePatent.setPublicNo(patent.getPublicNo());
- return quotePatent;
- }
- /**
- * 查询权利要求
- *
- * @param patentNo
- * @return
- */
- public SelectClaimDTO selectClaim(String patentNo) throws IOException {
- SearchRequest.Builder builder = new SearchRequest.Builder();
- //设置查询索引
- builder.index("patent");
- //原始数据
- Query q = QueryBuilders.term(t -> t.field("claim.if_origin").value(true));
- //申请号
- Query q1 = QueryBuilders.term(t -> t.field("app_no.keyword").value(patentNo));
- //公开号
- Query q2 = QueryBuilders.term(t -> t.field("public_no.keyword").value(patentNo));
- //授权号
- Query q3 = QueryBuilders.term(t -> t.field("grant_no.keyword").value(patentNo));
- Query query = QueryBuilders.bool(i -> i.should(q1, q2, q3));
- Query bool = QueryBuilders.bool(i -> i.must(q, query));
- builder.query(bool);
- SearchResponse<Patent> response = null;
- try {
- response = client.search(builder.build(), Patent.class);
- } catch (Exception e) {
- throw new XiaoShiException(ExceptionEnum.BUSINESS_ERROR, "ES检索失败");
- }
- SelectClaimDTO dto = new SelectClaimDTO();
- List<Hit<Patent>> hits = response.hits().hits();
- for (Hit<Patent> hit : hits) {
- Patent esMess = hit.source();
- if (esMess != null) {
- dto.setClaimContent(esMess.getClaim());
- dto.setClaim(esMess.getClaim().get(0).getTextContent());
- }
- }
- return dto;
- }
- /**
- * 装载摘要附图
- *
- * @param patentColumnDTOS
- * @return
- */
- public List<PatentColumnDTO> loadCoulumnDTO(List<PatentColumnDTO> patentColumnDTOS) {
- patentColumnDTOS.forEach(item -> {
- item.setPictureGuid(FormatUtil.getPictureFormat(item.getAppNo()));
- });
- return patentColumnDTOS;
- }
- /**
- * 根据专利号查询出其他专利号
- *
- * @param patentNos
- * @return
- * @throws IOException
- */
- public SelectClaimDTO selectPatentNo(List<String> patentNos) throws IOException {
- SearchRequest.Builder builder = new SearchRequest.Builder();
- //设置查询索引
- builder.index("patent");
- List<Query> queryList = new ArrayList<>();
- for (String patentNo : patentNos) {
- Query q1 = QueryBuilders.term(t -> t.field("patent_no.keyword").value(patentNo));
- queryList.add(q1);
- }
- //申请号
- Query query = QueryBuilders.bool(i -> i.mustNot(queryList));
- builder.query(query);
- SearchResponse<Patent> response = null;
- try {
- response = client.search(builder.build(), Patent.class);
- } catch (Exception e) {
- throw new XiaoShiException(ExceptionEnum.BUSINESS_ERROR, "ES检索失败");
- }
- SelectClaimDTO dto = new SelectClaimDTO();
- List<Hit<Patent>> hits = response.hits().hits();
- long value = response.hits().total().value();
- if (value > 1) {
- System.out.println("====================" + value);
- }
- return dto;
- }
- public PatentFamilyMessageWithId queryPatentFamily(Patent patent, String type) throws Exception {
- //查询该专利是否已有同族
- SearchRequest.Builder builderPatent = new SearchRequest.Builder();
- //设置查询索引
- builderPatent.index("patent_family");
- //申请号
- List<Query> queries = new ArrayList<>();
- if (patent.getAppNo() != null && !patent.getAppNo().equals("")) {
- Query q1 = QueryBuilders.term(t -> t.field("patent.app_no").value(patent.getAppNo()));
- //公开号
- Query q2 = QueryBuilders.term(t -> t.field("patent.public_no").value(patent.getAppNo()));
- //授权号
- Query q3 = QueryBuilders.term(t -> t.field("patent.grant_no").value(patent.getAppNo()));
- //类型
- queries.add(q1);
- queries.add(q2);
- queries.add(q3);
- }
- if (patent.getPublicNo() != null && !patent.getPublicNo().equals("")) {
- Query q1 = QueryBuilders.term(t -> t.field("patent.app_no").value(patent.getPublicNo()));
- //公开号
- Query q2 = QueryBuilders.term(t -> t.field("patent.public_no").value(patent.getPublicNo()));
- //授权号
- Query q3 = QueryBuilders.term(t -> t.field("patent.grant_no").value(patent.getPublicNo()));
- queries.add(q1);
- queries.add(q2);
- queries.add(q3);
- }
- if (patent.getGrantNo() != null && !patent.getGrantNo().equals("")) {
- Query q1 = QueryBuilders.term(t -> t.field("patent.app_no").value(patent.getGrantNo()));
- //公开号
- Query q2 = QueryBuilders.term(t -> t.field("patent.public_no").value(patent.getGrantNo()));
- //授权号
- Query q3 = QueryBuilders.term(t -> t.field("patent.grant_no").value(patent.getGrantNo()));
- queries.add(q1);
- queries.add(q2);
- queries.add(q3);
- }
- Query q4 = QueryBuilders.term(t -> t.field("family_type").value(type));
- Query p = QueryBuilders.bool(i -> i.should(queries));
- Query bool = QueryBuilders.bool(i -> i.must(q4, p));
- builderPatent.query(bool);
- SearchResponse<PatentFamilyMessage> response = client.search(builderPatent.build(), PatentFamilyMessage.class);
- long total = response.hits().total().value();
- if (total > 0) {
- List<FamilyPatent> list = new ArrayList<>();
- List<Hit<PatentFamilyMessage>> hits = response.hits().hits();
- Hit<PatentFamilyMessage> hit = hits.get(0);
- String id = hit.id();
- PatentFamilyMessageWithId patentFamilyMessageWithId = new PatentFamilyMessageWithId();
- patentFamilyMessageWithId.setId(id);
- patentFamilyMessageWithId.setPatentFamilyMessage(hit.source());
- return patentFamilyMessageWithId;
- }
- return null;
- }
- //根据父id获取相应专利的专利号
- public List<String> getPatentNos(List<String> ids) throws IOException {
- SearchRequest.Builder builder = new SearchRequest.Builder();
- //设置查询索引
- builder.index("patent");
- Query q = QueryBuilders.ids(i -> i.values(ids));
- builder.size(9999);
- builder.query(q);
- SearchResponse<Patent> response = null;
- try {
- response = client.search(builder.build(), Patent.class);
- } catch (Exception e) {
- throw new XiaoShiException(ExceptionEnum.BUSINESS_ERROR, "ES检索失败");
- }
- List<Hit<Patent>> hits = response.hits().hits();
- List<String> list = new ArrayList<>();
- for (Hit<Patent> hit : hits) {
- Patent patent = hit.source();
- list.add(patent.getPatentNo());
- }
- return list;
- }
- @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Throwable.class)
- public Boolean deletePatents(EsCustomFieldBatchVO vo) throws Exception {
- Integer projectId = vo.getProjectId();
- Integer startNum = vo.getStartNumber();
- Integer endNum = vo.getEndNumber();
- List<String> isAdd = vo.getIsAdd();
- List<String> isDel = vo.getIsDelete();
- if (!CollectionUtils.isEmpty(isAdd) && startNum <= 1 && endNum < 1) {
- isAdd.removeAll(isDel);
- for (String patentNo : isAdd) {
- Integer number = this.getPatent(patentNo, projectId);
- if (number < 1) {
- throw new XiaoShiException(ExceptionEnum.BUSINESS_ERROR, "删除失败");
- }
- }
- } else if (startNum >= 1 && endNum > 0) {
- List<String> patentNos = esCustomFieldService.getPatentNos(vo);
- for (String patentNo : patentNos) {
- Integer number = this.getPatent(patentNo, projectId);
- if (number < 1) {
- throw new XiaoShiException(ExceptionEnum.BUSINESS_ERROR, "删除失败");
- }
- }
- }
- return true;
- }
- public Integer getPatent(String patentNo, Integer projectId) throws IOException {
- SearchRequest.Builder builder = new SearchRequest.Builder();
- //设置查询索引
- builder.index("patent");
- Query query = QueryBuilders.term(i -> i.field("project_id").value(projectId));
- Query q = QueryBuilders.term(i -> i.field("patent_no.keyword").value(patentNo));
- Query query1 = QueryBuilders.hasParent(i -> i.parentType("patent").query(q));
- Query bool = QueryBuilders.bool(i -> i.must(query, query1));
- builder.query(bool);
- //解除最大条数限制
- builder.trackTotalHits(i -> i.enabled(true));
- SearchResponse<Patent> response = null;
- try {
- response = client.search(builder.build(), Patent.class);
- } catch (Exception e) {
- throw new XiaoShiException(ExceptionEnum.BUSINESS_ERROR, "ES检索失败");
- }
- List<Hit<Patent>> hits = response.hits().hits();
- List<String> list = new ArrayList<>();
- for (Hit<Patent> hit : hits) {
- String id = hit.id();
- list.add(id);
- }
- return this.deleteByIds(list);
- }
- public Integer deleteByIds(List<String> ids) {
- Query query = QueryBuilders.ids(n -> n.values(ids));
- DeleteByQueryRequest request = DeleteByQueryRequest.of(i -> i.index("patent").refresh(true).query(query)
- .refresh(true));
- try {
- client.deleteByQuery(request);
- return 1;
- } catch (IOException e) {
- throw new XiaoShiException(ExceptionEnum.BUSINESS_ERROR, "删除失败");
- }
- }
- /**
- * 装载条件
- *
- * @param queryRequest
- * @return
- */
- public EsPatentCommonVO tranPatentRequestToComVO(QueryRequest queryRequest) {
- StringRequest stringRequest = (StringRequest) queryRequest;
- EsPatentCommonVO commonVO = new EsPatentCommonVO();
- try {
- Integer taskId = stringRequest.getTaskId();
- Integer projectId = stringRequest.getProjectId();
- Integer productId = stringRequest.getProductId();
- Long current = stringRequest.getCurrent();
- Long size = stringRequest.getSize();
- String productFrom = stringRequest.getFrom();
- String searchCondition = stringRequest.getSearchQuery();
- String groupField = stringRequest.getGroupField();
- //判断同族分组
- String str = "";
- if (StringUtils.isNotEmpty(groupField)) {
- switch (groupField) {
- case "simpleFamilyId":
- str = "simple_family_id";
- break;
- case "inpadocFamilyId":
- str = "inpadoc_family_id";
- break;
- case "patsnapFamilyId":
- str = "patsnap_family_id";
- break;
- }
- }
- String esField = str;
- commonVO.setTaskId(taskId);
- commonVO.setProjectId(projectId);
- commonVO.setProductId(productId);
- if (StringUtils.isNotEmpty(searchCondition) && searchCondition.contains("罒")) {
- String replace = searchCondition.replace("罒", " OR ");
- commonVO.setSearchCondition(replace);
- } else {
- commonVO.setSearchCondition(searchCondition);
- }
- commonVO.setProductFrom(productFrom);
- commonVO.setCurrent(current);
- commonVO.setSize(size);
- commonVO.setEsField(esField);
- commonVO.setCustomFields(stringRequest.getCustomFields());
- commonVO.setOrderDTOList(stringRequest.getOrderDTOList());
- } catch (Exception e) {
- }
- return commonVO;
- }
- public void judgeQueryPatentRoot(QueryRequest queryRequest) {
- Integer projectId = queryRequest.getProjectId();
- Integer productId = queryRequest.getProductId();
- String productFrom = queryRequest.getFrom();
- if (projectId != null) {
- List<Integer> ids = assoProjectPersonService.getRootProjectIds();
- if (ids != null && (ids.size() == 0 || !ids.contains(projectId))) {
- throw new XiaoShiException(ExceptionEnum.PERMISSION_ERROR, "无法查询此专题库专利");
- }
- }
- }
- public Query getMeanSearch(Query query, String mean) {
- if (mean != null && !mean.trim().equals("")) {
- GetVectorVO getVectorVO = new GetVectorVO();
- getVectorVO.setText(mean);
- List<Double> doubles = pythonApiService.getStrVector(getVectorVO);
- String source = "if (!doc['abstract_vector'].isEmpty()) {return cosineSimilarity(params.queryVector,'abstract_vector');} else {return 0.1; }";
- InlineScript inlineScript = InlineScript.of(i -> i.source(source).lang("painless").params("queryVector", JsonData.of(doubles)));
- Script script = Script.of(i -> i.inline(inlineScript));
- Query newQuery = QueryBuilders.scriptScore(i -> i.script(script)
- .query(query));
- String a = newQuery.toString();
- System.out.println(a);
- return newQuery;
- }
- return query;
- }
- public long getMergePersonCount(Integer projectId, String patentId) {
- SearchRequest.Builder builder = new SearchRequest.Builder();
- //设置查询索引
- builder.index("patent");
- //合并申请人
- Query q = QueryBuilders.term(i -> i.field("merge_applicant.project_id").value(projectId));
- Query query1 = QueryBuilders.nested(i -> i.path("merge_applicant").query(q));
- //合并权利人
- Query q1 = QueryBuilders.term(i -> i.field("merge_right_holder.project_id").value(projectId));
- Query query2 = QueryBuilders.nested(i -> i.path("merge_right_holder").query(q1));
- //合并发明人
- Query q2 = QueryBuilders.term(i -> i.field("merge_inventor.project_id").value(projectId));
- Query query3 = QueryBuilders.nested(i -> i.path("merge_inventor").query(q2));
- Query query = QueryBuilders.bool(i -> i.should(query1, query2, query3));
- Query idQ = QueryBuilders.ids(i -> i.values(Collections.singletonList(patentId)));
- Query parentQ = QueryBuilders.hasParent(i -> i.parentType("patent").query(idQ));
- Query bool = QueryBuilders.bool(i -> i.must(parentQ, query));
- builder.query(bool);
- builder.size(10);
- SearchResponse<Patent> response = null;
- try {
- response = client.search(builder.build(), Patent.class);
- } catch (Exception e) {
- throw new XiaoShiException(ExceptionEnum.BUSINESS_ERROR, "ES检索失败");
- }
- return response.hits().total().value();
- }
- }
|