multiLibraryVectorSearchLegacy 原来的多库搜索流程(用于测试 mock 场景)
(eid int64, req *SearchRequest, configService *ChunkConfigService)
| 3535 | |
| 3536 | // multiLibraryVectorSearchLegacy 原来的多库搜索流程(用于测试 mock 场景) |
| 3537 | func (s *SearchService) multiLibraryVectorSearchLegacy(eid int64, req *SearchRequest, configService *ChunkConfigService) ([]SearchResultItem, error) { |
| 3538 | logger.SysDebugf("🔍 开始多知识库向量搜索-Legacy (eid=%d, libraries=%v)", eid, req.LibraryIDs) |
| 3539 | |
| 3540 | // 并发搜索各个知识库 |
| 3541 | type libraryResult struct { |
| 3542 | libraryID int64 |
| 3543 | results []SearchResultItem |
| 3544 | err error |
| 3545 | } |
| 3546 | |
| 3547 | resultChan := make(chan libraryResult, len(req.LibraryIDs)) |
| 3548 | maxConcurrentSearches := multiLibrarySearchConcurrencyLimit() |
| 3549 | sem := make(chan struct{}, maxConcurrentSearches) |
| 3550 | |
| 3551 | // 为每个知识库启动独立的搜索 |
| 3552 | for _, libraryID := range req.LibraryIDs { |
| 3553 | sem <- struct{}{} |
| 3554 | go func(libID int64) { |
| 3555 | defer func() { |
| 3556 | <-sem |
| 3557 | }() |
| 3558 | |
| 3559 | // 创建单知识库搜索请求 |
| 3560 | singleReq := &SearchRequest{ |
| 3561 | Query: req.Query, |
| 3562 | SearchType: req.SearchType, |
| 3563 | TopK: req.TopK, |
| 3564 | LibraryIDs: []int64{libID}, |
| 3565 | FileIDs: req.FileIDs, |
| 3566 | ChunkTypes: req.ChunkTypes, |
| 3567 | SearchConfig: normalizeSearchConfigForExecution(req.SearchConfig), |
| 3568 | trace: req.trace, |
| 3569 | } |
| 3570 | |
| 3571 | results, err := singleVectorSearchFn(s, eid, singleReq, configService) |
| 3572 | resultChan <- libraryResult{ |
| 3573 | libraryID: libID, |
| 3574 | results: results, |
| 3575 | err: err, |
| 3576 | } |
| 3577 | }(libraryID) |
| 3578 | } |
| 3579 | |
| 3580 | // 收集所有结果 |
| 3581 | var allResults []SearchResultItem |
| 3582 | var errors []error |
| 3583 | |
| 3584 | for i := 0; i < len(req.LibraryIDs); i++ { |
| 3585 | result := <-resultChan |
| 3586 | if result.err != nil { |
| 3587 | logger.SysLogf("❌ 知识库%d搜索失败: %v", result.libraryID, result.err) |
| 3588 | errors = append(errors, fmt.Errorf("知识库%d: %v", result.libraryID, result.err)) |
| 3589 | } else { |
| 3590 | logger.SysLogf("✅ 知识库%d搜索成功,返回%d个结果", result.libraryID, len(result.results)) |
| 3591 | allResults = append(allResults, result.results...) |
| 3592 | } |
| 3593 | } |
| 3594 |
no test coverage detected