test_search.py 53.4 KB
Newer Older
J
JinHai-CN 已提交
1
import pdb
G
groot 已提交
2
import struct
3
from random import sample
G
groot 已提交
4

J
JinHai-CN 已提交
5 6 7 8 9 10 11
import pytest
import threading
import datetime
import logging
from time import sleep
from multiprocessing import Process
import numpy
12
import sklearn.preprocessing
13
from milvus import IndexType, MetricType
J
JinHai-CN 已提交
14 15 16
from utils import *

dim = 128
X
Xiaohai Xu 已提交
17
collection_id = "test_search"
J
JinHai-CN 已提交
18
add_interval_time = 2
G
groot 已提交
19
vectors = gen_vectors(6000, dim)
20 21
vectors = sklearn.preprocessing.normalize(vectors, axis=1, norm='l2')
vectors = vectors.tolist()
D
del-zhenwu 已提交
22
top_k = 1
Z
zhenwu 已提交
23
nprobe = 1
J
JinHai-CN 已提交
24
epsilon = 0.001
Z
zhenwu 已提交
25
tag = "1970-01-01"
G
groot 已提交
26
raw_vectors, binary_vectors = gen_binary_vectors(6000, dim)
J
JinHai-CN 已提交
27 28 29


class TestSearchBase:
X
Xiaohai Xu 已提交
30
    def init_data(self, connect, collection, nb=6000, partition_tags=None):
J
JinHai-CN 已提交
31
        '''
X
Xiaohai Xu 已提交
32
        Generate vectors and add it in collection, before search vectors
J
JinHai-CN 已提交
33 34
        '''
        global vectors
G
groot 已提交
35
        if nb == 6000:
J
JinHai-CN 已提交
36 37 38
            add_vectors = vectors
        else:  
            add_vectors = gen_vectors(nb, dim)
D
del-zhenwu 已提交
39 40
            add_vectors = sklearn.preprocessing.normalize(add_vectors, axis=1, norm='l2')
            add_vectors = add_vectors.tolist()
41
        if partition_tags is None:
D
del-zhenwu 已提交
42
            status, ids = connect.insert(collection, add_vectors)
43 44
            assert status.OK()
        else:
D
del-zhenwu 已提交
45
            status, ids = connect.insert(collection, add_vectors, partition_tag=partition_tags)
46
            assert status.OK()
D
del-zhenwu 已提交
47
        connect.flush([collection])
J
JinHai-CN 已提交
48 49
        return add_vectors, ids

X
Xiaohai Xu 已提交
50
    def init_binary_data(self, connect, collection, nb=6000, insert=True, partition_tags=None):
G
groot 已提交
51
        '''
X
Xiaohai Xu 已提交
52
        Generate vectors and add it in collection, before search vectors
G
groot 已提交
53 54 55 56 57 58 59 60 61 62
        '''
        ids = []
        global binary_vectors
        global raw_vectors
        if nb == 6000:
            add_vectors = binary_vectors
            add_raw_vectors = raw_vectors
        else:  
            add_raw_vectors, add_vectors = gen_binary_vectors(nb, dim)
        if insert is True:
63
            if partition_tags is None:
D
del-zhenwu 已提交
64
                status, ids = connect.insert(collection, add_vectors)
65 66
                assert status.OK()
            else:
D
del-zhenwu 已提交
67
                status, ids = connect.insert(collection, add_vectors, partition_tag=partition_tags)
68
                assert status.OK()
D
del-zhenwu 已提交
69
            connect.flush([collection])
G
groot 已提交
70 71
        return add_raw_vectors, add_vectors, ids

J
JinHai-CN 已提交
72 73 74 75 76
    """
    generate valid create_index params
    """
    @pytest.fixture(
        scope="function",
77
        params=gen_index()
J
JinHai-CN 已提交
78
    )
79
    def get_index(self, request, connect):
G
groot 已提交
80
        if str(connect._cmd("mode")[1]) == "CPU":
81
            if request.param["index_type"] == IndexType.IVF_SQ8H:
82
                pytest.skip("sq8h not support in CPU mode")
83 84 85
        if str(connect._cmd("mode")[1]) == "GPU":
            if request.param["index_type"] == IndexType.IVF_PQ:
                pytest.skip("ivfpq not support in GPU mode")
86
        return request.param
J
JinHai-CN 已提交
87

Z
zhenwu 已提交
88 89
    @pytest.fixture(
        scope="function",
90
        params=gen_simple_index()
Z
zhenwu 已提交
91
    )
92
    def get_simple_index(self, request, connect):
G
groot 已提交
93
        if str(connect._cmd("mode")[1]) == "CPU":
Z
zhenwu 已提交
94
            if request.param["index_type"] == IndexType.IVF_SQ8H:
95
                pytest.skip("sq8h not support in CPU mode")
Z
zhenwu 已提交
96
        return request.param
G
groot 已提交
97 98 99

    @pytest.fixture(
        scope="function",
100
        params=gen_simple_index()
G
groot 已提交
101
    )
102
    def get_jaccard_index(self, request, connect):
G
groot 已提交
103 104 105 106 107 108 109 110
        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",
111
        params=gen_simple_index()
G
groot 已提交
112
    )
113
    def get_hamming_index(self, request, connect):
G
groot 已提交
114 115 116 117 118 119
        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")

D
del-zhenwu 已提交
120 121 122 123 124 125 126 127 128 129 130
    @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")

J
JinHai-CN 已提交
131 132 133 134 135
    """
    generate top-k params
    """
    @pytest.fixture(
        scope="function",
D
del-zhenwu 已提交
136
        params=[1, 99, 1024, 2049]
J
JinHai-CN 已提交
137 138 139 140 141
    )
    def get_top_k(self, request):
        yield request.param


X
Xiaohai Xu 已提交
142
    def test_search_top_k_flat_index(self, connect, collection, get_top_k):
J
JinHai-CN 已提交
143 144 145 146 147
        '''
        target: test basic search fuction, all the search params is corrent, change top-k value
        method: search with the given vectors, check the result
        expected: search status ok, and the length of the result is top_k
        '''
X
Xiaohai Xu 已提交
148
        vectors, ids = self.init_data(connect, collection)
J
JinHai-CN 已提交
149 150
        query_vec = [vectors[0]]
        top_k = get_top_k
D
del-zhenwu 已提交
151
        status, result = connect.search(collection, top_k, query_vec)
J
JinHai-CN 已提交
152 153 154 155 156 157 158 159
        if top_k <= 2048:
            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])
        else:
            assert not status.OK()

X
Xiaohai Xu 已提交
160
    def test_search_l2_index_params(self, connect, collection, get_simple_index):
J
JinHai-CN 已提交
161 162 163 164 165
        '''
        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
        '''
D
del-zhenwu 已提交
166
        top_k = 10
167 168 169
        index_param = get_simple_index["index_param"]
        index_type = get_simple_index["index_type"]
        logging.getLogger().info(get_simple_index)
D
del-zhenwu 已提交
170 171 172
        if index_type == IndexType.IVF_PQ:
            pytest.skip("Skip PQ")

X
Xiaohai Xu 已提交
173 174
        vectors, ids = self.init_data(connect, collection)
        status = connect.create_index(collection, index_type, index_param)
J
JinHai-CN 已提交
175
        query_vec = [vectors[0]]
176
        search_param = get_search_param(index_type)
D
del-zhenwu 已提交
177
        status, result = connect.search(collection, top_k, query_vec, params=search_param)
J
JinHai-CN 已提交
178 179 180 181 182 183 184 185 186
        logging.getLogger().info(result)
        if top_k <= 1024:
            assert status.OK()
            assert len(result[0]) == min(len(vectors), top_k)
            assert check_result(result[0], ids[0])
            assert result[0][0].distance <= epsilon
        else:
            assert not status.OK()

X
Xiaohai Xu 已提交
187
    def test_search_l2_large_nq_index_params(self, connect, collection, get_simple_index):
188 189 190 191 192
        '''
        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
        '''
D
del-zhenwu 已提交
193
        top_k = 10
194 195 196
        index_param = get_simple_index["index_param"]
        index_type = get_simple_index["index_type"]
        logging.getLogger().info(get_simple_index)
D
del-zhenwu 已提交
197 198 199
        if index_type == IndexType.IVF_PQ:
            pytest.skip("Skip PQ")

