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(chunk): keep the chunk list inside the requested document scope (#20914)
qinling0210560702778ca0
diff --git a/go.mod b/go.modindex f3d8935193..7a9b45766a 100644--- a/go.mod+++ b/go.mod@@ -270,6 +270,6 @@ require (modernc.org/sqlite v1.23.1 // indirect)-replace github.com/infiniflow/infinity-go-sdk => github.com/infiniflow/infinity/go v0.0.0-20261009034419-e1bd341a2083+replace github.com/infiniflow/infinity-go-sdk => github.com/infiniflow/infinity/go v0.0.0-20261013110814-32a6afce89dfreplace github.com/AkmalOt/gomsg => github.com/infiniflow/gomsg v0.0.0-20260407083308-985c3a1a76b7diff --git a/go.sum b/go.sumindex 48130b2922..61ade56138 100644--- a/go.sum+++ b/go.sum@@ -359,8 +359,8 @@ github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4Tgithub.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU=github.com/infiniflow/gomsg v0.0.0-20260407083308-985c3a1a76b7 h1:/4UAJqF04Rs8bPfJHEUvOpmv6cxX2hKXHscMktRiskU=github.com/infiniflow/gomsg v0.0.0-20260407083308-985c3a1a76b7/go.mod h1:TCjWm+lo7de/zYHb8rHfVSaKxOP4tZNrnRUzkqV4r28=-github.com/infiniflow/infinity/go v0.0.0-20261009034419-e1bd341a2083 h1:NJ8AHjkqckkTrNvy/d2jm4mCypbvmA+iz//OXrNtn6k=-github.com/infiniflow/infinity/go v0.0.0-20261009034419-e1bd341a2083/go.mod h1:hw3z5AwNFsGy1cdrE0Mfjot2y9jqVHTxBufUx9VzZ+0=+github.com/infiniflow/infinity/go v0.0.0-20261013110814-32a6afce89df h1:yxUjRaBeIOjKn0Gzb6C7QdbUawzXmZiqQvPKXKPM+EY=+github.com/infiniflow/infinity/go v0.0.0-20261013110814-32a6afce89df/go.mod h1:hw3z5AwNFsGy1cdrE0Mfjot2y9jqVHTxBufUx9VzZ+0=github.com/infiniflow/onnxruntime_go v1.29.0 h1:pI6v4Bcc7BJdKRF+ZZZkaf4HV6Of6Kl6XryaW2aGbGQ=github.com/infiniflow/onnxruntime_go v1.29.0/go.mod h1:dFFL0KdgeeEDXoE9cjrg/ipQg0wEvD0tkGIg2UH0jdE=github.com/jackc/pgpassfile v1.0.0 h1:/6Hmqy13Ss2zCq62VdNG8tM1wchn8zjSGOBJ6icpsIM=diff --git a/internal/engine/infinity/chunk.go b/internal/engine/infinity/chunk.goindex 0eb27bbfd8..5b9e7663ae 100644--- a/internal/engine/infinity/chunk.go+++ b/internal/engine/infinity/chunk.go@@ -1374,17 +1374,6 @@ func (e *Engine) Search(ctx context.Context, req *types.SearchRequest) (*types.S}}- // Filter-only queries (e.g. the management chunk list) are the ones- // that can silently span KBs, so echo per-table outcome at debug- // level: which table was queried with which filter, and how many- // rows came back.- if !hasTextMatch && !hasVectorMatch {- common.Debug("Infinity filter-only search",- zap.String("table", tableName),- zap.String("filter", filterStr),- zap.Int("rows", len(searchChunks)))- }-// Parse total_hits_count from ExtraInfovar tableTotal int64if df.ExtraInfo != "" {@@ -1396,6 +1385,17 @@ func (e *Engine) Search(ctx context.Context, req *types.SearchRequest) (*types.S}}+ // Log every query shape at debug level: which table, which filter, whether+ // a match was applied, and the row/total counts. A query that silently+ // spans the dataset is only visible here.+ common.Debug("Infinity search",+ zap.String("table", tableName),+ zap.String("filter", filterStr),+ zap.Bool("hasTextMatch", hasTextMatch),+ zap.Bool("hasVectorMatch", hasVectorMatch),+ zap.Int("rows", len(searchChunks)),+ zap.Int64("total", tableTotal))+searchResult := &types.SearchResult{Chunks: searchChunks,Total: tableTotal,diff --git a/internal/engine/infinity/chunk_filter_test.go b/internal/engine/infinity/chunk_filter_test.goindex 84c6716d8c..b65e3fd52b 100644--- a/internal/engine/infinity/chunk_filter_test.go+++ b/internal/engine/infinity/chunk_filter_test.go@@ -1,6 +1,8 @@package infinityimport (+ "fmt"+ "strings""testing""ragflow/internal/engine/types"@@ -17,3 +19,95 @@ func TestShouldDefaultAvailableFilter(t *testing.T) {t.Fatal("skill indexes do not use chunk availability")}}++// TestListFilterConditionKeepsDocumentScope pins the condition the management+// chunk list hands to Infinity (doc_id + optional chunk ids + "no compiled rows").+// A filter-only search that reaches the engine with an empty condition degenerates+// into a scan of the whole table, which is what makes the chunk panel render other+// documents' rows — so every clause must survive the conversion, and a scope that+// carries no ids must not turn into a dangling clause.+func TestListFilterConditionKeepsDocumentScope(t *testing.T) {+ const (+ docID = "d7c44b97f2544a3e99a8ca698e25ab0f"+ chunkID = "6877433186c7e860"+ )+ cases := []struct {+ name string+ filter map[string]interface{}+ contains []string+ excludes []string+ }{+ {+ name: "document scope only",+ filter: map[string]interface{}{+ "doc_id": docID,+ "must_not": map[string]interface{}{"exists": "compile_kwd"},+ },+ contains: []string{fmt.Sprintf("doc_id='%s'", docID), "NOT (compile_kwd!='')"},+ },+ {+ name: "document scope plus one chunk id",+ filter: map[string]interface{}{+ "doc_id": docID,+ "must_not": map[string]interface{}{"exists": "compile_kwd"},+ "id": []string{chunkID},+ },+ contains: []string{+ fmt.Sprintf("doc_id='%s'", docID),+ "NOT (compile_kwd!='')",+ fmt.Sprintf("id='%s'", chunkID),+ },+ },+ {+ name: "document scope plus several chunk ids",+ filter: map[string]interface{}{+ "doc_id": docID,+ "must_not": map[string]interface{}{"exists": "compile_kwd"},+ "id": []string{chunkID, "a1b2c3d4e5f60718"},+ },+ contains: []string{+ fmt.Sprintf("doc_id='%s'", docID),+ "NOT (compile_kwd!='')",+ fmt.Sprintf("id IN ('%s', 'a1b2c3d4e5f60718')", chunkID),+ },+ },+ {+ // A negation on its own is still a condition: it must not collapse to+ // an empty string and turn the query into a table scan.+ name: "negation without a document scope",+ filter: map[string]interface{}{"must_not": map[string]interface{}{"exists": "compile_kwd"}},+ contains: []string{"NOT (compile_kwd!='')"},+ },+ {+ // An empty id list means "no chunk filter", not "match id ''": the+ // condition must stay exactly document scope + negation.+ name: "empty chunk id list adds no clause",+ filter: map[string]interface{}{+ "doc_id": docID,+ "must_not": map[string]interface{}{"exists": "compile_kwd"},+ "id": []string{},+ },+ contains: []string{fmt.Sprintf("doc_id='%s'", docID), "NOT (compile_kwd!='')"},+ excludes: []string{"AND id", "id IN"},+ },+ }++ for _, tc := range cases {+ t.Run(tc.name, func(t *testing.T) {+ condition := equivalentConditionToStr(tc.filter, nil)+ if condition == "" {+ t.Fatal("filter-only chunk list must not reach the engine without a condition")+ }+ for _, want := range tc.contains {+ if !strings.Contains(condition, want) {+ t.Errorf("condition %q is missing %q", condition, want)+ }+ }+ for _, unwanted := range tc.excludes {+ if strings.Contains(condition, unwanted) {+ t.Errorf("condition %q must not contain %q", condition, unwanted)+ }+ }+ })+ }+}diff --git a/internal/service/chunk/chunk.go b/internal/service/chunk/chunk.goindex f963df348c..c48c57c2c3 100644--- a/internal/service/chunk/chunk.go+++ b/internal/service/chunk/chunk.go@@ -827,9 +827,14 @@ func (s *ChunkService) List(ctx context.Context, req *service.ListChunksRequest,"content_with_weight","img_id","position_int",- "docnm",- "important_keywords",- "questions",+ // Call for the ES field names: Elasticsearch does not translate+ // SelectFields (see elasticsearch/chunk.go), while Infinity's+ // convertSelectFields maps these names onto its own docnm /+ // important_keywords / questions columns. Asking for the Python-style+ // aliases here returned no document name at all on ES.+ "docnm_kwd",+ "important_kwd",+ "question_kwd","entities_kwd","entity_kwd","entity_type_kwd",