From 23387779c186647e3483912ad2875573223e64ba Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A7=9A=E7=AB=A0=E6=B5=A9?= Date: Wed, 28 Oct 2026 08:56:14 +0800 Subject: [PATCH] =?UTF-8?q?fix(index):=20=E4=BF=AE=E5=A4=8D=E7=88=B6?= =?UTF-8?q?=E5=AD=90=E6=A8=A1=E5=BC=8F=E9=81=97=E6=BC=8F=E6=97=A0=E7=88=B6?= =?UTF-8?q?=E8=8A=82=E7=82=B9=E6=AD=A3=E6=96=87=E7=B4=A2=E5=BC=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../application/service/knowledge_process.go | 24 +---------- .../service/knowledge_text_chunks.go | 27 ++++++++++++ .../service/knowledge_text_chunks_test.go | 43 +++++++++++++++++++ 3 files changed, 71 insertions(+), 23 deletions(-) create mode 100644 internal/application/service/knowledge_text_chunks.go create mode 100644 internal/application/service/knowledge_text_chunks_test.go diff --git a/internal/application/service/knowledge_process.go b/internal/application/service/knowledge_process.go index a1d0f4ce..4e275d64 100644 --- a/internal/application/service/knowledge_process.go +++ b/internal/application/service/knowledge_process.go @@ -457,29 +457,7 @@ func (s *knowledgeService) processChunks(ctx context.Context, return insertChunks[i].ChunkIndex < insertChunks[j].ChunkIndex }) - // 仅为文本类型的Chunk设置前后关系(child chunks only, parents already linked above) - textChunks := make([]*types.Chunk, 0, len(chunks)) - for _, chunk := range insertChunks { - if chunk.ChunkType == types.ChunkTypeText && chunk.ParentChunkID != "" { - // This is a child chunk in parent-child mode - textChunks = append(textChunks, chunk) - } else if chunk.ChunkType == types.ChunkTypeText && !hasParentChild { - // Normal flat chunk (no parent-child mode) - textChunks = append(textChunks, chunk) - } - } - - // 设置文本Chunk之间的前后关系 (skip if parent-child, children don't need prev/next links) - if !hasParentChild { - for i, chunk := range textChunks { - if i > 0 { - textChunks[i-1].NextChunkID = chunk.ID - } - if i < len(textChunks)-1 { - textChunks[i+1].PreChunkID = chunk.ID - } - } - } + textChunks := processTextChunks(insertChunks, hasParentChild) // Check if knowledge is being deleted/cancelled before writing chunks. // Nothing has been persisted yet, so both branches just bail. diff --git a/internal/application/service/knowledge_text_chunks.go b/internal/application/service/knowledge_text_chunks.go new file mode 100644 index 00000000..ff2034e2 --- /dev/null +++ b/internal/application/service/knowledge_text_chunks.go @@ -0,0 +1,27 @@ +package service + +import "github.com/Tencent/WeKnora/internal/types" + +// processTextChunks selects indexing inputs without linking parent-child context +// as if it were a flat document. +func processTextChunks(chunks []*types.Chunk, hasParentChild bool) []*types.Chunk { + textChunks := make([]*types.Chunk, 0, len(chunks)) + for _, chunk := range chunks { + // Parent-less text can coexist with parent-child output (for example + // table chunks). Persisting it without indexing silently loses recall. + if chunk.ChunkType == types.ChunkTypeText { + textChunks = append(textChunks, chunk) + } + } + if !hasParentChild { + for i, chunk := range textChunks { + if i > 0 { + textChunks[i-1].NextChunkID = chunk.ID + } + if i < len(textChunks)-1 { + textChunks[i+1].PreChunkID = chunk.ID + } + } + } + return textChunks +} diff --git a/internal/application/service/knowledge_text_chunks_test.go b/internal/application/service/knowledge_text_chunks_test.go new file mode 100644 index 00000000..1c1a74e6 --- /dev/null +++ b/internal/application/service/knowledge_text_chunks_test.go @@ -0,0 +1,43 @@ +package service + +import ( + "github.com/Tencent/WeKnora/internal/types" + "testing" +) + +func TestProcessTextChunksIncludesUnparentedText(t *testing.T) { + parent := &types.Chunk{ID: "parent", ChunkType: types.ChunkTypeParentText} + child := &types.Chunk{ID: "child", ChunkType: types.ChunkTypeText, ParentChunkID: "parent"} + standalone := &types.Chunk{ID: "standalone", ChunkType: types.ChunkTypeText} + got := processTextChunks([]*types.Chunk{parent, child, standalone}, true) + if len(got) != 2 { + t.Fatalf("indexed text count = %d, want 2; unparented text must not disappear in parent-child mode", len(got)) + } + if got[0] != child || got[1] != standalone { + t.Fatal("must preserve text identity/order and exclude parent_text") + } + if child.NextChunkID != "" || standalone.PreChunkID != "" { + t.Fatal("parent-child mode must not create flat adjacency links") + } +} + +func TestProcessTextChunksFlatLinks(t *testing.T) { + first := &types.Chunk{ID: "first", ChunkType: types.ChunkTypeText} + second := &types.Chunk{ID: "second", ChunkType: types.ChunkTypeText} + got := processTextChunks([]*types.Chunk{first, {ID: "summary", ChunkType: types.ChunkTypeSummary}, second}, false) + if len(got) != 2 || got[0] != first || got[1] != second { + t.Fatal("flat text identity/order changed") + } + if first.NextChunkID != "second" || second.PreChunkID != "first" { + t.Fatal("flat adjacency links missing") + } +} + +func TestProcessTextChunksParentsOnly(t *testing.T) { + if got := processTextChunks([]*types.Chunk{{ID: "parent", ChunkType: types.ChunkTypeParentText}}, true); len(got) != 0 { + t.Fatal("parent_text must remain stored-only") + } + if got := processTextChunks(nil, false); len(got) != 0 { + t.Fatal("empty input must remain empty") + } +}