X
Xiaohai Xu 已提交
200 201
        vectors, ids = self.init_data(connect, collection)
        status = connect.create_index(collection, index_type, index_param)
D
del-zhenwu 已提交
202
        query_vec = vectors[:1000]
203
        search_param = get_search_param(index_type)
D
del-zhenwu 已提交
204
        status, result = connect.search(collection, top_k, query_vec, params=search_param)
205 206 207 208 209 210
        logging.getLogger().info(result)
        assert status.OK()
        assert len(result[0]) == min(len(vectors), top_k)
        assert check_result(result[0], ids[0])
        assert result[0][0].distance <= epsilon

X
Xiaohai Xu 已提交
211
    def test_search_l2_index_params_partition(self, connect, collection, get_simple_index):
Z
zhenwu 已提交
212 213
        '''
        target: test basic search fuction, all the search params is corrent, test all index params, and build
X
Xiaohai Xu 已提交
214 215
        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
Z
zhenwu 已提交
216
        '''
D
del-zhenwu 已提交
217
        top_k = 10
218 219 220
        index_param = get_simple_index["index_param"]
        index_type = get_simple_index["index_type"]
        logging.getLogger().info(get_simple_index)
D
del-zhenwu 已提交
221 222
        if index_type == IndexType.IVF_PQ:
            pytest.skip("Skip PQ")
X
Xiaohai Xu 已提交
223 224 225
        status = connect.create_partition(collection, tag)
        vectors, ids = self.init_data(connect, collection)
        status = connect.create_index(collection, index_type, index_param)
Z
zhenwu 已提交
226
        query_vec = [vectors[0]]
227
        search_param = get_search_param(index_type)
D
del-zhenwu 已提交
228
        status, result = connect.search(collection, top_k, query_vec, params=search_param)
Z
zhenwu 已提交
229 230
        logging.getLogger().info(result)
        assert status.OK()
D
del-zhenwu 已提交
231 232 233
        assert len(result[0]) == min(len(vectors), top_k)
        assert check_result(result[0], ids[0])
        assert result[0][0].distance <= epsilon
D
del-zhenwu 已提交
234
        status, result = connect.search(collection, top_k, query_vec, partition_tags=[tag], params=search_param)
Z
zhenwu 已提交
235 236 237 238
        logging.getLogger().info(result)
        assert status.OK()
        assert len(result) == 0

X
Xiaohai Xu 已提交
239
    def test_search_l2_index_params_partition_A(self, connect, collection, get_simple_index):
Z
zhenwu 已提交
240 241 242 243 244
        '''
        target: test basic search fuction, all the search params is corrent, test all index params, and build
        method: search partition with the given vectors, check the result
        expected: search status ok, and the length of the result is 0
        '''
D
del-zhenwu 已提交
245
        top_k = 10
246 247 248
        index_param = get_simple_index["index_param"]
        index_type = get_simple_index["index_type"]
        logging.getLogger().info(get_simple_index)
D
del-zhenwu 已提交
249 250 251
        if index_type == IndexType.IVF_PQ:
            pytest.skip("Skip PQ")

X
Xiaohai Xu 已提交
252 253 254
        status = connect.create_partition(collection, tag)
        vectors, ids = self.init_data(connect, collection)
        status = connect.create_index(collection, index_type, index_param)
Z
zhenwu 已提交
255
        query_vec = [vectors[0]]
256
        search_param = get_search_param(index_type)
D
del-zhenwu 已提交
257
        status, result = connect.search(collection, top_k, query_vec, partition_tags=[tag], params=search_param)
Z
zhenwu 已提交
258 259 260 261
        logging.getLogger().info(result)
        assert status.OK()
        assert len(result) == 0

X
Xiaohai Xu 已提交
262
    def test_search_l2_index_params_partition_B(self, connect, collection, get_simple_index):
Z
zhenwu 已提交
263 264 265 266 267
        '''
        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
        '''
D
del-zhenwu 已提交
268
        top_k = 10
269 270 271
        index_param = get_simple_index["index_param"]
        index_type = get_simple_index["index_type"]
        logging.getLogger().info(get_simple_index)
D
del-zhenwu 已提交
272 273
        if index_type == IndexType.IVF_PQ:
            pytest.skip("Skip PQ")
X
Xiaohai Xu 已提交
274 275 276
        status = connect.create_partition(collection, tag)
        vectors, ids = self.init_data(connect, collection, partition_tags=tag)
        status = connect.create_index(collection, index_type, index_param)
Z
zhenwu 已提交
277
        query_vec = [vectors[0]]
278
        search_param = get_search_param(index_type)
D
del-zhenwu 已提交
279
        status, result = connect.search(collection, top_k, query_vec, params=search_param)
Z
zhenwu 已提交
280 281
        logging.getLogger().info(result)
        assert status.OK()
D
del-zhenwu 已提交
282 283 284
        assert len(result[0]) == min(len(vectors), top_k)
        assert check_result(result[0], ids[0])
        assert result[0][0].distance <= epsilon
D
del-zhenwu 已提交
285
        status, result = connect.search(collection, top_k, query_vec, partition_tags=[tag], params=search_param)
Z
zhenwu 已提交
286 287
        logging.getLogger().info(result)
        assert status.OK()
D
del-zhenwu 已提交
288 289 290
        assert len(result[0]) == min(len(vectors), top_k)
        assert check_result(result[0], ids[0])
        assert result[0][0].distance <= epsilon
Z
zhenwu 已提交
291

X
Xiaohai Xu 已提交
292
    def test_search_l2_index_params_partition_C(self, connect, collection, get_simple_index):
Z
zhenwu 已提交
293 294
        '''
        target: test basic search fuction, all the search params is corrent, test all index params, and build
X
Xiaohai Xu 已提交
295
        method: search with the given vectors and tags (one of the tags not existed in collection), check the result
Z
zhenwu 已提交
296 297
        expected: search status ok, and the length of the result is top_k
        '''
298 299 300
        index_param = get_simple_index["index_param"]
        index_type = get_simple_index["index_type"]
        logging.getLogger().info(get_simple_index)
D
del-zhenwu 已提交
301 302
        if index_type == IndexType.IVF_PQ:
            pytest.skip("Skip PQ")
X
Xiaohai Xu 已提交
303 304 305
        status = connect.create_partition(collection, tag)
        vectors, ids = self.init_data(connect, collection, partition_tags=tag)
        status = connect.create_index(collection, index_type, index_param)
Z
zhenwu 已提交
306 307
        query_vec = [vectors[0]]
        top_k = 10
308
        search_param = get_search_param(index_type)
D
del-zhenwu 已提交
309
        status, result = connect.search(collection, top_k, query_vec, partition_tags=[tag, "new_tag"], params=search_param)
Z
zhenwu 已提交
310 311
        logging.getLogger().info(result)
        assert status.OK()
D
del-zhenwu 已提交
312 313 314
        assert len(result[0]) == min(len(vectors), top_k)
        assert check_result(result[0], ids[0])
        assert result[0][0].distance <= epsilon
Z
zhenwu 已提交
315

D
del-zhenwu 已提交
316
    @pytest.mark.level(2)
X
Xiaohai Xu 已提交
317
    def test_search_l2_index_params_partition_D(self, connect, collection, get_simple_index):
Z
zhenwu 已提交
318 319
        '''
        target: test basic search fuction, all the search params is corrent, test all index params, and build
X
Xiaohai Xu 已提交
320
        method: search with the given vectors and tag (tag name not existed in collection), check the result
Z
zhenwu 已提交
321 322
        expected: search status ok, and the length of the result is top_k
        '''
323 324 325
        index_param = get_simple_index["index_param"]
        index_type = get_simple_index["index_type"]
        logging.getLogger().info(get_simple_index)
X
Xiaohai Xu 已提交
326 327 328
        status = connect.create_partition(collection, tag)
        vectors, ids = self.init_data(connect, collection, partition_tags=tag)
        status = connect.create_index(collection, index_type, index_param)
Z
zhenwu 已提交
329 330
        query_vec = [vectors[0]]
        top_k = 10
331
        search_param = get_search_param(index_type)
D
del-zhenwu 已提交
332
        status, result = connect.search(collection, top_k, query_vec, partition_tags=["new_tag"], params=search_param)
Z
zhenwu 已提交
333
        logging.getLogger().info(result)
T
Tinkerrr 已提交
334
        assert not status.OK()
