添加获取文件路径接口

This commit is contained in:
luke 2025-06-01 16:23:17 +08:00
parent e859dd2004
commit e19d6097e5
2 changed files with 36 additions and 1 deletions

View File

@ -52,7 +52,14 @@ public abstract class AbstractBaseFileImporter implements DocumentImporter {
logger.warn("路径未匹配 exclude.prefix使用全路径: {}", absPath); logger.warn("路径未匹配 exclude.prefix使用全路径: {}", absPath);
relativePathObj = absPath; relativePathObj = absPath;
} }
String relativePath = relativePathObj.toString().replace("\\", "/"); String relativePath = relativePathObj.toString().replace("\\", "/");
String fileName = path.getFileName().toString();
if(LocalCacheUtil.get(fileName) == null){
// 将文件的路径缓存下来
LocalCacheUtil.put(fileName, relativePath);
}
Long localMTime = Files.getLastModifiedTime(path).toMillis(); Long localMTime = Files.getLastModifiedTime(path).toMillis();
logger.info("absPath: {}", absPath.toString().replace("\\", "/")); logger.info("absPath: {}", absPath.toString().replace("\\", "/"));
logger.info("rerelativePath: {} localMTime {}", relativePath, localMTime); logger.info("rerelativePath: {} localMTime {}", relativePath, localMTime);
@ -98,7 +105,7 @@ public abstract class AbstractBaseFileImporter implements DocumentImporter {
// === 构建文档 === // === 构建文档 ===
Map<String, Object> doc = new HashMap<>(); Map<String, Object> doc = new HashMap<>();
doc.put("filename", path.getFileName().toString()); doc.put("filename", fileName);
doc.put("filepath", relativePath); doc.put("filepath", relativePath);
doc.put("content", content); doc.put("content", content);
doc.put("mtime", localMTime); doc.put("mtime", localMTime);

View File

@ -1,6 +1,10 @@
package com.doc.parser.infrastructure.north.controller; package com.doc.parser.infrastructure.north.controller;
import cn.hutool.core.map.MapUtil;
import cn.hutool.json.JSONUtil;
import com.doc.parser.infrastructure.north.dto.SearchReq; import com.doc.parser.infrastructure.north.dto.SearchReq;
import com.doc.parser.infrastructure.util.LocalCacheUtil;
import com.google.common.collect.Maps;
import org.elasticsearch.action.search.SearchRequest; import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse; import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.*; import org.elasticsearch.client.*;
@ -12,6 +16,8 @@ import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.elasticsearch.search.SearchHit; import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder; import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder;
import org.elasticsearch.search.sort.SortOrder; import org.elasticsearch.search.sort.SortOrder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value; import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
@ -27,6 +33,8 @@ import java.util.regex.Pattern;
@RequestMapping("/search") @RequestMapping("/search")
public class SearchController { public class SearchController {
private static final Logger LOGGER = LoggerFactory.getLogger(SearchController.class);
private final RestHighLevelClient esClient; private final RestHighLevelClient esClient;
@Value("${search.default-page-size:10}") @Value("${search.default-page-size:10}")
@ -76,6 +84,23 @@ public class SearchController {
return buildSearchResponse(response, flatKeywords, page, size); return buildSearchResponse(response, flatKeywords, page, size);
} }
@PostMapping("file-path")
public Map<String, Object> resolveFilePath(@RequestBody Map<String, Object> req) {
if (MapUtil.isEmpty(req) || Objects.isNull(req.get("fileNames")) || !(req.get("fileNames") instanceof List)) {
LOGGER.error("SearchController#resolveFilePath Error. Invalid Input: " + JSONUtil.toJsonStr(req));
return Maps.newHashMap();
}
List<String> fileNames = (List)req.get("fileNames");
Map<String, Object> result = Maps.newHashMap();
fileNames.forEach(name -> {
if(LocalCacheUtil.get(name) != null) {
result.put(name, LocalCacheUtil.get(name));
}
});
return result;
}
private QueryBuilder buildSearchQuery(List<List<String>> keywordGroups) { private QueryBuilder buildSearchQuery(List<List<String>> keywordGroups) {
BoolQueryBuilder outerQuery = QueryBuilders.boolQuery(); BoolQueryBuilder outerQuery = QueryBuilders.boolQuery();
@ -142,6 +167,9 @@ public class SearchController {
return String.join(" ... ", matchedSnippets); return String.join(" ... ", matchedSnippets);
} }
} }