use of com.qiwenshare.file.config.es.FileSearch in project qiwen-file by qiwenshare.
the class FileDealComp method uploadESByUserFileId.
public void uploadESByUserFileId(Long userFileId) {
try {
Map<String, Object> param = new HashMap<>();
param.put("userFileId", userFileId);
List<UserFile> userfileResult = userFileMapper.selectByMap(param);
if (userfileResult != null && userfileResult.size() > 0) {
FileSearch fileSearch = new FileSearch();
BeanUtil.copyProperties(userfileResult.get(0), fileSearch);
// if (fileSearch.getIsDir() == 0) {
//
// Reader reader = ufopFactory.getReader(fileSearch.getStorageType());
// ReadFile readFile = new ReadFile();
// readFile.setFileUrl(fileSearch.getFileUrl());
// String content = reader.read(readFile);
// //全文搜索
// fileSearch.setContent(content);
//
// }
elasticSearchService.save(fileSearch);
}
} catch (Exception e) {
log.debug("ES更新操作失败,请检查配置");
}
}
use of com.qiwenshare.file.config.es.FileSearch in project qiwen-file by qiwenshare.
the class FileController method searchFile.
@Operation(summary = "文件搜索", description = "文件搜索", tags = { "file" })
@GetMapping(value = "/search")
@MyLog(operation = "文件搜索", module = CURRENT_MODULE)
@ResponseBody
public RestResult<SearchHits<FileSearch>> searchFile(SearchFileDTO searchFileDTO) {
JwtUser sessionUserBean = SessionUtil.getSession();
NativeSearchQueryBuilder queryBuilder = new NativeSearchQueryBuilder();
HighlightBuilder.Field allHighLight = new HighlightBuilder.Field("*").preTags("<span class='keyword'>").postTags("</span>");
queryBuilder.withHighlightFields(allHighLight);
// 设置分页
int currentPage = (int) searchFileDTO.getCurrentPage() - 1;
int pageCount = (int) (searchFileDTO.getPageCount() == 0 ? 10 : searchFileDTO.getPageCount());
String order = searchFileDTO.getOrder();
Sort.Direction direction = null;
if (searchFileDTO.getDirection() == null) {
direction = Sort.Direction.DESC;
} else if ("asc".equals(searchFileDTO.getDirection())) {
direction = Sort.Direction.ASC;
} else if ("desc".equals(searchFileDTO.getDirection())) {
direction = Sort.Direction.DESC;
} else {
direction = Sort.Direction.DESC;
}
if (order == null) {
queryBuilder.withPageable(PageRequest.of(currentPage, pageCount));
} else {
queryBuilder.withPageable(PageRequest.of(currentPage, pageCount, Sort.by(direction, order)));
}
DisMaxQueryBuilder disMaxQueryBuilder = QueryBuilders.disMaxQuery();
QueryBuilder q1 = QueryBuilders.boolQuery().must(QueryBuilders.multiMatchQuery(searchFileDTO.getFileName(), "fileName", "content")).must(QueryBuilders.termQuery("userId", sessionUserBean.getUserId())).boost(// 分词
1f);
QueryBuilder q2 = QueryBuilders.boolQuery().must(QueryBuilders.wildcardQuery("fileName", "*" + searchFileDTO.getFileName() + "*")).must(QueryBuilders.wildcardQuery("content", "*" + searchFileDTO.getFileName() + "*")).must(QueryBuilders.termQuery("userId", sessionUserBean.getUserId())).boost(// 模糊匹配
2f);
disMaxQueryBuilder.add(q1);
disMaxQueryBuilder.add(q2);
NativeSearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(disMaxQueryBuilder).withHighlightFields(allHighLight).build();
//
// queryBuilder.withQuery(QueryBuilders.boolQuery()
// // .must(QueryBuilders.matchQuery("fileName", searchFileDTO.getFileName()))
// .must(QueryBuilders.multiMatchQuery(searchFileDTO.getFileName(),"fileName", "content"))
// .must(QueryBuilders.termQuery("userId", sessionUserBean.getUserId()))
// ).withQuery(QueryBuilders.wildcardQuery("fileName", "*" + searchFileDTO.getFileName() + "*"));
SearchHits<FileSearch> search = elasticsearchRestTemplate.search(searchQuery, FileSearch.class);
return RestResult.success().data(search);
}
Aggregations