+}
+
+export function updateFAQEntryFieldsBatch(kbId: string, data: FAQEntryFieldsBatchRequest) {
+ return put(`/api/v1/knowledge-bases/${kbId}/faq/entries/fields`, data);
}
export function deleteFAQEntries(kbId: string, ids: string[]) {
diff --git a/frontend/src/i18n/locales/en-US.ts b/frontend/src/i18n/locales/en-US.ts
index e2ebc3e5..f81930fc 100644
--- a/frontend/src/i18n/locales/en-US.ts
+++ b/frontend/src/i18n/locales/en-US.ts
@@ -37,7 +37,7 @@ export default {
tagCreateSuccess: 'Tag created',
tagEditSuccess: 'Tag updated',
tagDeleteTitle: 'Delete tag',
- tagDeleteDesc: 'Delete tag "{name}"? Related FAQs will become untagged.',
+ tagDeleteDesc: 'Delete tag "{name}"? All FAQ entries under this tag will also be deleted.',
tagDeleteSuccess: 'Tag deleted',
tagEditAction: 'Rename',
tagDeleteAction: 'Delete',
@@ -946,11 +946,19 @@ export default {
statusEnableSuccess: 'FAQ entry enabled',
statusDisableSuccess: 'FAQ entry disabled',
statusUpdateFailed: 'Failed to update status',
+ recommended: 'Recommend',
+ recommendedEnabled: 'Recommendation enabled',
+ recommendedDisabled: 'Recommendation disabled',
+ recommendedEnableSuccess: 'FAQ entry recommendation enabled',
+ recommendedDisableSuccess: 'FAQ entry recommendation disabled',
+ recommendedUpdateFailed: 'Failed to update recommendation status',
batchOperations: 'Batch Operations',
batchUpdateTag: 'Batch Update Category',
batchUpdateTagTip: 'Set category for {count} selected entries',
batchEnable: 'Batch Enable',
batchDisable: 'Batch Disable',
+ batchEnableRecommended: 'Batch Enable Recommendation',
+ batchDisableRecommended: 'Batch Disable Recommendation',
},
faqImport: {
title: 'Batch Import FAQ',
diff --git a/frontend/src/i18n/locales/ru-RU.ts b/frontend/src/i18n/locales/ru-RU.ts
index c30ab68b..e571859c 100644
--- a/frontend/src/i18n/locales/ru-RU.ts
+++ b/frontend/src/i18n/locales/ru-RU.ts
@@ -37,7 +37,7 @@ export default {
tagCreateSuccess: 'Тег создан',
tagEditSuccess: 'Тег обновлён',
tagDeleteTitle: 'Удаление тега',
- tagDeleteDesc: 'Удалить тег «{name}»? Связанные FAQ будут без метки.',
+ tagDeleteDesc: 'Удалить тег «{name}»? Все записи FAQ под этим тегом также будут удалены.',
tagDeleteSuccess: 'Тег удалён',
tagEditAction: 'Переименовать',
tagDeleteAction: 'Удалить',
@@ -1043,11 +1043,19 @@ export default {
statusEnableSuccess: 'Запись FAQ включена',
statusDisableSuccess: 'Запись FAQ отключена',
statusUpdateFailed: 'Не удалось обновить статус',
+ recommended: 'Рекомендовать',
+ recommendedEnabled: 'Рекомендация включена',
+ recommendedDisabled: 'Рекомендация отключена',
+ recommendedEnableSuccess: 'Рекомендация записи FAQ включена',
+ recommendedDisableSuccess: 'Рекомендация записи FAQ отключена',
+ recommendedUpdateFailed: 'Не удалось обновить статус рекомендации',
batchOperations: 'Пакетные операции',
batchUpdateTag: 'Пакетное обновление категории',
batchUpdateTagTip: 'Установить категорию для {count} выбранных записей',
batchEnable: 'Пакетное включение',
batchDisable: 'Пакетное отключение',
+ batchEnableRecommended: 'Пакетное включение рекомендации',
+ batchDisableRecommended: 'Пакетное отключение рекомендации',
},
faqImport: {
title: 'Пакетный импорт FAQ',
diff --git a/frontend/src/i18n/locales/zh-CN.ts b/frontend/src/i18n/locales/zh-CN.ts
index 2318d8d7..73ae01d2 100644
--- a/frontend/src/i18n/locales/zh-CN.ts
+++ b/frontend/src/i18n/locales/zh-CN.ts
@@ -37,7 +37,7 @@ export default {
tagCreateSuccess: "标签创建成功",
tagEditSuccess: "标签更新成功",
tagDeleteTitle: "删除标签",
- tagDeleteDesc: '确定删除标签"{name}"?已关联的 FAQ 将被标记为未分类',
+ tagDeleteDesc: '确定删除标签"{name}"?该标签下的所有 FAQ 条目将被一并删除',
tagDeleteSuccess: "标签已删除",
tagEditAction: "重命名",
tagDeleteAction: "删除",
@@ -1282,11 +1282,19 @@ export default {
statusEnableSuccess: "FAQ 条目已启用",
statusDisableSuccess: "FAQ 条目已禁用",
statusUpdateFailed: "更新状态失败",
+ recommended: "推荐",
+ recommendedEnabled: "已开启推荐",
+ recommendedDisabled: "已关闭推荐",
+ recommendedEnableSuccess: "FAQ 条目已开启推荐",
+ recommendedDisableSuccess: "FAQ 条目已关闭推荐",
+ recommendedUpdateFailed: "更新推荐状态失败",
batchOperations: "批量操作",
batchUpdateTag: "批量分类",
batchUpdateTagTip: "将为 {count} 个选中的条目设置分类",
batchEnable: "批量启用",
batchDisable: "批量禁用",
+ batchEnableRecommended: "批量开启推荐",
+ batchDisableRecommended: "批量关闭推荐",
},
faqImport: {
title: "批量导入 FAQ",
diff --git a/frontend/src/views/knowledge/KnowledgeBase.vue b/frontend/src/views/knowledge/KnowledgeBase.vue
index 43e9c94d..d080e3da 100644
--- a/frontend/src/views/knowledge/KnowledgeBase.vue
+++ b/frontend/src/views/knowledge/KnowledgeBase.vue
@@ -359,7 +359,7 @@ const confirmDeleteTag = (tag: any) => {
t('knowledgeBase.tagDeleteDesc', { name: tag.name }) as string,
);
if (!confirm) return;
- deleteKnowledgeBaseTag(kbId.value, tag.id)
+ deleteKnowledgeBaseTag(kbId.value, tag.id, { force: true })
.then(() => {
MessagePlugin.success(t('knowledgeBase.tagDeleteSuccess'));
if (selectedTagId.value === tag.id) {
diff --git a/frontend/src/views/knowledge/components/FAQEntryManager.vue b/frontend/src/views/knowledge/components/FAQEntryManager.vue
index 6e9fb1f4..889722f1 100644
--- a/frontend/src/views/knowledge/components/FAQEntryManager.vue
+++ b/frontend/src/views/knowledge/components/FAQEntryManager.vue
@@ -468,6 +468,25 @@
+
([])
const entryStatusLoading = reactive>({})
+const entryRecommendedLoading = reactive>({})
const selectedRowKeys = ref([])
const scrollContainer = ref(null)
const cardListRef = ref(null)
@@ -1335,6 +1358,18 @@ const toolbarActionOptions = computed(() => {
value: 'batchDisable',
icon: 'close-circle',
},
+ /* 暂时隐藏推荐批量操作
+ {
+ content: `${t('knowledgeEditor.faq.batchEnableRecommended')} (${selectedRowKeys.value.length})`,
+ value: 'batchEnableRecommended',
+ icon: 'thumb-up',
+ },
+ {
+ content: `${t('knowledgeEditor.faq.batchDisableRecommended')} (${selectedRowKeys.value.length})`,
+ value: 'batchDisableRecommended',
+ icon: 'thumb-down',
+ },
+ */
{
content: `${t('knowledgeEditor.faqImport.deleteSelected')} (${selectedRowKeys.value.length})`,
value: 'delete',
@@ -1363,6 +1398,12 @@ const handleToolbarAction = (data: { value: string }) => {
case 'batchDisable':
handleBatchStatusChange(false)
break
+ case 'batchEnableRecommended':
+ handleBatchRecommendedChange(true)
+ break
+ case 'batchDisableRecommended':
+ handleBatchRecommendedChange(false)
+ break
case 'delete':
handleBatchDelete()
break
@@ -1574,12 +1615,13 @@ const confirmDeleteTag = (tag: any) => {
cancelBtn: t('common.cancel'),
onConfirm: async () => {
try {
- await deleteKnowledgeBaseTag(props.kbId, tag.id)
+ await deleteKnowledgeBaseTag(props.kbId, tag.id, { force: true })
MessagePlugin.success(t('knowledgeBase.tagDeleteSuccess'))
if (selectedTagId.value === tag.id) {
handleTagFilterChange('')
}
await loadTags()
+ await loadEntries()
confirmDialog.hide()
} catch (error: any) {
MessagePlugin.error(error?.message || t('common.operationFailed'))
@@ -1682,7 +1724,7 @@ const handleEntryStatusChange = async (entry: FAQEntry, value: boolean) => {
actualEntry.is_enabled = value
entryStatusLoading[entry.id] = true
try {
- await updateFAQEntryStatusBatch(props.kbId, { updates: { [entry.id]: value } })
+ await updateFAQEntryFieldsBatch(props.kbId, { by_id: { [entry.id]: { is_enabled: value } } })
MessagePlugin.success(t(value ? 'knowledgeEditor.faq.statusEnableSuccess' : 'knowledgeEditor.faq.statusDisableSuccess'))
} catch (error: any) {
// 失败时回滚
@@ -1693,6 +1735,32 @@ const handleEntryStatusChange = async (entry: FAQEntry, value: boolean) => {
}
}
+const handleEntryRecommendedChange = async (entry: FAQEntry, value: boolean) => {
+ if (entryRecommendedLoading[entry.id]) {
+ return
+ }
+ const entryIndex = entries.value.findIndex(e => e.id === entry.id)
+ if (entryIndex === -1) {
+ return
+ }
+ const actualEntry = entries.value[entryIndex]
+ const previous = actualEntry.is_recommended
+ if (previous === value) {
+ return
+ }
+ actualEntry.is_recommended = value
+ entryRecommendedLoading[entry.id] = true
+ try {
+ await updateFAQEntryFieldsBatch(props.kbId, { by_id: { [entry.id]: { is_recommended: value } } })
+ MessagePlugin.success(t(value ? 'knowledgeEditor.faq.recommendedEnableSuccess' : 'knowledgeEditor.faq.recommendedDisableSuccess'))
+ } catch (error: any) {
+ actualEntry.is_recommended = previous
+ MessagePlugin.error(error?.message || t('knowledgeEditor.faq.recommendedUpdateFailed'))
+ } finally {
+ entryRecommendedLoading[entry.id] = false
+ }
+}
+
const editorRules: FormRules = {
standard_question: [
{ required: true, message: t('knowledgeEditor.messages.nameRequired') },
@@ -1967,11 +2035,11 @@ const handleBatchTag = async () => {
const handleBatchStatusChange = async (isEnabled: boolean) => {
if (!selectedRowKeys.value.length || !props.kbId) return
try {
- const updates: Record = {}
+ const by_id: Record = {}
selectedRowKeys.value.forEach(id => {
- updates[id] = isEnabled
+ by_id[id] = { is_enabled: isEnabled }
})
- await updateFAQEntryStatusBatch(props.kbId, { updates })
+ await updateFAQEntryFieldsBatch(props.kbId, { by_id })
MessagePlugin.success(t(isEnabled ? 'knowledgeEditor.faq.statusEnableSuccess' : 'knowledgeEditor.faq.statusDisableSuccess'))
selectedRowKeys.value = []
await loadEntries()
@@ -1980,6 +2048,22 @@ const handleBatchStatusChange = async (isEnabled: boolean) => {
}
}
+const handleBatchRecommendedChange = async (isRecommended: boolean) => {
+ if (!selectedRowKeys.value.length || !props.kbId) return
+ try {
+ const by_id: Record = {}
+ selectedRowKeys.value.forEach(id => {
+ by_id[id] = { is_recommended: isRecommended }
+ })
+ await updateFAQEntryFieldsBatch(props.kbId, { by_id })
+ MessagePlugin.success(t(isRecommended ? 'knowledgeEditor.faq.recommendedEnableSuccess' : 'knowledgeEditor.faq.recommendedDisableSuccess'))
+ selectedRowKeys.value = []
+ await loadEntries()
+ } catch (error: any) {
+ MessagePlugin.error(error?.message || t('common.operationFailed'))
+ }
+}
+
const handleMenuEdit = (entry: FAQEntry) => {
entry.showMore = false
openEditor(entry)
@@ -2092,7 +2176,8 @@ const parseCSVFile = async (file: File): Promise => {
answers: splitByDelimiter(record['机器人回答'] || record['answers']),
similar_questions: splitByDelimiter(record['相似问题'] || record['similar_questions']),
negative_questions: splitByDelimiter(record['反例问题'] || record['negative_questions']),
- tag_id: record['分类'] || record['tag_id'] || '',
+ tag_id: record['tag_id'] || '',
+ tag_name: record['分类'] || record['tag_name'] || '',
is_enabled: isDisabled !== undefined ? !isDisabled : undefined, // 是否停用:FALSE表示启用,TRUE表示停用,所以取反
}),
)
@@ -2138,7 +2223,8 @@ const parseExcelFile = async (file: File): Promise => {
answers: splitByDelimiter(normalizedRow['机器人回答'] || normalizedRow['answers']),
similar_questions: splitByDelimiter(normalizedRow['相似问题'] || normalizedRow['similar_questions']),
negative_questions: splitByDelimiter(normalizedRow['反例问题'] || normalizedRow['negative_questions']),
- tag_id: normalizedRow['分类'] || normalizedRow['tag_id'] || '',
+ tag_id: normalizedRow['tag_id'] || '',
+ tag_name: normalizedRow['分类'] || normalizedRow['tag_name'] || '',
is_enabled: isDisabled !== undefined ? !isDisabled : undefined, // 是否停用:FALSE表示启用,TRUE表示停用,所以取反
})
})
@@ -2181,6 +2267,7 @@ const normalizePayload = (payload: Partial): FAQEntryPayload =>
similar_questions: payload.similar_questions?.filter(Boolean) || [],
negative_questions: payload.negative_questions?.filter(Boolean) || [],
tag_id: payload.tag_id || '',
+ tag_name: payload.tag_name || '',
is_enabled: payload.is_enabled !== undefined ? payload.is_enabled : undefined,
})
@@ -2232,6 +2319,7 @@ const startPolling = (taskId: string) => {
if (processed > lastProcessed) {
lastProcessed = processed
await loadEntries()
+ await loadTags()
}
// 任务完成或失败,停止轮询(但不自动关闭进度条,让用户手动关闭)
@@ -2239,7 +2327,11 @@ const startPolling = (taskId: string) => {
stopPolling()
if (status === 'success') {
MessagePlugin.success(t('knowledgeEditor.faqImport.importSuccess'))
+ // 清除筛选条件,确保用户能看到所有新导入的数据
+ selectedTagId.value = ''
+ entrySearchKeyword.value = ''
await loadEntries()
+ await loadTags()
// 任务完成后,3秒后自动关闭进度条
setTimeout(() => {
if (importState.taskStatus?.status === 'success') {
@@ -2465,12 +2557,14 @@ const exampleData: FAQEntryPayload[] = [
answers: ['WeKnora 是一个智能知识库管理系统', '它支持多种知识库类型和导入方式'],
similar_questions: ['WeKnora 是什么?', '介绍一下 WeKnora'],
negative_questions: ['这不是 WeKnora', '与 WeKnora 无关'],
+ tag_name: '产品介绍',
},
{
standard_question: '如何创建知识库?',
answers: ['点击"新建知识库"按钮', '选择知识库类型并填写相关信息', '完成创建后即可开始使用'],
similar_questions: ['怎么创建知识库?', '如何新建知识库?'],
negative_questions: [],
+ tag_name: '使用指南',
},
]
@@ -2509,7 +2603,7 @@ const downloadCSVExample = () => {
const headers = ['分类(必填)', '问题(必填)', '相似问题(选填-多个用##分隔)', '反例问题(选填-多个用##分隔)', '机器人回答(必填-多个用##分隔)', '是否全部回复(选填-默认FALSE)', '是否停用(选填-默认FALSE)', '是否禁止被推荐(选填-默认False 可被推荐)']
const rows = exampleData.map((item) => {
return [
- '', // 分类
+ item.tag_name || '', // 分类
item.standard_question,
item.similar_questions.join('##'),
item.negative_questions.join('##'),
@@ -2544,7 +2638,7 @@ const downloadCSVExample = () => {
const downloadExcelExample = () => {
const worksheet = XLSX.utils.json_to_sheet(
exampleData.map((item) => ({
- '分类(必填)': '',
+ '分类(必填)': item.tag_name || '',
'问题(必填)': item.standard_question,
'相似问题(选填-多个用##分隔)': item.similar_questions.join('##'),
'反例问题(选填-多个用##分隔)': item.negative_questions.join('##'),
diff --git a/internal/application/repository/chunk.go b/internal/application/repository/chunk.go
index 21bfa98c..3653f2b6 100644
--- a/internal/application/repository/chunk.go
+++ b/internal/application/repository/chunk.go
@@ -3,6 +3,7 @@ package repository
import (
"context"
"errors"
+ "fmt"
"strings"
"github.com/Tencent/WeKnora/internal/common"
@@ -46,7 +47,7 @@ func (r *chunkRepository) ListChunksByID(
ctx context.Context, tenantID uint64, ids []string,
) ([]*types.Chunk, error) {
var chunks []*types.Chunk
- if err := r.db.Debug().WithContext(ctx).
+ if err := r.db.WithContext(ctx).
Where("tenant_id = ? AND id IN ?", tenantID, ids).
Find(&chunks).Error; err != nil {
return nil, err
@@ -103,10 +104,7 @@ func (r *chunkRepository) ListPagedChunksByKnowledgeID(
}
// Then query the paginated data
- dataQuery := baseFilter(
- r.db.WithContext(ctx).
- Select("id, content, knowledge_id, knowledge_base_id, start_at, end_at, chunk_index, is_enabled, chunk_type, parent_chunk_id, image_info, metadata, tag_id, status"),
- )
+ dataQuery := baseFilter(r.db.WithContext(ctx))
if err := dataQuery.
Order("chunk_index ASC").
@@ -133,20 +131,111 @@ func (r *chunkRepository) ListChunkByParentID(
return chunks, nil
}
-// UpdateChunk updates a chunk
+// UpdateChunk updates a chunk using GORM Save, which updates ALL fields.
+// Note: This will update all fields including metadata and content_hash.
+// Make sure the chunk object is complete (e.g., fetched from DB) before calling this method.
func (r *chunkRepository) UpdateChunk(ctx context.Context, chunk *types.Chunk) error {
return r.db.WithContext(ctx).Save(chunk).Error
}
-// UpdateChunks updates chunks in batch
+// UpdateChunks updates chunks in batch using raw SQL for efficiency.
+// Uses raw SQL to bypass GORM's default value handling for boolean fields.
+//
+// IMPORTANT: This method only updates the following fields:
+// - content
+// - is_enabled
+// - tag_id
+// - flags
+// - status
+// - updated_at
+//
+// Fields NOT updated by this method (will retain their original values):
+// - metadata
+// - content_hash
+// - embedding-related fields
+// - other fields not listed above
+//
+// If you need to update metadata or content_hash, use UpdateChunk (single) instead.
func (r *chunkRepository) UpdateChunks(ctx context.Context, chunks []*types.Chunk) error {
if len(chunks) == 0 {
return nil
}
+
+ // Build batch update SQL with CASE expressions
+ var ids []string
+ contentCases := make([]string, 0, len(chunks))
+ isEnabledCases := make([]string, 0, len(chunks))
+ tagIDCases := make([]string, 0, len(chunks))
+ flagsCases := make([]string, 0, len(chunks))
+ statusCases := make([]string, 0, len(chunks))
+
+ var contentArgs []interface{}
+ var isEnabledArgs []interface{}
+ var tagIDArgs []interface{}
+ var flagsArgs []interface{}
+ var statusArgs []interface{}
+
for _, chunk := range chunks {
- chunk.Content = common.CleanInvalidUTF8(chunk.Content)
+ ids = append(ids, chunk.ID)
+ content := common.CleanInvalidUTF8(chunk.Content)
+
+ contentCases = append(contentCases, "WHEN id = ? THEN ?")
+ contentArgs = append(contentArgs, chunk.ID, content)
+
+ // Convert bool to string for PostgreSQL compatibility
+ isEnabledStr := "false"
+ if chunk.IsEnabled {
+ isEnabledStr = "true"
+ }
+ isEnabledCases = append(isEnabledCases, "WHEN id = ? THEN ?")
+ isEnabledArgs = append(isEnabledArgs, chunk.ID, isEnabledStr)
+
+ tagIDCases = append(tagIDCases, "WHEN id = ? THEN ?")
+ tagIDArgs = append(tagIDArgs, chunk.ID, chunk.TagID)
+
+ flagsCases = append(flagsCases, "WHEN id = ? THEN ?")
+ flagsArgs = append(flagsArgs, chunk.ID, fmt.Sprintf("%d", chunk.Flags))
+
+ statusCases = append(statusCases, "WHEN id = ? THEN ?")
+ statusArgs = append(statusArgs, chunk.ID, fmt.Sprintf("%d", chunk.Status))
}
- return r.db.WithContext(ctx).Save(chunks).Error
+
+ // Build IN clause placeholders
+ inPlaceholders := make([]string, len(ids))
+ for i := range ids {
+ inPlaceholders[i] = "?"
+ }
+
+ // Combine args in correct order: content, is_enabled, tag_id, flags, status, then IN clause
+ var args []interface{}
+ args = append(args, contentArgs...)
+ args = append(args, isEnabledArgs...)
+ args = append(args, tagIDArgs...)
+ args = append(args, flagsArgs...)
+ args = append(args, statusArgs...)
+ for _, id := range ids {
+ args = append(args, id)
+ }
+
+ sql := fmt.Sprintf(`
+ UPDATE chunks SET
+ content = CASE %s END,
+ is_enabled = (CASE %s END)::boolean,
+ tag_id = CASE %s END,
+ flags = (CASE %s END)::integer,
+ status = (CASE %s END)::integer,
+ updated_at = NOW()
+ WHERE id IN (%s)
+ `,
+ strings.Join(contentCases, " "),
+ strings.Join(isEnabledCases, " "),
+ strings.Join(tagIDCases, " "),
+ strings.Join(flagsCases, " "),
+ strings.Join(statusCases, " "),
+ strings.Join(inPlaceholders, ","),
+ )
+
+ return r.db.WithContext(ctx).Exec(sql, args...).Error
}
// DeleteChunk deletes a chunk by its ID
@@ -176,6 +265,13 @@ func (r *chunkRepository) DeleteByKnowledgeList(ctx context.Context, tenantID ui
).Delete(&types.Chunk{}).Error
}
+// DeleteChunksByTagID deletes all chunks with the specified tag ID
+func (r *chunkRepository) DeleteChunksByTagID(ctx context.Context, tenantID uint64, kbID string, tagID string) error {
+ return r.db.WithContext(ctx).Where(
+ "tenant_id = ? AND knowledge_base_id = ? AND tag_id = ?", tenantID, kbID, tagID,
+ ).Delete(&types.Chunk{}).Error
+}
+
// CountChunksByKnowledgeBaseID counts the number of chunks in a knowledge base
func (r *chunkRepository) CountChunksByKnowledgeBaseID(
ctx context.Context,
@@ -292,3 +388,156 @@ func (r *chunkRepository) ListAllFAQChunksWithMetadataByKnowledgeBaseID(
return allChunks, nil
}
+
+// UpdateChunkFlagsBatch updates flags for multiple chunks in batch using SQL CASE expressions.
+// This is more efficient than updating chunks one by one.
+// setFlags: map of chunk ID to flags to set (OR operation)
+// clearFlags: map of chunk ID to flags to clear (AND NOT operation)
+func (r *chunkRepository) UpdateChunkFlagsBatch(
+ ctx context.Context,
+ tenantID uint64,
+ kbID string,
+ setFlags map[string]types.ChunkFlags,
+ clearFlags map[string]types.ChunkFlags,
+) error {
+ if len(setFlags) == 0 && len(clearFlags) == 0 {
+ return nil
+ }
+
+ // Collect all IDs
+ allIDs := make([]string, 0, len(setFlags)+len(clearFlags))
+ for id := range setFlags {
+ allIDs = append(allIDs, id)
+ }
+ for id := range clearFlags {
+ if _, exists := setFlags[id]; !exists {
+ allIDs = append(allIDs, id)
+ }
+ }
+
+ if len(allIDs) == 0 {
+ return nil
+ }
+
+ // Build CASE expression for flags update
+ // flags = (flags | setFlag) & ~clearFlag
+ var setCases, clearCases []string
+ var args []interface{}
+
+ // Build SET cases: flags | value
+ for id, flag := range setFlags {
+ setCases = append(setCases, "WHEN id = ? THEN ?")
+ args = append(args, id, int(flag))
+ }
+
+ // Build CLEAR cases: flags & ~value
+ for id, flag := range clearFlags {
+ clearCases = append(clearCases, "WHEN id = ? THEN ?")
+ args = append(args, id, int(flag))
+ }
+
+ setExpr := "0"
+ clearExpr := "0"
+
+ if len(setCases) > 0 {
+ setExpr = fmt.Sprintf("CASE %s ELSE 0 END", strings.Join(setCases, " "))
+ }
+
+ if len(clearCases) > 0 {
+ clearExpr = fmt.Sprintf("CASE %s ELSE 0 END", strings.Join(clearCases, " "))
+ }
+
+ // Build IN clause placeholders manually for raw SQL
+ inPlaceholders := make([]string, len(allIDs))
+ for i := range allIDs {
+ inPlaceholders[i] = "?"
+ }
+
+ sql := fmt.Sprintf(`
+ UPDATE chunks
+ SET flags = (flags | (%s)) & ~(%s),
+ updated_at = NOW()
+ WHERE tenant_id = ?
+ AND knowledge_base_id = ?
+ AND id IN (%s)
+`, setExpr, clearExpr, strings.Join(inPlaceholders, ","))
+
+ args = append(args, tenantID, kbID)
+ for _, id := range allIDs {
+ args = append(args, id)
+ }
+
+ return r.db.WithContext(ctx).Exec(sql, args...).Error
+}
+
+// UpdateChunkFieldsByTagID updates fields for all chunks with the specified tag ID.
+// Returns the list of affected chunk IDs for syncing with retriever engines.
+func (r *chunkRepository) UpdateChunkFieldsByTagID(
+ ctx context.Context,
+ tenantID uint64,
+ kbID string,
+ tagID string,
+ isEnabled *bool,
+ setFlags types.ChunkFlags,
+ clearFlags types.ChunkFlags,
+) ([]string, error) {
+ // First, get the IDs of chunks that will be affected (for is_enabled sync)
+ var affectedIDs []string
+ if isEnabled != nil {
+ var chunks []*types.Chunk
+ query := r.db.WithContext(ctx).
+ Select("id").
+ Where("tenant_id = ? AND knowledge_base_id = ? AND chunk_type = ?",
+ tenantID, kbID, types.ChunkTypeFAQ)
+ if tagID == "" {
+ query = query.Where("(tag_id = '' OR tag_id IS NULL)")
+ } else {
+ query = query.Where("tag_id = ?", tagID)
+ }
+ // Only get chunks that need to change
+ query = query.Where("is_enabled != ?", *isEnabled)
+ if err := query.Find(&chunks).Error; err != nil {
+ return nil, err
+ }
+ for _, c := range chunks {
+ affectedIDs = append(affectedIDs, c.ID)
+ }
+ }
+
+ // Build update query
+ updates := map[string]interface{}{
+ "updated_at": "NOW()",
+ }
+
+ if isEnabled != nil {
+ updates["is_enabled"] = *isEnabled
+ }
+
+ query := r.db.WithContext(ctx).Model(&types.Chunk{}).
+ Where("tenant_id = ? AND knowledge_base_id = ? AND chunk_type = ?",
+ tenantID, kbID, types.ChunkTypeFAQ)
+
+ if tagID == "" {
+ query = query.Where("(tag_id = '' OR tag_id IS NULL)")
+ } else {
+ query = query.Where("tag_id = ?", tagID)
+ }
+
+ // Handle flags update
+ if setFlags != 0 || clearFlags != 0 {
+ flagsExpr := "flags"
+ if setFlags != 0 {
+ flagsExpr = fmt.Sprintf("(%s | %d)", flagsExpr, int(setFlags))
+ }
+ if clearFlags != 0 {
+ flagsExpr = fmt.Sprintf("(%s & ~%d)", flagsExpr, int(clearFlags))
+ }
+ updates["flags"] = r.db.Raw(flagsExpr)
+ }
+
+ if err := query.Updates(updates).Error; err != nil {
+ return nil, err
+ }
+
+ return affectedIDs, nil
+}
diff --git a/internal/application/repository/tag.go b/internal/application/repository/tag.go
index 67dc06eb..8dbcac09 100644
--- a/internal/application/repository/tag.go
+++ b/internal/application/repository/tag.go
@@ -40,6 +40,17 @@ func (r *knowledgeTagRepository) GetByID(ctx context.Context, tenantID uint64, i
return &tag, nil
}
+// GetByName gets a knowledge tag by name
+func (r *knowledgeTagRepository) GetByName(ctx context.Context, tenantID uint64, kbID string, name string) (*types.KnowledgeTag, error) {
+ var tag types.KnowledgeTag
+ if err := r.db.WithContext(ctx).
+ Where("tenant_id = ? AND knowledge_base_id = ? AND name = ?", tenantID, kbID, name).
+ First(&tag).Error; err != nil {
+ return nil, err
+ }
+ return &tag, nil
+}
+
// ListByKB lists knowledge tags by knowledge base ID with pagination and optional keyword filtering.
func (r *knowledgeTagRepository) ListByKB(
ctx context.Context,
diff --git a/internal/application/service/knowledge.go b/internal/application/service/knowledge.go
index f8349054..088861cd 100644
--- a/internal/application/service/knowledge.go
+++ b/internal/application/service/knowledge.go
@@ -62,6 +62,7 @@ type knowledgeService struct {
chunkService interfaces.ChunkService
chunkRepo interfaces.ChunkRepository
tagRepo interfaces.KnowledgeTagRepository
+ tagService interfaces.KnowledgeTagService
fileSvc interfaces.FileService
modelService interfaces.ModelService
task *asynq.Client
@@ -84,6 +85,7 @@ func NewKnowledgeService(
chunkService interfaces.ChunkService,
chunkRepo interfaces.ChunkRepository,
tagRepo interfaces.KnowledgeTagRepository,
+ tagService interfaces.KnowledgeTagService,
fileSvc interfaces.FileService,
modelService interfaces.ModelService,
task *asynq.Client,
@@ -99,6 +101,7 @@ func NewKnowledgeService(
chunkService: chunkService,
chunkRepo: chunkRepo,
tagRepo: tagRepo,
+ tagService: tagService,
fileSvc: fileSvc,
modelService: modelService,
task: task,
@@ -2993,6 +2996,17 @@ func (s *knowledgeService) executeFAQImport(ctx context.Context, taskID string,
})
return fmt.Errorf("failed to sanitize entry at index %d: %w", i+idx, err)
}
+
+ // 解析 TagID
+ tagID, err := s.resolveTagID(ctx, kbID, &entry)
+ if err != nil {
+ logger.ErrorWithFields(ctx, err, map[string]interface{}{
+ "entry": entry,
+ "task_id": taskID,
+ })
+ return fmt.Errorf("failed to resolve tag for entry at index %d: %w", i+idx, err)
+ }
+
isEnabled := true
if entry.IsEnabled != nil {
isEnabled = *entry.IsEnabled
@@ -3007,7 +3021,7 @@ func (s *knowledgeService) executeFAQImport(ctx context.Context, taskID string,
// ChunkIndex: 0,
IsEnabled: isEnabled,
ChunkType: types.ChunkTypeFAQ,
- TagID: entry.TagID,
+ TagID: tagID, // 使用解析后的 TagID
Status: int(types.ChunkStatusStored), // store but not indexed
}
if err := chunk.SetFAQMetadata(meta); err != nil {
@@ -3122,6 +3136,12 @@ func (s *knowledgeService) CreateFAQEntry(ctx context.Context,
return nil, err
}
+ // 解析 TagID
+ tagID, err := s.resolveTagID(ctx, kbID, payload)
+ if err != nil {
+ return nil, err
+ }
+
// 检查标准问和相似问是否与其他条目重复
if err := s.checkFAQQuestionDuplicate(ctx, tenantID, kb.ID, "", meta); err != nil {
return nil, err
@@ -3150,6 +3170,11 @@ func (s *knowledgeService) CreateFAQEntry(ctx context.Context,
if payload.IsEnabled != nil {
isEnabled = *payload.IsEnabled
}
+ // 默认可推荐
+ flags := types.ChunkFlagRecommended
+ if payload.IsRecommended != nil && !*payload.IsRecommended {
+ flags = 0
+ }
chunk := &types.Chunk{
ID: uuid.New().String(),
@@ -3158,8 +3183,9 @@ func (s *knowledgeService) CreateFAQEntry(ctx context.Context,
KnowledgeBaseID: kb.ID,
Content: buildFAQChunkContent(meta, indexMode),
IsEnabled: isEnabled,
+ Flags: flags,
ChunkType: types.ChunkTypeFAQ,
- TagID: payload.TagID,
+ TagID: tagID, // 使用解析后的 TagID
Status: int(types.ChunkStatusStored),
}
@@ -3246,6 +3272,14 @@ func (s *knowledgeService) UpdateFAQEntry(ctx context.Context,
chunk.IsEnabled = *payload.IsEnabled
isEnabledUpdated = (oldEnabled != chunk.IsEnabled)
}
+ // 处理推荐状态
+ if payload.IsRecommended != nil {
+ if *payload.IsRecommended {
+ chunk.Flags = chunk.Flags.SetFlag(types.ChunkFlagRecommended)
+ } else {
+ chunk.Flags = chunk.Flags.ClearFlag(types.ChunkFlagRecommended)
+ }
+ }
chunk.UpdatedAt = time.Now()
if err := s.chunkService.UpdateChunk(ctx, chunk); err != nil {
return err
@@ -3317,11 +3351,15 @@ func (s *knowledgeService) UpdateFAQEntryStatus(ctx context.Context,
return nil
}
-// UpdateFAQEntryStatusBatch updates enable status for FAQ entries in batch.
-func (s *knowledgeService) UpdateFAQEntryStatusBatch(ctx context.Context,
- kbID string, updates map[string]bool,
+// UpdateFAQEntryFieldsBatch updates multiple fields for FAQ entries in batch.
+// This is the unified API for batch updating FAQ entry fields.
+// Supports two modes:
+// 1. By entry ID: use ByID field
+// 2. By Tag: use ByTag field to apply the same update to all entries under a tag
+func (s *knowledgeService) UpdateFAQEntryFieldsBatch(ctx context.Context,
+ kbID string, req *types.FAQEntryFieldsBatchUpdate,
) error {
- if len(updates) == 0 {
+ if req == nil || (len(req.ByID) == 0 && len(req.ByTag) == 0) {
return nil
}
kb, err := s.validateFAQKnowledgeBase(ctx, kbID)
@@ -3330,39 +3368,120 @@ func (s *knowledgeService) UpdateFAQEntryStatusBatch(ctx context.Context,
}
tenantID := ctx.Value(types.TenantIDContextKey).(uint64)
- // Get all chunks in batch
- entryIDs := make([]string, 0, len(updates))
- for entryID := range updates {
- entryIDs = append(entryIDs, entryID)
- }
- chunks, err := s.chunkRepo.ListChunksByID(ctx, tenantID, entryIDs)
- if err != nil {
- return err
+ enabledUpdates := make(map[string]bool)
+
+ // Handle ByTag updates first
+ if len(req.ByTag) > 0 {
+ for tagID, update := range req.ByTag {
+ var setFlags, clearFlags types.ChunkFlags
+
+ // Handle IsRecommended
+ if update.IsRecommended != nil {
+ if *update.IsRecommended {
+ setFlags = types.ChunkFlagRecommended
+ } else {
+ clearFlags = types.ChunkFlagRecommended
+ }
+ }
+
+ // Update all chunks with this tag
+ affectedIDs, err := s.chunkRepo.UpdateChunkFieldsByTagID(
+ ctx, tenantID, kb.ID, tagID,
+ update.IsEnabled, setFlags, clearFlags,
+ )
+ if err != nil {
+ return err
+ }
+
+ // Collect affected IDs for retriever sync
+ if update.IsEnabled != nil && len(affectedIDs) > 0 {
+ for _, id := range affectedIDs {
+ enabledUpdates[id] = *update.IsEnabled
+ }
+ }
+ }
}
- // Group chunks by enabled status for batch update
- chunkStatusMap := make(map[string]bool)
- for _, chunk := range chunks {
- if chunk.KnowledgeBaseID != kb.ID || chunk.ChunkType != types.ChunkTypeFAQ {
- continue
+ // Handle ByID updates
+ if len(req.ByID) > 0 {
+ entryIDs := make([]string, 0, len(req.ByID))
+ for entryID := range req.ByID {
+ entryIDs = append(entryIDs, entryID)
}
- isEnabled, exists := updates[chunk.ID]
- if !exists {
- continue
- }
- if chunk.IsEnabled == isEnabled {
- continue
- }
- chunk.IsEnabled = isEnabled
- chunk.UpdatedAt = time.Now()
- if err := s.chunkService.UpdateChunk(ctx, chunk); err != nil {
+ chunks, err := s.chunkRepo.ListChunksByID(ctx, tenantID, entryIDs)
+ if err != nil {
return err
}
- chunkStatusMap[chunk.ID] = isEnabled
+
+ setFlags := make(map[string]types.ChunkFlags)
+ clearFlags := make(map[string]types.ChunkFlags)
+ chunksToUpdate := make([]*types.Chunk, 0)
+
+ for _, chunk := range chunks {
+ if chunk.KnowledgeBaseID != kb.ID || chunk.ChunkType != types.ChunkTypeFAQ {
+ continue
+ }
+ update, exists := req.ByID[chunk.ID]
+ if !exists {
+ continue
+ }
+
+ needUpdate := false
+
+ // Handle IsEnabled
+ if update.IsEnabled != nil && chunk.IsEnabled != *update.IsEnabled {
+ chunk.IsEnabled = *update.IsEnabled
+ enabledUpdates[chunk.ID] = *update.IsEnabled
+ needUpdate = true
+ }
+
+ // Handle IsRecommended (via Flags)
+ if update.IsRecommended != nil {
+ currentRecommended := chunk.Flags.HasFlag(types.ChunkFlagRecommended)
+ if currentRecommended != *update.IsRecommended {
+ if *update.IsRecommended {
+ setFlags[chunk.ID] = types.ChunkFlagRecommended
+ } else {
+ clearFlags[chunk.ID] = types.ChunkFlagRecommended
+ }
+ }
+ }
+
+ // Handle TagID
+ if update.TagID != nil {
+ newTagID := ""
+ if *update.TagID != "" {
+ newTagID = *update.TagID
+ }
+ if chunk.TagID != newTagID {
+ chunk.TagID = newTagID
+ needUpdate = true
+ }
+ }
+
+ if needUpdate {
+ chunk.UpdatedAt = time.Now()
+ chunksToUpdate = append(chunksToUpdate, chunk)
+ }
+ }
+
+ // Batch update chunks (for IsEnabled and TagID)
+ if len(chunksToUpdate) > 0 {
+ if err := s.chunkRepo.UpdateChunks(ctx, chunksToUpdate); err != nil {
+ return err
+ }
+ }
+
+ // Batch update flags (for IsRecommended)
+ if len(setFlags) > 0 || len(clearFlags) > 0 {
+ if err := s.chunkRepo.UpdateChunkFlagsBatch(ctx, tenantID, kb.ID, setFlags, clearFlags); err != nil {
+ return err
+ }
+ }
}
- // Sync update to retriever engines
- if len(chunkStatusMap) > 0 {
+ // Sync enabled status to retriever engines
+ if len(enabledUpdates) > 0 {
tenantInfo := ctx.Value(types.TenantInfoContextKey).(*types.Tenant)
retrieveEngine, err := retriever.NewCompositeRetrieveEngine(
s.retrieveEngine,
@@ -3371,7 +3490,7 @@ func (s *knowledgeService) UpdateFAQEntryStatusBatch(ctx context.Context,
if err != nil {
return err
}
- if err := retrieveEngine.BatchUpdateChunkEnabledStatus(ctx, chunkStatusMap); err != nil {
+ if err := retrieveEngine.BatchUpdateChunkEnabledStatus(ctx, enabledUpdates); err != nil {
return err
}
}
@@ -3878,6 +3997,7 @@ func (s *knowledgeService) chunkToFAQEntry(chunk *types.Chunk, kb *types.Knowled
KnowledgeBaseID: chunk.KnowledgeBaseID,
TagID: chunk.TagID,
IsEnabled: chunk.IsEnabled,
+ IsRecommended: chunk.Flags.HasFlag(types.ChunkFlagRecommended),
StandardQuestion: meta.StandardQuestion,
SimilarQuestions: meta.SimilarQuestions,
NegativeQuestions: meta.NegativeQuestions,
@@ -3969,6 +4089,26 @@ func (s *knowledgeService) checkFAQQuestionDuplicate(
return nil
}
+// resolveTagID resolves tag ID from payload, prioritizing tag_id over tag_name
+func (s *knowledgeService) resolveTagID(ctx context.Context, kbID string, payload *types.FAQEntryPayload) (string, error) {
+ // 如果提供了 tag_id,优先使用 tag_id
+ if payload.TagID != "" {
+ return payload.TagID, nil
+ }
+
+ // 如果提供了 tag_name,查找或创建标签
+ if payload.TagName != "" && payload.TagName != "未分类" {
+ tag, err := s.tagService.FindOrCreateTagByName(ctx, kbID, payload.TagName)
+ if err != nil {
+ return "", fmt.Errorf("failed to resolve tag by name '%s': %w", payload.TagName, err)
+ }
+ return tag.ID, nil
+ }
+
+ // 都没有提供,返回空字符串
+ return "", nil
+}
+
func sanitizeFAQEntryPayload(payload *types.FAQEntryPayload) (*types.FAQChunkMetadata, error) {
meta := &types.FAQChunkMetadata{
StandardQuestion: strings.TrimSpace(payload.StandardQuestion),
diff --git a/internal/application/service/tag.go b/internal/application/service/tag.go
index 1efc05a8..06896310 100644
--- a/internal/application/service/tag.go
+++ b/internal/application/service/tag.go
@@ -2,6 +2,7 @@ package service
import (
"context"
+ "errors"
"strings"
"time"
@@ -10,22 +11,26 @@ import (
"github.com/Tencent/WeKnora/internal/types"
"github.com/Tencent/WeKnora/internal/types/interfaces"
"github.com/google/uuid"
+ "gorm.io/gorm"
)
// knowledgeTagService implements KnowledgeTagService.
type knowledgeTagService struct {
kbService interfaces.KnowledgeBaseService
repo interfaces.KnowledgeTagRepository
+ chunkRepo interfaces.ChunkRepository
}
// NewKnowledgeTagService creates a new tag service.
func NewKnowledgeTagService(
kbService interfaces.KnowledgeBaseService,
repo interfaces.KnowledgeTagRepository,
+ chunkRepo interfaces.ChunkRepository,
) (interfaces.KnowledgeTagService, error) {
return &knowledgeTagService{
kbService: kbService,
repo: repo,
+ chunkRepo: chunkRepo,
}, nil
}
@@ -147,7 +152,7 @@ func (s *knowledgeTagService) UpdateTag(
return tag, nil
}
-// DeleteTag deletes a tag. When force=false, deletion is only allowed if no references exist.
+// DeleteTag deletes a tag. When force=true, also deletes all chunks under this tag.
func (s *knowledgeTagService) DeleteTag(ctx context.Context, id string, force bool) error {
if id == "" {
return werrors.NewBadRequestError("标签ID不能为空")
@@ -164,5 +169,42 @@ func (s *knowledgeTagService) DeleteTag(ctx context.Context, id string, force bo
if !force && (kCount > 0 || cCount > 0) {
return werrors.NewBadRequestError("标签仍有知识或FAQ条目引用,无法删除")
}
+ // When force=true, delete all chunks under this tag first
+ if force && cCount > 0 {
+ if err := s.chunkRepo.DeleteChunksByTagID(ctx, tenantID, tag.KnowledgeBaseID, tag.ID); err != nil {
+ logger.Errorf(ctx, "Failed to delete chunks by tag ID %s: %v", tag.ID, err)
+ return werrors.NewInternalServerError("删除标签下的数据失败")
+ }
+ logger.Infof(ctx, "Deleted %d chunks under tag %s", cCount, tag.ID)
+ }
return s.repo.Delete(ctx, tenantID, id)
}
+
+// FindOrCreateTagByName finds a tag by name or creates it if not exists.
+func (s *knowledgeTagService) FindOrCreateTagByName(ctx context.Context, kbID string, name string) (*types.KnowledgeTag, error) {
+ name = strings.TrimSpace(name)
+ if kbID == "" || name == "" {
+ return nil, werrors.NewBadRequestError("知识库ID和标签名称不能为空")
+ }
+
+ kb, err := s.kbService.GetKnowledgeBaseByID(ctx, kbID)
+ if err != nil {
+ return nil, err
+ }
+
+ tenantID := kb.TenantID
+
+ // 先尝试查找现有标签
+ tag, err := s.repo.GetByName(ctx, tenantID, kbID, name)
+ if err == nil {
+ return tag, nil
+ }
+
+ // 如果不是 not found 错误,直接返回
+ if !errors.Is(err, gorm.ErrRecordNotFound) {
+ return nil, err
+ }
+
+ // 创建新标签
+ return s.CreateTag(ctx, kbID, name, "", 0)
+}
diff --git a/internal/handler/faq.go b/internal/handler/faq.go
index 27a1b1a9..46a3e2ea 100644
--- a/internal/handler/faq.go
+++ b/internal/handler/faq.go
@@ -138,17 +138,19 @@ func (h *FAQHandler) UpdateEntryTagBatch(c *gin.Context) {
})
}
-// UpdateEntryStatusBatch updates the enable status of FAQ entries in batch.
-func (h *FAQHandler) UpdateEntryStatusBatch(c *gin.Context) {
+// UpdateEntryFieldsBatch updates multiple fields for FAQ entries in batch.
+// This is the unified API for batch updating FAQ entry fields.
+// Supports updating is_enabled, is_recommended, tag_id in a single call.
+func (h *FAQHandler) UpdateEntryFieldsBatch(c *gin.Context) {
ctx := c.Request.Context()
- var req faqEntryStatusBatchRequest
+ var req types.FAQEntryFieldsBatchUpdate
if err := c.ShouldBindJSON(&req); err != nil {
- logger.Error(ctx, "Failed to bind FAQ entry status batch payload", err)
+ logger.Error(ctx, "Failed to bind FAQ entry fields batch payload", err)
c.Error(errors.NewBadRequestError("请求参数不合法").WithDetails(err.Error()))
return
}
- if err := h.knowledgeService.UpdateFAQEntryStatusBatch(ctx,
- secutils.SanitizeForLog(c.Param("id")), req.Updates); err != nil {
+ if err := h.knowledgeService.UpdateFAQEntryFieldsBatch(ctx,
+ secutils.SanitizeForLog(c.Param("id")), &req); err != nil {
logger.ErrorWithFields(ctx, err, nil)
c.Error(err)
return
@@ -163,11 +165,6 @@ type faqDeleteRequest struct {
IDs []string `json:"ids" binding:"required,min=1,dive,required"`
}
-// faqEntryStatusBatchRequest is a request for updating the enable status of FAQ entries in batch
-type faqEntryStatusBatchRequest struct {
- Updates map[string]bool `json:"updates" binding:"required,min=1"`
-}
-
// faqEntryTagBatchRequest is a request for updating tags for FAQ entries in batch
type faqEntryTagBatchRequest struct {
Updates map[string]*string `json:"updates" binding:"required,min=1"`
diff --git a/internal/router/router.go b/internal/router/router.go
index 5758eab0..78d743e8 100644
--- a/internal/router/router.go
+++ b/internal/router/router.go
@@ -166,7 +166,8 @@ func RegisterFAQRoutes(r *gin.RouterGroup, handler *handler.FAQHandler) {
faq.POST("/entries", handler.UpsertEntries)
faq.POST("/entry", handler.CreateEntry)
faq.PUT("/entries/:entry_id", handler.UpdateEntry)
- faq.PUT("/entries/status", handler.UpdateEntryStatusBatch)
+ // Unified batch update API - supports is_enabled, is_recommended, tag_id
+ faq.PUT("/entries/fields", handler.UpdateEntryFieldsBatch)
faq.PUT("/entries/tags", handler.UpdateEntryTagBatch)
faq.DELETE("/entries", handler.DeleteEntries)
faq.POST("/search", handler.SearchFAQ)
diff --git a/internal/types/chunk.go b/internal/types/chunk.go
index 99a16053..5242bb1c 100644
--- a/internal/types/chunk.go
+++ b/internal/types/chunk.go
@@ -41,6 +41,38 @@ const (
ChunkStatusIndexed ChunkStatus = 2
)
+// ChunkFlags 定义 Chunk 的标志位,用于管理多个布尔状态
+type ChunkFlags int
+
+const (
+ // ChunkFlagRecommended 表示可推荐状态(1 << 0 = 1)
+ // 当设置此标志时,该 Chunk 可以被推荐给用户
+ ChunkFlagRecommended ChunkFlags = 1 << 0
+ // 未来可扩展更多标志位:
+ // ChunkFlagPinned ChunkFlags = 1 << 1 // 置顶
+ // ChunkFlagHot ChunkFlags = 1 << 2 // 热门
+)
+
+// HasFlag 检查是否设置了指定标志
+func (f ChunkFlags) HasFlag(flag ChunkFlags) bool {
+ return f&flag != 0
+}
+
+// SetFlag 设置指定标志
+func (f ChunkFlags) SetFlag(flag ChunkFlags) ChunkFlags {
+ return f | flag
+}
+
+// ClearFlag 清除指定标志
+func (f ChunkFlags) ClearFlag(flag ChunkFlags) ChunkFlags {
+ return f &^ flag
+}
+
+// ToggleFlag 切换指定标志
+func (f ChunkFlags) ToggleFlag(flag ChunkFlags) ChunkFlags {
+ return f ^ flag
+}
+
// ImageInfo 表示与 Chunk 关联的图片信息
type ImageInfo struct {
// 图片URL(COS)
@@ -80,6 +112,9 @@ type Chunk struct {
ChunkIndex int `json:"chunk_index"`
// Whether the chunk is enabled, can be used to temporarily disable certain chunks
IsEnabled bool `json:"is_enabled" gorm:"default:true"`
+ // Flags 存储多个布尔状态的位标志(如推荐状态等)
+ // 默认值为 ChunkFlagRecommended (1),表示默认可推荐
+ Flags ChunkFlags `json:"flags" gorm:"default:1"`
// Status of the chunk
Status int `json:"status" gorm:"default:0"`
// Starting character position in the original text
diff --git a/internal/types/faq.go b/internal/types/faq.go
index d8d21c71..0c134fe9 100644
--- a/internal/types/faq.go
+++ b/internal/types/faq.go
@@ -170,6 +170,7 @@ type FAQEntry struct {
KnowledgeBaseID string `json:"knowledge_base_id"`
TagID string `json:"tag_id"`
IsEnabled bool `json:"is_enabled"`
+ IsRecommended bool `json:"is_recommended"`
StandardQuestion string `json:"standard_question"`
SimilarQuestions []string `json:"similar_questions"`
NegativeQuestions []string `json:"negative_questions"`
@@ -189,7 +190,9 @@ type FAQEntryPayload struct {
NegativeQuestions []string `json:"negative_questions"`
Answers []string `json:"answers" binding:"required"`
TagID string `json:"tag_id"`
+ TagName string `json:"tag_name"`
IsEnabled *bool `json:"is_enabled,omitempty"`
+ IsRecommended *bool `json:"is_recommended,omitempty"`
}
const (
@@ -211,6 +214,25 @@ type FAQSearchRequest struct {
MatchCount int `json:"match_count"`
}
+// FAQEntryFieldsUpdate 单个FAQ条目的字段更新
+type FAQEntryFieldsUpdate struct {
+ IsEnabled *bool `json:"is_enabled,omitempty"`
+ IsRecommended *bool `json:"is_recommended,omitempty"`
+ TagID *string `json:"tag_id,omitempty"`
+ // 后续可扩展更多字段
+}
+
+// FAQEntryFieldsBatchUpdate 批量更新FAQ条目字段的请求
+// 支持两种模式:
+// 1. 按条目ID更新:使用 ByID 字段
+// 2. 按Tag更新:使用 ByTag 字段,将该Tag下所有条目应用相同的更新
+type FAQEntryFieldsBatchUpdate struct {
+ // ByID 按条目ID更新,key为条目ID
+ ByID map[string]FAQEntryFieldsUpdate `json:"by_id,omitempty"`
+ // ByTag 按Tag批量更新,key为TagID(空字符串表示未分类)
+ ByTag map[string]FAQEntryFieldsUpdate `json:"by_tag,omitempty"`
+}
+
// FAQImportTaskStatus 导入任务状态
type FAQImportTaskStatus string
diff --git a/internal/types/interfaces/chunk.go b/internal/types/interfaces/chunk.go
index 1d3e35a1..0a0c0f2c 100644
--- a/internal/types/interfaces/chunk.go
+++ b/internal/types/interfaces/chunk.go
@@ -40,6 +40,8 @@ type ChunkRepository interface {
DeleteChunksByKnowledgeID(ctx context.Context, tenantID uint64, knowledgeID string) error
// DeleteByKnowledgeList deletes all chunks for a knowledge list
DeleteByKnowledgeList(ctx context.Context, tenantID uint64, knowledgeIDs []string) error
+ // DeleteChunksByTagID deletes all chunks with the specified tag ID
+ DeleteChunksByTagID(ctx context.Context, tenantID uint64, kbID string, tagID string) error
// CountChunksByKnowledgeBaseID counts the number of chunks in a knowledge base.
CountChunksByKnowledgeBaseID(ctx context.Context, tenantID uint64, kbID string) (int64, error)
// DeleteUnindexedChunks deletes unindexed chunks by knowledge id and chunk index range
@@ -50,6 +52,13 @@ type ChunkRepository interface {
// ListAllFAQChunksWithMetadataByKnowledgeBaseID lists all FAQ chunks for a knowledge base ID
// returns ID and Metadata fields for duplicate question checking
ListAllFAQChunksWithMetadataByKnowledgeBaseID(ctx context.Context, tenantID uint64, kbID string) ([]*types.Chunk, error)
+ // UpdateChunkFlagsBatch updates flags for multiple chunks in batch using a single SQL statement.
+ // setFlags: map of chunk ID to flags to set (OR operation)
+ // clearFlags: map of chunk ID to flags to clear (AND NOT operation)
+ UpdateChunkFlagsBatch(ctx context.Context, tenantID uint64, kbID string, setFlags map[string]types.ChunkFlags, clearFlags map[string]types.ChunkFlags) error
+ // UpdateChunkFieldsByTagID updates fields for all chunks with the specified tag ID.
+ // Supports updating is_enabled and flags fields.
+ UpdateChunkFieldsByTagID(ctx context.Context, tenantID uint64, kbID string, tagID string, isEnabled *bool, setFlags types.ChunkFlags, clearFlags types.ChunkFlags) ([]string, error)
}
// ChunkService defines the interface for chunk service operations
diff --git a/internal/types/interfaces/knowledge.go b/internal/types/interfaces/knowledge.go
index 6c739c75..f51e132d 100644
--- a/internal/types/interfaces/knowledge.go
+++ b/internal/types/interfaces/knowledge.go
@@ -88,8 +88,9 @@ type KnowledgeService interface {
CreateFAQEntry(ctx context.Context, kbID string, payload *types.FAQEntryPayload) (*types.FAQEntry, error)
// UpdateFAQEntry updates a single FAQ entry.
UpdateFAQEntry(ctx context.Context, kbID string, entryID string, payload *types.FAQEntryPayload) error
- // UpdateFAQEntryStatusBatch updates enable status for FAQ entries in batch.
- UpdateFAQEntryStatusBatch(ctx context.Context, kbID string, updates map[string]bool) error
+ // UpdateFAQEntryFieldsBatch updates multiple fields for FAQ entries in batch.
+ // Supports updating is_enabled, is_recommended, tag_id, and other fields in a single call.
+ UpdateFAQEntryFieldsBatch(ctx context.Context, kbID string, req *types.FAQEntryFieldsBatchUpdate) error
// DeleteFAQEntries deletes FAQ entries in batch.
DeleteFAQEntries(ctx context.Context, kbID string, entryIDs []string) error
// SearchFAQEntries searches FAQ entries using hybrid search.
diff --git a/internal/types/interfaces/tag.go b/internal/types/interfaces/tag.go
index d1cd0321..4d413b19 100644
--- a/internal/types/interfaces/tag.go
+++ b/internal/types/interfaces/tag.go
@@ -16,6 +16,8 @@ type KnowledgeTagService interface {
UpdateTag(ctx context.Context, id string, name *string, color *string, sortOrder *int) (*types.KnowledgeTag, error)
// DeleteTag deletes a tag.
DeleteTag(ctx context.Context, id string, force bool) error
+ // FindOrCreateTagByName finds a tag by name or creates it if not exists.
+ FindOrCreateTagByName(ctx context.Context, kbID string, name string) (*types.KnowledgeTag, error)
}
// KnowledgeTagRepository defines persistence operations for tags.
@@ -23,6 +25,7 @@ type KnowledgeTagRepository interface {
Create(ctx context.Context, tag *types.KnowledgeTag) error
Update(ctx context.Context, tag *types.KnowledgeTag) error
GetByID(ctx context.Context, tenantID uint64, id string) (*types.KnowledgeTag, error)
+ GetByName(ctx context.Context, tenantID uint64, kbID string, name string) (*types.KnowledgeTag, error)
ListByKB(
ctx context.Context,
tenantID uint64,
diff --git a/migrations/versioned/000003_chunk_flags.down.sql b/migrations/versioned/000003_chunk_flags.down.sql
new file mode 100644
index 00000000..25161b1b
--- /dev/null
+++ b/migrations/versioned/000003_chunk_flags.down.sql
@@ -0,0 +1,11 @@
+-- Migration: chunk_flags (rollback)
+-- Description: Remove flags column from chunks table
+
+DO $$
+BEGIN
+ RAISE NOTICE '[Migration 000003] Removing flags column from chunks table...';
+
+ ALTER TABLE chunks DROP COLUMN IF EXISTS flags;
+
+ RAISE NOTICE '[Migration 000003] Flags column removed successfully';
+END $$;
diff --git a/migrations/versioned/000003_chunk_flags.up.sql b/migrations/versioned/000003_chunk_flags.up.sql
new file mode 100644
index 00000000..e9932161
--- /dev/null
+++ b/migrations/versioned/000003_chunk_flags.up.sql
@@ -0,0 +1,13 @@
+-- Migration: chunk_flags
+-- Description: Add flags column to chunks table for managing multiple boolean states
+
+DO $$
+BEGIN
+ RAISE NOTICE '[Migration 000003] Adding flags column to chunks table...';
+
+ -- Add flags column with default value 1 (ChunkFlagRecommended)
+ -- This means all existing chunks will be recommended by default
+ ALTER TABLE chunks ADD COLUMN IF NOT EXISTS flags INTEGER NOT NULL DEFAULT 1;
+
+ RAISE NOTICE '[Migration 000003] Flags column added successfully';
+END $$;