Z
zhenwu 已提交
335

D
del-zhenwu 已提交
336
    @pytest.mark.level(2)
X
Xiaohai Xu 已提交
337
    def test_search_l2_index_params_partition_E(self, connect, collection, get_simple_index):
Z
zhenwu 已提交
338 339
        '''
        target: test basic search fuction, all the search params is corrent, test all index params, and build
X
Xiaohai Xu 已提交
340
        method: search collection with the given vectors and tags, check the result
Z
zhenwu 已提交
341 342
        expected: search status ok, and the length of the result is top_k
        '''
D
del-zhenwu 已提交
343
        top_k = 10
Z
zhenwu 已提交
344
        new_tag = "new_tag"
345
        index_type = get_simple_index["index_type"]
D
del-zhenwu 已提交
346 347 348
        index_param = get_simple_index["index_param"]
        if index_type == IndexType.IVF_PQ:
            pytest.skip("Skip PQ")
349
        logging.getLogger().info(get_simple_index)
X
Xiaohai Xu 已提交
350 351 352 353 354
        status = connect.create_partition(collection, tag)
        status = connect.create_partition(collection, new_tag)
        vectors, ids = self.init_data(connect, collection, partition_tags=tag)
        new_vectors, new_ids = self.init_data(connect, collection, nb=6001, partition_tags=new_tag)
        status = connect.create_index(collection, index_type, index_param)
Z
zhenwu 已提交
355
        query_vec = [vectors[0], new_vectors[0]]
356
        search_param = get_search_param(index_type)
D
del-zhenwu 已提交
357
        status, result = connect.search(collection, top_k, query_vec, partition_tags=[tag, new_tag], params=search_param)
Z
zhenwu 已提交
358 359
        logging.getLogger().info(result)
        assert status.OK()
D
del-zhenwu 已提交
360 361 362 363 364
        assert len(result[0]) == min(len(vectors), top_k)
        assert check_result(result[0], ids[0])
        assert check_result(result[1], new_ids[0])
        assert result[0][0].distance <= epsilon
        assert result[1][0].distance <= epsilon
D
del-zhenwu 已提交
365
        status, result = connect.search(collection, top_k, query_vec, partition_tags=[new_tag], params=search_param)
Z
zhenwu 已提交
366 367
        logging.getLogger().info(result)
        assert status.OK()
D
del-zhenwu 已提交
368 369 370
        assert len(result[0]) == min(len(vectors), top_k)
        assert check_result(result[1], new_ids[0])
        assert result[1][0].distance <= epsilon
Z
zhenwu 已提交
371

X
Xiaohai Xu 已提交
372
    def test_search_l2_index_params_partition_F(self, connect, collection, get_simple_index):
Z
zhenwu 已提交
373 374
        '''
        target: test basic search fuction, all the search params is corrent, test all index params, and build
X
Xiaohai Xu 已提交
375
        method: search collection with the given vectors and tags with "re" expr, check the result
Z
zhenwu 已提交
376 377
        expected: search status ok, and the length of the result is top_k
        '''
Z
zhenwu 已提交
378
        tag = "atag"
Z
zhenwu 已提交
379
        new_tag = "new_tag"
380 381 382
        index_param = get_simple_index["index_param"]
        index_type = get_simple_index["index_type"]
        logging.getLogger().info(get_simple_index)
D
del-zhenwu 已提交
383 384
        if index_type == IndexType.IVF_PQ:
            pytest.skip("Skip PQ")
X
Xiaohai Xu 已提交
385 386 387 388 389
        status = connect.create_partition(collection, tag)
        status = connect.create_partition(collection, new_tag)
        vectors, ids = self.init_data(connect, collection, partition_tags=tag)
        new_vectors, new_ids = self.init_data(connect, collection, nb=6001, partition_tags=new_tag)
        status = connect.create_index(collection, index_type, index_param)
Z
zhenwu 已提交
390 391
        query_vec = [vectors[0], new_vectors[0]]
        top_k = 10
392
        search_param = get_search_param(index_type)
D
del-zhenwu 已提交
393
        status, result = connect.search(collection, top_k, query_vec, partition_tags=["new(.*)"], params=search_param)
Z
zhenwu 已提交
394 395
        logging.getLogger().info(result)
        assert status.OK()
D
del-zhenwu 已提交
396 397
        assert result[0][0].distance > epsilon
        assert result[1][0].distance <= epsilon
D
del-zhenwu 已提交
398
        status, result = connect.search(collection, top_k, query_vec, partition_tags=["(.*)tag"], params=search_param)
Z
zhenwu 已提交
399 400
        logging.getLogger().info(result)
        assert status.OK()
D
del-zhenwu 已提交
401 402
        assert result[0][0].distance <= epsilon
        assert result[1][0].distance <= epsilon
Z
zhenwu 已提交
403

D
del-zhenwu 已提交
404
    @pytest.mark.level(2)
X
Xiaohai Xu 已提交
405
    def test_search_ip_index_params(self, connect, ip_collection, get_simple_index):
J
JinHai-CN 已提交
406 407 408 409 410
        '''
        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
        '''
D
del-zhenwu 已提交
411
        top_k = 10
412 413 414
        index_param = get_simple_index["index_param"]
        index_type = get_simple_index["index_type"]
        logging.getLogger().info(get_simple_index)
D
del-zhenwu 已提交
415 416 417
        if index_type in [IndexType.RNSG, IndexType.IVF_PQ]:
            pytest.skip("rnsg not support in ip, skip pq")

X
Xiaohai Xu 已提交
418 419
        vectors, ids = self.init_data(connect, ip_collection)
        status = connect.create_index(ip_collection, index_type, index_param)
J
JinHai-CN 已提交
420
        query_vec = [vectors[0]]
421
        search_param = get_search_param(index_type)
D
del-zhenwu 已提交
422
        status, result = connect.search(ip_collection, top_k, query_vec, params=search_param)
J
JinHai-CN 已提交
423
        logging.getLogger().info(result)
D
del-zhenwu 已提交
424 425 426 427
        assert status.OK()
        assert len(result[0]) == min(len(vectors), top_k)
        assert check_result(result[0], ids[0])
        assert result[0][0].distance >= 1 - gen_inaccuracy(result[0][0].distance)
428

X
Xiaohai Xu 已提交
429
    def test_search_ip_large_nq_index_params(self, connect, ip_collection, get_simple_index):
