Skip to content

Commit

Permalink
feat: support alter index
Browse files Browse the repository at this point in the history
Signed-off-by: yah01 <[email protected]>
  • Loading branch information
yah01 committed Dec 28, 2023
1 parent 2b5a27d commit dce31bc
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 0 deletions.
17 changes: 17 additions & 0 deletions pymilvus/client/grpc_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -922,6 +922,23 @@ 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)
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)
Expand Down
14 changes: 14 additions & 0 deletions pymilvus/client/prepare.py
Original file line number Diff line number Diff line change
Expand Up @@ -726,6 +726,20 @@ 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):
if isinstance(tv, dict):
return ujson.dumps(tv)
return 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
Expand Down
42 changes: 42 additions & 0 deletions pymilvus/orm/collection.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit dce31bc

Please sign in to comment.