chore: 初始化仓库,导入项目基线代码
Academy v0.8.3 完整代码基线:Go 后端(internal/cmd/cli/mcp-server/docreader)、 Vue 前端(frontend)、数据库迁移(migrations)、部署编排(docker/helm/deploy)与文档。 今日开发的培训推送管理改动随后单独提交。
This commit is contained in:
commit
7ec21f35b0
5767 files changed
+1436788
No files matched your search
@@ -0,0 +1,104 @@
|
||||
package postgres
|
||||
|
||||
import (
|
||||
"context"
|
||||
"os"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/require"
|
||||
gormpostgres "gorm.io/driver/postgres"
|
||||
"gorm.io/gorm"
|
||||
"gorm.io/gorm/logger"
|
||||
)
|
||||
|
||||
// TestKeywordScoreCatalogCompatibility checks the actual PostgreSQL catalog
|
||||
// query, including preference and signatures, rather than mocking its result.
|
||||
// Set ACADEMY_RETRIEVER_TEST_POSTGRES_DSN to a disposable PostgreSQL database
|
||||
// without pg_search installed. Fixtures are rolled back after each subtest;
|
||||
// existing scoring schemas are never replaced or dropped.
|
||||
func TestKeywordScoreCatalogCompatibility(t *testing.T) {
|
||||
dsn := os.Getenv("ACADEMY_RETRIEVER_TEST_POSTGRES_DSN")
|
||||
if dsn == "" {
|
||||
t.Skip("set ACADEMY_RETRIEVER_TEST_POSTGRES_DSN to run PostgreSQL catalog tests")
|
||||
}
|
||||
db, err := gorm.Open(gormpostgres.Open(dsn), &gorm.Config{
|
||||
Logger: logger.Default.LogMode(logger.Silent),
|
||||
})
|
||||
require.NoError(t, err)
|
||||
sqlDB, err := db.DB()
|
||||
require.NoError(t, err)
|
||||
t.Cleanup(func() { _ = sqlDB.Close() })
|
||||
|
||||
var existing int
|
||||
require.NoError(t, db.Raw(`SELECT count(*) FROM pg_catalog.pg_namespace
|
||||
WHERE nspname IN ('paradedb', 'pgsearch', 'unrelated_score_test')`).Scan(&existing).Error)
|
||||
require.Zero(t, existing, "use a disposable database without existing scoring schemas")
|
||||
|
||||
paradedb := "CREATE FUNCTION paradedb.score(anyelement) RETURNS real LANGUAGE sql AS 'SELECT 1::real'"
|
||||
pgsearch := "CREATE FUNCTION pgsearch.score(anyelement) RETURNS real LANGUAGE sql AS 'SELECT 1::real'"
|
||||
for _, tc := range []struct {
|
||||
name string
|
||||
functions []string
|
||||
want string
|
||||
}{
|
||||
{name: "ParadeDB", functions: []string{paradedb}, want: "paradedb.score"},
|
||||
{name: "PolarDB_layout", functions: []string{pgsearch}, want: "pgsearch.score"},
|
||||
{name: "prefer_ParadeDB", functions: []string{pgsearch, paradedb}, want: "paradedb.score"},
|
||||
{name: "missing_functions"},
|
||||
{
|
||||
name: "incompatible_argument_falls_back",
|
||||
functions: []string{
|
||||
"CREATE FUNCTION paradedb.score(integer) RETURNS real LANGUAGE sql AS 'SELECT 1::real'",
|
||||
pgsearch,
|
||||
},
|
||||
want: "pgsearch.score",
|
||||
},
|
||||
{
|
||||
name: "incompatible_result_falls_back",
|
||||
functions: []string{
|
||||
"CREATE FUNCTION paradedb.score(anyelement) RETURNS double precision LANGUAGE sql AS 'SELECT 1::real'",
|
||||
pgsearch,
|
||||
},
|
||||
want: "pgsearch.score",
|
||||
},
|
||||
{
|
||||
name: "reject_wrong_arity",
|
||||
functions: []string{
|
||||
"CREATE FUNCTION pgsearch.score(anyelement, integer) RETURNS real LANGUAGE sql AS 'SELECT 1::real'",
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "reject_unlisted_schema",
|
||||
functions: []string{
|
||||
"CREATE FUNCTION unrelated_score_test.score(anyelement) RETURNS real LANGUAGE sql AS 'SELECT 1::real'",
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "reject_wrong_result",
|
||||
functions: []string{
|
||||
"CREATE FUNCTION pgsearch.score(anyelement) RETURNS double precision LANGUAGE sql AS 'SELECT 1::real'",
|
||||
},
|
||||
},
|
||||
} {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
tx := db.Begin()
|
||||
require.NoError(t, tx.Error)
|
||||
t.Cleanup(func() { require.NoError(t, tx.Rollback().Error) })
|
||||
for _, schema := range []string{"paradedb", "pgsearch", "unrelated_score_test"} {
|
||||
require.NoError(t, tx.Exec("CREATE SCHEMA "+schema).Error)
|
||||
}
|
||||
for _, ddl := range tc.functions {
|
||||
require.NoError(t, tx.Exec(ddl).Error)
|
||||
}
|
||||
repo := &pgRepository{db: tx}
|
||||
function, err := repo.resolveKeywordScoreFunction(context.Background())
|
||||
if tc.want == "" {
|
||||
require.ErrorContains(t, err, "check pg_search extension compatibility")
|
||||
require.Empty(t, function)
|
||||
return
|
||||
}
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, tc.want, function)
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,193 @@
|
||||
package postgres
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"regexp"
|
||||
"sync"
|
||||
"testing"
|
||||
|
||||
"github.com/DATA-DOG/go-sqlmock"
|
||||
"academy/internal/types"
|
||||
"github.com/stretchr/testify/require"
|
||||
gormpostgres "gorm.io/driver/postgres"
|
||||
"gorm.io/gorm"
|
||||
"gorm.io/gorm/logger"
|
||||
)
|
||||
|
||||
func newKeywordTestRepository(t *testing.T) (*pgRepository, sqlmock.Sqlmock) {
|
||||
t.Helper()
|
||||
db, mock, err := sqlmock.New()
|
||||
require.NoError(t, err)
|
||||
t.Cleanup(func() { require.NoError(t, mock.ExpectationsWereMet()); _ = db.Close() })
|
||||
gdb, err := gorm.Open(gormpostgres.New(gormpostgres.Config{Conn: db}), &gorm.Config{
|
||||
DisableAutomaticPing: true,
|
||||
Logger: logger.Default.LogMode(logger.Silent),
|
||||
})
|
||||
require.NoError(t, err)
|
||||
return NewPostgresRetrieveEngineRepository(gdb).(*pgRepository), mock
|
||||
}
|
||||
|
||||
// Exercise the actual query path for both backends, including scope filters,
|
||||
// bound query text, score mapping, and reuse of the per-connection detection.
|
||||
func TestKeywordsRetrieveScoreFunctionCompatibility(t *testing.T) {
|
||||
for _, scoreFunction := range []string{"paradedb.score", "pgsearch.score"} {
|
||||
t.Run(scoreFunction, func(t *testing.T) {
|
||||
repo, mock := newKeywordTestRepository(t)
|
||||
mock.ExpectQuery(regexp.QuoteMeta(keywordScoreFunctionSQL)).
|
||||
WillReturnRows(sqlmock.NewRows([]string{"score_function"}).AddRow(scoreFunction))
|
||||
params := types.RetrieveParams{
|
||||
RetrieverType: types.KeywordsRetrieverType,
|
||||
Query: "关键词测试 ' OR 1=1 --", TopK: 5,
|
||||
KnowledgeBaseIDs: []string{"kb-test"}, KnowledgeIDs: []string{"doc-test"},
|
||||
TagIDs: []string{"tag-test"},
|
||||
}
|
||||
query := `SELECT ` + regexp.QuoteMeta(scoreFunction) + `\(id\) as score,.*` +
|
||||
`FROM "embeddings" WHERE "knowledge_base_id" = \$1.*` +
|
||||
`"knowledge_id" = \$2.*"tag_id" = \$3.*` +
|
||||
`content \|\|\| \$4.*is_enabled IS NULL OR is_enabled = \$5.*` +
|
||||
`ORDER BY "score" DESC LIMIT \$6`
|
||||
for range 2 {
|
||||
mock.ExpectQuery(query).
|
||||
WithArgs("kb-test", "doc-test", "tag-test", params.Query, true, 5).
|
||||
WillReturnRows(sqlmock.NewRows([]string{
|
||||
"id", "chunk_id", "knowledge_id", "knowledge_base_id", "tag_id", "score",
|
||||
}).
|
||||
AddRow(1, "chunk-test", "doc-test", "kb-test", "tag-test", 31.15))
|
||||
results, err := repo.Retrieve(context.Background(), params)
|
||||
require.NoError(t, err)
|
||||
require.Len(t, results, 1)
|
||||
require.Len(t, results[0].Results, 1)
|
||||
require.Equal(t, "chunk-test", results[0].Results[0].ChunkID)
|
||||
require.Equal(t, 31.15, results[0].Results[0].Score)
|
||||
require.Equal(t, types.MatchTypeKeywords, results[0].Results[0].MatchType)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestKeywordScoreDetectionFailuresDoNotPoisonCache(t *testing.T) {
|
||||
for _, kind := range []string{"missing", "unexpected", "database_error"} {
|
||||
t.Run(kind, func(t *testing.T) {
|
||||
repo, mock := newKeywordTestRepository(t)
|
||||
probe := mock.ExpectQuery(regexp.QuoteMeta(keywordScoreFunctionSQL))
|
||||
cause := errors.New("catalog unavailable")
|
||||
switch kind {
|
||||
case "missing":
|
||||
probe.WillReturnRows(sqlmock.NewRows([]string{"score_function"}))
|
||||
case "unexpected":
|
||||
probe.WillReturnRows(sqlmock.NewRows([]string{"score_function"}).AddRow("untrusted.score"))
|
||||
default:
|
||||
probe.WillReturnError(cause)
|
||||
}
|
||||
results, err := repo.KeywordsRetrieve(context.Background(), types.RetrieveParams{Query: "关键词测试", TopK: 5})
|
||||
require.Nil(t, results)
|
||||
if kind == "database_error" {
|
||||
require.ErrorIs(t, err, cause)
|
||||
} else {
|
||||
require.ErrorContains(t, err, "check pg_search extension compatibility")
|
||||
}
|
||||
mock.ExpectQuery(regexp.QuoteMeta(keywordScoreFunctionSQL)).
|
||||
WillReturnRows(sqlmock.NewRows([]string{"score_function"}).AddRow("pgsearch.score"))
|
||||
function, err := repo.resolveKeywordScoreFunction(context.Background())
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, "pgsearch.score", function)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestKeywordScoreDetectionConcurrentAndPerRepository(t *testing.T) {
|
||||
repo, mock := newKeywordTestRepository(t)
|
||||
mock.ExpectQuery(regexp.QuoteMeta(keywordScoreFunctionSQL)).
|
||||
WillReturnRows(sqlmock.NewRows([]string{"score_function"}).AddRow("paradedb.score"))
|
||||
var wg sync.WaitGroup
|
||||
for range 16 {
|
||||
wg.Go(func() {
|
||||
function, err := repo.resolveKeywordScoreFunction(context.Background())
|
||||
if err != nil || function != "paradedb.score" {
|
||||
t.Errorf("resolve score function = %q, %v", function, err)
|
||||
}
|
||||
})
|
||||
}
|
||||
wg.Wait()
|
||||
other, otherMock := newKeywordTestRepository(t)
|
||||
otherMock.ExpectQuery(regexp.QuoteMeta(keywordScoreFunctionSQL)).
|
||||
WillReturnRows(sqlmock.NewRows([]string{"score_function"}).AddRow("pgsearch.score"))
|
||||
function, err := other.resolveKeywordScoreFunction(context.Background())
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, "pgsearch.score", function)
|
||||
}
|
||||
|
||||
func TestKeywordScoreDetectionCanceledContextCanRetry(t *testing.T) {
|
||||
repo, mock := newKeywordTestRepository(t)
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
cancel()
|
||||
|
||||
// A canceled caller must not cache a failed detection or block later calls.
|
||||
_, err := repo.resolveKeywordScoreFunction(ctx)
|
||||
require.ErrorIs(t, err, context.Canceled)
|
||||
mock.ExpectQuery(regexp.QuoteMeta(keywordScoreFunctionSQL)).
|
||||
WillReturnRows(sqlmock.NewRows([]string{"score_function"}).AddRow("paradedb.score"))
|
||||
function, err := repo.resolveKeywordScoreFunction(context.Background())
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, "paradedb.score", function)
|
||||
}
|
||||
|
||||
func TestKeywordsRetrieveQueryFailurePreservesResolvedFunction(t *testing.T) {
|
||||
repo, mock := newKeywordTestRepository(t)
|
||||
mock.ExpectQuery(regexp.QuoteMeta(keywordScoreFunctionSQL)).
|
||||
WillReturnRows(sqlmock.NewRows([]string{"score_function"}).AddRow("pgsearch.score"))
|
||||
query := `SELECT pgsearch\.score\(id\) as score,.*FROM "embeddings".*`
|
||||
cause := errors.New("keyword index unavailable")
|
||||
mock.ExpectQuery(query).WithArgs("关键词测试", true, 5).WillReturnError(cause)
|
||||
params := types.RetrieveParams{Query: "关键词测试", TopK: 5}
|
||||
results, err := repo.KeywordsRetrieve(context.Background(), params)
|
||||
require.Nil(t, results)
|
||||
require.ErrorIs(t, err, cause)
|
||||
|
||||
// The catalog probe succeeded: retry only the retrieval, not detection.
|
||||
mock.ExpectQuery(query).WithArgs("关键词测试", true, 5).
|
||||
WillReturnRows(sqlmock.NewRows([]string{"id", "score"}))
|
||||
results, err = repo.KeywordsRetrieve(context.Background(), params)
|
||||
require.NoError(t, err)
|
||||
require.Len(t, results, 1)
|
||||
require.Empty(t, results[0].Results)
|
||||
}
|
||||
|
||||
func TestVectorRetrieveDoesNotRequireKeywordScoreFunction(t *testing.T) {
|
||||
for _, keywordFailed := range []bool{false, true} {
|
||||
name := "without_keyword_probe"
|
||||
if keywordFailed {
|
||||
name = "after_unsupported_keyword_probe"
|
||||
}
|
||||
t.Run(name, func(t *testing.T) {
|
||||
repo, mock := newKeywordTestRepository(t)
|
||||
if keywordFailed {
|
||||
mock.ExpectQuery(regexp.QuoteMeta(keywordScoreFunctionSQL)).
|
||||
WillReturnRows(sqlmock.NewRows([]string{"score_function"}))
|
||||
_, err := repo.KeywordsRetrieve(context.Background(), types.RetrieveParams{Query: "关键词测试", TopK: 5})
|
||||
require.Error(t, err)
|
||||
}
|
||||
|
||||
// Expect only vector SQL: any score-function probe fails this test.
|
||||
mock.ExpectBegin()
|
||||
mock.ExpectExec("SET LOCAL hnsw.ef_search = 100").WillReturnResult(sqlmock.NewResult(0, 0))
|
||||
mock.ExpectExec("SET LOCAL hnsw.iterative_scan = strict_order").WillReturnResult(sqlmock.NewResult(0, 0))
|
||||
mock.ExpectQuery(`SELECT .*FROM embeddings.*`).
|
||||
WithArgs(sqlmock.AnyArg(), 2, true, 100, 0.5, 3).
|
||||
WillReturnRows(sqlmock.NewRows([]string{"id", "chunk_id", "score"}).AddRow(1, "vector-chunk", 0.9))
|
||||
mock.ExpectCommit()
|
||||
results, err := repo.Retrieve(context.Background(), types.RetrieveParams{
|
||||
RetrieverType: types.VectorRetrieverType,
|
||||
Embedding: []float32{0.1, 0.2},
|
||||
Threshold: 0.5,
|
||||
TopK: 3,
|
||||
})
|
||||
require.NoError(t, err)
|
||||
require.Len(t, results, 1)
|
||||
require.Len(t, results[0].Results, 1)
|
||||
require.Equal(t, "vector-chunk", results[0].Results[0].ChunkID)
|
||||
require.Equal(t, types.MatchTypeEmbedding, results[0].Results[0].MatchType)
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
// Package postgres implements retrieval against its configured vector backend.
|
||||
package postgres
|
||||
|
||||
import "context"
|
||||
|
||||
func (g *pgRepository) MoveKnowledgeIndices(
|
||||
ctx context.Context,
|
||||
sourceKB, targetKB, knowledgeID string,
|
||||
_ []string,
|
||||
_ int,
|
||||
_ string,
|
||||
) error {
|
||||
return g.db.WithContext(ctx).Model(&pgVector{}).
|
||||
Where("knowledge_base_id = ? AND knowledge_id = ?", sourceKB, knowledgeID).
|
||||
Updates(map[string]any{"knowledge_base_id": targetKB, "tag_id": ""}).Error
|
||||
}
|
||||
@@ -0,0 +1,775 @@
|
||||
package postgres
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"strings"
|
||||
"sync"
|
||||
|
||||
"academy/internal/common"
|
||||
"academy/internal/logger"
|
||||
"academy/internal/types"
|
||||
"academy/internal/types/interfaces"
|
||||
"github.com/google/uuid"
|
||||
"github.com/pgvector/pgvector-go"
|
||||
"gorm.io/gorm"
|
||||
"gorm.io/gorm/clause"
|
||||
)
|
||||
|
||||
// pgRepository implements PostgreSQL-based retrieval operations
|
||||
type pgRepository struct {
|
||||
db *gorm.DB // Database connection
|
||||
keywordScoreMu sync.Mutex
|
||||
keywordScoreFunction string
|
||||
}
|
||||
|
||||
// NewPostgresRetrieveEngineRepository creates a new PostgreSQL retriever repository
|
||||
func NewPostgresRetrieveEngineRepository(db *gorm.DB) interfaces.RetrieveEngineRepository {
|
||||
logger.GetLogger(context.Background()).Info("[Postgres] Initializing PostgreSQL retriever engine repository")
|
||||
return &pgRepository{db: db}
|
||||
}
|
||||
|
||||
// keywordScoreFunctionSQL only selects fixed, schema-qualified function names.
|
||||
// Some PolarDB pg_search builds expose pgsearch.score instead of paradedb.score.
|
||||
const keywordScoreFunctionSQL = `SELECT n.nspname || '.score' AS score_function
|
||||
FROM pg_catalog.pg_proc p
|
||||
JOIN pg_catalog.pg_namespace n ON n.oid = p.pronamespace
|
||||
WHERE n.nspname IN ('paradedb', 'pgsearch')
|
||||
AND p.proname = 'score' AND p.prokind = 'f' AND p.pronargs = 1
|
||||
AND p.proargtypes[0] = 'pg_catalog.anyelement'::regtype
|
||||
AND p.prorettype = 'pg_catalog.float4'::regtype
|
||||
ORDER BY CASE n.nspname WHEN 'paradedb' THEN 0 ELSE 1 END
|
||||
LIMIT 1`
|
||||
|
||||
// resolveKeywordScoreFunction probes lazily so vector-only stores remain usable.
|
||||
// Cache successes per repository/database; transient failures can be retried.
|
||||
func (g *pgRepository) resolveKeywordScoreFunction(ctx context.Context) (string, error) {
|
||||
g.keywordScoreMu.Lock()
|
||||
defer g.keywordScoreMu.Unlock()
|
||||
if g.keywordScoreFunction != "" {
|
||||
return g.keywordScoreFunction, nil
|
||||
}
|
||||
var row struct{ ScoreFunction string }
|
||||
if err := g.db.WithContext(ctx).Raw(keywordScoreFunctionSQL).Scan(&row).Error; err != nil {
|
||||
return "", fmt.Errorf("detect PostgreSQL keyword score function: %w", err)
|
||||
}
|
||||
if row.ScoreFunction != "paradedb.score" && row.ScoreFunction != "pgsearch.score" {
|
||||
return "", errors.New("PostgreSQL keyword retrieval requires paradedb.score(anyelement) or " +
|
||||
"pgsearch.score(anyelement); check pg_search extension compatibility")
|
||||
}
|
||||
g.keywordScoreFunction = row.ScoreFunction
|
||||
return g.keywordScoreFunction, nil
|
||||
}
|
||||
|
||||
// EngineType returns the retriever engine type (PostgreSQL)
|
||||
func (r *pgRepository) EngineType() types.RetrieverEngineType {
|
||||
return types.PostgresRetrieverEngineType
|
||||
}
|
||||
|
||||
// Support returns supported retriever types (keywords and vector)
|
||||
func (r *pgRepository) Support() []types.RetrieverType {
|
||||
return []types.RetrieverType{types.KeywordsRetrieverType, types.VectorRetrieverType}
|
||||
}
|
||||
|
||||
// calculateIndexStorageSize calculates storage size for a single index entry
|
||||
func (g *pgRepository) calculateIndexStorageSize(embeddingDB *pgVector) int64 {
|
||||
// 1. Text content size
|
||||
contentSizeBytes := int64(len(embeddingDB.Content))
|
||||
|
||||
// 2. Vector storage size (2 bytes per dimension for half-precision float)
|
||||
var vectorSizeBytes int64 = 0
|
||||
if embeddingDB.Dimension > 0 {
|
||||
vectorSizeBytes = int64(embeddingDB.Dimension * 2)
|
||||
}
|
||||
|
||||
// 3. Metadata size (fixed overhead for IDs, timestamps etc.)
|
||||
metadataSizeBytes := int64(200)
|
||||
|
||||
// 4. Index overhead (HNSW index is ~2x vector size)
|
||||
indexOverheadBytes := vectorSizeBytes * 2
|
||||
|
||||
// Total size in bytes
|
||||
totalSizeBytes := contentSizeBytes + vectorSizeBytes + metadataSizeBytes + indexOverheadBytes
|
||||
|
||||
return totalSizeBytes
|
||||
}
|
||||
|
||||
// EstimateStorageSize estimates total storage size for multiple indices
|
||||
func (g *pgRepository) EstimateStorageSize(
|
||||
ctx context.Context, indexInfoList []*types.IndexInfo, additionalParams map[string]any,
|
||||
) int64 {
|
||||
var totalStorageSize int64 = 0
|
||||
for _, indexInfo := range indexInfoList {
|
||||
embeddingDB := toDBVectorEmbedding(indexInfo, additionalParams)
|
||||
totalStorageSize += g.calculateIndexStorageSize(embeddingDB)
|
||||
}
|
||||
logger.GetLogger(ctx).Infof(
|
||||
"[Postgres] Estimated storage size for %d indices: %d bytes",
|
||||
len(indexInfoList), totalStorageSize,
|
||||
)
|
||||
return totalStorageSize
|
||||
}
|
||||
|
||||
// upsertBySource makes a re-index of an existing (source_id, source_type)
|
||||
// replace the stored row. Callers such as FAQ edits re-index under the old
|
||||
// source ID and rely on the new content winning; DO NOTHING kept the stale
|
||||
// content and vector.
|
||||
var upsertBySource = clause.OnConflict{
|
||||
Columns: []clause.Column{{Name: "source_id"}, {Name: "source_type"}},
|
||||
DoUpdates: clause.AssignmentColumns([]string{
|
||||
"chunk_id", "knowledge_id", "knowledge_base_id", "tag_id",
|
||||
"content", "dimension", "embedding", "is_enabled", "updated_at",
|
||||
}),
|
||||
}
|
||||
|
||||
// Save stores a single index entry
|
||||
func (g *pgRepository) Save(ctx context.Context, indexInfo *types.IndexInfo, additionalParams map[string]any) error {
|
||||
logger.GetLogger(ctx).Debugf("[Postgres] Saving index for source ID: %s", indexInfo.SourceID)
|
||||
embeddingDB := toDBVectorEmbedding(indexInfo, additionalParams)
|
||||
err := g.db.WithContext(ctx).Clauses(upsertBySource).Create(embeddingDB).Error
|
||||
if err != nil {
|
||||
logger.GetLogger(ctx).Errorf("[Postgres] Failed to save index: %v", err)
|
||||
return err
|
||||
}
|
||||
logger.GetLogger(ctx).Infof("[Postgres] Successfully saved index for source ID: %s", indexInfo.SourceID)
|
||||
return nil
|
||||
}
|
||||
|
||||
// BatchSave stores multiple index entries in batch
|
||||
func (g *pgRepository) BatchSave(
|
||||
ctx context.Context, indexInfoList []*types.IndexInfo, additionalParams map[string]any,
|
||||
) error {
|
||||
logger.GetLogger(ctx).Infof("[Postgres] Batch saving %d indices", len(indexInfoList))
|
||||
// ON CONFLICT DO UPDATE rejects a statement that touches the same row
|
||||
// twice, so a source repeated in one batch keeps only its last entry.
|
||||
type sourceKey struct {
|
||||
id string
|
||||
typ int
|
||||
}
|
||||
positions := make(map[sourceKey]int, len(indexInfoList))
|
||||
indexInfoDBList := make([]*pgVector, 0, len(indexInfoList))
|
||||
for _, indexInfo := range indexInfoList {
|
||||
row := toDBVectorEmbedding(indexInfo, additionalParams)
|
||||
key := sourceKey{id: row.SourceID, typ: row.SourceType}
|
||||
if i, ok := positions[key]; ok {
|
||||
indexInfoDBList[i] = row
|
||||
continue
|
||||
}
|
||||
positions[key] = len(indexInfoDBList)
|
||||
indexInfoDBList = append(indexInfoDBList, row)
|
||||
}
|
||||
err := g.db.WithContext(ctx).Clauses(upsertBySource).Create(indexInfoDBList).Error
|
||||
if err != nil {
|
||||
logger.GetLogger(ctx).Errorf("[Postgres] Batch save failed: %v", err)
|
||||
return err
|
||||
}
|
||||
logger.GetLogger(ctx).Infof("[Postgres] Successfully batch saved %d indices", len(indexInfoList))
|
||||
return nil
|
||||
}
|
||||
|
||||
// DeleteByChunkIDList deletes indices by chunk IDs
|
||||
func (g *pgRepository) DeleteByChunkIDList(ctx context.Context, chunkIDList []string, dimension int, knowledgeType string) error {
|
||||
logger.GetLogger(ctx).Infof("[Postgres] Deleting indices by chunk IDs, count: %d", len(chunkIDList))
|
||||
result := g.db.WithContext(ctx).Where("chunk_id IN ?", chunkIDList).Delete(&pgVector{})
|
||||
if result.Error != nil {
|
||||
logger.GetLogger(ctx).Errorf("[Postgres] Failed to delete indices by chunk IDs: %v", result.Error)
|
||||
return result.Error
|
||||
}
|
||||
logger.GetLogger(ctx).Infof("[Postgres] Successfully deleted %d indices by chunk IDs", result.RowsAffected)
|
||||
return nil
|
||||
}
|
||||
|
||||
// DeleteBySourceIDList deletes indices by source IDs
|
||||
func (g *pgRepository) DeleteBySourceIDList(ctx context.Context, sourceIDList []string, dimension int, knowledgeType string) error {
|
||||
if len(sourceIDList) == 0 {
|
||||
return nil
|
||||
}
|
||||
logger.GetLogger(ctx).Infof("[Postgres] Deleting indices by source IDs, count: %d", len(sourceIDList))
|
||||
result := g.db.WithContext(ctx).Where("source_id IN ?", sourceIDList).Delete(&pgVector{})
|
||||
if result.Error != nil {
|
||||
logger.GetLogger(ctx).Errorf("[Postgres] Failed to delete indices by source IDs: %v", result.Error)
|
||||
return result.Error
|
||||
}
|
||||
logger.GetLogger(ctx).Infof("[Postgres] Successfully deleted %d indices by source IDs", result.RowsAffected)
|
||||
return nil
|
||||
}
|
||||
|
||||
// DeleteByKnowledgeIDList deletes indices by knowledge IDs
|
||||
func (g *pgRepository) DeleteByKnowledgeIDList(ctx context.Context, knowledgeIDList []string, dimension int, knowledgeType string) error {
|
||||
logger.GetLogger(ctx).Infof("[Postgres] Deleting indices by knowledge IDs, count: %d", len(knowledgeIDList))
|
||||
result := g.db.WithContext(ctx).Where("knowledge_id IN ?", knowledgeIDList).Delete(&pgVector{})
|
||||
if result.Error != nil {
|
||||
logger.GetLogger(ctx).Errorf("[Postgres] Failed to delete indices by knowledge IDs: %v", result.Error)
|
||||
return result.Error
|
||||
}
|
||||
logger.GetLogger(ctx).Infof("[Postgres] Successfully deleted %d indices by knowledge IDs", result.RowsAffected)
|
||||
return nil
|
||||
}
|
||||
|
||||
// Retrieve handles retrieval requests and routes to appropriate method
|
||||
func (g *pgRepository) Retrieve(ctx context.Context, params types.RetrieveParams) ([]*types.RetrieveResult, error) {
|
||||
logger.GetLogger(ctx).Debugf("[Postgres] Processing retrieval request of type: %s", params.RetrieverType)
|
||||
switch params.RetrieverType {
|
||||
case types.KeywordsRetrieverType:
|
||||
return g.KeywordsRetrieve(ctx, params)
|
||||
case types.VectorRetrieverType:
|
||||
return g.VectorRetrieve(ctx, params)
|
||||
}
|
||||
err := errors.New("invalid retriever type")
|
||||
logger.GetLogger(ctx).Errorf("[Postgres] %v: %s", err, params.RetrieverType)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// KeywordsRetrieve performs keyword-based search using PostgreSQL full-text search
|
||||
func (g *pgRepository) KeywordsRetrieve(ctx context.Context,
|
||||
params types.RetrieveParams,
|
||||
) ([]*types.RetrieveResult, error) {
|
||||
logger.GetLogger(ctx).Infof("[Postgres] Keywords retrieval: query=%s, topK=%d", params.Query, params.TopK)
|
||||
scoreFunction, err := g.resolveKeywordScoreFunction(ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
conds := make([]clause.Expression, 0)
|
||||
|
||||
// KnowledgeBaseIDs and KnowledgeIDs use AND logic
|
||||
// - If only KnowledgeBaseIDs: search entire knowledge bases
|
||||
// - If only KnowledgeIDs: search specific documents
|
||||
// - If both: search specific documents within the knowledge bases (AND)
|
||||
if len(params.KnowledgeBaseIDs) > 0 {
|
||||
logger.GetLogger(ctx).Debugf("[Postgres] Filtering by knowledge base IDs: %v", params.KnowledgeBaseIDs)
|
||||
conds = append(conds, clause.IN{
|
||||
Column: "knowledge_base_id",
|
||||
Values: common.ToInterfaceSlice(params.KnowledgeBaseIDs),
|
||||
})
|
||||
}
|
||||
if len(params.KnowledgeIDs) > 0 {
|
||||
logger.GetLogger(ctx).Debugf("[Postgres] Filtering by knowledge IDs: %v", params.KnowledgeIDs)
|
||||
conds = append(conds, clause.IN{
|
||||
Column: "knowledge_id",
|
||||
Values: common.ToInterfaceSlice(params.KnowledgeIDs),
|
||||
})
|
||||
}
|
||||
// Filter by tag IDs if specified
|
||||
if len(params.TagIDs) > 0 {
|
||||
logger.GetLogger(ctx).Debugf("[Postgres] Filtering by tag IDs: %v", params.TagIDs)
|
||||
conds = append(conds, clause.IN{
|
||||
Column: "tag_id",
|
||||
Values: common.ToInterfaceSlice(params.TagIDs),
|
||||
})
|
||||
}
|
||||
|
||||
// Use ParadeDB's ||| operator for matching any token
|
||||
conds = append(conds, clause.Expr{
|
||||
SQL: "content ||| ?",
|
||||
Vars: []interface{}{params.Query},
|
||||
})
|
||||
|
||||
// Filter by is_enabled = true or NULL (NULL means enabled for historical data)
|
||||
conds = append(conds, clause.Expr{
|
||||
SQL: "(is_enabled IS NULL OR is_enabled = ?)",
|
||||
Vars: []interface{}{true},
|
||||
})
|
||||
conds = append(conds, clause.OrderBy{Columns: []clause.OrderByColumn{
|
||||
{Column: clause.Column{Name: "score"}, Desc: true},
|
||||
}})
|
||||
|
||||
var embeddingDBList []pgVectorWithScore
|
||||
err = g.db.WithContext(ctx).Clauses(conds...).
|
||||
Select([]string{
|
||||
scoreFunction + "(id) as score",
|
||||
"id",
|
||||
"content",
|
||||
"source_id",
|
||||
"source_type",
|
||||
"chunk_id",
|
||||
"knowledge_id",
|
||||
"knowledge_base_id",
|
||||
"tag_id",
|
||||
}).
|
||||
Limit(int(params.TopK)).
|
||||
Find(&embeddingDBList).Error
|
||||
|
||||
if err == gorm.ErrRecordNotFound {
|
||||
logger.GetLogger(ctx).Warnf("[Postgres] No records found for keywords query: %s", params.Query)
|
||||
return nil, nil
|
||||
}
|
||||
if err != nil {
|
||||
logger.GetLogger(ctx).Errorf("[Postgres] Keywords retrieval failed: %v", err)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
logger.GetLogger(ctx).Infof("[Postgres] Keywords retrieval found %d results", len(embeddingDBList))
|
||||
results := make([]*types.IndexWithScore, len(embeddingDBList))
|
||||
const maxKeywordResultLog = 8
|
||||
for i := range embeddingDBList {
|
||||
results[i] = fromDBVectorEmbeddingWithScore(&embeddingDBList[i], types.MatchTypeKeywords)
|
||||
if i < maxKeywordResultLog {
|
||||
logger.GetLogger(ctx).Debugf("[Postgres] Keywords result %d: chunk=%s, score=%f",
|
||||
i, results[i].ChunkID, results[i].Score)
|
||||
}
|
||||
}
|
||||
if len(results) > maxKeywordResultLog {
|
||||
logger.GetLogger(ctx).Debugf(
|
||||
"[Postgres] Keywords result summary: total=%d logged=%d truncated=%d",
|
||||
len(results), maxKeywordResultLog, len(results)-maxKeywordResultLog,
|
||||
)
|
||||
}
|
||||
return []*types.RetrieveResult{
|
||||
{
|
||||
Results: results,
|
||||
RetrieverEngineType: types.PostgresRetrieverEngineType,
|
||||
RetrieverType: types.KeywordsRetrieverType,
|
||||
Error: nil,
|
||||
},
|
||||
}, nil
|
||||
}
|
||||
|
||||
// VectorRetrieve performs vector similarity search using pgvector
|
||||
// Optimized to use HNSW index efficiently and avoid recalculating vector distance
|
||||
func (g *pgRepository) VectorRetrieve(ctx context.Context,
|
||||
params types.RetrieveParams,
|
||||
) ([]*types.RetrieveResult, error) {
|
||||
logger.GetLogger(ctx).Infof("[Postgres] Vector retrieval: dim=%d, topK=%d, threshold=%.4f",
|
||||
len(params.Embedding), params.TopK, params.Threshold)
|
||||
|
||||
dimension := len(params.Embedding)
|
||||
queryVector := pgvector.NewHalfVector(params.Embedding)
|
||||
|
||||
// Build WHERE conditions for filtering
|
||||
whereParts := make([]string, 0)
|
||||
allVars := make([]interface{}, 0)
|
||||
|
||||
// Add query vector first (used in ORDER BY for HNSW index)
|
||||
allVars = append(allVars, queryVector)
|
||||
|
||||
// Dimension filter (required for HNSW index WHERE clause)
|
||||
whereParts = append(whereParts, fmt.Sprintf("dimension = $%d", len(allVars)+1))
|
||||
allVars = append(allVars, dimension)
|
||||
|
||||
// KnowledgeBaseIDs and KnowledgeIDs use AND logic
|
||||
// - If only KnowledgeBaseIDs: search entire knowledge bases
|
||||
// - If only KnowledgeIDs: search specific documents
|
||||
// - If both: search specific documents within the knowledge bases (AND)
|
||||
if len(params.KnowledgeBaseIDs) > 0 {
|
||||
logger.GetLogger(ctx).Debugf(
|
||||
"[Postgres] Filtering vector search by knowledge base IDs: %v",
|
||||
params.KnowledgeBaseIDs,
|
||||
)
|
||||
placeholders := make([]string, len(params.KnowledgeBaseIDs))
|
||||
paramStart := len(allVars) + 1
|
||||
for i := range params.KnowledgeBaseIDs {
|
||||
placeholders[i] = fmt.Sprintf("$%d", paramStart+i)
|
||||
allVars = append(allVars, params.KnowledgeBaseIDs[i])
|
||||
}
|
||||
whereParts = append(whereParts, fmt.Sprintf("knowledge_base_id IN (%s)",
|
||||
strings.Join(placeholders, ", ")))
|
||||
}
|
||||
if len(params.KnowledgeIDs) > 0 {
|
||||
logger.GetLogger(ctx).Debugf(
|
||||
"[Postgres] Filtering vector search by knowledge IDs: %v",
|
||||
params.KnowledgeIDs,
|
||||
)
|
||||
placeholders := make([]string, len(params.KnowledgeIDs))
|
||||
paramStart := len(allVars) + 1
|
||||
for i := range params.KnowledgeIDs {
|
||||
placeholders[i] = fmt.Sprintf("$%d", paramStart+i)
|
||||
allVars = append(allVars, params.KnowledgeIDs[i])
|
||||
}
|
||||
whereParts = append(whereParts, fmt.Sprintf("knowledge_id IN (%s)",
|
||||
strings.Join(placeholders, ", ")))
|
||||
}
|
||||
// Filter by tag IDs if specified
|
||||
if len(params.TagIDs) > 0 {
|
||||
logger.GetLogger(ctx).Debugf(
|
||||
"[Postgres] Filtering vector search by tag IDs: %v",
|
||||
params.TagIDs,
|
||||
)
|
||||
placeholders := make([]string, len(params.TagIDs))
|
||||
paramStart := len(allVars) + 1
|
||||
for i := range params.TagIDs {
|
||||
placeholders[i] = fmt.Sprintf("$%d", paramStart+i)
|
||||
allVars = append(allVars, params.TagIDs[i])
|
||||
}
|
||||
whereParts = append(whereParts, fmt.Sprintf("tag_id IN (%s)",
|
||||
strings.Join(placeholders, ", ")))
|
||||
}
|
||||
|
||||
// is_enabled filter
|
||||
whereParts = append(whereParts, fmt.Sprintf("(is_enabled IS NULL OR is_enabled = $%d)", len(allVars)+1))
|
||||
allVars = append(allVars, true)
|
||||
|
||||
// Build WHERE clause string
|
||||
whereClause := ""
|
||||
if len(whereParts) > 0 {
|
||||
whereClause = "WHERE " + strings.Join(whereParts, " AND ")
|
||||
}
|
||||
|
||||
// Expand TopK to get more candidates before threshold filtering.
|
||||
//
|
||||
// HNSW requires `ef_search >= LIMIT`, and a very large LIMIT (e.g. 1000)
|
||||
// forces HNSW to walk a near-exhaustive portion of the graph, often making
|
||||
// it slower than a sequential scan and pushing the planner to pick Seq Scan
|
||||
// even when an index exists. 200 is a good sweet spot: it gives enough
|
||||
// headroom for threshold/filter post-processing without ballooning ef_search.
|
||||
expandedTopK := params.TopK * 2
|
||||
if expandedTopK < 100 {
|
||||
expandedTopK = 100 // Minimum 100 candidates
|
||||
}
|
||||
if expandedTopK > 200 {
|
||||
expandedTopK = 200 // Maximum 200 candidates (keeps HNSW efficient)
|
||||
}
|
||||
if expandedTopK < params.TopK {
|
||||
expandedTopK = params.TopK // Ensure subquery limit is at least final limit
|
||||
}
|
||||
|
||||
// Optimized query: Use subquery to calculate distance once.
|
||||
//
|
||||
// IMPORTANT: The HNSW index in this project is built on the EXPRESSION
|
||||
// (embedding::halfvec(<dim>)) halfvec_cosine_ops
|
||||
// because the `embedding` column itself is `halfvec` without a fixed dimension
|
||||
// (so the table can store multiple embedding sizes such as 798 / 3584 / ...).
|
||||
//
|
||||
// pgvector requires the ORDER BY expression to match the indexed expression
|
||||
// EXACTLY, otherwise the planner falls back to a sequential scan. The
|
||||
// `embedding::halfvec(%d)` cast on both sides of `<=>` is therefore NOT
|
||||
// redundant — it is the only way to make the HNSW index get used at all.
|
||||
// See: pgvector issues #702, #835 and ParadeDB "indexing-expressions" docs.
|
||||
subqueryLimitParam := len(allVars) + 1
|
||||
thresholdParam := len(allVars) + 2
|
||||
finalLimitParam := len(allVars) + 3
|
||||
|
||||
querySQL := fmt.Sprintf(`
|
||||
SELECT
|
||||
id, content, source_id, source_type, chunk_id, knowledge_id, knowledge_base_id, tag_id,
|
||||
(1 - distance) as score
|
||||
FROM (
|
||||
SELECT
|
||||
id, content, source_id, source_type, chunk_id, knowledge_id, knowledge_base_id, tag_id,
|
||||
embedding::halfvec(%[1]d) <=> $1::halfvec(%[1]d) as distance
|
||||
FROM embeddings
|
||||
%[2]s
|
||||
ORDER BY embedding::halfvec(%[1]d) <=> $1::halfvec(%[1]d)
|
||||
LIMIT $%[3]d
|
||||
) AS candidates
|
||||
WHERE distance <= $%[4]d
|
||||
ORDER BY distance ASC
|
||||
LIMIT $%[5]d
|
||||
`, dimension, whereClause, subqueryLimitParam, thresholdParam, finalLimitParam)
|
||||
|
||||
allVars = append(allVars, expandedTopK) // LIMIT in subquery
|
||||
allVars = append(allVars, 1-params.Threshold) // Distance threshold
|
||||
allVars = append(allVars, params.TopK) // Final LIMIT
|
||||
|
||||
// HNSW's `ef_search` defaults to 40, which is much smaller than our
|
||||
// `expandedTopK` budget (up to 1000). Without raising it, HNSW would only
|
||||
// return ~40 candidates per scan and our outer threshold/filter step would
|
||||
// silently lose recall. `SET LOCAL` requires a transaction so we wrap the
|
||||
// query in one. We never write inside this transaction, so the cost is
|
||||
// negligible.
|
||||
efSearch := expandedTopK
|
||||
if efSearch < 40 {
|
||||
efSearch = 40
|
||||
}
|
||||
|
||||
var embeddingDBList []pgVectorWithScore
|
||||
|
||||
err := g.db.WithContext(ctx).Transaction(func(tx *gorm.DB) error {
|
||||
if err := tx.Exec(fmt.Sprintf("SET LOCAL hnsw.ef_search = %d", efSearch)).Error; err != nil {
|
||||
// Treat as non-fatal: pgvector should always expose this GUC, but if
|
||||
// for any reason it does not we still want the query to run (just
|
||||
// with default recall). We must rollback first because a failed
|
||||
// statement aborts the transaction in PostgreSQL.
|
||||
logger.GetLogger(ctx).Warnf("[Postgres] Failed to set hnsw.ef_search=%d: %v", efSearch, err)
|
||||
return err
|
||||
}
|
||||
// pgvector >= 0.8 supports iterative scan, which keeps pulling more
|
||||
// candidates from HNSW until the post-filter (knowledge_base_id /
|
||||
// knowledge_id / tag_id / is_enabled) yields enough rows. Without it,
|
||||
// HNSW returns at most ef_search candidates and the outer filter may
|
||||
// silently lose recall when the filter is selective.
|
||||
// Best-effort: ignore failure on older pgvector versions.
|
||||
if err := tx.Exec("SET LOCAL hnsw.iterative_scan = strict_order").Error; err != nil {
|
||||
logger.GetLogger(ctx).Debugf("[Postgres] hnsw.iterative_scan not available: %v", err)
|
||||
// abort transaction and let the fallback path below handle it.
|
||||
return err
|
||||
}
|
||||
return tx.Raw(querySQL, allVars...).Scan(&embeddingDBList).Error
|
||||
})
|
||||
|
||||
// Fallback: if the transaction failed because of an unsupported GUC (e.g.
|
||||
// older pgvector that doesn't have hnsw.ef_search or hnsw.iterative_scan),
|
||||
// retry the query without the SETs so we still return results.
|
||||
if err != nil && len(embeddingDBList) == 0 &&
|
||||
(strings.Contains(err.Error(), "hnsw.ef_search") ||
|
||||
strings.Contains(err.Error(), "hnsw.iterative_scan")) {
|
||||
logger.GetLogger(ctx).Warnf("[Postgres] Retrying vector query without HNSW GUC overrides: %v", err)
|
||||
err = g.db.WithContext(ctx).Raw(querySQL, allVars...).Scan(&embeddingDBList).Error
|
||||
}
|
||||
|
||||
if err == gorm.ErrRecordNotFound {
|
||||
logger.GetLogger(ctx).Warnf("[Postgres] No vector matches found that meet threshold %.4f", params.Threshold)
|
||||
return nil, nil
|
||||
}
|
||||
if err != nil {
|
||||
logger.GetLogger(ctx).Errorf("[Postgres] Vector retrieval failed: %v", err)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Apply final TopK limit (in case we got more results than needed)
|
||||
if len(embeddingDBList) > int(params.TopK) {
|
||||
embeddingDBList = embeddingDBList[:params.TopK]
|
||||
}
|
||||
|
||||
logger.GetLogger(ctx).Infof("[Postgres] Vector retrieval found %d results", len(embeddingDBList))
|
||||
results := make([]*types.IndexWithScore, len(embeddingDBList))
|
||||
const maxVectorResultLog = 8
|
||||
for i := range embeddingDBList {
|
||||
results[i] = fromDBVectorEmbeddingWithScore(&embeddingDBList[i], types.MatchTypeEmbedding)
|
||||
if i < maxVectorResultLog {
|
||||
logger.GetLogger(ctx).Debugf("[Postgres] Vector search result %d: chunk_id %s, score %.4f",
|
||||
i, results[i].ChunkID, results[i].Score)
|
||||
}
|
||||
}
|
||||
if len(results) > maxVectorResultLog {
|
||||
logger.GetLogger(ctx).Debugf(
|
||||
"[Postgres] Vector search result summary: total=%d logged=%d truncated=%d",
|
||||
len(results), maxVectorResultLog, len(results)-maxVectorResultLog,
|
||||
)
|
||||
}
|
||||
return []*types.RetrieveResult{
|
||||
{
|
||||
Results: results,
|
||||
RetrieverEngineType: types.PostgresRetrieverEngineType,
|
||||
RetrieverType: types.VectorRetrieverType,
|
||||
Error: nil,
|
||||
},
|
||||
}, nil
|
||||
}
|
||||
|
||||
// CopyIndices copies index data
|
||||
func (g *pgRepository) CopyIndices(ctx context.Context,
|
||||
sourceKnowledgeBaseID string,
|
||||
sourceToTargetKBIDMap map[string]string,
|
||||
sourceToTargetChunkIDMap map[string]string,
|
||||
targetKnowledgeBaseID string,
|
||||
dimension int,
|
||||
knowledgeType string,
|
||||
) error {
|
||||
logger.GetLogger(ctx).Infof(
|
||||
"[Postgres] Copying indices, source knowledge base: %s, target knowledge base: %s, mapping count: %d",
|
||||
sourceKnowledgeBaseID, targetKnowledgeBaseID, len(sourceToTargetChunkIDMap),
|
||||
)
|
||||
|
||||
if len(sourceToTargetChunkIDMap) == 0 {
|
||||
logger.GetLogger(ctx).Warnf("[Postgres] Mapping is empty, no need to copy")
|
||||
return nil
|
||||
}
|
||||
|
||||
// Only the mapped knowledge is copied (callers clone one document at a
|
||||
// time), so filter on it instead of paging the whole source knowledge
|
||||
// base. Keyset pagination on id keeps pages stable; OFFSET without an
|
||||
// ORDER BY could skip or repeat rows.
|
||||
sourceKnowledgeIDs := make([]string, 0, len(sourceToTargetKBIDMap))
|
||||
for sourceKnowledgeID := range sourceToTargetKBIDMap {
|
||||
sourceKnowledgeIDs = append(sourceKnowledgeIDs, sourceKnowledgeID)
|
||||
}
|
||||
|
||||
// Batch processing parameters
|
||||
batchSize := 500 // Number of records to process per batch
|
||||
var lastID uint // Keyset cursor
|
||||
totalCopied := 0 // Total number of copied records
|
||||
|
||||
for {
|
||||
// Paginated query for source data
|
||||
var sourceVectors []*pgVector
|
||||
if err := g.db.WithContext(ctx).
|
||||
Where("knowledge_base_id = ? AND knowledge_id IN ? AND id > ?",
|
||||
sourceKnowledgeBaseID, sourceKnowledgeIDs, lastID).
|
||||
Order("id").
|
||||
Limit(batchSize).
|
||||
Find(&sourceVectors).Error; err != nil {
|
||||
logger.GetLogger(ctx).Errorf("[Postgres] Failed to query source index data: %v", err)
|
||||
return err
|
||||
}
|
||||
|
||||
// If no more data, exit the loop
|
||||
if len(sourceVectors) == 0 {
|
||||
if lastID == 0 {
|
||||
logger.GetLogger(ctx).Warnf("[Postgres] No source index data found")
|
||||
}
|
||||
break
|
||||
}
|
||||
|
||||
batchCount := len(sourceVectors)
|
||||
logger.GetLogger(ctx).Infof(
|
||||
"[Postgres] Found %d source index data, batch start after id: %d",
|
||||
batchCount, lastID,
|
||||
)
|
||||
|
||||
// Create target vector index
|
||||
targetVectors := make([]*pgVector, 0, batchCount)
|
||||
for _, sourceVector := range sourceVectors {
|
||||
// Get the mapped target chunk ID
|
||||
targetChunkID, ok := sourceToTargetChunkIDMap[sourceVector.ChunkID]
|
||||
if !ok {
|
||||
logger.GetLogger(ctx).Warnf(
|
||||
"[Postgres] Source chunk %s not found in target chunk mapping, skipping",
|
||||
sourceVector.ChunkID,
|
||||
)
|
||||
continue
|
||||
}
|
||||
|
||||
// Get the mapped target knowledge ID
|
||||
targetKnowledgeID, ok := sourceToTargetKBIDMap[sourceVector.KnowledgeID]
|
||||
if !ok {
|
||||
logger.GetLogger(ctx).Warnf(
|
||||
"[Postgres] Source knowledge %s not found in target knowledge mapping, skipping",
|
||||
sourceVector.KnowledgeID,
|
||||
)
|
||||
continue
|
||||
}
|
||||
|
||||
// Handle SourceID transformation for generated questions
|
||||
// Generated questions have SourceID format: {chunkID}-{questionID}
|
||||
// Regular chunks have SourceID == ChunkID
|
||||
var targetSourceID string
|
||||
if sourceVector.SourceID == sourceVector.ChunkID {
|
||||
// Regular chunk, use targetChunkID as SourceID
|
||||
targetSourceID = targetChunkID
|
||||
} else if strings.HasPrefix(sourceVector.SourceID, sourceVector.ChunkID+"-") {
|
||||
// This is a generated question, preserve the questionID part
|
||||
questionID := strings.TrimPrefix(sourceVector.SourceID, sourceVector.ChunkID+"-")
|
||||
targetSourceID = fmt.Sprintf("%s-%s", targetChunkID, questionID)
|
||||
} else {
|
||||
// For other complex scenarios, generate new unique SourceID
|
||||
targetSourceID = uuid.New().String()
|
||||
}
|
||||
|
||||
// Create new vector index, copy the content and vector of the source index
|
||||
targetVector := &pgVector{
|
||||
Content: sourceVector.Content,
|
||||
SourceID: targetSourceID, // Handle SourceID transformation properly
|
||||
SourceType: sourceVector.SourceType,
|
||||
ChunkID: targetChunkID, // Update to target chunk ID
|
||||
KnowledgeID: targetKnowledgeID, // Update to target knowledge ID
|
||||
KnowledgeBaseID: targetKnowledgeBaseID, // Update to target knowledge base ID
|
||||
Dimension: sourceVector.Dimension,
|
||||
Embedding: sourceVector.Embedding, // Copy the vector embedding directly, avoid recalculation
|
||||
IsEnabled: sourceVector.IsEnabled,
|
||||
}
|
||||
|
||||
targetVectors = append(targetVectors, targetVector)
|
||||
}
|
||||
|
||||
// Batch insert target vector index
|
||||
if len(targetVectors) > 0 {
|
||||
if err := g.db.WithContext(ctx).
|
||||
Clauses(clause.OnConflict{DoNothing: true}).Create(targetVectors).Error; err != nil {
|
||||
logger.GetLogger(ctx).Errorf("[Postgres] Failed to batch create target index: %v", err)
|
||||
return err
|
||||
}
|
||||
|
||||
totalCopied += len(targetVectors)
|
||||
logger.GetLogger(ctx).Infof(
|
||||
"[Postgres] Successfully copied batch data, batch size: %d, total copied: %d",
|
||||
len(targetVectors),
|
||||
totalCopied,
|
||||
)
|
||||
}
|
||||
|
||||
// Move to the next batch
|
||||
lastID = sourceVectors[batchCount-1].ID
|
||||
|
||||
// If the number of returned records is less than the requested size, it means the last page has been reached
|
||||
if batchCount < batchSize {
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
logger.GetLogger(ctx).Infof("[Postgres] Index copying completed, total copied: %d", totalCopied)
|
||||
return nil
|
||||
}
|
||||
|
||||
// BatchUpdateChunkEnabledStatus updates the enabled status of chunks in batch
|
||||
func (g *pgRepository) BatchUpdateChunkEnabledStatus(ctx context.Context, chunkStatusMap map[string]bool) error {
|
||||
if len(chunkStatusMap) == 0 {
|
||||
logger.GetLogger(ctx).Warnf("[Postgres] Chunk status map is empty, skipping update")
|
||||
return nil
|
||||
}
|
||||
|
||||
logger.GetLogger(ctx).Infof("[Postgres] Batch updating chunk enabled status, count: %d", len(chunkStatusMap))
|
||||
|
||||
// Group chunks by enabled status for batch updates
|
||||
enabledChunkIDs := make([]string, 0)
|
||||
disabledChunkIDs := make([]string, 0)
|
||||
|
||||
for chunkID, enabled := range chunkStatusMap {
|
||||
if enabled {
|
||||
enabledChunkIDs = append(enabledChunkIDs, chunkID)
|
||||
} else {
|
||||
disabledChunkIDs = append(disabledChunkIDs, chunkID)
|
||||
}
|
||||
}
|
||||
|
||||
// Batch update enabled chunks
|
||||
if len(enabledChunkIDs) > 0 {
|
||||
result := g.db.WithContext(ctx).Model(&pgVector{}).
|
||||
Where("chunk_id IN ?", enabledChunkIDs).
|
||||
Update("is_enabled", true)
|
||||
if result.Error != nil {
|
||||
logger.GetLogger(ctx).Errorf("[Postgres] Failed to update enabled chunks: %v", result.Error)
|
||||
return result.Error
|
||||
}
|
||||
logger.GetLogger(ctx).
|
||||
Infof("[Postgres] Updated %d chunks to enabled, rows affected: %d", len(enabledChunkIDs), result.RowsAffected)
|
||||
}
|
||||
|
||||
// Batch update disabled chunks
|
||||
if len(disabledChunkIDs) > 0 {
|
||||
result := g.db.WithContext(ctx).Model(&pgVector{}).
|
||||
Where("chunk_id IN ?", disabledChunkIDs).
|
||||
Update("is_enabled", false)
|
||||
if result.Error != nil {
|
||||
logger.GetLogger(ctx).Errorf("[Postgres] Failed to update disabled chunks: %v", result.Error)
|
||||
return result.Error
|
||||
}
|
||||
logger.GetLogger(ctx).
|
||||
Infof("[Postgres] Updated %d chunks to disabled, rows affected: %d", len(disabledChunkIDs), result.RowsAffected)
|
||||
}
|
||||
|
||||
logger.GetLogger(ctx).Infof("[Postgres] Successfully batch updated chunk enabled status")
|
||||
return nil
|
||||
}
|
||||
|
||||
// BatchUpdateChunkTagID updates the tag ID of chunks in batch
|
||||
func (g *pgRepository) BatchUpdateChunkTagID(ctx context.Context, chunkTagMap map[string]string) error {
|
||||
if len(chunkTagMap) == 0 {
|
||||
logger.GetLogger(ctx).Warnf("[Postgres] Chunk tag map is empty, skipping update")
|
||||
return nil
|
||||
}
|
||||
|
||||
logger.GetLogger(ctx).Infof("[Postgres] Batch updating chunk tag ID, count: %d", len(chunkTagMap))
|
||||
|
||||
// Group chunks by tag ID for batch updates
|
||||
tagGroups := make(map[string][]string)
|
||||
for chunkID, tagID := range chunkTagMap {
|
||||
tagGroups[tagID] = append(tagGroups[tagID], chunkID)
|
||||
}
|
||||
|
||||
// Batch update chunks for each tag ID
|
||||
for tagID, chunkIDs := range tagGroups {
|
||||
result := g.db.WithContext(ctx).Model(&pgVector{}).
|
||||
Where("chunk_id IN ?", chunkIDs).
|
||||
Update("tag_id", tagID)
|
||||
if result.Error != nil {
|
||||
logger.GetLogger(ctx).Errorf("[Postgres] Failed to update chunks with tag_id %s: %v", tagID, result.Error)
|
||||
return result.Error
|
||||
}
|
||||
logger.GetLogger(ctx).
|
||||
Infof("[Postgres] Updated %d chunks to tag_id=%s, rows affected: %d", len(chunkIDs), tagID, result.RowsAffected)
|
||||
}
|
||||
|
||||
logger.GetLogger(ctx).Infof("[Postgres] Successfully batch updated chunk tag ID")
|
||||
return nil
|
||||
}
|
||||
@@ -0,0 +1,105 @@
|
||||
package postgres
|
||||
|
||||
import (
|
||||
"maps"
|
||||
"slices"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
"academy/internal/common"
|
||||
"academy/internal/types"
|
||||
"github.com/pgvector/pgvector-go"
|
||||
)
|
||||
|
||||
// pgVector defines the database model for vector embeddings storage
|
||||
type pgVector struct {
|
||||
ID uint `json:"id" gorm:"primarykey"`
|
||||
CreatedAt time.Time `json:"created_at" gorm:"column:created_at"`
|
||||
UpdatedAt time.Time `json:"updated_at" gorm:"column:updated_at"`
|
||||
SourceID string `json:"source_id" gorm:"column:source_id;not null"`
|
||||
SourceType int `json:"source_type" gorm:"column:source_type;not null"`
|
||||
ChunkID string `json:"chunk_id" gorm:"column:chunk_id"`
|
||||
KnowledgeID string `json:"knowledge_id" gorm:"column:knowledge_id"`
|
||||
KnowledgeBaseID string `json:"knowledge_base_id" gorm:"column:knowledge_base_id"`
|
||||
TagID string `json:"tag_id" gorm:"column:tag_id;index"`
|
||||
Content string `json:"content" gorm:"column:content;not null"`
|
||||
Dimension int `json:"dimension" gorm:"column:dimension;not null"`
|
||||
Embedding pgvector.HalfVector `json:"embedding" gorm:"column:embedding;not null"`
|
||||
// No GORM default tag: GORM swaps a zero bool for the tag's default on
|
||||
// insert, which indexed disabled chunks as enabled. Writers always set it.
|
||||
IsEnabled bool `json:"is_enabled" gorm:"column:is_enabled;index"`
|
||||
}
|
||||
|
||||
// pgVectorWithScore extends pgVector with similarity score field
|
||||
type pgVectorWithScore struct {
|
||||
ID uint `json:"id" gorm:"primarykey"`
|
||||
CreatedAt time.Time `json:"created_at" gorm:"column:created_at"`
|
||||
UpdatedAt time.Time `json:"updated_at" gorm:"column:updated_at"`
|
||||
SourceID string `json:"source_id" gorm:"column:source_id;not null"`
|
||||
SourceType int `json:"source_type" gorm:"column:source_type;not null"`
|
||||
ChunkID string `json:"chunk_id" gorm:"column:chunk_id"`
|
||||
KnowledgeID string `json:"knowledge_id" gorm:"column:knowledge_id"`
|
||||
KnowledgeBaseID string `json:"knowledge_base_id" gorm:"column:knowledge_base_id"`
|
||||
TagID string `json:"tag_id" gorm:"column:tag_id;index"`
|
||||
Content string `json:"content" gorm:"column:content;not null"`
|
||||
Dimension int `json:"dimension" gorm:"column:dimension;not null"`
|
||||
Embedding pgvector.HalfVector `json:"embedding" gorm:"column:embedding;not null"`
|
||||
IsEnabled bool `json:"is_enabled" gorm:"column:is_enabled;default:true;index"`
|
||||
Score float64 `json:"score" gorm:"column:score"`
|
||||
}
|
||||
|
||||
// TableName specifies the database table name for pgVectorWithScore
|
||||
func (pgVectorWithScore) TableName() string {
|
||||
return "embeddings"
|
||||
}
|
||||
|
||||
// TableName specifies the database table name for pgVector
|
||||
func (pgVector) TableName() string {
|
||||
return "embeddings"
|
||||
}
|
||||
|
||||
// toDBVectorEmbedding converts IndexInfo to pgVector database model
|
||||
func toDBVectorEmbedding(indexInfo *types.IndexInfo, additionalParams map[string]any) *pgVector {
|
||||
pgVector := &pgVector{
|
||||
SourceID: indexInfo.SourceID,
|
||||
SourceType: int(indexInfo.SourceType),
|
||||
ChunkID: indexInfo.ChunkID,
|
||||
KnowledgeID: indexInfo.KnowledgeID,
|
||||
KnowledgeBaseID: indexInfo.KnowledgeBaseID,
|
||||
TagID: indexInfo.TagID,
|
||||
Content: common.CleanInvalidUTF8(indexInfo.Content),
|
||||
IsEnabled: indexInfo.IsEnabled,
|
||||
}
|
||||
// Add embedding data if available in additionalParams
|
||||
if additionalParams != nil && slices.Contains(slices.Collect(maps.Keys(additionalParams)), "embedding") {
|
||||
if embeddingMap, ok := additionalParams["embedding"].(map[string][]float32); ok {
|
||||
pgVector.Embedding = pgvector.NewHalfVector(embeddingMap[indexInfo.SourceID])
|
||||
pgVector.Dimension = len(pgVector.Embedding.Slice())
|
||||
}
|
||||
}
|
||||
// Get is_enabled from additionalParams if available
|
||||
if additionalParams != nil {
|
||||
if chunkEnabledMap, ok := additionalParams["chunk_enabled"].(map[string]bool); ok {
|
||||
if enabled, exists := chunkEnabledMap[indexInfo.ChunkID]; exists {
|
||||
pgVector.IsEnabled = enabled
|
||||
}
|
||||
}
|
||||
}
|
||||
return pgVector
|
||||
}
|
||||
|
||||
// fromDBVectorEmbeddingWithScore converts pgVectorWithScore to IndexWithScore domain model
|
||||
func fromDBVectorEmbeddingWithScore(embedding *pgVectorWithScore, matchType types.MatchType) *types.IndexWithScore {
|
||||
return &types.IndexWithScore{
|
||||
ID: strconv.FormatInt(int64(embedding.ID), 10),
|
||||
SourceID: embedding.SourceID,
|
||||
SourceType: types.SourceType(embedding.SourceType),
|
||||
ChunkID: embedding.ChunkID,
|
||||
KnowledgeID: embedding.KnowledgeID,
|
||||
KnowledgeBaseID: embedding.KnowledgeBaseID,
|
||||
TagID: embedding.TagID,
|
||||
Content: embedding.Content,
|
||||
Score: embedding.Score,
|
||||
MatchType: matchType,
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user