This commit is contained in:
Luke.Ye 2025-07-08 12:45:50 +08:00
parent 068bf0419c
commit f9507b5d92
2 changed files with 40 additions and 7 deletions

View File

@ -78,13 +78,10 @@ public class FileQueryController {
LOGGER.error("SearchController#resolveFilePath Error. Invalid Input: " + JSONUtil.toJsonStr(req)); LOGGER.error("SearchController#resolveFilePath Error. Invalid Input: " + JSONUtil.toJsonStr(req));
return Maps.newHashMap(); return Maps.newHashMap();
} }
List<String> namesWithRelativePath = (List)req.get("fileNames"); List<String> fileNames = (List<String>) req.get("fileNames");
Map<String, Object> filePaths = Maps.newHashMap();
namesWithRelativePath.forEach(pathFile -> { Map<String, String> filePaths = esGateway.getUrlsByFileNames(fileNames);
if(fileCacheService.getMeta(pathFile).isPresent()) {
filePaths.put(pathFile, CacheUtil.loadFileMetaProp(pathFile, DocMetaPropEnum.ACCESS_URL.code, String.class));
}
});
Map<String, Object> result = Maps.newHashMap(); Map<String, Object> result = Maps.newHashMap();
result.put("filePaths", filePaths); result.put("filePaths", filePaths);
return result; return result;

View File

@ -33,6 +33,7 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import java.io.IOException; import java.io.IOException;
import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.stream.Collectors; import java.util.stream.Collectors;
@ -275,5 +276,40 @@ public class FileElasticsearchGateway {
} }
} }
/**
* 根据 fileName带相对路径批量查出 url
*/
public Map<String, String> getUrlsByFileNames(List<String> fileNames) {
Map<String, String> 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<String, Object> 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;
}
} }