430 431 432 433 434 435 436 437
        '''
        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"]
        logging.getLogger().info(get_simple_index)
D
del-zhenwu 已提交
438 439
        if index_type in [IndexType.RNSG, IndexType.IVF_PQ]:
            pytest.skip("rnsg not support in ip, skip pq")
X
Xiaohai Xu 已提交
440 441
        vectors, ids = self.init_data(connect, ip_collection)
        status = connect.create_index(ip_collection, index_type, index_param)
442 443 444 445 446
        query_vec = []
        for i in range (1200):
            query_vec.append(vectors[i])
        top_k = 10
        search_param = get_search_param(index_type)
D
del-zhenwu 已提交
447
        status, result = connect.search(ip_collection, top_k, query_vec, params=search_param)
448 449
        logging.getLogger().info(result)
        assert status.OK()
D
del-zhenwu 已提交
450 451 452
        assert len(result[0]) == min(len(vectors), top_k)
        assert check_result(result[0], ids[0])
        assert result[0][0].distance >= 1 - gen_inaccuracy(result[0][0].distance)
J
JinHai-CN 已提交
453

D
del-zhenwu 已提交
454
    @pytest.mark.level(2)
X
Xiaohai Xu 已提交
455
    def test_search_ip_index_params_partition(self, connect, ip_collection, get_simple_index):
Z
zhenwu 已提交
456 457 458 459 460
        '''
        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
        '''
D
del-zhenwu 已提交
461
        top_k = 10
462 463 464
        index_param = get_simple_index["index_param"]
        index_type = get_simple_index["index_type"]
        logging.getLogger().info(index_param)
D
del-zhenwu 已提交
465 466 467
        if index_type in [IndexType.RNSG, IndexType.IVF_PQ]:
            pytest.skip("rnsg not support in ip, skip pq")

X
Xiaohai Xu 已提交
468 469 470
        status = connect.create_partition(ip_collection, tag)
        vectors, ids = self.init_data(connect, ip_collection)
        status = connect.create_index(ip_collection, index_type, index_param)
Z
zhenwu 已提交
471
        query_vec = [vectors[0]]
472
        search_param = get_search_param(index_type)
D
del-zhenwu 已提交
473
        status, result = connect.search(ip_collection, top_k, query_vec, params=search_param)
Z
zhenwu 已提交
474 475
        logging.getLogger().info(result)
        assert status.OK()
D
del-zhenwu 已提交
476 477 478
        assert len(result[0]) == min(len(vectors), top_k)
        assert check_result(result[0], ids[0])
        assert result[0][0].distance >= 1 - gen_inaccuracy(result[0][0].distance)
D
del-zhenwu 已提交
479
        status, result = connect.search(ip_collection, top_k, query_vec, partition_tags=[tag], params=search_param)
Z
zhenwu 已提交
480 481 482 483
        logging.getLogger().info(result)
        assert status.OK()
        assert len(result) == 0

D
del-zhenwu 已提交
484
    @pytest.mark.level(2)
X
Xiaohai Xu 已提交
485
    def test_search_ip_index_params_partition_A(self, connect, ip_collection, get_simple_index):
Z
zhenwu 已提交
486 487 488 489 490
        '''
        target: test basic search fuction, all the search params is corrent, test all index params, and build
        method: search with the given vectors and tag, check the result
        expected: search status ok, and the length of the result is top_k
        '''
D
del-zhenwu 已提交
491
        top_k = 10
492 493 494
        index_param = get_simple_index["index_param"]
        index_type = get_simple_index["index_type"]
        logging.getLogger().info(index_param)
D
del-zhenwu 已提交
495 496 497
        if index_type in [IndexType.RNSG, IndexType.IVF_PQ]:
            pytest.skip("rnsg not support in ip, skip pq")

X
Xiaohai Xu 已提交
498 499 500
        status = connect.create_partition(ip_collection, tag)
        vectors, ids = self.init_data(connect, ip_collection, partition_tags=tag)
        status = connect.create_index(ip_collection, index_type, index_param)
Z
zhenwu 已提交
501
        query_vec = [vectors[0]]
502
        search_param = get_search_param(index_type)
D
del-zhenwu 已提交
503
        status, result = connect.search(ip_collection, top_k, query_vec, partition_tags=[tag], params=search_param)
Z
zhenwu 已提交
504 505
        logging.getLogger().info(result)
        assert status.OK()
D
del-zhenwu 已提交
506 507 508
        assert len(result[0]) == min(len(vectors), top_k)
        assert check_result(result[0], ids[0])
        assert result[0][0].distance >= 1 - gen_inaccuracy(result[0][0].distance)
Z
zhenwu 已提交
509

D
del-zhenwu 已提交
510 511 512 513 514 515 516 517 518 519 520
    @pytest.mark.level(2)
    def test_search_vectors_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_vectors = [vectors[0]]
        nprobe = 1
        with pytest.raises(Exception) as e:
            status, ids = dis_connect.search(collection, top_k, query_vectors)
J
JinHai-CN 已提交
521

X
Xiaohai Xu 已提交
522
    def test_search_collection_name_not_existed(self, connect, collection):
J
JinHai-CN 已提交
523
        '''
X
Xiaohai Xu 已提交
524 525
        target: search collection not existed
        method: search with the random collection_name, which is not in db
J
JinHai-CN 已提交
526 527
        expected: status not ok
        '''
X
Xiaohai Xu 已提交
528
        collection_name = gen_unique_str("not_existed_collection")
J
JinHai-CN 已提交
529 530
        nprobe = 1
        query_vecs = [vectors[0]]
D
del-zhenwu 已提交
531
        status, result = connect.search(collection_name, top_k, query_vecs)
J
JinHai-CN 已提交
532 533
        assert not status.OK()

X
Xiaohai Xu 已提交
534
    def test_search_collection_name_None(self, connect, collection):
J
JinHai-CN 已提交
535
        '''
X
Xiaohai Xu 已提交
536 537
        target: search collection that collection name is None
        method: search with the collection_name: None
J
JinHai-CN 已提交
538 539
        expected: status not ok
        '''
X
Xiaohai Xu 已提交
540
        collection_name = None
J
JinHai-CN 已提交
541 542 543
        nprobe = 1
        query_vecs = [vectors[0]]
        with pytest.raises(Exception) as e: 
D
del-zhenwu 已提交
544
            status, result = connect.search(collection_name, top_k, query_vecs)
J
JinHai-CN 已提交
545

X
Xiaohai Xu 已提交
546
    def test_search_top_k_query_records(self, connect, collection):
J
JinHai-CN 已提交
547 548 549 550 551 552
        '''
        target: test search fuction, with search params: query_records
        method: search with the given query_records, which are subarrays of the inserted vectors
        expected: status ok and the returned vectors should be query_records
        '''
        top_k = 10
X
Xiaohai Xu 已提交
553
        vectors, ids = self.init_data(connect, collection)
J
JinHai-CN 已提交
554
        query_vecs = [vectors[0],vectors[55],vectors[99]]
D
del-zhenwu 已提交
555
        status, result = connect.search(collection, top_k, query_vecs)
J
JinHai-CN 已提交
556 557 558 559 560 561
        assert status.OK()
        assert len(result) == len(query_vecs)
        for i in range(len(query_vecs)):
            assert len(result[i]) == top_k
            assert result[i][0].distance <= epsilon

X
Xiaohai Xu 已提交
562
    def test_search_distance_l2_flat_index(self, connect, collection):
J
JinHai-CN 已提交
563
        '''
X
Xiaohai Xu 已提交
564
        target: search collection, and check the result: distance
J
JinHai-CN 已提交
565 566 567 568
        method: compare the return distance value with value computed with Euclidean
        expected: the return distance equals to the computed value
        '''
        nb = 2
X
Xiaohai Xu 已提交
569
        vectors, ids = self.init_data(connect, collection, nb=nb)
J
JinHai-CN 已提交
570 571 572
        query_vecs = [[0.50 for i in range(dim)]]
        distance_0 = numpy.linalg.norm(numpy.array(query_vecs[0]) - numpy.array(vectors[0]))
        distance_1 = numpy.linalg.norm(numpy.array(query_vecs[0]) - numpy.array(vectors[1]))
D
del-zhenwu 已提交
573
        status, result = connect.search(collection, top_k, query_vecs)
J
JinHai-CN 已提交
574 575
        assert abs(numpy.sqrt(result[0][0].distance) - min(distance_0, distance_1)) <= gen_inaccuracy(result[0][0].distance)

X
Xiaohai Xu 已提交
576
    def test_search_distance_ip_flat_index(self, connect, ip_collection):
J
JinHai-CN 已提交
577
        '''
X
Xiaohai Xu 已提交
578
        target: search ip_collection, and check the result: distance
J
JinHai-CN 已提交
579 580 581 582 583
        method: compare the return distance value with value computed with Inner product
        expected: the return distance equals to the computed value
        '''
        nb = 2
        nprobe = 1
X
Xiaohai Xu 已提交
584
        vectors, ids = self.init_data(connect, ip_collection, nb=nb)
585 586
        index_type = IndexType.FLAT
        index_param = {
J
JinHai-CN 已提交
587 588
            "nlist": 16384
        }
X
Xiaohai Xu 已提交
589
        connect.create_index(ip_collection, index_type, index_param)
D
del-zhenwu 已提交
590
        logging.getLogger().info(connect.get_index_info(ip_collection))
J
JinHai-CN 已提交
591 592 593
        query_vecs = [[0.50 for i in range(dim)]]
        distance_0 = numpy.inner(numpy.array(query_vecs[0]), numpy.array(vectors[0]))
        distance_1 = numpy.inner(numpy.array(query_vecs[0]), numpy.array(vectors[1]))
594
        search_param = get_search_param(index_type)
D
del-zhenwu 已提交
595
        status, result = connect.search(ip_collection, top_k, query_vecs, params=search_param)
J
JinHai-CN 已提交
596 597
        assert abs(result[0][0].distance - max(distance_0, distance_1)) <= gen_inaccuracy(result[0][0].distance)

X
Xiaohai Xu 已提交
598
    def test_search_distance_jaccard_flat_index(self, connect, jac_collection):
G
groot 已提交
599
        '''
X
Xiaohai Xu 已提交
600
        target: search ip_collection, and check the result: distance
