diff --git a/pymilvus/client/check.py b/pymilvus/client/check.py index 009e1358f..9682fc46a 100644 --- a/pymilvus/client/check.py +++ b/pymilvus/client/check.py @@ -98,6 +98,10 @@ def is_legal_field_name(field_name: Any) -> bool: return field_name and isinstance(field_name, str) +def is_legal_index_name(index_name: Any) -> bool: + return index_name and isinstance(index_name, str) + + def is_legal_nlist(nlist: Any) -> bool: return not isinstance(nlist, bool) and isinstance(nlist, int) @@ -323,6 +327,7 @@ def __init__(self) -> None: "min_iterations": is_legal_min_iterations, "max_iterations": is_legal_max_iterations, "team_size": is_legal_team_size, + "index_name": is_legal_index_name, } def check(self, key: str, value: Callable): diff --git a/pymilvus/client/grpc_handler.py b/pymilvus/client/grpc_handler.py index 89046dec2..7b075b154 100644 --- a/pymilvus/client/grpc_handler.py +++ b/pymilvus/client/grpc_handler.py @@ -922,6 +922,26 @@ def _check(): return Status(status.code, status.reason) + @retry_on_rpc_failure() + def alter_index( + self, + collection_name: str, + index_name: str, + extra_params: dict, + timeout: Optional[float] = None, + **kwargs, + ): + check_pass_param(collection_name=collection_name, index_name=index_name) + if extra_params is None: + raise ParamError(message="extra_params should not be None") + + request = Prepare.alter_index_request(collection_name, index_name, extra_params) + + rf = self._stub.AlterIndex.future(request, timeout=timeout) + response = rf.result() + status = response.status + check_status(status) + @retry_on_rpc_failure() def list_indexes(self, collection_name: str, timeout: Optional[float] = None, **kwargs): check_pass_param(collection_name=collection_name) diff --git a/pymilvus/client/prepare.py b/pymilvus/client/prepare.py index 138146011..da936cba3 100644 --- a/pymilvus/client/prepare.py +++ b/pymilvus/client/prepare.py @@ -713,9 +713,7 @@ def create_index_request(cls, collection_name: str, field_name: str, params: Dic ) def dump(tv: Dict): - if isinstance(tv, dict): - return ujson.dumps(tv) - return str(tv) + return ujson.dumps(tv) if isinstance(tv, dict) else str(tv) if isinstance(params, dict): for tk, tv in params.items(): @@ -726,6 +724,18 @@ def dump(tv: Dict): return index_params + @classmethod + def alter_index_request(cls, collection_name: str, index_name: str, extra_params: dict): + def dump(tv: Dict): + return ujson.dumps(tv) if isinstance(tv, dict) else str(tv) + + params = [] + for k, v in extra_params.items(): + params.append(common_types.KeyValuePair(key=str(k), value=dump(v))) + return milvus_types.AlterIndexRequest( + collection_name=collection_name, index_name=index_name, extra_params=params + ) + @classmethod def describe_index_request( cls, collection_name: str, index_name: str, timestamp: Optional[int] = None diff --git a/pymilvus/orm/collection.py b/pymilvus/orm/collection.py index 4c2ddddd1..c2bde1ae0 100644 --- a/pymilvus/orm/collection.py +++ b/pymilvus/orm/collection.py @@ -1349,6 +1349,48 @@ def create_index( conn = self._get_connection() return conn.create_index(self._name, field_name, index_params, timeout=timeout, **kwargs) + def alter_index( + self, + index_name: str, + extra_params: dict, + timeout: Optional[float] = None, + ): + """Alter index for a specified field, with a index name. + + Args: + index_name (``str``): The name of the index to alter + extra_params (``dict``): The parameters to index + * *mmap.enabled* (``str``) + "mmap.enabled" as the key, example values: True or False. + + timeout (``float``, optional): An optional duration of time in seconds to allow + for the RPC. When timeout is set to None, client waits until server + response or error occur. + + Raises: + MilvusException: If anything goes wrong. + + Examples: + >>> from pymilvus import Collection, FieldSchema, CollectionSchema, DataType + >>> from pymilvus import IndexType, MetricType + >>> schema = CollectionSchema([ + ... FieldSchema("film_id", DataType.INT64, is_primary=True), + ... FieldSchema("title", DataType.STRING), + ... FieldSchema("films", dtype=DataType.FLOAT_VECTOR, dim=2) + ... ]) + >>> collection = Collection("test_collection_alter_index", schema) + >>> index_params = { + ... "index_type": IndexType.IVF_FLAT, + ... "metric_type": MetricType.L2, + ... "params": {"nlist": 128} + ... } + >>> collection.create_index("films", index_params, index_name="idx") + Status(code=0, message='') + >>> collection.alter_index("idx", {"mmap.enabled": True}) + """ + conn = self._get_connection() + return conn.alter_index(self._name, index_name, extra_params, timeout=timeout) + def has_index(self, timeout: Optional[float] = None, **kwargs) -> bool: """Check whether a specified index exists.