123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918 |
- package cn.cslg.pas.service.business.es;
- import cn.cslg.pas.common.core.base.IfConstant;
- 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.PatentStarListDTO;
- import cn.cslg.pas.common.dto.business.ContentDetailDTO;
- 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.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.utils.parseQueryToTree.expressManager;
- import cn.cslg.pas.common.utils.parseQueryToTree.operateNode;
- import cn.cslg.pas.common.utils.parseQueryToTree.treeNode;
- import cn.cslg.pas.common.vo.*;
- import cn.cslg.pas.domain.es.*;
- import cn.cslg.pas.service.business.CommonService;
- import cn.cslg.pas.service.common.PatentStarApiService;
- import cn.cslg.pas.service.query.FormatQueryService;
- import co.elastic.clients.elasticsearch.ElasticsearchClient;
- import co.elastic.clients.elasticsearch._types.SortOrder;
- import co.elastic.clients.elasticsearch._types.query_dsl.*;
- import co.elastic.clients.elasticsearch.core.*;
- import co.elastic.clients.elasticsearch.core.search.Hit;
- import com.alibaba.fastjson.JSON;
- import com.alibaba.fastjson2.JSONObject;
- import lombok.RequiredArgsConstructor;
- 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.util.CollectionUtils;
- import java.io.IOException;
- import java.util.ArrayList;
- import java.util.Arrays;
- import java.util.List;
- import java.util.Map;
- @Service
- @RequiredArgsConstructor(onConstructor_ = {@Lazy})
- public class EsService {
- private final ElasticsearchClient client;
- @Autowired
- private FormatQueryService formatQueryService;
- @Autowired
- private PatentStarApiService patentStarApiService;
- /**
- * @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 {
- 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));
- builder.query(query);
- SearchResponse<Patent> response = client.search(builder.build(), 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;
- }
- /**
- * Es检索
- *
- * @param queryRequest 检索条件
- * @return
- */
- public PatentDTO esSearch(QueryRequest queryRequest) throws Exception {
- PatentDTO dto = new PatentDTO();
- Integer taskId =queryRequest.getTaskId();
- String searchCondition = "";
- List<EsCustomFieldValueDTO> customFields = queryRequest.getCustomFields();
- if (!CollectionUtils.isEmpty(customFields)) {
- searchCondition = this.parseCustomField(customFields,queryRequest.getIfHaveChild());
- }
- 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();
- }
- if( taskId != null){
- if (searchCondition != null && !"".equals(searchCondition.trim())) {
- searchCondition = "taskId = " + taskId + " AND " + searchCondition;
- } else {
- searchCondition = "taskId = " + taskId;
- }
- }
- else {
- if (projectId != null) {
- if (searchCondition != null && !"".equals(searchCondition.trim())) {
- searchCondition = "projectId = " + projectId + " AND " + searchCondition;
- } else {
- searchCondition = "projectId = " + projectId;
- }
- }
- }
- SearchRequest.Builder builder = new SearchRequest.Builder();
- //设置查询索引
- builder.index("patent");
- //1. 解析检索条件
- treeNode tree = expressManager.getInstance().Parse(searchCondition, false);
- //格式化检索式
- //3. 从es中检索数据
- Query q = formatQueryService.EsQueryToQuery((operateNode) tree, "patent");
- //4. 返回数据
- builder.query(q);
- //排序
- // List<OrderDTO> dtoList = queryRequest.getOrderDTOList();
- // 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)) {
- // builder.sort(sortOptionsBuilder -> sortOptionsBuilder
- // .field(fieldSortBuilder -> fieldSortBuilder
- // .field(configVO.getEsField()).order(SortOrder.Asc)));
- // } else {
- // builder.sort(sortOptionsBuilder -> sortOptionsBuilder
- // .field(fieldSortBuilder -> fieldSortBuilder
- // .field(configVO.getEsField()).order(SortOrder.Desc)));
- // }
- // }
- // }
- // } else {
- // builder.sort(sortOptionsBuilder -> sortOptionsBuilder
- // .field(fieldSortBuilder -> fieldSortBuilder
- // .field("patent_no.keyword").order(SortOrder.Desc)));
- // }
- //分页
- if (current > 0 && size > 0) {
- builder.from((current.intValue() - 1) * size.intValue()).size(size.intValue());
- }
- SearchResponse<Patent> response = client.search(builder.build(), Patent.class);
- List<PatentColumnDTO> list = new ArrayList<>();
- List<Hit<Patent>> hits = response.hits().hits();
- long total = response.hits().total().value();
- for (Hit<Patent> hit : hits) {
- Patent esMess = hit.source();
- PatentColumnDTO columnDTO = new PatentColumnDTO();
- // columnDTO.setPatentNo(esMess.getPatentNo());
- BeanUtils.copyProperties(esMess, columnDTO);
- ContentVO titleVO = new ContentVO();
- String title = StringUtils.strip(JSON.toJSONString(esMess.getTitle()), "[]");
- ContentDetailDTO titleContent = JSONObject.parseObject(title, ContentDetailDTO.class);
- titleVO.setContent(titleContent.getTextContent());
- columnDTO.setTitle(titleVO);
- ContentVO abstractVO = new ContentVO();
- String abstractStr = StringUtils.strip(JSON.toJSONString(esMess.getAbstractStr()), "[]");
- ContentDetailDTO abstractContent = JSONObject.parseObject(abstractStr, ContentDetailDTO.class);
- abstractVO.setContent(abstractContent.getTextContent());
- columnDTO.setAbstractStr(abstractVO);
- list.add(columnDTO);
- }
- this.loadCoulumnDTO(list);
- dto.setTotal(total);
- dto.setPatents(list);
- dto.setPageNum(current);
- dto.setPageSize(size);
- return dto;
- }
- /**
- * 解析自定义栏位和值
- *
- * @param customFields
- * @return
- */
- public String parseCustomField(List<EsCustomFieldValueDTO> customFields,Boolean ifHaveChild) {
- int m = 1;
- int n = 0;
- StringBuilder builder = new StringBuilder();
- long start = System.currentTimeMillis();
- 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,ifHaveChild);
- } else {
- builder.append("(");
- this.appendStr(customField, builder, m,ifHaveChild);
- }
- }
- } 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,ifHaveChild);
- } else {
- builder.append("(");
- this.appendStr(customField, builder, m,ifHaveChild);
- }
- }
- }
- long end = System.currentTimeMillis();
- System.out.println("耗时" + (end - start));
- return builder.toString();
- }
- public void appendStr(EsCustomFieldValueDTO customField, StringBuilder builder, int m,boolean ifHaveChild) {
- 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 {
- builder.append(s).append(")").append(")");
- }
- }
- } else {
- for (String value : values) {
- builder.append(value).append(")");
- }
- }
- } else {
- builder.append(")");
- }
- }
- /**
- * @param key
- * @param page
- * @param limit
- * @return
- */
- 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.match(v -> v
- .query(key)
- //字段名
- .field("patent_no")
- ));
- //多字段匹配
- // 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 updatePatent(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;
- }
- /**
- * 查询是否存在专利
- *
- * @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 = client.search(builder.build(), Patent.class);
- 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 = client.search(builder.build(), Patent.class);
- long total = response.hits().total().value();
- if (total > 0) {
- flag = true;
- }
- return flag;
- }
- /**
- * 查询同族
- *
- * @param nos
- * @param type
- * @return
- * @throws IOException
- */
- public EsPatentFamilyDTO addEsPatentFamily(List<String> nos, String type) throws IOException {
- EsPatentFamilyDTO esDTO = new EsPatentFamilyDTO();
- List<String> absentList = new ArrayList<>();
- PatentFamilyMessage patentFamilyMessage = null;
- String id = "";
- //遍历,根据专利号查询同族
- for (String no : nos) {
- SearchRequest.Builder builder = new SearchRequest.Builder();
- //设置查询索引
- builder.index("patent_family");
- //申请号
- Query q1 = QueryBuilders.term(t -> t.field("app_no").value(no));
- //公开号
- Query q2 = QueryBuilders.term(t -> t.field("public_no").value(no));
- //授权号
- Query q3 = QueryBuilders.term(t -> t.field("grant_no").value(no));
- //类型
- Query q4 = QueryBuilders.term(t -> t.field("family_type").value(type));
- Query bool = QueryBuilders.bool(i -> i.must(q4).should(q1, q2, q3));
- builder.query(bool);
- SearchResponse<PatentFamilyMessage> response = client.search(builder.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);
- id = hit.id();
- patentFamilyMessage = hit.source();
- break;
- }
- }
- List<String> notInNos = new ArrayList<>();
- //当查询到同族时
- if (patentFamilyMessage != null) {
- List<FamilyPatent> familyPatents = patentFamilyMessage.getPatent();
- for (String no : nos) {
- FamilyPatent familyPatent = familyPatents.stream()
- .filter(item -> item.getAppNo() != null && item.getAppNo().equals(no) ||
- item.getPublicNo() != null && item.getPublicNo().equals(no) ||
- item.getGrantNo() != null && item.getGrantNo().equals(no))
- .findFirst().orElse(null);
- if (familyPatent == null) {
- notInNos.add(no);
- }
- }
- }
- //当未查询到同族时
- else {
- patentFamilyMessage = new PatentFamilyMessage();
- patentFamilyMessage.setFamilyType(type);
- patentFamilyMessage.setPatent(new ArrayList<>());
- notInNos.addAll(nos);
- }
- if (notInNos.size() > 0) {
- //所有专利号的专利详情
- List<StarPatentVO> starPatents = new ArrayList<>();
- PatentStarListDTO patentStarListDTO = new PatentStarListDTO();
- List<PatentStarListDTO> patentStarListDtos = patentStarApiService.splitPatentNoByType(notInNos, patentStarListDTO);
- while (patentStarListDtos.size() > 0) {
- PatentStarListDTO patentStarListDTO1 = patentStarListDtos.remove(0);
- patentStarListDTO1.setPageNum(1);
- patentStarListDTO1.setRowCount(50);
- Map<String, Object> resultMap = patentStarApiService.patentStarSearchApi(patentStarListDTO1);
- if (resultMap == null || (Integer) resultMap.get("total") == 0) {
- continue;
- }
- starPatents.addAll((List<StarPatentVO>) resultMap.get("records"));
- }
- List<FamilyPatent> patents = new ArrayList<>();
- starPatents.forEach(item -> {
- FamilyPatent familyPatent = new FamilyPatent();
- familyPatent.setAppNo(item.getApplicationNo());
- familyPatent.setPublicNo(item.getPublicNo());
- familyPatent.setGrantNo(item.getPublicAccreditNo());
- patents.add(familyPatent);
- });
- patentFamilyMessage.getPatent().addAll(patents);
- //当id不为null时
- if (!id.equals("")) {
- this.updatePatentFamily(patentFamilyMessage, id);
- } else {
- try {
- id = this.addPatentFamily(patentFamilyMessage);
- } catch (Exception e) {
- }
- }
- }
- esDTO.setPatentFamilyId(id);
- esDTO.setFamilyNum(patentFamilyMessage.getPatent().size());
- return esDTO;
- }
- /**
- * 添加同族
- *
- * @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)
- );
- try {
- client.update(req, LegalEvent.class);
- return 1;
- } catch (IOException e) {
- return -1;
- }
- }
- /**
- * 添加法律事务
- *
- * @param
- * @return
- * @throws Exception
- */
- public String addLegalEvent(LegalEvent legalEvent) throws Exception {
- IndexResponse indexResponse = client.index(i -> i
- .index("legal_event")
- .document(legalEvent)
- );
- return indexResponse.id();
- }
- /**
- * 更新专利事务
- *
- * @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 String addEsQuotePatent(String no, List<String> nos) throws Exception {
- StarPatentVO starPatentVO = patentStarApiService.getPatentByNo(no);
- if (starPatentVO == null) {
- return null;
- }
- QuotePatent quotePatent = this.reQuotePatent(starPatentVO);
- 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(starPatentVO.getApplicationNo()));
- Query bool = QueryBuilders.bool(i -> i.must(q1));
- builder.query(bool);
- SearchResponse<PatentQuoteMessage> response = client.search(builder.build(), PatentQuoteMessage.class);
- 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();
- }
- for (String patentNo : nos) {
- StarPatentVO starPatentVO1 = patentStarApiService.getPatentByNo(patentNo);
- QuotePatent quotePatent1 = null;
- if (quotePatents.size() > 0) {
- quotePatent1 = quotePatents.stream()
- .filter(item -> item.getAppNo().equals(starPatentVO1.getApplicationNo())).findFirst().orElse(null);
- }
- if (quotePatent1 == null) {
- quotePatent1 = this.reQuotePatent(starPatentVO1);
- 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 "";
- }
- private QuotePatent reQuotePatent(StarPatentVO starPatentVO) {
- QuotePatent quotePatent = new QuotePatent();
- //装载申请人
- if (starPatentVO.getApplicantStr() != null && !starPatentVO.getApplicantStr().equals("")) {
- List<String> names = Arrays.asList(starPatentVO.getApplicantStr().split(";"));
- List<PatentPerson> patentPeople = new ArrayList<>();
- for (int i = 0; i < names.size(); i++) {
- PatentPerson patentPerson = new PatentPerson();
- patentPerson.setOrder(i + 1);
- patentPerson.setType("1");
- patentPerson.setName(names.get(i));
- patentPeople.add(patentPerson);
- }
- quotePatent.setApplicant(patentPeople);
- }
- //装载权利人
- if (starPatentVO.getCurrentApplicantStr() != null && !starPatentVO.getCurrentApplicantStr().equals("")) {
- List<String> names = Arrays.asList(starPatentVO.getCurrentApplicantStr().split(";"));
- List<PatentPerson> patentPeople = new ArrayList<>();
- for (int i = 0; i < names.size(); i++) {
- PatentPerson patentPerson = new PatentPerson();
- patentPerson.setOrder(0);
- patentPerson.setType("1");
- patentPerson.setName(names.get(i));
- patentPeople.add(patentPerson);
- }
- quotePatent.setRightHolder(patentPeople);
- }
- quotePatent.setAppNo(starPatentVO.getApplicationNo());
- quotePatent.setGrantNo(starPatentVO.getPublicAccreditNo());
- quotePatent.setPublicNo(starPatentVO.getPublicNo());
- return quotePatent;
- }
- public String addEsLegalEvent(String patentNo) {
- LegalEvent legalEvent = null;
- String id = "";
- //根据专利号查询
- StarPatentVO starPatentVO = patentStarApiService.getPatentByNo(patentNo);
- if (starPatentVO == null) {
- return null;
- }
- String cnLegalApiStr = patentStarApiService.getCnLegalApi(starPatentVO.getRowApplicationNo());
- if (cnLegalApiStr != null && !cnLegalApiStr.equals("")) {
- List<LegalEvent> legalEvents = new ArrayList<>();
- //根据专利号查询是否有引用信息
- SearchRequest.Builder builder = new SearchRequest.Builder();
- //设置查询索引
- builder.index("legal_event");
- //申请号
- Query q1 = QueryBuilders.term(t -> t.field("app_no").value(starPatentVO.getApplicationNo()));
- Query bool = QueryBuilders.bool(i -> i.must(q1));
- builder.query(bool);
- SearchResponse<LegalEvent> response = null;
- try {
- response = client.search(builder.build(), LegalEvent.class);
- } catch (IOException e) {
- e.printStackTrace();
- }
- long total = response.hits().total().value();
- if (total > 0) {
- response.hits().hits().forEach(item -> {
- legalEvents.add(item.source());
- });
- }
- List<ChinaLeagalStatus> chinaLeagalStatuses = JSON.parseArray(cnLegalApiStr, ChinaLeagalStatus.class);
- chinaLeagalStatuses.forEach(item -> {
- if (legalEvents.size() > 0) {
- LegalEvent tem = legalEvents.stream().filter(em -> em.getEventDate() != null && em.getEventDate().compareTo(item.getLegalDate()) == 0).findFirst().orElse(null);
- if (tem == null) {
- LegalEvent legalEvent1 = new LegalEvent();
- legalEvent1.setEventDate(item.getLegalDate());
- legalEvent1.setCode(item.getLegalCode());
- legalEvent1.setAppNo(starPatentVO.getApplicationNo());
- legalEvent1.setDescription(item.getLegalStatusInfo());
- legalEvent1.setName(item.getLegalStatus());
- try {
- this.addLegalEvent(legalEvent1);
- } catch (Exception e) {
- }
- }
- }
- });
- }
- return "";
- }
- /**
- * 查询权利要求
- *
- * @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 = client.search(builder.build(), Patent.class);
- SelectClaimDTO dto = new SelectClaimDTO();
- List<Hit<Patent>> hits = response.hits().hits();
- for (Hit<Patent> hit : hits) {
- Patent esMess = hit.source();
- if (esMess != null) {
- String claim = StringUtils.strip(JSON.toJSONString(esMess.getClaim()), "[]");
- ContentDetailDTO contentDetailDTO = JSONObject.parseObject(claim, ContentDetailDTO.class);
- if (contentDetailDTO.getIfOrigin()) {
- dto.setClaim(contentDetailDTO.getTextContent().replaceAll("\t",""));
- }
- }
- }
- return dto;
- }
- 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 = client.search(builder.build(), Patent.class);
- 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;
- }
- }
|