Skip to content

Commit 8df12ae

Browse files
Dounxlyingbug
authored andcommitted
fix: make Milvus vector metric type configurable via MILVUS_METRIC_TYPE
1 parent 5af8e4e commit 8df12ae

File tree

4 files changed

+25
-1
lines changed

4 files changed

+25
-1
lines changed

.env.example

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,10 @@ APK_MIRROR_ARG=mirrors.tencent.com
267267
# Milvus集合名称,用于存储向量数据
268268
# MILVUS_COLLECTION=weknora_embeddings
269269

270+
# Milvus向量搜索度量类型,支持 IP(默认)、COSINE、L2
271+
# 注意:修改度量类型后需要重建collection才能生效
272+
# MILVUS_METRIC_TYPE=IP
273+
270274
# Milvus 用户名(可选)
271275
# MILVUS_USERNAME=your_milvus_username
272276

docker-compose.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ services:
8484
- QDRANT_USE_TLS=${QDRANT_USE_TLS:-false}
8585
- MILVUS_ADDRESS=milvus:19530
8686
- MILVUS_COLLECTION=${MILVUS_COLLECTION:-weknora_embeddings}
87+
- MILVUS_METRIC_TYPE=${MILVUS_METRIC_TYPE:-IP}
8788
- DOCREADER_ADDR=${DOCREADER_ADDR:-docreader:50051}
8889
- DOCREADER_TRANSPORT=${DOCREADER_TRANSPORT:-grpc}
8990
- WEAVIATE_HOST=${WEAVIATE_HOST:-weaviate:8080}

internal/application/repository/retriever/milvus/repository.go

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121

2222
const (
2323
envMilvusCollection = "MILVUS_COLLECTION"
24+
envMilvusMetricType = "MILVUS_METRIC_TYPE"
2425
defaultCollectionName = "weknora_embeddings"
2526
fieldContent = "content"
2627
fieldSourceID = "source_id"
@@ -51,10 +52,26 @@ func NewMilvusRetrieveEngineRepository(client *client.Client) interfaces.Retriev
5152
collectionBaseName = defaultCollectionName
5253
}
5354

55+
metricType := entity.IP
56+
if mt := os.Getenv(envMilvusMetricType); mt != "" {
57+
switch strings.ToUpper(mt) {
58+
case "COSINE":
59+
metricType = entity.COSINE
60+
case "L2":
61+
metricType = entity.L2
62+
case "IP":
63+
metricType = entity.IP
64+
default:
65+
log.Warnf("[Milvus] Unknown MILVUS_METRIC_TYPE '%s', using default IP", mt)
66+
}
67+
}
68+
log.Infof("[Milvus] Using metric type: %s", metricType)
69+
5470
res := &milvusRepository{
5571
filter: filter{},
5672
client: client,
5773
collectionBaseName: collectionBaseName,
74+
metricType: metricType,
5875
}
5976

6077
log.Info("[Milvus] Successfully initialized repository")
@@ -150,7 +167,7 @@ func (m *milvusRepository) ensureCollection(ctx context.Context, dimension int)
150167

151168
indexOpts := make([]client.CreateIndexOption, 0)
152169
// hnsw index for embedding field
153-
indexOpts = append(indexOpts, client.NewCreateIndexOption(collectionName, fieldEmbedding, index.NewHNSWIndex(entity.IP, 16, 128)))
170+
indexOpts = append(indexOpts, client.NewCreateIndexOption(collectionName, fieldEmbedding, index.NewHNSWIndex(m.metricType, 16, 128)))
154171
indexOpts = append(indexOpts, client.NewCreateIndexOption(collectionName, fieldContentSparse, index.NewAutoIndex(entity.BM25)))
155172
// Create payload indexes for filtering
156173
indexFields := []string{fieldChunkID, fieldKnowledgeID, fieldKnowledgeBaseID, fieldSourceID, fieldIsEnabled}

internal/application/repository/retriever/milvus/structs.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,15 @@ package milvus
33
import (
44
"sync"
55

6+
"github.com/milvus-io/milvus/client/v2/entity"
67
client "github.com/milvus-io/milvus/client/v2/milvusclient"
78
)
89

910
type milvusRepository struct {
1011
filter
1112
client *client.Client
1213
collectionBaseName string
14+
metricType entity.MetricType
1315
// Cache for initialized collections (dimension -> true)
1416
initializedCollections sync.Map
1517
}

0 commit comments

Comments
 (0)