G
groot 已提交
601 602 603 604 605
        method: compare the return distance value with value computed with Inner product
        expected: the return distance equals to the computed value
        '''
        # from scipy.spatial import distance
        nprobe = 512
X
Xiaohai Xu 已提交
606
        int_vectors, vectors, ids = self.init_binary_data(connect, jac_collection, nb=2)
607 608
        index_type = IndexType.FLAT
        index_param = {
G
groot 已提交
609 610
            "nlist": 16384
        }
X
Xiaohai Xu 已提交
611
        connect.create_index(jac_collection, index_type, index_param)
D
del-zhenwu 已提交
612 613
        logging.getLogger().info(connect.get_collection_info(jac_collection))
        logging.getLogger().info(connect.get_index_info(jac_collection))
X
Xiaohai Xu 已提交
614
        query_int_vectors, query_vecs, tmp_ids = self.init_binary_data(connect, jac_collection, nb=1, insert=False)
G
groot 已提交
615 616
        distance_0 = jaccard(query_int_vectors[0], int_vectors[0])
        distance_1 = jaccard(query_int_vectors[0], int_vectors[1])
617
        search_param = get_search_param(index_type)
D
del-zhenwu 已提交
618
        status, result = connect.search(jac_collection, top_k, query_vecs, params=search_param)
G
groot 已提交
619 620 621 622
        logging.getLogger().info(status)
        logging.getLogger().info(result)
        assert abs(result[0][0].distance - min(distance_0, distance_1)) <= epsilon

X
Xiaohai Xu 已提交
623
    def test_search_distance_hamming_flat_index(self, connect, ham_collection):
G
groot 已提交
624
        '''
X
Xiaohai Xu 已提交
625
        target: search ip_collection, and check the result: distance
G
groot 已提交
626 627 628 629 630
        method: compare the return distance value with value computed with Inner product
        expected: the return distance equals to the computed value
        '''
        # from scipy.spatial import distance
        nprobe = 512
X
Xiaohai Xu 已提交
631
        int_vectors, vectors, ids = self.init_binary_data(connect, ham_collection, nb=2)
632 633
        index_type = IndexType.FLAT
        index_param = {
G
groot 已提交
634 635
            "nlist": 16384
        }
X
Xiaohai Xu 已提交
636
        connect.create_index(ham_collection, index_type, index_param)
D
del-zhenwu 已提交
637 638
        logging.getLogger().info(connect.get_collection_info(ham_collection))
        logging.getLogger().info(connect.get_index_info(ham_collection))
X
Xiaohai Xu 已提交
639
        query_int_vectors, query_vecs, tmp_ids = self.init_binary_data(connect, ham_collection, nb=1, insert=False)
G
groot 已提交
640 641
        distance_0 = hamming(query_int_vectors[0], int_vectors[0])
        distance_1 = hamming(query_int_vectors[0], int_vectors[1])
642
        search_param = get_search_param(index_type)
D
del-zhenwu 已提交
643
        status, result = connect.search(ham_collection, top_k, query_vecs, params=search_param)
G
groot 已提交
644 645 646 647
        logging.getLogger().info(status)
        logging.getLogger().info(result)
        assert abs(result[0][0].distance - min(distance_0, distance_1).astype(float)) <= epsilon

D
del-zhenwu 已提交
648 649 650 651 652 653 654 655 656 657 658 659 660 661
    def test_search_distance_substructure_flat_index(self, connect, substructure_collection):
        '''
        target: search ip_collection, and check the result: distance
        method: compare the return distance value with value computed with Inner product
        expected: the return distance equals to the computed value
        '''
        # from scipy.spatial import distance
        nprobe = 512
        int_vectors, vectors, ids = self.init_binary_data(connect, substructure_collection, nb=2)
        index_type = IndexType.FLAT
        index_param = {
            "nlist": 16384
        }
        connect.create_index(substructure_collection, index_type, index_param)
D
del-zhenwu 已提交
662 663
        logging.getLogger().info(connect.get_collection_info(substructure_collection))
        logging.getLogger().info(connect.get_index_info(substructure_collection))
D
del-zhenwu 已提交
664 665 666 667
        query_int_vectors, query_vecs, tmp_ids = self.init_binary_data(connect, substructure_collection, nb=1, insert=False)
        distance_0 = substructure(query_int_vectors[0], int_vectors[0])
        distance_1 = substructure(query_int_vectors[0], int_vectors[1])
        search_param = get_search_param(index_type)
D
del-zhenwu 已提交
668
        status, result = connect.search(substructure_collection, top_k, query_vecs, params=search_param)
D
del-zhenwu 已提交
669 670
        logging.getLogger().info(status)
        logging.getLogger().info(result)
D
del-zhenwu 已提交
671
        assert len(result[0]) == 0
672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687

    def test_search_distance_substructure_flat_index_B(self, connect, substructure_collection):
        '''
        target: search ip_collection, and check the result: distance
        method: compare the return distance value with value computed with SUB 
        expected: the return distance equals to the computed value
        '''
        # from scipy.spatial import distance
        top_k = 3
        nprobe = 512
        int_vectors, vectors, ids = self.init_binary_data(connect, substructure_collection, nb=2)
        index_type = IndexType.FLAT
        index_param = {
            "nlist": 16384
        }
        connect.create_index(substructure_collection, index_type, index_param)
D
del-zhenwu 已提交
688 689
        logging.getLogger().info(connect.get_collection_info(substructure_collection))
        logging.getLogger().info(connect.get_index_info(substructure_collection))
690 691
        query_int_vectors, query_vecs = gen_binary_sub_vectors(int_vectors, 2)
        search_param = get_search_param(index_type)
D
del-zhenwu 已提交
692
        status, result = connect.search(substructure_collection, top_k, query_vecs, params=search_param)
693 694
        logging.getLogger().info(status)
        logging.getLogger().info(result) 
D
del-zhenwu 已提交
695 696
        assert len(result[0]) == 1
        assert len(result[1]) == 1
697 698 699 700
        assert result[0][0].distance <= epsilon
        assert result[0][0].id == ids[0]
        assert result[1][0].distance <= epsilon
        assert result[1][0].id == ids[1]
D
del-zhenwu 已提交
701 702 703 704 705 706 707 708 709 710 711 712 713 714 715

    def test_search_distance_superstructure_flat_index(self, connect, superstructure_collection):
        '''
        target: search ip_collection, and check the result: distance
        method: compare the return distance value with value computed with Inner product
        expected: the return distance equals to the computed value
        '''
        # from scipy.spatial import distance
        nprobe = 512
        int_vectors, vectors, ids = self.init_binary_data(connect, superstructure_collection, nb=2)
        index_type = IndexType.FLAT
        index_param = {
            "nlist": 16384
        }
        connect.create_index(superstructure_collection, index_type, index_param)
D
del-zhenwu 已提交
716 717
        logging.getLogger().info(connect.get_collection_info(superstructure_collection))
        logging.getLogger().info(connect.get_index_info(superstructure_collection))
D
del-zhenwu 已提交
718 719 720 721
        query_int_vectors, query_vecs, tmp_ids = self.init_binary_data(connect, superstructure_collection, nb=1, insert=False)
        distance_0 = superstructure(query_int_vectors[0], int_vectors[0])
        distance_1 = superstructure(query_int_vectors[0], int_vectors[1])
        search_param = get_search_param(index_type)
D
del-zhenwu 已提交
722
        status, result = connect.search(superstructure_collection, top_k, query_vecs, params=search_param)
D
del-zhenwu 已提交
723 724
        logging.getLogger().info(status)
        logging.getLogger().info(result)
D
del-zhenwu 已提交
725
        assert len(result[0]) == 0
726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741

    def test_search_distance_superstructure_flat_index_B(self, connect, superstructure_collection):
        '''
        target: search ip_collection, and check the result: distance
        method: compare the return distance value with value computed with SUPER
        expected: the return distance equals to the computed value
        '''
        # from scipy.spatial import distance
        top_k = 3
        nprobe = 512
        int_vectors, vectors, ids = self.init_binary_data(connect, superstructure_collection, nb=2)
        index_type = IndexType.FLAT
        index_param = {
            "nlist": 16384
        }
        connect.create_index(superstructure_collection, index_type, index_param)
D
del-zhenwu 已提交
742 743
        logging.getLogger().info(connect.get_collection_info(superstructure_collection))
        logging.getLogger().info(connect.get_index_info(superstructure_collection))
744 745
        query_int_vectors, query_vecs = gen_binary_super_vectors(int_vectors, 2)
        search_param = get_search_param(index_type)
D
del-zhenwu 已提交
746
        status, result = connect.search(superstructure_collection, top_k, query_vecs, params=search_param)
747 748
        logging.getLogger().info(status)
        logging.getLogger().info(result)
D
del-zhenwu 已提交
749 750
        assert len(result[0]) == 2
        assert len(result[1]) == 2
751 752 753 754
        assert result[0][0].id in ids
        assert result[0][0].distance <= epsilon
        assert result[1][0].id in ids
        assert result[1][0].distance <= epsilon
D
del-zhenwu 已提交
755

X
Xiaohai Xu 已提交
756
    def test_search_distance_tanimoto_flat_index(self, connect, tanimoto_collection):
G
groot 已提交
757
        '''
