@@ -102,6 +102,48 @@ async def test_ensure_collection_exists_custom_dict(store, data_model_def):
102102 await collection .ensure_collection_deleted ()
103103
104104
105+ @mark .parametrize ("use_index_dict" , [False , True ], ids = ["single-index" , "index-dict" ])
106+ @mark .parametrize ("index_kind" , ["flat-l2" , "flat-ip" , "hnsw" ])
107+ async def test_custom_index_upsert_and_search (store , data_model_def , use_index_dict , index_kind ):
108+ collection = store .get_collection (collection_name = "test" , record_type = dict , definition = data_model_def )
109+ records = [
110+ {"id" : "first" , "content" : "first record" , "vector" : [1.0 , 0.0 , 0.0 , 0.0 , 0.0 ]},
111+ {"id" : "second" , "content" : "second record" , "vector" : [- 1.0 , 0.0 , 0.0 , 0.0 , 0.0 ]},
112+ ]
113+
114+ for _ in range (2 ):
115+ if index_kind == "flat-l2" :
116+ index = faiss .IndexFlatL2 (5 )
117+ elif index_kind == "flat-ip" :
118+ index = faiss .IndexFlatIP (5 )
119+ else :
120+ index = faiss .IndexHNSWFlat (5 , 4 )
121+ kwargs = {"indexes" : {"vector" : index }} if use_index_dict else {"index" : index }
122+ await collection .ensure_collection_exists (** kwargs )
123+ assert collection .indexes ["vector" ] is index
124+
125+ assert await collection .upsert (records [0 ]) == "first"
126+ key_map = collection .indexes_key_map ["vector" ]
127+ await collection .ensure_collection_exists (** kwargs )
128+ assert collection .indexes_key_map ["vector" ] is key_map
129+ assert key_map == {"first" : 0 }
130+ assert await collection .upsert (records [1 :]) == ["second" ]
131+ assert key_map == {"first" : 0 , "second" : 1 }
132+
133+ results = await collection .search (
134+ vector = records [0 ]["vector" ], top = 2 , include_total_count = True , include_vectors = True
135+ )
136+ assert results .total_count == 2
137+ assert [result .record async for result in results .results ] == records
138+ assert await collection .get ("first" , include_vectors = True ) == records [0 ]
139+ assert index .ntotal == 2
140+
141+ await collection .ensure_collection_deleted ()
142+ assert collection .indexes == {}
143+ assert collection .indexes_key_map == {}
144+ assert collection .inner_storage == {}
145+
146+
105147async def test_upsert (faiss_collection ):
106148 await faiss_collection .ensure_collection_exists ()
107149 record = {"id" : "testid" , "content" : "test content" , "vector" : [0.1 , 0.2 , 0.3 , 0.4 , 0.5 ]}
0 commit comments