@PostMapping("/executeAnalyze")
@ApiOperation("执行词频分析")
public WebResponse<List<FileUtilExecuteResult>> executeAnalyze(@RequestBody @Validated FileUtilDTO fileDTO) {
List<FileUtilExecuteResult> fileUtilExecuteResults = elasticSearchService.executeAnalyze(fileDTO.getBatchNo(), fileDTO.getKeywords(), fileDTO.getSatisfyAll());
return ResponseUtil.success(fileUtilExecuteResults, "执行成功");
}
@Override
public List<FileUtilExecuteResult> executeAnalyze(String batchNo, List<String> keywords, String satisfyAll) {
boolean isSatisfyAll = (satisfyAll == null || satisfyAll.equals("0")) ? false : true;
List<FileUtilExecuteResult> fileUtilExecuteResults = new ArrayList<>();
Date now = DateUtils.getNow();
QueryWrapper<FileUtilContent> queryWrapper = new QueryWrapper<>();
queryWrapper.eq("batch_no", batchNo);
List<FileUtilContent> fileContentList = contractFileService.list(queryWrapper);
String operateNo = IDUtil.generateNo("", redisTemplate);
try {
for (FileUtilContent fileUtilContent : fileContentList) {
Map<String, Integer> keyWordMap = new HashMap<>();
String id = fileUtilContent.getEsId();
TermVectorsRequest request = new TermVectorsRequest(SystemConstant.ELASTICSEARCH_IDEX_NAME, id);
request.setFields("content");
request.setFieldStatistics(true);
request.setTermStatistics(true);
request.setPositions(true);
request.setOffsets(true);
request.setPayloads(true);
TermVectorsResponse termResponse =
highLevelClient.termvectors(request, RequestOptions.DEFAULT);
boolean found = termResponse.getFound();
if (!found) {
continue;
}
List<TermVectorsResponse.TermVector> termVectorsList = termResponse.getTermVectorsList();
for (TermVectorsResponse.TermVector termVector : termVectorsList) {
List<TermVectorsResponse.TermVector.Term> terms = termVector.getTerms();
for (TermVectorsResponse.TermVector.Term term : terms) {
for (String keyword : keywords) {
if (keyword.equals(term.getTerm())) {
Integer docFreq = term.getTermFreq();
keyWordMap.put(keyword, docFreq);
}
}
}
}
if (!keyWordMap.isEmpty()) {
if (isSatisfyAll && !(keyWordMap.size() == keywords.size())) {
continue;
}
FileUtilExecuteResult fileUtilExecuteResult = new FileUtilExecuteResult();
fileUtilExecuteResult.setId(IDUtil.genUUID());
fileUtilExecuteResult.setBatchNo(batchNo);
fileUtilExecuteResult.setCreateTime(now);
fileUtilExecuteResult.setOperateNo(operateNo);
fileUtilExecuteResult.setTitle(fileUtilContent.getTitle());
fileUtilExecuteResult.setSerialNo(fileUtilContent.getSerialNo());
fileUtilExecuteResult.setResult(keyWordMap.toString());
fileUtilExecuteResult.setPackageName(fileUtilContent.getPackageName());
fileUtilExecuteResult.setContractNo(fileUtilContent.getContractNo());
fileUtilExecuteResult.setContractName(fileUtilContent.getContractName());
fileUtilExecuteResultService.save(fileUtilExecuteResult);
fileUtilExecuteResults.add(fileUtilExecuteResult);
}
}
} catch (IOException e) {
e.printStackTrace();
}
return fileUtilExecuteResults;
}