test_search_by_id.py 22.1 KB
Newer Older
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
# import pdb
# import copy
# import struct
# import pytest
# import threading
# import datetime
# import logging
# from time import sleep
# from multiprocessing import Process
# import numpy
# import sklearn.preprocessing
# from milvus import Milvus, IndexType, MetricType
# from utils import *
# 
# dim = 128
# collection_id = "test_search_by_id"
# nb = 6000
# vectors = gen_vectors(nb, dim)
# vectors = sklearn.preprocessing.normalize(vectors, axis=1, norm='l2')
# vectors = vectors.tolist()
# nprobe = 1
# epsilon = 0.001
# tag = "overallpaper"
# top_k = 5
# nq = 10
# nprobe = 1
# non_exist_id = [9527]
# raw_vectors, binary_vectors = gen_binary_vectors(6000, dim)
# 
# 
# class TestSearchBase:
#     # @pytest.fixture(scope="function", autouse=True)
#     # def skip_check(self, connect):
#     #     if str(connect._cmd("mode")[1]) == "CPU":
#     #         if request.param["index_type"] == IndexType.IVF_SQ8H:
#     #             pytest.skip("sq8h not support in CPU mode")
#     #     if str(connect._cmd("mode")[1]) == "GPU":
#     #         if request.param["index_type"] == IndexType.IVF_PQ:
#     #             pytest.skip("ivfpq not support in GPU mode")
# 
#     def init_data(self, connect, collection, nb=6000):
#         '''
#         Generate vectors and add it in collection, before search vectors
#         '''
#         global vectors
#         if nb == 6000:
D
del-zhenwu 已提交
47
#             insert = vectors
48
#         else:  
D
del-zhenwu 已提交
49 50
#             insert = gen_vectors(nb, dim)
#         status, ids = connect.insert(collection, insert)
51
#         connect.flush([collection])
D
del-zhenwu 已提交
52
#         return insert, ids
53 54 55 56 57 58 59
# 
#     def init_data_binary(self, connect, collection, nb=6000):
#         '''
#         Generate vectors and add it in collection, before search vectors
#         '''
#         global binary_vectors
#         if nb == 6000:
D
del-zhenwu 已提交
60
#             insert = binary_vectors
61
#         else:
D
del-zhenwu 已提交
62 63
#             insert = gen_binary_vectors(nb, dim)
#         status, ids = connect.insert(collection, insert)
64
#         connect.flush([collection])
D
del-zhenwu 已提交
65
#         return insert, ids
66 67 68 69
# 
#     def init_data_no_flush(self, connect, collection, nb=6000):
#         global vectors
#         if nb == 6000:
D
del-zhenwu 已提交
70
#             insert = vectors
71
#         else:  
D
del-zhenwu 已提交
72 73 74
#             insert = gen_vectors(nb, dim)
#         status, ids = connect.insert(collection, insert)
#         return insert, ids
75 76 77 78 79
# 
#     def init_data_ids(self, connect, collection, nb=6000):
#         global vectors
#         my_ids = [i for i in range(nb)]
#         if nb == 6000:
D
del-zhenwu 已提交
80
#             insert = vectors
81
#         else:  
D
del-zhenwu 已提交
82 83
#             insert = gen_vectors(nb, dim)
#         status, ids = connect.insert(collection, insert, my_ids)
84
#         connect.flush([collection])
D
del-zhenwu 已提交
85
#         return insert, ids
86 87 88 89 90 91 92
# 
#     def init_data_partition(self, connect, collection, partition_tag, nb=6000):
#         '''
#         Generate vectors and add it in collection, before search vectors
#         '''
#         global vectors
#         if nb == 6000:
D
del-zhenwu 已提交
93
#             insert = vectors
94
#         else:  
D
del-zhenwu 已提交
95 96 97 98
#             insert = gen_vectors(nb, dim)
#             insert = sklearn.preprocessing.normalize(insert, axis=1, norm='l2')
#             insert = insert.tolist()
#         status, ids = connect.insert(collection, insert, partition_tag=partition_tag)
99 100
#         assert status.OK()
#         connect.flush([collection])
D
del-zhenwu 已提交
101
#         return insert, ids
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 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530
# 
#     @pytest.fixture(
#         scope="function",
#         params=gen_simple_index()
#     )
#     def get_simple_index(self, request, connect):
#         if str(connect._cmd("mode")[1]) == "CPU":
#             if request.param["index_type"] == IndexType.IVF_SQ8H:
#                 pytest.skip("sq8h not support in CPU mode")
#         if str(connect._cmd("mode")[1]) == "GPU":
#             if request.param["index_type"] == IndexType.IVF_PQ:
#                 pytest.skip("ivfpq not support in GPU mode")
#         return request.param
# 
#     @pytest.fixture(
#         scope="function",
#         params=gen_simple_index()
#     )
#     def get_jaccard_index(self, request, connect):
#         logging.getLogger().info(request.param)
#         if request.param["index_type"] == IndexType.IVFLAT or request.param["index_type"] == IndexType.FLAT:
#             return request.param
#         else:
#             pytest.skip("Skip index Temporary")
# 
#     @pytest.fixture(
#         scope="function",
#         params=gen_simple_index()
#     )
#     def get_hamming_index(self, request, connect):
#         logging.getLogger().info(request.param)
#         if request.param["index_type"] == IndexType.IVFLAT or request.param["index_type"] == IndexType.FLAT:
#             return request.param
#         else:
#             pytest.skip("Skip index Temporary")
# 
#     @pytest.fixture(
#         scope="function",
#         params=gen_simple_index()
#     )
#     def get_structure_index(self, request, connect):
#         logging.getLogger().info(request.param)
#         if request.param["index_type"] == IndexType.FLAT:
#             return request.param
#         else:
#             pytest.skip("Skip index Temporary")
# 
#     """
#     generate top-k params
#     """
#     @pytest.fixture(
#         scope="function",
#         params=[1, 2048]
#     )
#     def get_top_k(self, request):
#         yield request.param
# 
#     def test_search_flat_normal_topk(self, connect, collection, get_top_k):
#         '''
#         target: test basic search fuction, all the search params is corrent, change top-k value
#         method: search with the given vector id, check the result
#         expected: search status ok, and the length of the result is top_k
#         '''
#         top_k = get_top_k
#         vectors, ids = self.init_data(connect, collection)
#         query_ids = [ids[0]]
#         status, result = connect.search_by_id(collection, query_ids, top_k, params={})
#         assert status.OK()
#         assert len(result[0]) == min(len(vectors), top_k)
#         assert result[0][0].distance <= epsilon
#         assert check_result(result[0], ids[0])
# 
#     def test_search_flat_same_ids(self, connect, collection):
#         '''
#         target: test basic search fuction, all the search params is corrent, change top-k value
#         method: search with the given vector id, check the result
#         expected: search status ok, and the length of the result is top_k
#         '''
#         vectors, ids = self.init_data(connect, collection)
#         query_ids = [ids[0], ids[0]]
#         status, result = connect.search_by_id(collection, query_ids, top_k, params={})
#         assert status.OK()
#         assert len(result[0]) == min(len(vectors), top_k)
#         assert result[0][0].distance <= epsilon
#         assert result[1][0].distance <= epsilon
#         assert check_result(result[0], ids[0])
#         assert check_result(result[1], ids[0])
# 
#     def test_search_flat_max_topk(self, connect, collection):
#         '''
#         target: test basic search fuction, all the search params is corrent, change top-k value
#         method: search with the given vector id, check the result
#         expected: search status ok, and the length of the result is top_k
#         '''
#         top_k = 2049
#         vectors, ids = self.init_data(connect, collection)
#         query_ids = [ids[0]]
#         status, result = connect.search_by_id(collection, query_ids, top_k, params={})
#         assert not status.OK()
# 
#     def test_search_id_not_existed(self, connect, collection):
#         '''
#         target: test basic search fuction, all the search params is corrent, change top-k value
#         method: search with the given vector id, check the result
#         expected: search status ok, and the length of the result is top_k
#         '''
#         vectors, ids = self.init_data(connect, collection)
#         query_ids = non_exist_id
#         status, result = connect.search_by_id(collection, query_ids, top_k, params={})
#         assert status.OK()
#         assert len(result[0]) == 0
# 
#     def test_search_collection_empty(self, connect, collection):
#         '''
#         target: test basic search fuction, all the search params is corrent, change top-k value
#         method: search with the given vector id, check the result
#         expected: search status ok, and the length of the result is top_k
#         '''
#         query_ids = non_exist_id
#         logging.getLogger().info(query_ids)
#         logging.getLogger().info(collection)
#         logging.getLogger().info(connect.get_collection_info(collection))
#         status, result = connect.search_by_id(collection, query_ids, top_k, params={})
#         assert not status.OK()
# 
#     def test_search_index_l2(self, connect, collection, get_simple_index):
#         '''
#         target: test basic search fuction, all the search params is corrent, test all index params, and build
#         method: search with the given vectors, check the result
#         expected: search status ok, and the length of the result is top_k
#         '''
#         index_param = get_simple_index["index_param"]
#         index_type = get_simple_index["index_type"]
#         if index_type == IndexType.IVF_PQ:
#             pytest.skip("skip pq")
#         vectors, ids = self.init_data(connect, collection)
#         status = connect.create_index(collection, index_type, index_param)
#         query_ids = [ids[0]]
#         search_param = get_search_param(index_type)
#         status, result = connect.search_by_id(collection, query_ids, top_k, params=search_param)
#         assert status.OK()
#         assert len(result[0]) == min(len(vectors), top_k)
#         assert result[0][0].distance <= epsilon
#         assert check_result(result[0], ids[0])
# 
#     def test_search_index_l2_B(self, connect, collection, get_simple_index):
#         '''
#         target: test basic search fuction, all the search params is corrent, test all index params, and build
#         method: search with the given vectors, check the result
#         expected: search status ok, and the length of the result is top_k
#         '''
#         index_param = get_simple_index["index_param"]
#         index_type = get_simple_index["index_type"]
#         if index_type == IndexType.IVF_PQ:
#             pytest.skip("skip pq")
#         vectors, ids = self.init_data(connect, collection)
#         status = connect.create_index(collection, index_type, index_param)
#         query_ids = ids[0:nq]
#         search_param = get_search_param(index_type)
#         status, result = connect.search_by_id(collection, query_ids, top_k, params=search_param)
#         assert status.OK()
#         assert len(result) == nq
#         for i in range(nq):
#             assert len(result[i]) == min(len(vectors), top_k)
#             assert result[i][0].distance <= epsilon
#             assert check_result(result[i], ids[i])
# 
#     def test_search_index_l2_C(self, connect, collection, get_simple_index):
#         '''
#         target: test basic search fuction, all the search params is corrent, one id is not existed
#         method: search with the given vectors, check the result
#         expected: search status ok, and the length of the result is top_k
#         '''
#         index_param = get_simple_index["index_param"]
#         index_type = get_simple_index["index_type"]
#         if index_type == IndexType.IVF_PQ:
#             pytest.skip("skip pq")
#         vectors, ids = self.init_data(connect, collection)
#         status = connect.create_index(collection, index_type, index_param)
#         query_ids = ids[0:nq]
#         query_ids[0] = 1
#         search_param = get_search_param(index_type)
#         status, result = connect.search_by_id(collection, query_ids, top_k, params=search_param)
#         assert status.OK()
#         assert len(result) == nq
#         for i in range(nq):
#             if i == 0:
#                 assert len(result[i]) == 0
#             else:
#                 assert len(result[i]) == min(len(vectors), top_k)
#                 assert result[i][0].distance <= epsilon
#                 assert check_result(result[i], ids[i])
# 
#     def test_search_index_delete(self, connect, collection):
#         vectors, ids = self.init_data(connect, collection)
#         query_ids = ids[0:nq]
#         status = connect.delete_entity_by_id(collection, [query_ids[0]])
#         assert status.OK()
#         status = connect.flush([collection])
#         status, result = connect.search_by_id(collection, query_ids, top_k, params={})
#         assert status.OK()
#         assert len(result) == nq 
#         assert len(result[0]) == 0
#         assert len(result[1]) == top_k 
#         assert result[1][0].distance <= epsilon
# 
#     def test_search_l2_partition_tag_not_existed(self, connect, collection):
#         '''
#         target: test basic search fuction, all the search params is corrent, test all index params, and build
#         method: add vectors into collection, search with the given vectors, check the result
#         expected: search status ok, and the length of the result is top_k, search collection with partition tag return empty
#         '''
#         status = connect.create_partition(collection, tag)
#         vectors, ids = self.init_data(connect, collection)
#         query_ids = [ids[0]]
#         new_tag = gen_unique_str()
#         status, result = connect.search_by_id(collection, query_ids, top_k, partition_tags=[new_tag], params={})
#         assert not status.OK() 
#         logging.getLogger().info(status)
#         assert len(result) == 0
# 
#     def test_search_l2_partition_empty(self, connect, collection):
#         status = connect.create_partition(collection, tag)
#         vectors, ids = self.init_data(connect, collection)
#         query_ids = [ids[0]]
#         status, result = connect.search_by_id(collection, query_ids, top_k, partition_tags=[tag], params={})
#         assert not status.OK()
#         logging.getLogger().info(status)
#         assert len(result) == 0
# 
#     def test_search_l2_partition(self, connect, collection):
#         status = connect.create_partition(collection, tag)
#         vectors, ids = self.init_data_partition(connect, collection, tag)
#         query_ids = ids[-1:]
#         status, result = connect.search_by_id(collection, query_ids, top_k, partition_tags=[tag])
#         assert status.OK() 
#         assert len(result) == 1
#         assert len(result[0]) == min(len(vectors), top_k)
#         assert check_result(result[0], query_ids[-1])
# 
#     def test_search_l2_partition_B(self, connect, collection):
#         status = connect.create_partition(collection, tag)
#         vectors, ids = self.init_data_partition(connect, collection, tag)
#         query_ids = ids[0:nq]
#         status, result = connect.search_by_id(collection, query_ids, top_k, partition_tags=[tag])
#         assert status.OK()
#         assert len(result) == nq
#         for i in range(nq):
#             assert len(result[i]) == min(len(vectors), top_k)
#             assert result[i][0].distance <= epsilon
#             assert check_result(result[i], ids[i])
# 
#     def test_search_l2_index_partitions(self, connect, collection):
#         new_tag = "new_tag"
#         status = connect.create_partition(collection, tag)
#         status = connect.create_partition(collection, new_tag)
#         vectors, ids = self.init_data_partition(connect, collection, tag)
#         vectors, new_ids = self.init_data_partition(connect, collection, new_tag, nb=nb+1)
#         tmp = 2
#         query_ids = ids[0:tmp]
#         query_ids.extend(new_ids[tmp:nq])
#         status, result = connect.search_by_id(collection, query_ids, top_k, partition_tags=[tag, new_tag], params={})
#         assert status.OK()
#         assert len(result) == nq
#         for i in range(nq):
#             assert len(result[i]) == min(len(vectors), top_k)
#             assert result[i][0].distance <= epsilon
#             if i < tmp:
#                 assert result[i][0].id == ids[i]
#             else:
#                 assert result[i][0].id == new_ids[i]
# 
#     def test_search_l2_index_partitions_match_one_tag(self, connect, collection):
#         new_tag = "new_tag"
#         status = connect.create_partition(collection, tag)
#         status = connect.create_partition(collection, new_tag)
#         vectors, ids = self.init_data_partition(connect, collection, tag)
#         vectors, new_ids = self.init_data_partition(connect, collection, new_tag, nb=nb+1)
#         tmp = 2
#         query_ids = ids[0:tmp]
#         query_ids.extend(new_ids[tmp:nq])
#         status, result = connect.search_by_id(collection, query_ids, top_k, partition_tags=[new_tag], params={})
#         assert status.OK()
#         assert len(result) == nq
#         for i in range(nq):
#             if i < tmp:
#                 assert result[i][0].distance > epsilon
#                 assert result[i][0].id != ids[i]
#             else:
#                 assert len(result[i]) == min(len(vectors), top_k)
#                 assert result[i][0].distance <= epsilon
#                 assert result[i][0].id == new_ids[i]
#                 assert result[i][1].distance > epsilon
# 
#     # def test_search_by_id_without_connect(self, dis_connect, collection):
#     #     '''
#     #     target: test search vectors without connection
#     #     method: use dis connected instance, call search method and check if search successfully
#     #     expected: raise exception
#     #     '''
#     #     query_ids = [1]
#     #     with pytest.raises(Exception) as e:
#     #         status, ids = dis_connect.search_by_id(collection, query_ids, top_k, params={})
# 
#     def test_search_collection_name_not_existed(self, connect, collection):
#         '''
#         target: search collection not existed
#         method: search with the random collection_name, which is not in db
#         expected: status not ok
#         '''
#         collection_name = gen_unique_str("not_existed_collection")
#         query_ids = non_exist_id
#         status, result = connect.search_by_id(collection_name, query_ids, top_k, params={})
#         assert not status.OK()
# 
#     def test_search_collection_name_None(self, connect, collection):
#         '''
#         target: search collection that collection name is None
#         method: search with the collection_name: None
#         expected: status not ok
#         '''
#         collection_name = None
#         query_ids = non_exist_id
#         with pytest.raises(Exception) as e: 
#             status, result = connect.search_by_id(collection_name, query_ids, top_k, params={})
# 
#     def test_search_jac(self, connect, jac_collection, get_jaccard_index):
#         index_param = get_jaccard_index["index_param"]
#         index_type = get_jaccard_index["index_type"]
#         vectors, ids = self.init_data_binary(connect, jac_collection)
#         status = connect.create_index(jac_collection, index_type, index_param)
#         assert status.OK()
#         query_ids = ids[0:nq]
#         search_param = get_search_param(index_type)
#         status, result = connect.search_by_id(jac_collection, query_ids, top_k, params=search_param)
#         assert status.OK()
#         assert len(result) == nq
#         for i in range(nq):
#             assert len(result[i]) == min(len(vectors), top_k)
#             assert result[i][0].distance <= epsilon
#             assert check_result(result[i], ids[i])
# 
# 
# """
# ******************************************************************
# #  The following cases are used to test `search_by_id` function 
# #  with invalid collection_name top-k / ids / tags
# ******************************************************************
# """
# 
# class TestSearchParamsInvalid(object):
#     nlist = 16384
#     index_param = {"index_type": IndexType.IVF_SQ8, "nlist": nlist}
# 
#     """
#     Test search collection with invalid collection names
#     """
#     @pytest.fixture(
#         scope="function",
#         params=gen_invalid_collection_names()
#     )
#     def get_collection_name(self, request):
#         yield request.param
# 
#     @pytest.mark.level(2)
#     def test_search_with_invalid_collectionname(self, connect, get_collection_name):
#         collection_name = get_collection_name
#         query_ids = non_exist_id
#         status, result = connect.search_by_id(collection_name, query_ids, top_k, params={})
#         assert not status.OK()
# 
#     @pytest.mark.level(1)
#     def test_search_with_invalid_tag_format(self, connect, collection):
#         query_ids = non_exist_id
#         with pytest.raises(Exception) as e:
#             status, result = connect.search_by_id(collection_name, query_ids, top_k, partition_tags="tag")
# 
#     """
#     Test search collection with invalid top-k
#     """
#     @pytest.fixture(
#         scope="function",
#         params=gen_invalid_top_ks()
#     )
#     def get_top_k(self, request):
#         yield request.param
# 
#     @pytest.mark.level(1)
#     def test_search_with_invalid_top_k(self, connect, collection, get_top_k):
#         top_k = get_top_k
#         query_ids = non_exist_id
#         if isinstance(top_k, int):
#             status, result = connect.search_by_id(collection, query_ids, top_k)
#             assert not status.OK()
#         else:
#             with pytest.raises(Exception) as e:
#                 status, result = connect.search_by_id(collection, query_ids, top_k)
# 
#     """
#     Test search collection with invalid query ids 
#     """
#     @pytest.fixture(
#         scope="function",
#         params=gen_invalid_vector_ids()
#     )
#     def get_ids(self, request):
#         yield request.param
# 
#     @pytest.mark.level(1)
#     def test_search_with_invalid_ids(self, connect, collection, get_ids):
#         id = get_ids
#         query_ids = [id]
#         if not isinstance(id, int):
#             with pytest.raises(Exception) as e:
#                 status, result = connect.search_by_id(collection, query_ids, top_k)
# 
#     @pytest.mark.level(2)
#     def test_search_with_part_invalid_ids(self, connect, collection, get_ids):
#         id = get_ids
#         query_ids = [1, id]
#         with pytest.raises(Exception) as e:
#             status, result = connect.search_by_id(collection, query_ids, top_k)
# 
# 
# def check_result(result, id):
#     if len(result) >= top_k:
#         return id in [x.id for x in result[:top_k]]
#     else:
#         return id in (i.id for i in result)