|
@@ -1,5 +1,6 @@
|
|
|
package com.example.xiaoshiweixinback.service.importPatent;
|
|
|
|
|
|
+import cn.hutool.core.util.IdUtil;
|
|
|
import co.elastic.clients.elasticsearch.ElasticsearchClient;
|
|
|
import co.elastic.clients.elasticsearch._types.InlineScript;
|
|
|
import co.elastic.clients.elasticsearch._types.Script;
|
|
@@ -13,18 +14,19 @@ 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.example.xiaoshiweixinback.business.common.ResponseEnum;
|
|
|
+import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
|
import com.example.xiaoshiweixinback.business.common.base.Records;
|
|
|
-import com.example.xiaoshiweixinback.business.common.base.RedisConf;
|
|
|
+import com.example.xiaoshiweixinback.business.common.base.SystemFile;
|
|
|
import com.example.xiaoshiweixinback.business.exception.BusinessException;
|
|
|
import com.example.xiaoshiweixinback.business.redis.RedisService;
|
|
|
import com.example.xiaoshiweixinback.business.utils.BeanUtil;
|
|
|
+import com.example.xiaoshiweixinback.business.utils.FileUtils;
|
|
|
import com.example.xiaoshiweixinback.business.utils.JSONUtil;
|
|
|
import com.example.xiaoshiweixinback.business.utils.RegexUtil;
|
|
|
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.AssoPersonFunction;
|
|
|
+import com.example.xiaoshiweixinback.domain.AssoProductFile;
|
|
|
import com.example.xiaoshiweixinback.domain.Product;
|
|
|
import com.example.xiaoshiweixinback.domain.es.PatentVector;
|
|
|
import com.example.xiaoshiweixinback.entity.dto.esPicture.EsPictureNoDTO;
|
|
@@ -34,13 +36,15 @@ import com.example.xiaoshiweixinback.entity.dto.searchRecord.AddSearchRecordDTO;
|
|
|
import com.example.xiaoshiweixinback.entity.dto.searchRecord.SearchRecordConditionDTO;
|
|
|
import com.example.xiaoshiweixinback.entity.vo.esPicture.EsPictureNoVo;
|
|
|
import com.example.xiaoshiweixinback.entity.vo.esPicture.EsPatentVectorVo;
|
|
|
+import com.example.xiaoshiweixinback.entity.vo.esPicture.GetImageVO;
|
|
|
+import com.example.xiaoshiweixinback.mapper.AssoProductFileMapper;
|
|
|
import com.example.xiaoshiweixinback.mapper.ProductMapper;
|
|
|
import com.example.xiaoshiweixinback.service.SearchRecordService;
|
|
|
import com.example.xiaoshiweixinback.service.VipService;
|
|
|
import com.example.xiaoshiweixinback.service.common.FileManagerService;
|
|
|
import com.example.xiaoshiweixinback.service.common.GetVectorService;
|
|
|
-import com.example.xiaoshiweixinback.service.importPatent.FormatQueryService;
|
|
|
import lombok.RequiredArgsConstructor;
|
|
|
+import org.apache.commons.compress.utils.IOUtils;
|
|
|
import org.apache.commons.lang3.StringUtils;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
@@ -50,8 +54,7 @@ import org.springframework.transaction.annotation.Propagation;
|
|
|
import org.springframework.transaction.annotation.Transactional;
|
|
|
import org.springframework.util.CollectionUtils;
|
|
|
|
|
|
-import java.io.File;
|
|
|
-import java.io.IOException;
|
|
|
+import java.io.*;
|
|
|
import java.text.SimpleDateFormat;
|
|
|
import java.util.*;
|
|
|
import java.util.stream.Collectors;
|
|
@@ -75,6 +78,9 @@ public class EsDenseVectorService {
|
|
|
private ProductMapper productMapper;
|
|
|
|
|
|
@Autowired
|
|
|
+ private AssoProductFileMapper assoProductFileMapper;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
private FileManagerService fileManagerService;
|
|
|
|
|
|
@Lazy
|
|
@@ -86,10 +92,11 @@ public class EsDenseVectorService {
|
|
|
|
|
|
@Autowired
|
|
|
private RedisService redisService;
|
|
|
- @Value("${ES.patentVector}")
|
|
|
- private String patentVectorName;
|
|
|
- @Value("${ES.patent}")
|
|
|
- private String patentMapName;
|
|
|
+ @Value("${ES.patentVector}")
|
|
|
+ private String patentVectorName;
|
|
|
+ @Value("${ES.patent}")
|
|
|
+ private String patentMapName;
|
|
|
+
|
|
|
/**
|
|
|
* 根据图片排序获取列表
|
|
|
*
|
|
@@ -118,22 +125,11 @@ public class EsDenseVectorService {
|
|
|
q = formatQueryService.EsQueryToQuery((operateNode) tree, "patentVector", null);
|
|
|
}
|
|
|
if (q == null) {
|
|
|
- throw new BusinessException("608","检索式错误,请重新输入");
|
|
|
+ throw new BusinessException("608", "检索式错误,请重新输入");
|
|
|
}
|
|
|
- //获取图片向量
|
|
|
- List<Float> imageList = new ArrayList<>();
|
|
|
- List<String> stringList = new ArrayList<>();
|
|
|
- List<String> getGuids = new ArrayList<>();
|
|
|
- if (file != null && file.exists() && file.length() != 0) {
|
|
|
- stringList = getVectorService.getVectorByFile(file);
|
|
|
- getGuids = fileManagerService.uploadFileGetGuid2(Collections.singletonList(file));
|
|
|
- } else if (StringUtils.isNotEmpty(dto.getDescription())) {
|
|
|
- stringList = getVectorService.getVectorByText(dto.getDescription());
|
|
|
- }
|
|
|
- stringList.forEach(item -> {
|
|
|
- Float a = Float.parseFloat(item);
|
|
|
- imageList.add(a);
|
|
|
- });
|
|
|
+ GetImageVO imageVO = this.getImageList(dto.getProductId(), file, dto.getDescription());
|
|
|
+ List<Float> imageList = imageVO.getImageList();
|
|
|
+ List<String> getGuids = imageVO.getGetGuids();
|
|
|
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));
|
|
@@ -233,6 +229,58 @@ public class EsDenseVectorService {
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
+ * 获取图片矢量
|
|
|
+ *
|
|
|
+ * @param productId
|
|
|
+ * @param file
|
|
|
+ * @param description
|
|
|
+ * @return
|
|
|
+ * @throws IOException
|
|
|
+ */
|
|
|
+ public GetImageVO getImageList(Integer productId, File file, String description) throws IOException {
|
|
|
+ GetImageVO imageVO = new GetImageVO();
|
|
|
+ //获取图片向量
|
|
|
+ List<Float> imageList = new ArrayList<>();
|
|
|
+ List<String> stringList = new ArrayList<>();
|
|
|
+ List<String> getGuids = new ArrayList<>();
|
|
|
+ if (productId != null) {
|
|
|
+ List<AssoProductFile> productFiles = assoProductFileMapper.selectList(new LambdaQueryWrapper<AssoProductFile>()
|
|
|
+ .eq(AssoProductFile::getProductId, productId));
|
|
|
+ if (!CollectionUtils.isEmpty(productFiles)) {
|
|
|
+ String fileGuid = productFiles.get(0).getFileGuid();
|
|
|
+ if (StringUtils.isNotEmpty(fileGuid)) {
|
|
|
+ //获取文件信息
|
|
|
+ byte[] bytes = fileManagerService.downloadSystemFileFromFMS(fileGuid);
|
|
|
+ getGuids.add(fileGuid);
|
|
|
+ List<SystemFile> systemFileByGuids = fileManagerService.getSystemFileByGuids(getGuids);
|
|
|
+ if (!CollectionUtils.isEmpty(systemFileByGuids)) {
|
|
|
+ SystemFile systemFile = systemFileByGuids.get(0);
|
|
|
+ String suffix = systemFile.getFileName().substring(systemFile.getFileName().lastIndexOf("."));
|
|
|
+ String prefix = IdUtil.simpleUUID();
|
|
|
+ File tempFile = FileUtils.getFileByBytes(bytes, prefix, suffix);
|
|
|
+ stringList = getVectorService.getVectorByFile(tempFile);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ if (file != null && file.exists() && file.length() != 0) {
|
|
|
+ stringList = getVectorService.getVectorByFile(file);
|
|
|
+ getGuids = fileManagerService.uploadFileGetGuid2(Collections.singletonList(file));
|
|
|
+ } else if (StringUtils.isNotEmpty(description)) {
|
|
|
+ stringList = getVectorService.getVectorByText(description);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ stringList.forEach(item -> {
|
|
|
+ Float a = Float.parseFloat(item);
|
|
|
+ imageList.add(a);
|
|
|
+ });
|
|
|
+ imageVO.setImageList(imageList);
|
|
|
+ imageVO.setGetGuids(getGuids);
|
|
|
+ return imageVO;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
* 拼接检索条件
|
|
|
*
|
|
|
* @param productId
|