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(dify): keep retrieval unscoped when metadata conditions are empty (#21766)
KingPointerdf647ddaf210
diff --git a/internal/handler/dify_retrieval_handler.go b/internal/handler/dify_retrieval_handler.goindex 5bee1ecb0c..4c3bdff5f4 100644--- a/internal/handler/dify_retrieval_handler.go+++ b/internal/handler/dify_retrieval_handler.go@@ -247,19 +247,22 @@ func (h *DifyRetrievalHandler) Retrieval(c *gin.Context) {return}- // Metadata filter+ // Metadata filter. An empty conditions list means no metadata filtering,+ // the same as the dataset retrieval path. Only conditions that were+ // actually supplied, and matched nothing, narrow the search to the+ // no-match sentinel.metas, metaErr := h.metadataSvc.GetFlattedMetaByKBs(ctx, []string{req.KnowledgeID})docIDs := make([]string, 0)- if metaErr == nil && req.MetadataCondition != nil {+ if metaErr == nil && req.MetadataCondition != nil && len(req.MetadataCondition.Conditions) > 0 {logic := req.MetadataCondition.Logicif logic == "" {logic = "and"}filteredIDs := service.ApplyMetaFilter(metas, req.MetadataCondition.toMetaFilterConditions(), logic)docIDs = append(docIDs, filteredIDs...)- }- if len(docIDs) == 0 && req.MetadataCondition != nil {- docIDs = []string{service.NoMatchDocIDSentinel}+ if len(docIDs) == 0 {+ docIDs = []string{service.NoMatchDocIDSentinel}+ }}// Hand the label result over unconditionally: an empty map says "issue nodiff --git a/internal/handler/dify_retrieval_handler_test.go b/internal/handler/dify_retrieval_handler_test.goindex 38517b95eb..29a71c0506 100644--- a/internal/handler/dify_retrieval_handler_test.go+++ b/internal/handler/dify_retrieval_handler_test.go@@ -415,6 +415,59 @@ func TestDifyRetrieval_WithMetadataFilter(t *testing.T) {}}+func TestDifyRetrieval_EmptyMetadataConditionDoesNotUseSentinel(t *testing.T) {+ h, r := setupDifyTest("user1")+ var docIDs []string+ h.retrievalSvc = &mockRetrievalService{+ retrievalFn: func(ctx context.Context, req *nlp.RetrievalRequest) (*nlp.RetrievalResult, error) {+ docIDs = append([]string(nil), req.DocIDs...)+ return &nlp.RetrievalResult{+ Chunks: []map[string]interface{}{+ {"doc_id": "doc1", "docnm_kwd": "Test Doc", "content_with_weight": "test content", "similarity": 0.85},+ },+ }, nil+ },+ }+ body := `{"knowledge_id":"kb1","query":"test","metadata_condition":{"logic":"and","conditions":[]}}`+ w := httptest.NewRecorder()+ req, _ := http.NewRequest("POST", "/api/v1/dify/retrieval", strings.NewReader(body))+ req.Header.Set("Content-Type", "application/json")+ r.ServeHTTP(w, req)+ if w.Code != http.StatusOK {+ t.Fatalf("expected 200, got %d: %s", w.Code, w.Body.String())+ }+ if len(docIDs) != 0 {+ t.Fatalf("DocIDs = %v, want an unscoped search", docIDs)+ }+}++func TestDifyRetrieval_UnmatchedMetadataConditionUsesSentinel(t *testing.T) {+ h, r := setupDifyTest("user1")+ var docIDs []string+ h.retrievalSvc = &mockRetrievalService{+ retrievalFn: func(ctx context.Context, req *nlp.RetrievalRequest) (*nlp.RetrievalResult, error) {+ docIDs = append([]string(nil), req.DocIDs...)+ return &nlp.RetrievalResult{Chunks: []map[string]interface{}{}}, nil+ },+ }+ h.metadataSvc = &mockMetadataService{+ getFlattedMetaFn: func(ctx context.Context, kbIDs []string) (common.MetaData, error) {+ return common.MetaData{}, nil+ },+ }+ body := `{"knowledge_id":"kb1","query":"test","metadata_condition":{"conditions":[{"name":"author","comparison_operator":"is","value":"nobody"}],"logic":"and"}}`+ w := httptest.NewRecorder()+ req, _ := http.NewRequest("POST", "/api/v1/dify/retrieval", strings.NewReader(body))+ req.Header.Set("Content-Type", "application/json")+ r.ServeHTTP(w, req)+ if w.Code != http.StatusOK {+ t.Fatalf("expected 200, got %d: %s", w.Code, w.Body.String())+ }+ if len(docIDs) != 1 || docIDs[0] != service.NoMatchDocIDSentinel {+ t.Fatalf("DocIDs = %v, want no-match sentinel", docIDs)+ }+}+func TestDifyRetrieval_InvalidJSON(t *testing.T) {_, r := setupDifyTest("user1")w := httptest.NewRecorder()