X
Xiaohai Xu 已提交
758
        target: search ip_collection, and check the result: distance
G
groot 已提交
759 760 761 762 763
        method: compare the return distance value with value computed with Inner product
        expected: the return distance equals to the computed value
        '''
        # from scipy.spatial import distance
        nprobe = 512
X
Xiaohai Xu 已提交
764
        int_vectors, vectors, ids = self.init_binary_data(connect, tanimoto_collection, nb=2)
765 766
        index_type = IndexType.FLAT
        index_param = {
G
groot 已提交
767 768
            "nlist": 16384
        }
X
Xiaohai Xu 已提交
769
        connect.create_index(tanimoto_collection, index_type, index_param)
D
del-zhenwu 已提交
770 771
        logging.getLogger().info(connect.get_collection_info(tanimoto_collection))
        logging.getLogger().info(connect.get_index_info(tanimoto_collection))
X
Xiaohai Xu 已提交
772
        query_int_vectors, query_vecs, tmp_ids = self.init_binary_data(connect, tanimoto_collection, nb=1, insert=False)
G
groot 已提交
773 774
        distance_0 = tanimoto(query_int_vectors[0], int_vectors[0])
        distance_1 = tanimoto(query_int_vectors[0], int_vectors[1])
775
        search_param = get_search_param(index_type)
D
del-zhenwu 已提交
776
        status, result = connect.search(tanimoto_collection, top_k, query_vecs, params=search_param)
G
groot 已提交
777 778 779 780
        logging.getLogger().info(status)
        logging.getLogger().info(result)
        assert abs(result[0][0].distance - min(distance_0, distance_1)) <= epsilon

X
Xiaohai Xu 已提交
781
    def test_search_distance_ip_index_params(self, connect, ip_collection, get_index):
J
JinHai-CN 已提交
782
        '''
X
Xiaohai Xu 已提交
783
        target: search collection, and check the result: distance
J
JinHai-CN 已提交
784 785 786 787 788
        method: compare the return distance value with value computed with Inner product
        expected: the return distance equals to the computed value
        '''
        top_k = 2
        nprobe = 1
789 790
        index_param = get_index["index_param"]
        index_type = get_index["index_type"]
D
del-zhenwu 已提交
791 792 793
        if index_type == IndexType.RNSG:
            pytest.skip("rnsg not support in ip")
        vectors, ids = self.init_data(connect, ip_collection, nb=2)
X
Xiaohai Xu 已提交
794
        connect.create_index(ip_collection, index_type, index_param)
D
del-zhenwu 已提交
795
        logging.getLogger().info(connect.get_index_info(ip_collection))
J
JinHai-CN 已提交
796
        query_vecs = [[0.50 for i in range(dim)]]
797
        search_param = get_search_param(index_type)
D
del-zhenwu 已提交
798
        status, result = connect.search(ip_collection, top_k, query_vecs, params=search_param)
G
groot 已提交
799 800
        logging.getLogger().debug(status)
        logging.getLogger().debug(result)
J
JinHai-CN 已提交
801 802 803 804 805 806 807
        distance_0 = numpy.inner(numpy.array(query_vecs[0]), numpy.array(vectors[0]))
        distance_1 = numpy.inner(numpy.array(query_vecs[0]), numpy.array(vectors[1]))
        assert abs(result[0][0].distance - max(distance_0, distance_1)) <= gen_inaccuracy(result[0][0].distance)

    # TODO: enable
    # @pytest.mark.repeat(5)
    @pytest.mark.timeout(30)
X
Xiaohai Xu 已提交
808 809
    def _test_search_concurrent(self, connect, collection):
        vectors, ids = self.init_data(connect, collection)
J
JinHai-CN 已提交
810 811 812 813 814 815
        thread_num = 10
        nb = 100
        top_k = 10
        threads = []
        query_vecs = vectors[nb//2:nb]
        def search():
D
del-zhenwu 已提交
816
            status, result = connect.search(collection, top_k, query_vecs)
J
JinHai-CN 已提交
817 818 819 820 821 822 823 824 825 826 827
            assert len(result) == len(query_vecs)
            for i in range(len(query_vecs)):
                assert result[i][0].id in ids
                assert result[i][0].distance == 0.0
        for i in range(thread_num):
            x = threading.Thread(target=search, args=())
            threads.append(x)
            x.start()
        for th in threads:
            th.join()

D
del-zhenwu 已提交
828
    @pytest.mark.level(2)
829 830 831 832 833 834 835 836 837 838 839
    @pytest.mark.timeout(30)
    def test_search_concurrent_multithreads(self, args):
        '''
        target: test concurrent search with multiprocessess
        method: search with 10 processes, each process uses dependent connection
        expected: status ok and the returned vectors should be query_records
        '''
        nb = 100
        top_k = 10
        threads_num = 4
        threads = []
X
Xiaohai Xu 已提交
840
        collection = gen_unique_str("test_search_concurrent_multiprocessing")
841
        uri = "tcp://%s:%s" % (args["ip"], args["port"])
X
Xiaohai Xu 已提交
842
        param = {'collection_name': collection,
843 844 845
                 'dimension': dim,
                 'index_type': IndexType.FLAT,
                 'store_raw_vector': False}
X
Xiaohai Xu 已提交
846
        # create collection
D
del-zhenwu 已提交
847
        milvus = get_milvus(args["ip"], args["port"], handler=args["handler"])
X
Xiaohai Xu 已提交
848 849
        milvus.create_collection(param)
        vectors, ids = self.init_data(milvus, collection, nb=nb)
850 851
        query_vecs = vectors[nb//2:nb]
        def search(milvus):
D
del-zhenwu 已提交
852
            status, result = milvus.search(collection, top_k, query_vecs)
853 854 855 856 857 858
            assert len(result) == len(query_vecs)
            for i in range(len(query_vecs)):
                assert result[i][0].id in ids
                assert result[i][0].distance == 0.0

        for i in range(threads_num):
D
del-zhenwu 已提交
859
            milvus = get_milvus(args["ip"], args["port"], handler=args["handler"])
860 861 862 863 864 865 866
            t = threading.Thread(target=search, args=(milvus, ))
            threads.append(t)
            t.start()
            time.sleep(0.2)
        for t in threads:
            t.join()

J
JinHai-CN 已提交
867 868 869 870 871 872 873 874 875 876 877 878
    # TODO: enable
    @pytest.mark.timeout(30)
    def _test_search_concurrent_multiprocessing(self, args):
        '''
        target: test concurrent search with multiprocessess
        method: search with 10 processes, each process uses dependent connection
        expected: status ok and the returned vectors should be query_records
        '''
        nb = 100
        top_k = 10
        process_num = 4
        processes = []
X
Xiaohai Xu 已提交
879
        collection = gen_unique_str("test_search_concurrent_multiprocessing")
J
JinHai-CN 已提交
880
        uri = "tcp://%s:%s" % (args["ip"], args["port"])
X
Xiaohai Xu 已提交
881
        param = {'collection_name': collection,
J
JinHai-CN 已提交
882 883 884
             'dimension': dim,
             'index_type': IndexType.FLAT,
             'store_raw_vector': False}
X
Xiaohai Xu 已提交
885
        # create collection
D
del-zhenwu 已提交
886
        milvus = get_milvus(args["ip"], args["port"], handler=args["handler"])
X
Xiaohai Xu 已提交
887 888
        milvus.create_collection(param)
        vectors, ids = self.init_data(milvus, collection, nb=nb)
J
JinHai-CN 已提交
889 890
        query_vecs = vectors[nb//2:nb]
        def search(milvus):
D
del-zhenwu 已提交
891
            status, result = milvus.search(collection, top_k, query_vecs)
J
JinHai-CN 已提交
892 893 894 895 896 897
            assert len(result) == len(query_vecs)
            for i in range(len(query_vecs)):
                assert result[i][0].id in ids
                assert result[i][0].distance == 0.0

        for i in range(process_num):
D
del-zhenwu 已提交
898
            milvus = get_milvus(args["ip"], args["port"], handler=args["handler"])
J
JinHai-CN 已提交
899 900 901 902 903 904 905
            p = Process(target=search, args=(milvus, ))
            processes.append(p)
            p.start()
            time.sleep(0.2)
        for p in processes:
            p.join()

X
Xiaohai Xu 已提交
906
    def test_search_multi_collection_L2(search, args):
J
JinHai-CN 已提交
907
        '''
