feat: pass IndexConfig to repositories with scalability options
This commit is contained in:
@@ -4,7 +4,6 @@ import (
|
||||
"context"
|
||||
"fmt"
|
||||
"maps"
|
||||
"os"
|
||||
"slices"
|
||||
"strings"
|
||||
"unicode/utf8"
|
||||
@@ -35,19 +34,19 @@ const (
|
||||
fieldID = "id"
|
||||
)
|
||||
|
||||
func NewWeaviateRetrieveEngineRepository(client *weaviate.Client) interfaces.RetrieveEngineRepository {
|
||||
// NewWeaviateRetrieveEngineRepository creates and initializes a new Weaviate repository.
|
||||
// indexCfg is optional — pass nil to use env var / default values (env path).
|
||||
func NewWeaviateRetrieveEngineRepository(client *weaviate.Client, indexCfg *types.IndexConfig) interfaces.RetrieveEngineRepository {
|
||||
log := logger.GetLogger(context.Background())
|
||||
log.Info("[Weaviate] Initializing Weaviate retriever engine repository")
|
||||
|
||||
collectionBaseName := os.Getenv(envWeaviateCollection)
|
||||
if collectionBaseName == "" {
|
||||
log.Warn("[Weaviate] WEAVIATE_COLLECTION environment variable not set, using default collection name")
|
||||
collectionBaseName = defaultCollectionName
|
||||
}
|
||||
collectionBaseName := types.ResolveCollectionName(indexCfg, envWeaviateCollection, defaultCollectionName)
|
||||
|
||||
res := &weaviateRepository{
|
||||
client: client,
|
||||
collectionBaseName: collectionBaseName,
|
||||
replicationFactor: indexCfg.GetReplicationFactor(0),
|
||||
desiredShardCount: indexCfg.GetDesiredShardCount(0),
|
||||
}
|
||||
|
||||
log.Info("[Weaviate] Successfully initialized repository")
|
||||
@@ -137,6 +136,18 @@ func (w *weaviateRepository) ensureCollection(ctx context.Context, dimension int
|
||||
},
|
||||
},
|
||||
}
|
||||
// Set replication factor if explicitly configured (> 0)
|
||||
if w.replicationFactor > 0 {
|
||||
classObj.ReplicationConfig = &models.ReplicationConfig{
|
||||
Factor: int64(w.replicationFactor),
|
||||
}
|
||||
}
|
||||
// Set shard count if explicitly configured (> 0)
|
||||
if w.desiredShardCount > 0 {
|
||||
classObj.ShardingConfig = map[string]interface{}{
|
||||
"desiredCount": w.desiredShardCount,
|
||||
}
|
||||
}
|
||||
//创建collection
|
||||
if err = w.client.Schema().ClassCreator().WithClass(&classObj).Do(ctx); err != nil {
|
||||
log.Errorf("[Weaviate] Failed to create collection: %v", err)
|
||||
|
||||
@@ -9,6 +9,8 @@ import (
|
||||
type weaviateRepository struct {
|
||||
client *weaviate.Client
|
||||
collectionBaseName string
|
||||
replicationFactor int // 0 = use Weaviate server default
|
||||
desiredShardCount int // 0 = use Weaviate server default
|
||||
// Cache for initialized collections (dimension -> true)
|
||||
initializedCollections sync.Map
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user