-
Notifications
You must be signed in to change notification settings - Fork 0
/
milvus_operator.py
75 lines (59 loc) · 2.2 KB
/
milvus_operator.py
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
from pymilvus import connections, db, Collection
class MilvusOperator:
def __init__(self, database, collection, metric_type):
self.database = database
self.coll_name = collection
self.metric_type = metric_type
self.connect = connections.connect(alias="default", host='10.66.12.37', port='19530')
db.using_database(database)
def insert_data(self, data):
collection = Collection(self.coll_name)
mr = collection.insert(data)
def search_data(self, embeding):
collection = Collection(self.coll_name)
collection.load()
search_params = {
"metric_type": self.metric_type,
"offset": 0,
"ignore_growing": False,
"params": {"nprobe": 16}
}
results = collection.search(
data=[embeding],
anns_field="embeding",
param=search_params,
limit=16,
expr=None,
output_fields=['m_id', 'path'],
consistency_level="Strong"
)
entity_list = []
if results[0] is not None:
for idx in range(len(results[0])):
hit = results[0][idx]
entity_list.append({'m_id': results[0].ids[idx],
'distance': results[0].distances[idx],
'path': hit.entity.get('path')})
return entity_list
def query_by_ids(self, ids: list):
collection = Collection(self.coll_name)
collection.load()
str_list = [str(id) for id in ids]
temp_str = ', '.join(str_list)
query_expr = f'M_id in [{temp_str}]'
res = collection.query(
expr=query_expr,
offset=0,
limit=16384,
output_fields=["m_id", "embeding", "path"],
)
return res
def delete_by_ids(self, ids: list):
collection = Collection(self.coll_name)
collection.load()
str_list = [str(id) for id in ids]
temp_str = ', '.join(str_list)
query_expr = f'm_id in [{temp_str}]'
collection.delete(query_expr)
return
text_image_vector = MilvusOperator('text_image_db', 'text_image_vector', 'IP')