X
Xiaohai Xu 已提交
908 909
        target: test search multi collections of L2
        method: add vectors into 10 collections, and search
J
JinHai-CN 已提交
910 911 912 913
        expected: search status ok, the length of result
        '''
        num = 10
        top_k = 10
X
Xiaohai Xu 已提交
914
        collections = []
J
JinHai-CN 已提交
915 916
        idx = []
        for i in range(num):
X
Xiaohai Xu 已提交
917
            collection = gen_unique_str("test_add_multicollection_%d" % i)
J
JinHai-CN 已提交
918
            uri = "tcp://%s:%s" % (args["ip"], args["port"])
X
Xiaohai Xu 已提交
919
            param = {'collection_name': collection,
J
JinHai-CN 已提交
920 921 922
                     'dimension': dim,
                     'index_file_size': 10,
                     'metric_type': MetricType.L2}
X
Xiaohai Xu 已提交
923
            # create collection
D
del-zhenwu 已提交
924
            milvus = get_milvus(args["ip"], args["port"], handler=args["handler"])
X
Xiaohai Xu 已提交
925
            milvus.create_collection(param)
D
del-zhenwu 已提交
926
            status, ids = milvus.insert(collection, vectors)
J
JinHai-CN 已提交
927 928
            assert status.OK()
            assert len(ids) == len(vectors)
X
Xiaohai Xu 已提交
929
            collections.append(collection)
J
JinHai-CN 已提交
930 931 932
            idx.append(ids[0])
            idx.append(ids[10])
            idx.append(ids[20])
933
            milvus.flush([collection])
J
JinHai-CN 已提交
934
        query_vecs = [vectors[0], vectors[10], vectors[20]]
X
Xiaohai Xu 已提交
935
        # start query from random collection
J
JinHai-CN 已提交
936
        for i in range(num):
X
Xiaohai Xu 已提交
937
            collection = collections[i]
D
del-zhenwu 已提交
938
            status, result = milvus.search(collection, top_k, query_vecs)
J
JinHai-CN 已提交
939 940 941 942 943 944 945
            assert status.OK()
            assert len(result) == len(query_vecs)
            for j in range(len(query_vecs)):
                assert len(result[j]) == top_k
            for j in range(len(query_vecs)):
                assert check_result(result[j], idx[3 * i + j])

X
Xiaohai Xu 已提交
946
    def test_search_multi_collection_IP(search, args):
J
JinHai-CN 已提交
947
        '''
X
Xiaohai Xu 已提交
948 949
        target: test search multi collections of IP
        method: add vectors into 10 collections, and search
J
JinHai-CN 已提交
950 951 952 953
        expected: search status ok, the length of result
        '''
        num = 10
        top_k = 10
X
Xiaohai Xu 已提交
954
        collections = []
J
JinHai-CN 已提交
955 956
        idx = []
        for i in range(num):
X
Xiaohai Xu 已提交
957
            collection = gen_unique_str("test_add_multicollection_%d" % i)
J
JinHai-CN 已提交
958
            uri = "tcp://%s:%s" % (args["ip"], args["port"])
X
Xiaohai Xu 已提交
959
            param = {'collection_name': collection,
J
JinHai-CN 已提交
960 961 962
                     'dimension': dim,
                     'index_file_size': 10,
                     'metric_type': MetricType.L2}
X
Xiaohai Xu 已提交
963
            # create collection
D
del-zhenwu 已提交
964
            milvus = get_milvus(args["ip"], args["port"], handler=args["handler"])
X
Xiaohai Xu 已提交
965
            milvus.create_collection(param)
D
del-zhenwu 已提交
966
            status, ids = milvus.insert(collection, vectors)
J
JinHai-CN 已提交
967 968
            assert status.OK()
            assert len(ids) == len(vectors)
X
Xiaohai Xu 已提交
969
            collections.append(collection)
J
JinHai-CN 已提交
970 971 972
            idx.append(ids[0])
            idx.append(ids[10])
            idx.append(ids[20])
973
            milvus.flush([collection])
J
JinHai-CN 已提交
974
        query_vecs = [vectors[0], vectors[10], vectors[20]]
X
Xiaohai Xu 已提交
975
        # start query from random collection
J
JinHai-CN 已提交
976
        for i in range(num):
X
Xiaohai Xu 已提交
977
            collection = collections[i]
D
del-zhenwu 已提交
978
            status, result = milvus.search(collection, top_k, query_vecs)
J
JinHai-CN 已提交
979 980 981 982 983 984 985 986 987
            assert status.OK()
            assert len(result) == len(query_vecs)
            for j in range(len(query_vecs)):
                assert len(result[j]) == top_k
            for j in range(len(query_vecs)):
                assert check_result(result[j], idx[3 * i + j])
"""
******************************************************************
#  The following cases are used to test `search_vectors` function 
X
Xiaohai Xu 已提交
988
#  with invalid collection_name top-k / nprobe / query_range
J
JinHai-CN 已提交
989 990 991 992
******************************************************************
"""

class TestSearchParamsInvalid(object):
Z
zhenwu 已提交
993
    nlist = 16384
994 995
    index_type = IndexType.IVF_SQ8
    index_param = {"nlist": nlist}
Z
zhenwu 已提交
996
    logging.getLogger().info(index_param)
J
JinHai-CN 已提交
997

X
Xiaohai Xu 已提交
998
    def init_data(self, connect, collection, nb=6000):
J
JinHai-CN 已提交
999
        '''
X
Xiaohai Xu 已提交
1000
        Generate vectors and add it in collection, before search vectors
