fix(index): 修复父子模式遗漏无父节点正文索引
This commit is contained in:
@@ -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.
|
||||
|
||||
@@ -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
|
||||
}
|
||||
@@ -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")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user