From 557081c8f80974e77af21dfdcce71e337459d597 Mon Sep 17 00:00:00 2001 From: wizardchen Date: Wed, 24 Dec 2025 17:48:06 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8D=E4=B8=8D=E5=90=8C?= =?UTF-8?q?=E6=95=B0=E6=8D=AE=E5=BA=93=E7=B1=BB=E5=9E=8B=E7=9A=84JSON?= =?UTF-8?q?=E6=9F=A5=E8=AF=A2=E5=85=BC=E5=AE=B9=E6=80=A7=E9=97=AE=E9=A2=98?= =?UTF-8?q?=EF=BC=8C=E6=94=AF=E6=8C=81MySQL=E5=92=8CPostgreSQL=E7=9A=84?= =?UTF-8?q?=E5=B7=AE=E5=BC=82=E5=A4=84=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- internal/application/repository/chunk.go | 29 ++++++++++++++++++++---- 1 file changed, 25 insertions(+), 4 deletions(-) diff --git a/internal/application/repository/chunk.go b/internal/application/repository/chunk.go index 81216dbff..b7a71ea71 100644 --- a/internal/application/repository/chunk.go +++ b/internal/application/repository/chunk.go @@ -98,19 +98,40 @@ func (r *chunkRepository) ListPagedChunksByKnowledgeID( } if keyword != "" { like := "%" + keyword + "%" + // 根据数据库类型使用不同的 JSON 查询语法 + isPostgres := db.Dialector.Name() == "postgres" + switch searchField { case "standard_question": // Search only in standard_question field of metadata - db = db.Where("metadata->>'standard_question' ILIKE ?", like) + if isPostgres { + db = db.Where("metadata->>'standard_question' ILIKE ?", like) + } else { + // MySQL: metadata->>'$.standard_question' (MySQL 5.7.13+) + // 也可以用 JSON_UNQUOTE(JSON_EXTRACT(metadata, '$.standard_question')) + db = db.Where("metadata->>'$.standard_question' LIKE ?", like) + } case "similar_questions": // Search in similar_questions array of metadata - db = db.Where("metadata->'similar_questions'::text ILIKE ?", like) + if isPostgres { + db = db.Where("metadata->'similar_questions'::text ILIKE ?", like) + } else { + db = db.Where("JSON_EXTRACT(metadata, '$.similar_questions') LIKE ?", like) + } case "answers": // Search in answers array of metadata - db = db.Where("metadata->'answers'::text ILIKE ?", like) + if isPostgres { + db = db.Where("metadata->'answers'::text ILIKE ?", like) + } else { + db = db.Where("JSON_EXTRACT(metadata, '$.answers') LIKE ?", like) + } default: // Search in all fields (content and metadata) - db = db.Where("(content ILIKE ? OR metadata::text ILIKE ?)", like, like) + if isPostgres { + db = db.Where("(content ILIKE ? OR metadata::text ILIKE ?)", like, like) + } else { + db = db.Where("(content LIKE ? OR CAST(metadata AS CHAR) LIKE ?)", like, like) + } } } return db