def add_vectors(self): vectors = Prepare.records([[random.random() for _ in range(256)] for _ in range(20)]) status, ids = self.milvus.add_vectors( table_name=self.table_name_exists, records=vectors) assert status.OK() assert isinstance(ids, list)
def test_table_schema(self): param = { 'table_name': fake.table_name(), 'dimension': random.randint(0, 999), 'index_file_size': 1024, 'metric_type': MetricType.L2 } res = Prepare.table_schema(param) assert isinstance(res, milvus_pb2.TableSchema)
def test_search_vectors(self): q_records = Prepare.records([[random.random() for _ in range(256)] for _ in range(2)]) sta, results = self.milvus.search_vectors( table_name=self.table_name_exists, query_records=q_records, top_k=10) pprint(results) assert sta.OK() assert isinstance(results, (TopKQueryResult, list))
def main(): milvus = Milvus() milvus.connect(host=_HOST, port=_PORT) # # table_name = 'test_search_in_file' # dimension = 256 # vectors = Prepare.records([[random.random()for _ in range(dimension)] for _ in range(20)]) # param = { # 'table_name': table_name, # 'file_ids': ['1'], # 'query_records': vectors, # 'top_k': 5, # # 'query_ranges': [] # Not fully tested yet # } # status, result = milvus.search_vectors_in_files(**param) # if status.OK(): # pprint(result) # # _, result = milvus.get_table_row_count(table_name) # print('# Count: {}'.format(result)) table_name = 'test_search' dimension = 256 # param = {'start_date': '2019-06-24', 'end_date': '2019-06-25'} ranges = [['2019-06-25', '2019-06-25']] vectors = Prepare.records([[random.random() for _ in range(dimension)] for _ in range(1)]) # ranges = [Prepare.range(**param)] LOGGER.info(ranges) param = { 'table_name': table_name, 'query_records': vectors, 'top_k': 5, 'query_ranges': ranges # Not fully tested yet } status, result = milvus.search_vectors(**param) if status.OK(): pprint(result) _, result = milvus.get_table_row_count(table_name) print('# Count: {}'.format(result)) milvus.disconnect()
def main(): results = [] nq = 3 topK = 5 try: for table_id in ['test_group']*1: dim = 256 query_records = Prepare.records([[random.random()for _ in range(dim)] for _ in range(nq)]) async_result = workflow.query_vectors_1_n_1_workflow(table_id, query_records, topK) results.append(async_result) for result in results: ret = result.get(propagate=True, follow_parents=True) if not ret: logger.error('no topk') continue for pos, r in enumerate(ret): logger.info('-------------Q:={}--------'.format(pos)) for idx, i in enumerate(r): logger.info('{} - \t{} {}'.format(idx, i.id, i.score)) except Exception as exc: logger.exception('')
from milvus import Milvus, Prepare, IndexType, Status import random milvus = Milvus() # Connect Milvus server, please change HOST and PORT to correct one milvus.connect(host='localhost', port='33001') # Table name is defined table_name = 'table_' + str(random.randint(0, 100)) # Create table: table name, vector dimension and index type milvus.create_table( Prepare.table_schema(table_name, dimension=256, index_type=IndexType.IDMAP)) # Add 20 256-dim-vectors into table vectors = Prepare.records([[random.random() for _ in range(256)] for _ in range(20)]) milvus.add_vectors(table_name=table_name, records=vectors) # Get table row count _, result = milvus.get_table_row_count(table_name=table_name) print('Table {}, row counts: {}'.format(table_name, result))
def main(): milvus = Milvus() # Print client version print('# Client version: {}'.format(milvus.client_version())) # Connect milvus server # Please change HOST and PORT to the correct one param = {'host': _HOST, 'port': _PORT} cnn_status = milvus.connect(**param) print('# Connect Status: {}'.format(cnn_status)) # Check if connected # is_connected = milvus.connected print('# Is connected: {}'.format(milvus.connected)) # Print milvus server version print('# Server version: {}'.format(milvus.server_version())) # Describe table table_name = 'table01' res_status, table = milvus.describe_table(table_name) print('# Describe table status: {}'.format(res_status)) print('# Describe table:{}'.format(table)) # Create table # Check if `table01` exists, if not, create a table `table01` dimension = 256 if not table: param = { 'table_name': table_name, 'dimension': dimension, 'index_type': IndexType.IDMAP, 'store_raw_vector': False } res_status = milvus.create_table(Prepare.table_schema(**param)) print('# Create table status: {}'.format(res_status)) # Show tables and their description status, tables = milvus.show_tables() pprint(tables) # Add vectors # Prepare vector with 256 dimension vectors = Prepare.records([[random.random() for _ in range(dimension)] for _ in range(20)]) # Insert vectors into table 'table01' status, ids = milvus.add_vectors(table_name=table_name, records=vectors) print('# Add vector status: {}'.format(status)) pprint(ids) # Search vectors # When adding vectors for the first time, server will take at least 5s to # persist vector data, so you have to wait for 6s after adding vectors for # the first time. print('# Waiting for 6s...') time.sleep(6) q_records = Prepare.records([[random.random() for _ in range(dimension)] for _ in range(2)]) param = { 'table_name': table_name, 'query_records': q_records, 'top_k': 10, } status, results = milvus.search_vectors(**param) print('# Search vectors status: {}'.format(status)) pprint(results) # Get table row count status, result = milvus.get_table_row_count(table_name) print('# Status: {}'.format(status)) print('# Count: {}'.format(result)) # Disconnect status = milvus.disconnect() print('# Disconnect Status: {}'.format(status))
def test_collection_schema(self): res = Prepare.table_schema(fake.collection_name(), random.randint(0, 999), 1024, MetricType.L2, {}) assert isinstance(res, milvus_pb2.TableSchema)
def main(): milvus = Milvus() # Connect to Milvus server # You may need to change _HOST and _PORT accordingly param = {'host': _HOST, 'port': _PORT} status = milvus.connect(**param) # Create table demo_table if it dosen't exist. table_name = 'demo_table' if not milvus.has_table(table_name): param = { 'table_name': table_name, 'dimension': 16, 'index_type': IndexType.FLAT, 'store_raw_vector': False } milvus.create_table(Prepare.table_schema(**param)) # Show tables in Milvus server _, tables = milvus.show_tables() # Describe demo_table _, table = milvus.describe_table(table_name) # create 10 vectors with 16 dimension vector_list = [ [ 0.66, 0.01, 0.29, 0.64, 0.75, 0.94, 0.26, 0.79, 0.61, 0.11, 0.25, 0.50, 0.74, 0.37, 0.28, 0.63 ], [ 0.77, 0.65, 0.57, 0.68, 0.29, 0.93, 0.17, 0.15, 0.95, 0.09, 0.78, 0.37, 0.76, 0.21, 0.42, 0.15 ], [ 0.61, 0.38, 0.32, 0.39, 0.54, 0.93, 0.09, 0.81, 0.52, 0.30, 0.20, 0.59, 0.15, 0.27, 0.04, 0.37 ], [ 0.33, 0.03, 0.87, 0.47, 0.79, 0.61, 0.46, 0.77, 0.62, 0.70, 0.85, 0.01, 0.30, 0.41, 0.74, 0.98 ], [ 0.19, 0.80, 0.03, 0.75, 0.22, 0.49, 0.52, 0.91, 0.40, 0.91, 0.79, 0.08, 0.27, 0.16, 0.07, 0.24 ], [ 0.44, 0.36, 0.16, 0.88, 0.30, 0.79, 0.45, 0.31, 0.45, 0.99, 0.15, 0.93, 0.37, 0.25, 0.78, 0.84 ], [ 0.33, 0.37, 0.59, 0.66, 0.76, 0.11, 0.19, 0.38, 0.14, 0.37, 0.97, 0.50, 0.08, 0.69, 0.16, 0.67 ], [ 0.68, 0.97, 0.20, 0.13, 0.30, 0.16, 0.85, 0.21, 0.26, 0.17, 0.81, 0.96, 0.18, 0.40, 0.13, 0.74 ], [ 0.11, 0.26, 0.44, 0.91, 0.89, 0.79, 0.98, 0.91, 0.09, 0.45, 0.07, 0.88, 0.71, 0.35, 0.97, 0.41 ], [ 0.17, 0.54, 0.61, 0.58, 0.25, 0.63, 0.65, 0.71, 0.26, 0.80, 0.28, 0.77, 0.69, 0.02, 0.63, 0.60 ], ] vectors = Prepare.records(vector_list) # Insert vectors into demo_table status, ids = milvus.add_vectors(table_name=table_name, records=vectors) # Get demo_table row count status, result = milvus.get_table_row_count(table_name) # Wait for 6 seconds, since Milvus server persist vector data every 5 seconds by default. # You can set data persist interval in Milvus config file. time.sleep(6) # Use the 3rd vector for similarity search query_list = [vector_list[3]] query_vectors = Prepare.records(query_list) # execute vector similarity search param = { 'table_name': table_name, 'query_records': query_vectors, 'top_k': 1, } status, results = milvus.search_vectors(**param) if results[0][0].score == 100.0 or result[0][0].id == ids[3]: print('Query result is correct') else: print('Query result isn\'t correct') # Delete demo_table status = milvus.delete_table(table_name) # Disconnect from Milvus status = milvus.disconnect()