J
JinHai-CN 已提交
1001 1002
        '''
        global vectors
G
groot 已提交
1003
        if nb == 6000:
D
del-zhenwu 已提交
1004
            insert = vectors
J
JinHai-CN 已提交
1005
        else:  
D
del-zhenwu 已提交
1006 1007
            insert = gen_vectors(nb, dim)
        status, ids = connect.insert(collection, insert)
J
JinHai-CN 已提交
1008
        sleep(add_interval_time)
D
del-zhenwu 已提交
1009
        return insert, ids
J
JinHai-CN 已提交
1010 1011

    """
X
Xiaohai Xu 已提交
1012
    Test search collection with invalid collection names
J
JinHai-CN 已提交
1013 1014 1015
    """
    @pytest.fixture(
        scope="function",
X
Xiaohai Xu 已提交
1016
        params=gen_invalid_collection_names()
J
JinHai-CN 已提交
1017
    )
X
Xiaohai Xu 已提交
1018
    def get_collection_name(self, request):
J
JinHai-CN 已提交
1019 1020 1021
        yield request.param

    @pytest.mark.level(2)
X
Xiaohai Xu 已提交
1022 1023 1024
    def test_search_with_invalid_collectionname(self, connect, get_collection_name):
        collection_name = get_collection_name
        logging.getLogger().info(collection_name)
J
JinHai-CN 已提交
1025 1026
        nprobe = 1 
        query_vecs = gen_vectors(1, dim)
D
del-zhenwu 已提交
1027
        status, result = connect.search(collection_name, top_k, query_vecs)
J
JinHai-CN 已提交
1028 1029
        assert not status.OK()

Z
zhenwu 已提交
1030
    @pytest.mark.level(1)
X
Xiaohai Xu 已提交
1031
    def test_search_with_invalid_tag_format(self, connect, collection):
Z
zhenwu 已提交
1032 1033 1034
        nprobe = 1 
        query_vecs = gen_vectors(1, dim)
        with pytest.raises(Exception) as e:
D
del-zhenwu 已提交
1035
            status, result = connect.search(collection, top_k, query_vecs, partition_tags="tag")
1036 1037 1038 1039 1040 1041
            logging.getLogger().debug(result)

    @pytest.mark.level(1)
    def test_search_with_tag_not_existed(self, connect, collection):
        nprobe = 1
        query_vecs = gen_vectors(1, dim)
D
del-zhenwu 已提交
1042
        status, result = connect.search(collection, top_k, query_vecs, partition_tags=["tag"])
1043 1044
        logging.getLogger().info(result)
        assert not status.OK()
Z
zhenwu 已提交
1045

J
JinHai-CN 已提交
1046
    """
X
Xiaohai Xu 已提交
1047
    Test search collection with invalid top-k
J
JinHai-CN 已提交
1048 1049 1050 1051 1052 1053 1054 1055
    """
    @pytest.fixture(
        scope="function",
        params=gen_invalid_top_ks()
    )
    def get_top_k(self, request):
        yield request.param

Z
zhenwu 已提交
1056
    @pytest.mark.level(1)
X
Xiaohai Xu 已提交
1057
    def test_search_with_invalid_top_k(self, connect, collection, get_top_k):
J
JinHai-CN 已提交
1058 1059 1060 1061 1062 1063 1064 1065 1066
        '''
        target: test search fuction, with the wrong top_k
        method: search with top_k
        expected: raise an error, and the connection is normal
        '''
        top_k = get_top_k
        logging.getLogger().info(top_k)
        nprobe = 1
        query_vecs = gen_vectors(1, dim)
Z
zhenwu 已提交
1067
        if isinstance(top_k, int):
D
del-zhenwu 已提交
1068
            status, result = connect.search(collection, top_k, query_vecs)
Z
zhenwu 已提交
1069 1070 1071
            assert not status.OK()
        else:
            with pytest.raises(Exception) as e:
D
del-zhenwu 已提交
1072
                status, result = connect.search(collection, top_k, query_vecs)
J
JinHai-CN 已提交
1073 1074

    @pytest.mark.level(2)
X
Xiaohai Xu 已提交
1075
    def test_search_with_invalid_top_k_ip(self, connect, ip_collection, get_top_k):
J
JinHai-CN 已提交
1076 1077 1078 1079 1080 1081 1082 1083 1084
        '''
        target: test search fuction, with the wrong top_k
        method: search with top_k
        expected: raise an error, and the connection is normal
        '''
        top_k = get_top_k
        logging.getLogger().info(top_k)
        nprobe = 1
        query_vecs = gen_vectors(1, dim)
Z
zhenwu 已提交
1085
        if isinstance(top_k, int):
D
del-zhenwu 已提交
1086
            status, result = connect.search(ip_collection, top_k, query_vecs)
Z
zhenwu 已提交
1087 1088 1089
            assert not status.OK()
        else:
            with pytest.raises(Exception) as e:
D
del-zhenwu 已提交
1090
                status, result = connect.search(ip_collection, top_k, query_vecs)
J
JinHai-CN 已提交
1091
    """
X
Xiaohai Xu 已提交
1092
    Test search collection with invalid nprobe
J
JinHai-CN 已提交
1093 1094 1095 1096 1097 1098 1099 1100
    """
    @pytest.fixture(
        scope="function",
        params=gen_invalid_nprobes()
    )
    def get_nprobes(self, request):
        yield request.param

Z
zhenwu 已提交
1101
    @pytest.mark.level(1)
X
Xiaohai Xu 已提交
1102
    def test_search_with_invalid_nprobe(self, connect, collection, get_nprobes):
J
JinHai-CN 已提交
1103
        '''
1104 1105
        target: test search fuction, with the wrong nprobe
        method: search with nprobe
J
JinHai-CN 已提交
1106 1107
        expected: raise an error, and the connection is normal
        '''
1108 1109
        index_type = IndexType.IVF_SQ8
        index_param = {"nlist": 16384}
X
Xiaohai Xu 已提交
1110
        connect.create_index(collection, index_type, index_param)
J
JinHai-CN 已提交
1111
        nprobe = get_nprobes
1112
        search_param = {"nprobe": nprobe}
J
JinHai-CN 已提交
1113 1114
        logging.getLogger().info(nprobe)
        query_vecs = gen_vectors(1, dim)
1115
        # if isinstance(nprobe, int):
D
del-zhenwu 已提交
1116
        status, result = connect.search(collection, top_k, query_vecs, params=search_param)
1117 1118 1119
        assert not status.OK()
        # else:
        #     with pytest.raises(Exception) as e:
D
del-zhenwu 已提交
1120
        #         status, result = connect.search(collection, top_k, query_vecs, params=search_param)
J
JinHai-CN 已提交
1121 1122

    @pytest.mark.level(2)
X
Xiaohai Xu 已提交
1123
    def test_search_with_invalid_nprobe_ip(self, connect, ip_collection, get_nprobes):
J
JinHai-CN 已提交
1124 1125 1126 1127 1128
        '''
        target: test search fuction, with the wrong top_k
        method: search with top_k
        expected: raise an error, and the connection is normal
        '''
1129 1130
        index_type = IndexType.IVF_SQ8
        index_param = {"nlist": 16384}
X
Xiaohai Xu 已提交
1131
        connect.create_index(ip_collection, index_type, index_param)
J
JinHai-CN 已提交
1132
        nprobe = get_nprobes
1133
        search_param = {"nprobe": nprobe}
J
JinHai-CN 已提交
1134 1135
        logging.getLogger().info(nprobe)
        query_vecs = gen_vectors(1, dim)
1136 1137

        # if isinstance(nprobe, int):
D
del-zhenwu 已提交
1138
        status, result = connect.search(ip_collection, top_k, query_vecs, params=search_param)
1139 1140 1141
        assert not status.OK()
        # else:
        #     with pytest.raises(Exception) as e:
D
del-zhenwu 已提交
1142
        #         status, result = connect.search(ip_collection, top_k, query_vecs, params=search_param)
1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156

    @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

1157
    def test_search_with_empty_params(self, connect, collection, args, get_simple_index):
1158 1159 1160 1161 1162
        '''
        target: test search fuction, with empty search params
        method: search with params
        expected: search status not ok, and the connection is normal
        '''
1163 1164
        if args["handler"] == "HTTP":
            pytest.skip("skip in http mode")
1165 1166
        index_type = get_simple_index["index_type"]
        index_param = get_simple_index["index_param"]
X
Xiaohai Xu 已提交
1167
        connect.create_index(collection, index_type, index_param)
1168
        query_vecs = gen_vectors(1, dim)
D
del-zhenwu 已提交
1169
        status, result = connect.search(collection, top_k, query_vecs, params={})
1170 1171 1172

        if index_type == IndexType.FLAT:
            assert status.OK()
J
JinHai-CN 已提交
1173
        else:
1174 1175 1176 1177 1178 1179
            assert not status.OK()

    @pytest.fixture(
        scope="function",
        params=gen_invaild_search_params()
    )
D
del-zhenwu 已提交
1180
    def get_invalid_search_param(self, request, connect):
1181 1182 1183 1184 1185 1186 1187 1188
        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

D
del-zhenwu 已提交
1189
    def test_search_with_invalid_params(self, connect, collection, get_invalid_search_param):
1190 1191 1192 1193 1194
        '''
        target: test search fuction, with invalid search params
        method: search with params
        expected: search status not ok, and the connection is normal
        '''
D
del-zhenwu 已提交
1195 1196 1197 1198 1199
        index_type = get_invalid_search_param["index_type"]
        search_param = get_invalid_search_param["search_param"]
        for index in gen_simple_index():
            if index_type == index["index_type"]:
                connect.create_index(collection, index_type, index["index_param"])
1200
        query_vecs = gen_vectors(1, dim)
D
del-zhenwu 已提交
1201
        status, result = connect.search(collection, top_k, query_vecs, params=search_param)
1202
        assert not status.OK()
J
JinHai-CN 已提交
1203 1204 1205 1206 1207

def check_result(result, id):
    if len(result) >= 5:
        return id in [result[0].id, result[1].id, result[2].id, result[3].id, result[4].id]
    else:
1208
        return id in (i.id for i in result)