RAGFlow is an agent-native database: a Retrieval-Augmented Generation (RAG) engine that grew into context infrastructure.
Go agent-harnessagent-native-databaseagentic-aiagentic-retrievalagentic-searchaiai-agentscontext-enginecontext-engineeringcontext-managementharness-engineeringknowledge-compilationragretrieval-augmented-generationretrieval-harnesssearch-harness
fix: correct multi-tenant retrieval scope (#20824)
adwse7a3aef79923
diff --git a/internal/dao/kb.go b/internal/dao/kb.goindex b383d3ae1a..5dcd312b18 100644--- a/internal/dao/kb.go+++ b/internal/dao/kb.go@@ -42,6 +42,45 @@ func GetTenantIDByKBID(ctx context.Context, db *gorm.DB, kbID string) (string, ereturn kb.TenantID, nil}+// TenantKBIDs groups knowledge bases stored in the same tenant index/table.+type TenantKBIDs struct {+ TenantID string+ KBIDs []string+}++// GroupKBIDsByTenant resolves every requested knowledge base and preserves+// first-seen tenant order. Missing datasets must not yield a partial scope.+func GroupKBIDsByTenant(ctx context.Context, db *gorm.DB, kbIDs []string) ([]TenantKBIDs, error) {+ if len(kbIDs) == 0 {+ return nil, nil+ }+ kbIDs = common.Deduplicate(kbIDs)+ kbs, err := NewKnowledgebaseDAO().GetByIDs(ctx, db, kbIDs)+ if err != nil {+ return nil, err+ }+ tenants := make(map[string]string, len(kbs))+ for _, kb := range kbs {+ tenants[kb.ID] = kb.TenantID+ }+ groups := make([]TenantKBIDs, 0)+ positions := make(map[string]int)+ for _, kbID := range kbIDs {+ tenantID, ok := tenants[kbID]+ if !ok || tenantID == "" {+ return nil, fmt.Errorf("dataset %s has no tenant: %w", kbID, gorm.ErrRecordNotFound)+ }+ pos, ok := positions[tenantID]+ if !ok {+ pos = len(groups)+ positions[tenantID] = pos+ groups = append(groups, TenantKBIDs{TenantID: tenantID})+ }+ groups[pos].KBIDs = append(groups[pos].KBIDs, kbID)+ }+ return groups, nil+}+// KnowledgebaseDAO knowledge base data access objecttype KnowledgebaseDAO struct{}diff --git a/internal/engine/elasticsearch/chunk.go b/internal/engine/elasticsearch/chunk.goindex eeff56e0b0..fa5a82d1da 100644--- a/internal/engine/elasticsearch/chunk.go+++ b/internal/engine/elasticsearch/chunk.go@@ -2846,14 +2846,22 @@ func (e *Engine) KNNScores(ctx context.Context, chunks []map[string]interface{},// Extract chunk IDs from first search resultschunkIDs := make([]string, 0, len(chunks))+ indexNames := make([]string, 0, len(chunks))for _, chunk := range chunks {- if id, ok := chunk["_id"].(string); ok {+ if id, ok := chunk["_id"].(string); ok && id != "" {+ indexName, ok := chunk["_index"].(string)+ if !ok || indexName == "" {+ return nil, fmt.Errorf("KNN candidate %s has no index", id)+ }chunkIDs = append(chunkIDs, id)+ indexNames = append(indexNames, indexName)}}if len(chunkIDs) == 0 {return nil, nil}+ chunkIDs = common.Deduplicate(chunkIDs)+ indexNames = common.Deduplicate(indexNames)common.Info("KNNScores starting", zap.Int("chunkCount", len(chunkIDs)), zap.Strings("chunkIDs", chunkIDs), zap.Int("vectorSize", len(queryVector)))@@ -2882,19 +2890,10 @@ func (e *Engine) KNNScores(ctx context.Context, chunks []map[string]interface{},return nil, fmt.Errorf("failed to marshal KNN query: %w", err)}- //common.Info("KNNScores query body", zap.String("body", string(body)))-- // Execute search - use first index name from chunks if available- indexName := ""- if len(chunks) > 0 {- if idx, ok := chunks[0]["_index"].(string); ok {- indexName = idx- }- }-res, err := e.client.Search(e.client.Search.WithContext(ctx),- e.client.Search.WithIndex(indexName),+ e.client.Search.WithIndex(indexNames...),+ e.client.Search.WithAllowPartialSearchResults(false),e.client.Search.WithBody(bytes.NewReader(body)),)if err != nil {diff --git a/internal/engine/elasticsearch/chunk_helpers_test.go b/internal/engine/elasticsearch/chunk_helpers_test.goindex 238c8f0aa2..4f2b38935a 100644--- a/internal/engine/elasticsearch/chunk_helpers_test.go+++ b/internal/engine/elasticsearch/chunk_helpers_test.go@@ -133,6 +133,48 @@ func TestSearchUsesConfiguredKNNNumCandidates(t *testing.T) {}}+func TestKNNScoresSearchesAllCandidateIndices(t *testing.T) {+ searches := 0+ server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {+ searches+++ if r.URL.Path != "/ragflow_a,ragflow_b/_search" {+ t.Errorf("KNN search path = %s, want both candidate indices once", r.URL.Path)+ }+ if r.URL.Query().Get("allow_partial_search_results") != "false" {+ t.Error("partial KNN results must trigger the local reranking fallback")+ }+ var query map[string]interface{}+ if err := json.NewDecoder(r.Body).Decode(&query); err != nil {+ t.Error(err)+ }+ knn, _ := query["knn"].(map[string]interface{})+ filter, _ := json.Marshal(knn["filter"])+ if string(filter) != `{"terms":{"id":["a1","a2","b1"]}}` {+ t.Errorf("KNN candidate filter = %s, want candidates from both indices", filter)+ }+ w.Header().Set("X-Elastic-Product", "Elasticsearch")+ _, _ = w.Write([]byte(`{"hits":{"hits":[{"_id":"a1","_score":0.9},{"_id":"a2","_score":0.8},{"_id":"b1","_score":0.7}]}}`))+ }))+ defer server.Close()+ eng := newTestEngine(t, server.URL)+ chunks := []map[string]interface{}{+ {"_id": "a1", "_index": "ragflow_a"},+ {"_id": "a2", "_index": "ragflow_a"},+ {"_id": "b1", "_index": "ragflow_b"},+ }+ result, err := eng.KNNScores(t.Context(), chunks, []float64{0.1, 0.2}, 3)+ if err != nil {+ t.Fatal(err)+ }+ if got := eng.GetScores(result); !reflect.DeepEqual(got, map[string]float64{"a1": 0.9, "a2": 0.8, "b1": 0.7}) {+ t.Fatalf("KNN scores = %v, want scores from both indices", got)+ }+ result, err = eng.KNNScores(t.Context(), []map[string]interface{}{{"_id": "unscoped"}}, []float64{0.1, 0.2}, 1)+ if err == nil || result != nil || searches != 1 {+ t.Fatalf("missing index: result = %v, err = %v, searches = %d, want fallback without a cluster-wide query", result, err, searches)+ }+}+func TestUpdateSingleMemoryMessageWaitsForRefresh(t *testing.T) {var gotRefresh stringserver := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {diff --git a/internal/engine/elasticsearch/metadata.go b/internal/engine/elasticsearch/metadata.goindex b805ecadc4..bc7f8107cf 100644--- a/internal/engine/elasticsearch/metadata.go+++ b/internal/engine/elasticsearch/metadata.go@@ -723,19 +723,19 @@ func (e *Engine) FilterDocIdsByMetaPushdown(ctx context.Context, sqlDB *gorm.DB,return nil}- // Execute search for each tenant (use first KB to get tenant)- kb := kbIDs[0]- tenantID, err := dao.GetTenantIDByKBID(ctx, sqlDB, kb)+ groups, err := dao.GroupKBIDsByTenant(ctx, sqlDB, kbIDs)if err != nil {- common.Warn("FilterDocIdsByMetaPushdown: failed to get tenant for KB", zap.String("kbID", kb), zap.Error(err))+ common.Warn("FilterDocIdsByMetaPushdown: failed to resolve KB tenants", zap.Strings("kbIDs", kbIDs), zap.Error(err))return nil}- indexName := buildMetadataIndexName(tenantID)-- // Check if index exists- exists, err := e.indexExists(ctx, indexName)- if err != nil || !exists {- return nil+ indexNames := make([]string, 0, len(groups))+ for _, group := range groups {+ indexName := buildMetadataIndexName(group.TenantID)+ exists, err := e.indexExists(ctx, indexName)+ if err != nil || !exists {+ return nil+ }+ indexNames = append(indexNames, indexName)}// Build the filter query using the full meta_filter logic@@ -765,13 +765,17 @@ func (e *Engine) FilterDocIdsByMetaPushdown(ctx context.Context, sqlDB *gorm.DB,res, err := e.client.Search(e.client.Search.WithContext(ctx),- e.client.Search.WithIndex(indexName),+ e.client.Search.WithIndex(indexNames...),+ e.client.Search.WithAllowPartialSearchResults(false),e.client.Search.WithBody(&buf),)- if err != nil || res.IsError() {+ if err != nil {return nil}defer res.Body.Close()+ if res.IsError() {+ return nil+ }var result map[string]interface{}if err := json.NewDecoder(res.Body).Decode(&result); err != nil {diff --git a/internal/engine/elasticsearch/metadata_pushdown_test.go b/internal/engine/elasticsearch/metadata_pushdown_test.gonew file mode 100644index 0000000000..7821241c43--- /dev/null+++ b/internal/engine/elasticsearch/metadata_pushdown_test.go@@ -0,0 +1,98 @@+//+// Copyright 2026 The InfiniFlow Authors. All Rights Reserved.+//+// Licensed under the Apache License, Version 2.0 (the "License");+// you may not use this file except in compliance with the License.+// You may obtain a copy of the License at+//+// http://www.apache.org/licenses/LICENSE-2.0+//+// Unless required by applicable law or agreed to in writing, software+// distributed under the License is distributed on an "AS IS" BASIS,+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.+// See the License for the specific language governing permissions and+// limitations under the License.+//++package elasticsearch++import (+ "fmt"+ "net/http"+ "net/http/httptest"+ "reflect"+ "testing"++ "github.com/glebarez/sqlite"+ "gorm.io/gorm"++ "ragflow/internal/entity"+)++func TestMetadataPushdownSearchesAllTenantIndices(t *testing.T) {+ db, err := gorm.Open(sqlite.Open(":memory:"), &gorm.Config{})+ if err != nil {+ t.Fatal(err)+ }+ if err := db.AutoMigrate(&entity.Knowledgebase{}); err != nil {+ t.Fatal(err)+ }+ status := string(entity.StatusValid)+ kbs := []entity.Knowledgebase{+ {ID: "kb-a", TenantID: "a", Status: &status},+ {ID: "kb-b", TenantID: "a", Status: &status},+ {ID: "kb-c", TenantID: "b", Status: &status},+ }+ if err := db.Create(&kbs).Error; err != nil {+ t.Fatal(err)+ }+ conditions := []map[string]interface{}{{"key": "author", "op": "=", "value": "Alice"}}+ for _, tc := range []struct {+ name string+ missingIndex bool+ missingKB bool+ total int+ }{+ {name: "both tenants", total: 2},+ {name: "overflow falls back", total: metaPushdownMaxSize + 1},+ {name: "missing index falls back", missingIndex: true},+ {name: "missing dataset falls back", missingKB: true},+ } {+ t.Run(tc.name, func(t *testing.T) {+ searches := 0+ srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {+ w.Header().Set("X-Elastic-Product", "Elasticsearch")+ if r.Method == http.MethodHead {+ if tc.missingIndex && r.URL.Path == "/ragflow_doc_meta_b" {+ w.WriteHeader(http.StatusNotFound)+ }+ return+ }+ searches+++ if r.URL.Path != "/ragflow_doc_meta_a,ragflow_doc_meta_b/_search" {+ t.Errorf("search path = %s, want both tenant indices once", r.URL.Path)+ }+ if r.URL.Query().Get("allow_partial_search_results") != "false" {+ t.Error("partial tenant search results must trigger fallback")+ }+ fmt.Fprintf(w, `{"hits":{"total":{"value":%d,"relation":"eq"},"hits":[{"_id":"doc-a"},{"_id":"doc-b"}]}}`, tc.total)+ }))+ defer srv.Close()+ kbIDs := []string{"kb-a", "kb-b", "kb-c", "kb-a"}+ if tc.missingKB {+ kbIDs = append(kbIDs, "missing")+ }+ got := newTestEngine(t, srv.URL).FilterDocIdsByMetaPushdown(t.Context(), db, kbIDs, conditions, "and")+ if tc.missingIndex || tc.missingKB || tc.total > metaPushdownMaxSize {+ if got != nil {+ t.Fatalf("partial result = %v, want fallback", got)+ }+ } else if !reflect.DeepEqual(got, []string{"doc-a", "doc-b"}) {+ t.Fatalf("doc IDs = %v, want both tenants' documents", got)+ }+ if (tc.missingIndex || tc.missingKB) && searches != 0 {+ t.Fatal("searched despite incomplete tenant scope")+ }+ })+ }+}diff --git a/internal/engine/infinity/metadata.go b/internal/engine/infinity/metadata.goindex 65441cb8da..aaf7087343 100644--- a/internal/engine/infinity/metadata.go+++ b/internal/engine/infinity/metadata.go@@ -806,15 +806,12 @@ func (e *Engine) FilterDocIdsByMetaPushdown(ctx context.Context, sqlDB *gorm.DB,return nil}- // Get tenant ID from first KB- tenantID, err := dao.GetTenantIDByKBID(ctx, sqlDB, kbIDs[0])+ groups, err := dao.GroupKBIDsByTenant(ctx, sqlDB, kbIDs)if err != nil {- common.Warn("FilterDocIdsByMetaPushdown: failed to get tenant for KB", zap.String("kbID", kbIDs[0]), zap.Error(err))+ common.Warn("FilterDocIdsByMetaPushdown: failed to resolve KB tenants", zap.Strings("kbIDs", kbIDs), zap.Error(err))return nil}- tableName := buildMetadataTableName(tenantID)-// Build SQL WHERE clause using the full meta_filter logicwhereClause, err := BuildInfinityFilter(conditions, logic)if err != nil {@@ -822,6 +819,33 @@ func (e *Engine) FilterDocIdsByMetaPushdown(ctx context.Context, sqlDB *gorm.DB,return nil}+ return collectMetadataDocIDs(groups, func(group dao.TenantKBIDs) []string {+ return e.filterDocIDsByMetaInTenant(ctx, group.TenantID, group.KBIDs, whereClause)+ })+}++// collectMetadataDocIDs combines complete tenant results within the push-down cap.+// A failed tenant discards all results; zero matches returns a non-nil slice.+func collectMetadataDocIDs(groups []dao.TenantKBIDs, query func(dao.TenantKBIDs) []string) []string {+ docIDs := make([]string, 0)+ for _, group := range groups {+ ids := query(group)+ if ids == nil {+ return nil+ }+ docIDs = append(docIDs, ids...)+ if len(docIDs) > metaPushdownMaxSize {+ common.Warn("FilterDocIdsByMetaPushdown: combined result exceeds push-down cap, falling back to in-memory",+ zap.Int("count", len(docIDs)), zap.Any("tenantGroups", groups))+ return nil+ }+ }+ return common.Deduplicate(docIDs)+}++func (e *Engine) filterDocIDsByMetaInTenant(ctx context.Context, tenantID string, kbIDs []string, whereClause string) []string {+ tableName := buildMetadataTableName(tenantID)+// Add KB filter using IN clause. Escape any single quotes in the IDs// defensively — KB IDs are normally UUIDs, but malformed input must// not be able to break out of the literal and alter the query.@@ -868,11 +892,9 @@ func (e *Engine) FilterDocIdsByMetaPushdown(ctx context.Context, sqlDB *gorm.DB,return nil}- // Detect overflow via the SDK's ExtraInfo payload (a JSON string set- // when total_hits_count is requested). If we can't parse it, log- // and fall through — the in-memory path is still correct, just- // slower.- if total, parsed := totalHitsFromInfinityExtraInfo(qr.ExtraInfo); parsed {+ // A partial tenant result cannot be combined into a definitive answer.+ total, parsed := totalHitsFromInfinityExtraInfo(qr.ExtraInfo)+ if parsed {if total > int64(metaPushdownMaxSize) {common.Warn("FilterDocIdsByMetaPushdown: result exceeds push-down cap, falling back to in-memory",zap.Int64("total", total),@@ -899,6 +921,9 @@ func (e *Engine) FilterDocIdsByMetaPushdown(ctx context.Context, sqlDB *gorm.DB,}}}+ if !parsed && len(docIDs) >= metaPushdownMaxSize {+ return nil+ }common.Debug("FilterDocIdsByMetaPushdown returned doc IDs", zap.Int("count", len(docIDs)))return docIDsdiff --git a/internal/engine/infinity/metadata_pushdown_test.go b/internal/engine/infinity/metadata_pushdown_test.gonew file mode 100644index 0000000000..78a565561e--- /dev/null+++ b/internal/engine/infinity/metadata_pushdown_test.go@@ -0,0 +1,93 @@+// Copyright 2026 The InfiniFlow Authors. All Rights Reserved.+//+// Licensed under the Apache License, Version 2.0 (the "License");+// you may not use this file except in compliance with the License.+// You may obtain a copy of the License at+//+// http://www.apache.org/licenses/LICENSE-2.0+//+// Unless required by applicable law or agreed to in writing, software+// distributed under the License is distributed on an "AS IS" BASIS,+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.+// See the License for the specific language governing permissions and+// limitations under the License.++package infinity++import (+ "fmt"+ "reflect"+ "testing"++ "ragflow/internal/dao"+)++func TestCollectMetadataDocIDs(t *testing.T) {+ groups := []dao.TenantKBIDs{+ {TenantID: "tenant-a", KBIDs: []string{"kb-a", "kb-b"}},+ {TenantID: "tenant-b", KBIDs: []string{"kb-c"}},+ {TenantID: "tenant-c", KBIDs: []string{"kb-d"}},+ }+ docIDs := make([]string, metaPushdownMaxSize+1)+ for i := range docIDs {+ docIDs[i] = fmt.Sprintf("doc-%d", i)+ }+ half := metaPushdownMaxSize / 2+ tests := []struct {+ name string+ results [][]string+ want []string+ wantCalls int+ }{+ {+ name: "merges all tenants and deduplicates",+ results: [][]string{{"doc-a", "shared"}, {"shared", "doc-b"}, {"doc-c"}},+ want: []string{"doc-a", "shared", "doc-b", "doc-c"},+ wantCalls: 3,+ },+ {+ name: "tenant failure discards partial results and stops",+ results: [][]string{{"doc-a"}, nil, {"doc-c"}},+ wantCalls: 2,+ },+ {+ name: "combined overflow falls back and stops",+ results: [][]string{docIDs[:half], docIDs[half:], {"doc-c"}},+ wantCalls: 2,+ },+ {+ name: "exact cap succeeds",+ results: [][]string{docIDs[:half], docIDs[half:metaPushdownMaxSize], {}},+ want: docIDs[:metaPushdownMaxSize],+ wantCalls: 3,+ },+ {+ name: "all tenants have zero matches",+ results: [][]string{{}, {}, {}},+ want: []string{},+ wantCalls: 3,+ },+ }+ for _, tc := range tests {+ t.Run(tc.name, func(t *testing.T) {+ calls := 0+ got := collectMetadataDocIDs(groups, func(group dao.TenantKBIDs) []string {+ if calls >= len(groups) {+ t.Fatal("queried more tenants than requested")+ }+ if !reflect.DeepEqual(group, groups[calls]) {+ t.Fatalf("tenant scope = %#v, want %#v", group, groups[calls])+ }+ ids := tc.results[calls]+ calls+++ return ids+ })+ if !reflect.DeepEqual(got, tc.want) {+ t.Fatalf("doc IDs: length = %d, nil = %t; want length = %d, nil = %t", len(got), got == nil, len(tc.want), tc.want == nil)+ }+ if calls != tc.wantCalls {+ t.Fatalf("tenant queries = %d, want %d", calls, tc.wantCalls)+ }+ })+ }+}diff --git a/internal/service/metadata.go b/internal/service/metadata.goindex 87e7f7102a..29972971ed 100644--- a/internal/service/metadata.go+++ b/internal/service/metadata.go@@ -90,17 +90,8 @@ func (s *MetadataService) GetTenantIDByKBID(ctx context.Context, kbID string) (sreturn dao.GetTenantIDByKBID(ctx, dao.DB, kbID)}-// GetTenantIDByKBIDs retrieves tenant ID from the first knowledge base ID in the list-func (s *MetadataService) GetTenantIDByKBIDs(ctx context.Context, kbIDs []string) (string, error) {- if len(kbIDs) == 0 {- return "", fmt.Errorf("no kb_ids provided")- }- return dao.GetTenantIDByKBID(ctx, dao.DB, kbIDs[0])-}-// SearchMetadataResponse holds the result of a metadata searchtype SearchMetadataResponse struct {- IndexName stringMetadataRecords []map[string]interface{}}@@ -122,40 +113,38 @@ func (s *MetadataService) SearchMetadata(ctx context.Context, kbID, tenantID str}return &SearchMetadataResponse{- IndexName: BuildMetadataIndexName(tenantID),MetadataRecords: searchResult.MetadataRecords,}, nil}-// SearchMetadataByKBs searches the metadata index for multiple knowledge bases+// SearchMetadataByKBs searches each tenant's metadata store, with size as the+// per-tenant limit and the KB filter scoped to that tenant.func (s *MetadataService) SearchMetadataByKBs(ctx context.Context, kbIDs []string, size int) (*SearchMetadataResponse, error) {if len(kbIDs) == 0 {return &SearchMetadataResponse{MetadataRecords: []map[string]interface{}{}}, nil}- tenantID, err := s.GetTenantIDByKBIDs(ctx, kbIDs)+ groups, err := dao.GroupKBIDsByTenant(ctx, dao.DB, kbIDs)if err != nil {return nil, err}- searchReq := &types.SearchMetadataRequest{- TenantID: tenantID,- Offset: 0,- Limit: size,- Filter: map[string]interface{}{- "kb_id": kbIDs,- },- }-- searchResult, err := s.docEngine.SearchMetadata(ctx, searchReq)- if err != nil {- return nil, fmt.Errorf("search failed: %w", err)+ response := &SearchMetadataResponse{MetadataRecords: make([]map[string]interface{}, 0)}+ for _, group := range groups {+ searchReq := &types.SearchMetadataRequest{+ TenantID: group.TenantID,+ Limit: size,+ Filter: map[string]interface{}{+ "kb_id": group.KBIDs,+ },+ }+ searchResult, err := s.docEngine.SearchMetadata(ctx, searchReq)+ if err != nil {+ return nil, fmt.Errorf("metadata search for tenant %s failed: %w", group.TenantID, err)+ }+ response.MetadataRecords = append(response.MetadataRecords, searchResult.MetadataRecords...)}-- return &SearchMetadataResponse{- IndexName: BuildMetadataIndexName(tenantID),- MetadataRecords: searchResult.MetadataRecords,- }, nil+ return response, nil}// MetadataForDocIDs returns doc_id → its metadata fields, reading ONLY the given documentsdiff --git a/internal/service/metadata_resolver_test.go b/internal/service/metadata_resolver_test.goindex 842896fab9..3fedf4d40e 100644--- a/internal/service/metadata_resolver_test.go+++ b/internal/service/metadata_resolver_test.go@@ -18,11 +18,59 @@ package serviceimport ("context"+ "errors"+ "reflect""testing""gorm.io/gorm"++ "ragflow/internal/dao"+ "ragflow/internal/engine/types")+type tenantMetadataEngine struct {+ fakeChatDocEngine+ requests []*types.SearchMetadataRequest+ fail bool+}++func (e *tenantMetadataEngine) SearchMetadata(_ context.Context, req *types.SearchMetadataRequest) (*types.SearchMetadataResult, error) {+ e.requests = append(e.requests, req)+ if e.fail && req.TenantID == "tenant-b" {+ return nil, errors.New("metadata unavailable")+ }+ return &types.SearchMetadataResult{MetadataRecords: []map[string]interface{}{+ {"id": "doc-" + req.TenantID, "meta_fields": map[string]interface{}{"author": "Alice"}},+ }}, nil+}++func TestMetadataFallbackSearchesAllTenants(t *testing.T) {+ pushServiceDB(t, setupServiceTestDB(t))+ insertTestKB(t, "kb-a", "tenant-a", 0, 0, 0)+ insertTestKB(t, "kb-b", "tenant-a", 0, 0, 0)+ insertTestKB(t, "kb-c", "tenant-b", 0, 0, 0)+ eng := &tenantMetadataEngine{}+ svc := NewMetadataServiceForTest(dao.NewKnowledgebaseDAO(), eng)+ meta, err := svc.GetFlattedMetaByKBs(t.Context(), []string{"kb-a", "kb-c", "kb-b", "kb-a"})+ if err != nil {+ t.Fatal(err)+ }+ if got := meta["author"]["Alice"]; !reflect.DeepEqual(got, []string{"doc-tenant-a", "doc-tenant-b"}) {+ t.Fatalf("matching documents = %v, want both tenants", got)+ }+ if len(eng.requests) != 2 || eng.requests[0].TenantID != "tenant-a" || eng.requests[1].TenantID != "tenant-b" {+ t.Fatalf("metadata requests = %#v, want one per tenant", eng.requests)+ }+ if !reflect.DeepEqual(eng.requests[0].Filter["kb_id"], []string{"kb-a", "kb-b"}) ||+ !reflect.DeepEqual(eng.requests[1].Filter["kb_id"], []string{"kb-c"}) {+ t.Fatal("metadata queries were not scoped to each tenant's datasets")+ }+ eng.fail = true+ if got, err := svc.GetFlattedMetaByKBs(t.Context(), []string{"kb-a", "kb-c"}); err == nil || got != nil {+ t.Fatalf("partial metadata = %v, err = %v, want error without partial result", got, err)+ }+}+// pushdownDocEngine is the shared fake with a scripted push-down answer: the agentic// metadata_search adapter is only interesting for what it does with the engine's// nil / non-nil contract.diff --git a/internal/service/nlp/retrieval.go b/internal/service/nlp/retrieval.goindex fcf241c5fb..f32b620130 100644--- a/internal/service/nlp/retrieval.go+++ b/internal/service/nlp/retrieval.go@@ -1076,7 +1076,7 @@ func RetrievalByChildren(chunks []map[string]interface{}, tenantIDs []string, dofor key, childList := range momChunks {momID := key.momIDparentResult, err := docEngine.Search(ctx, &types.SearchRequest{- IndexNames: []string{indexNames[0]},+ IndexNames: indexNames,KbIDs: []string{key.kbID},Limit: 1,IncludeUnavailable: true,diff --git a/internal/service/nlp/retrieval_test.go b/internal/service/nlp/retrieval_test.goindex 8a6dc45861..94229d9687 100644--- a/internal/service/nlp/retrieval_test.go+++ b/internal/service/nlp/retrieval_test.go@@ -270,7 +270,7 @@ func TestRetrievalByChildrenScopesParentLookupToChildDocument(t *testing.T) {"similarity": 0.8,}- got := RetrievalByChildren([]map[string]interface{}{child}, []string{"tenant-1"}, engine, t.Context())+ got := RetrievalByChildren([]map[string]interface{}{child}, []string{"tenant-1", "tenant-2"}, engine, t.Context())if len(got) != 1 || got[0]["chunk_id"] != "child-a" {t.Fatalf("retrieval result = %#v, want fallback child", got)@@ -281,6 +281,12 @@ func TestRetrievalByChildrenScopesParentLookupToChildDocument(t *testing.T) {if engine.parentSearch.Filter["doc_id"] != "doc-a" {t.Fatalf("parent lookup filter = %#v, want doc_id doc-a", engine.parentSearch.Filter)}+ if !slices.Equal(engine.parentSearch.IndexNames, buildIndexNames([]string{"tenant-1", "tenant-2"})) {+ t.Fatalf("parent lookup indices = %v, want both tenant indices", engine.parentSearch.IndexNames)+ }+ if !slices.Equal(engine.parentSearch.KbIDs, []string{"kb-1"}) {+ t.Fatalf("parent lookup KBs = %v, want the child's dataset", engine.parentSearch.KbIDs)+ }if !engine.parentSearch.IncludeUnavailable {t.Fatal("parent lookup must include hidden parent chunks")}