-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmilvus_dao_impl.go
More file actions
180 lines (156 loc) · 4.18 KB
/
milvus_dao_impl.go
File metadata and controls
180 lines (156 loc) · 4.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
package dao
import (
"context"
"encoding/json"
"fmt"
"os"
"time"
"github.com/homingos/campaign-svc/dtos"
"github.com/homingos/flam-go-common/errors"
"github.com/milvus-io/milvus-sdk-go/v2/client"
"github.com/milvus-io/milvus-sdk-go/v2/entity"
"go.uber.org/zap"
)
type MilvusDaoImpl struct {
lgr *zap.SugaredLogger
milvusClient client.Client
}
func NewMilvusDao(lgr *zap.SugaredLogger, milvusClient client.Client) *MilvusDaoImpl {
return &MilvusDaoImpl{lgr: lgr, milvusClient: milvusClient}
}
// L2DistanceToSimilarity converts L2 distance to similarity score
// range: [0, 1], where 1 is most similar
// func l2DistanceToSimilarity(l2Distance float32) float32 {
// similarity := 1 / (1 + l2Distance)
// return similarity
// }
// NormalizeCOSINESimilarity converts Milvus score to similarity score
// Milvus returns Cosine Similarity [-1, 1] when using entity.COSINE.
func NormalizeCOSINESimilarity(score float32) float32 {
if score < 0 {
return 0
}
return score
}
func (impl *MilvusDaoImpl) Search(ctx context.Context, embeddings []float32, siteCode string) ([]dtos.SearchResult, error) {
fmt.Println("searching in milvus")
searchParams, err := entity.NewIndexFlatSearchParam()
if err != nil {
return nil, errors.InternalServerError(err.Error())
}
expr := fmt.Sprintf("catalog_id == '%s'", siteCode)
Env := os.Getenv("APP_ENV")
Env = "prod"
// if Env == "non-prod" {
// Env = "qa"
// }
// if Env == "" {
// Env = "dev"
// }
milvusColl := fmt.Sprintf("product_vectors_%s", Env)
// fmt.Println(milvusColl)
// fmt.Println(embeddings)
// Ensure collection is loaded (no-op if already loaded)
loaded, err := impl.milvusClient.GetLoadState(ctx, milvusColl, nil)
if err != nil || loaded != entity.LoadStateLoaded {
_ = impl.milvusClient.LoadCollection(ctx, milvusColl, false)
}
results, err := impl.milvusClient.Search(ctx,
milvusColl,
[]string{},
expr,
[]string{"*"},
[]entity.Vector{entity.FloatVector(embeddings)},
"vector_information",
entity.COSINE,
5,
searchParams,
)
if err != nil {
return nil, errors.InternalServerError(err.Error())
}
var searchResults []dtos.SearchResult
if len(results) > 0 {
numHits := results[0].ResultCount
if numHits == 0 {
return nil, fmt.Errorf("no hits found")
}
for i := 0; i < numHits; i++ {
id, err := results[0].IDs.GetAsString(i)
if err != nil {
continue
}
score := results[0].Scores[i]
catalogID := ""
clientID := ""
description := ""
name := ""
if len(results[0].Fields) > 0 {
for _, field := range results[0].Fields {
switch field.Name() {
case "catalog_id":
catalogID, _ = field.GetAsString(i)
case "client_id":
clientID, _ = field.GetAsString(i)
case "description":
description, _ = field.GetAsString(i)
case "name":
name, _ = field.GetAsString(i)
case "product_name":
name, _ = field.GetAsString(i)
case "short_code":
if name == "" {
name, _ = field.GetAsString(i)
}
default:
//
}
}
}
_ = catalogID
_ = clientID
_ = description
candidate := dtos.SearchResult{
Document: dtos.Document{
ID: id,
Name: name,
CatalogID: catalogID,
ClientID: clientID,
Description: description,
},
Score: score,
}
searchResults = append(searchResults, candidate)
}
} else {
return nil, errors.InternalServerError("no results found")
}
PrettyPrint(searchResults)
return searchResults, nil
}
func (impl *MilvusDaoImpl) Delete(ctx context.Context, clientID string, milvusRefID string) error {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
Env := os.Getenv("APP_ENV")
if Env == "non-prod" {
Env = "qa"
}
if Env == "" {
Env = "prod"
}
milvusColl := fmt.Sprintf("product_vectors_%s", Env)
expr := fmt.Sprintf("id == '%s'", milvusRefID)
err := impl.milvusClient.Delete(ctx, milvusColl, "", expr)
if err != nil {
return errors.InternalServerError(err.Error())
}
return nil
}
func PrettyPrint(data interface{}) {
jsonData, err := json.MarshalIndent(data, "", " ")
if err != nil {
fmt.Println("Error marshaling data:", err)
return
}
fmt.Println(string(jsonData))
}