Skip to content

Commit

Permalink
feat: support alter index (#1833)
Browse files Browse the repository at this point in the history
Signed-off-by: yah01 <[email protected]>
  • Loading branch information
yah01 authored Dec 28, 2023
1 parent 2b5a27d commit 62c526a
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 3 deletions.
5 changes: 5 additions & 0 deletions pymilvus/client/check.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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):
Expand Down
20 changes: 20 additions & 0 deletions pymilvus/client/grpc_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
16 changes: 13 additions & 3 deletions pymilvus/client/prepare.py
Original file line number Diff line number Diff line change
Expand Up @@ -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():
Expand All @@ -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
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 62c526a

Please sign in to comment.