Skip to content

Commit 5571f68

Browse files
authored
fix: reindex ignore empty rag_id (#248)
1 parent 08c154c commit 5571f68

3 files changed

Lines changed: 14 additions & 4 deletions

File tree

backend/repo/kb_document.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ func (d *KBDocument) UpsertSpaceFolderTree(ctx context.Context, folder *model.KB
161161
})
162162
}
163163

164-
func (d *KBDocument) UpdateSpaceFolderAllDoc(ctx context.Context, folderID uint, statusFilter []model.DocStatus, docStatus model.DocStatus, msg string) error {
164+
func (d *KBDocument) UpdateSpaceFolderAllDoc(ctx context.Context, folderID uint, statusFilter []model.DocStatus, docStatus model.DocStatus, msg string, ignoreEmptyRag bool) error {
165165
sql := `WITH RECURSIVE all_folder_doc AS (
166166
SELECT id, parent_id FROM kb_documents WHERE parent_id = ?
167167
UNION
@@ -172,6 +172,10 @@ UPDATE kb_documents SET status = ?, message = ?, updated_at = ? FROM all_folder_
172172
folderID, docStatus, msg, time.Now(), model.DocTypeSpace, model.FileTypeFolder,
173173
}
174174

175+
if ignoreEmptyRag {
176+
sql += " AND kb_documents.rag_id != ''"
177+
}
178+
175179
if len(statusFilter) > 0 {
176180
sql += " AND kb_documents.status IN (?)"
177181
params = append(params, statusFilter)

backend/sub/kb_space.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -351,7 +351,7 @@ func (k *kbSpace) handleUpdate(ctx context.Context, logger *glog.Logger, msg top
351351
if err != nil {
352352
logger.WithErr(err).Warn("list doc failed")
353353

354-
e := k.repoDoc.UpdateSpaceFolderAllDoc(ctx, exportFolderID, []model.DocStatus{model.DocStatusPendingExec}, model.DocStatusExportFailed, err.Error())
354+
e := k.repoDoc.UpdateSpaceFolderAllDoc(ctx, exportFolderID, []model.DocStatus{model.DocStatusPendingExec}, model.DocStatusExportFailed, err.Error(), false)
355355
if e != nil {
356356
logger.WithErr(e).Warn("set doc export failed error")
357357
}

backend/svc/kb_docment.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1091,12 +1091,16 @@ func (d *KBDocument) ReindexSpaceFolder(ctx context.Context, folderID uint) erro
10911091
return err
10921092
}
10931093

1094-
err = d.repoDoc.UpdateSpaceFolderAllDoc(ctx, folderID, nil, model.DocStatusPendingApply, "")
1094+
err = d.repoDoc.UpdateSpaceFolderAllDoc(ctx, folderID, nil, model.DocStatusPendingApply, "", true)
10951095
if err != nil {
10961096
return err
10971097
}
10981098

10991099
for _, doc := range docs {
1100+
if doc.RagID == "" {
1101+
continue
1102+
}
1103+
11001104
err = d.rag.ReindexDocument(ctx, d.repoDataset.GetBackendID(ctx), doc.RagID)
11011105
if err != nil {
11021106
e := d.repoDoc.Update(ctx, map[string]any{
@@ -1408,7 +1412,8 @@ func (d *KBDocument) DocReindex(ctx context.Context, req DocReindexReq) error {
14081412
err := d.repoDoc.Update(ctx, map[string]any{
14091413
"status": model.DocStatusPendingApply,
14101414
}, repo.QueryWithEqual("id", req.IDs, repo.EqualOPEqAny),
1411-
repo.QueryWithEqual("file_type", model.FileTypeFolder, repo.EqualOPNE))
1415+
repo.QueryWithEqual("file_type", model.FileTypeFolder, repo.EqualOPNE),
1416+
repo.QueryWithEqual("rag_id", "", repo.EqualOPNE))
14121417
if err != nil {
14131418
return err
14141419
}
@@ -1432,6 +1437,7 @@ func (d *KBDocument) DocReindex(ctx context.Context, req DocReindexReq) error {
14321437
},
14331438
repo.QueryWithEqual("id", req.IDs, repo.EqualOPEqAny),
14341439
repo.QueryWithEqual("file_type", model.FileTypeFolder, repo.EqualOPNE),
1440+
repo.QueryWithEqual("rag_id", "", repo.EqualOPNE),
14351441
repo.QueryWithSelectColumn("id, rag_id"),
14361442
)
14371443
}

0 commit comments

Comments
 (0)