1- """MilvusClient for dealing with simple workflows."""
2-
31import logging
42from typing import Dict , List , Optional , Union
53from uuid import uuid4
3230from pymilvus .orm .iterator import QueryIterator , SearchIterator
3331from pymilvus .orm .types import DataType
3432
35- from .index import IndexParams
33+ from .check import validate_param
34+ from .index import IndexParam , IndexParams
3635
3736logger = logging .getLogger (__name__ )
3837
@@ -112,9 +111,8 @@ def _fast_create_collection(
112111 timeout : Optional [float ] = None ,
113112 ** kwargs ,
114113 ):
115- if dimension is None :
116- msg = "missing requried argument: 'dimension'"
117- raise TypeError (msg )
114+ validate_param ("dimension" , dimension , int )
115+
118116 if "enable_dynamic_field" not in kwargs :
119117 kwargs ["enable_dynamic_field" ] = True
120118
@@ -132,8 +130,7 @@ def _fast_create_collection(
132130 pk_args ["max_length" ] = kwargs ["max_length" ]
133131
134132 schema .add_field (primary_field_name , pk_data_type , is_primary = True , ** pk_args )
135- vector_type = DataType .FLOAT_VECTOR
136- schema .add_field (vector_field_name , vector_type , dim = dimension )
133+ schema .add_field (vector_field_name , DataType .FLOAT_VECTOR , dim = dimension )
137134 schema .verify ()
138135
139136 conn = self ._get_connection ()
@@ -147,7 +144,7 @@ def _fast_create_collection(
147144 raise ex from ex
148145
149146 index_params = IndexParams ()
150- index_params .add_index (vector_field_name , "" , " " , metric_type = metric_type )
147+ index_params .add_index (vector_field_name , index_type = "AUTOINDEX " , metric_type = metric_type )
151148 self .create_index (collection_name , index_params , timeout = timeout )
152149 self .load_collection (collection_name , timeout = timeout )
153150
@@ -158,24 +155,29 @@ def create_index(
158155 timeout : Optional [float ] = None ,
159156 ** kwargs ,
160157 ):
158+ validate_param ("collection_name" , collection_name , str )
159+ validate_param ("index_params" , index_params , IndexParams )
160+ if len (index_params ) == 0 :
161+ raise ParamError (message = "IndexParams is empty, no index can be created" )
162+
161163 for index_param in index_params :
162164 self ._create_index (collection_name , index_param , timeout = timeout , ** kwargs )
163165
164166 def _create_index (
165- self , collection_name : str , index_param : Dict , timeout : Optional [float ] = None , ** kwargs
167+ self ,
168+ collection_name : str ,
169+ index_param : IndexParam ,
170+ timeout : Optional [float ] = None ,
171+ ** kwargs ,
166172 ):
167173 conn = self ._get_connection ()
168174 try :
169- params = index_param .pop ("params" , {})
170- field_name = index_param .pop ("field_name" , "" )
171- index_name = index_param .pop ("index_name" , "" )
172- params .update (index_param )
173175 conn .create_index (
174176 collection_name ,
175- field_name ,
176- params ,
177+ index_param . field_name ,
178+ index_param . get_index_configs () ,
177179 timeout = timeout ,
178- index_name = index_name ,
180+ index_name = index_param . index_name ,
179181 ** kwargs ,
180182 )
181183 logger .debug ("Successfully created an index on collection: %s" , collection_name )
@@ -872,8 +874,11 @@ def create_schema(cls, **kwargs):
872874 return CollectionSchema ([], ** kwargs )
873875
874876 @classmethod
875- def prepare_index_params (cls , field_name : str = "" , ** kwargs ):
876- return IndexParams (field_name , ** kwargs )
877+ def prepare_index_params (cls , field_name : str = "" , ** kwargs ) -> IndexParams :
878+ index_params = IndexParams ()
879+ if field_name and validate_param ("field_name" , field_name , str ):
880+ index_params .add_index (field_name , ** kwargs )
881+ return index_params
877882
878883 def _create_collection_with_schema (
879884 self ,
0 commit comments