diff --git a/src/main/java/com/knowledge/base/infrastructure/north/controller/FileQueryController.java b/src/main/java/com/knowledge/base/infrastructure/north/controller/FileQueryController.java index f1d1020..209a6fb 100644 --- a/src/main/java/com/knowledge/base/infrastructure/north/controller/FileQueryController.java +++ b/src/main/java/com/knowledge/base/infrastructure/north/controller/FileQueryController.java @@ -78,13 +78,10 @@ public class FileQueryController { LOGGER.error("SearchController#resolveFilePath Error. Invalid Input: " + JSONUtil.toJsonStr(req)); return Maps.newHashMap(); } - List namesWithRelativePath = (List)req.get("fileNames"); - Map filePaths = Maps.newHashMap(); - namesWithRelativePath.forEach(pathFile -> { - if(fileCacheService.getMeta(pathFile).isPresent()) { - filePaths.put(pathFile, CacheUtil.loadFileMetaProp(pathFile, DocMetaPropEnum.ACCESS_URL.code, String.class)); - } - }); + List fileNames = (List) req.get("fileNames"); + + Map filePaths = esGateway.getUrlsByFileNames(fileNames); + Map result = Maps.newHashMap(); result.put("filePaths", filePaths); return result; diff --git a/src/main/java/com/knowledge/base/infrastructure/south/es/FileElasticsearchGateway.java b/src/main/java/com/knowledge/base/infrastructure/south/es/FileElasticsearchGateway.java index 5f523d9..7eaf67f 100644 --- a/src/main/java/com/knowledge/base/infrastructure/south/es/FileElasticsearchGateway.java +++ b/src/main/java/com/knowledge/base/infrastructure/south/es/FileElasticsearchGateway.java @@ -33,6 +33,7 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import java.io.IOException; +import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.stream.Collectors; @@ -275,5 +276,40 @@ public class FileElasticsearchGateway { } } + /** + * 根据 fileName(带相对路径)批量查出 url + */ + public Map getUrlsByFileNames(List fileNames) { + Map filePathUrlMap = new HashMap<>(); + if (CollectionUtil.isEmpty(fileNames)) { + return filePathUrlMap; + } + // ES 批量 term 查询 + BoolQueryBuilder boolQuery = QueryBuilders.boolQuery(); + boolQuery.must(QueryBuilders.termsQuery(FileEsField.FILEPATH, fileNames)); + boolQuery.filter(QueryBuilders.rangeQuery(FileEsField.EXPIRE_TIME) + .gte(System.currentTimeMillis()) + .timeZone("+08:00")); + + SearchSourceBuilder builder = new SearchSourceBuilder() + .query(boolQuery) + .size(fileNames.size()); // 最多查 fileNames.size() 条 + SearchRequest request = new SearchRequest(INDEX_NAME).source(builder); + + try { + SearchResponse response = esClient.search(request, RequestOptions.DEFAULT); + for (SearchHit hit : response.getHits().getHits()) { + Map src = hit.getSourceAsMap(); + String filePath = String.valueOf(src.get(FileEsField.FILEPATH)); + String url = String.valueOf(src.get("url")); + filePathUrlMap.put(filePath, url); + } + } catch (IOException e) { + log.error("[getUrlsByFileNames] error.", e); + } + return filePathUrlMap; + } + + }