Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion pymilvus/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,14 @@
from .orm.index import Index
from .orm.partition import Partition
from .orm.role import Role
from .orm.schema import CollectionSchema, FieldSchema, Function, FunctionScore, StructFieldSchema
from .orm.schema import (
CollectionSchema,
FieldSchema,
Function,
FunctionScore,
LexicalHighlighter,
StructFieldSchema,
)
from .orm.utility import (
create_resource_group,
create_user,
Expand Down Expand Up @@ -96,6 +103,7 @@
"Hits",
"Index",
"IndexType",
"LexicalHighlighter",
"MilvusClient",
"MilvusException",
"MilvusUnavailableException",
Expand Down
4 changes: 3 additions & 1 deletion pymilvus/client/async_grpc_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
)
from pymilvus.grpc_gen import common_pb2, milvus_pb2_grpc
from pymilvus.grpc_gen import milvus_pb2 as milvus_types
from pymilvus.orm.schema import Function
from pymilvus.orm.schema import Function, Highlighter
from pymilvus.settings import Config

from . import entity_helper, ts_utils, utils
Expand Down Expand Up @@ -830,6 +830,7 @@ async def search(
round_decimal: int = -1,
timeout: Optional[float] = None,
ranker: Optional[Function] = None,
highlighter: Optional[Highlighter] = None,
**kwargs,
):
await self.ensure_channel_ready()
Expand Down Expand Up @@ -860,6 +861,7 @@ async def search(
output_fields,
round_decimal,
ranker=ranker,
highlighter=highlighter,
**kwargs,
)
return await self._execute_search(request, timeout, round_decimal=round_decimal, **kwargs)
Expand Down
4 changes: 3 additions & 1 deletion pymilvus/client/grpc_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
)
from pymilvus.grpc_gen import common_pb2, milvus_pb2_grpc
from pymilvus.grpc_gen import milvus_pb2 as milvus_types
from pymilvus.orm.schema import Function, FunctionScore
from pymilvus.orm.schema import Function, FunctionScore, Highlighter
from pymilvus.settings import Config

from . import entity_helper, interceptor, ts_utils, utils
Expand Down Expand Up @@ -1010,6 +1010,7 @@ def search(
round_decimal: int = -1,
timeout: Optional[float] = None,
ranker: Union[Function, FunctionScore] = None,
highlighter: Optional[Highlighter] = None,
**kwargs,
):
check_pass_param(
Expand All @@ -1034,6 +1035,7 @@ def search(
output_fields,
round_decimal,
ranker=ranker,
highlighter=highlighter,
**kwargs,
)
return self._execute_search(request, timeout, round_decimal=round_decimal, **kwargs)
Expand Down
15 changes: 15 additions & 0 deletions pymilvus/client/prepare.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
FieldSchema,
Function,
FunctionScore,
Highlighter,
isVectorDataType,
)
from pymilvus.orm.types import infer_dtype_by_scalar_data
Expand Down Expand Up @@ -1370,6 +1371,7 @@ def search_requests_with_expr(
output_fields: Optional[List[str]] = None,
round_decimal: int = -1,
ranker: Optional[Union[Function, FunctionScore]] = None,
highlighter: Optional[Highlighter] = None,
**kwargs,
) -> milvus_types.SearchRequest:
use_default_consistency = ts_utils.construct_guarantee_ts(collection_name, kwargs)
Expand Down Expand Up @@ -1524,6 +1526,9 @@ def search_requests_with_expr(
elif ranker is not None:
raise ParamError(message="The search ranker must be a Function or FunctionScore.")

if highlighter is not None:
request.highlighter.CopyFrom(Prepare.highlighter_schema(highlighter))

return request

@classmethod
Expand Down Expand Up @@ -1611,6 +1616,16 @@ def common_kv_value(v: Any) -> str:
return json.dumps(v)
return str(v)

@staticmethod
def highlighter_schema(highlighter: Highlighter) -> common_types.Highlighter:
return common_types.Highlighter(
type=highlighter.type,
params=[
common_types.KeyValuePair(key=str(k), value=Prepare.common_kv_value(v))
for k, v in highlighter.params.items()
],
)

@staticmethod
def function_score_schema(function_score: FunctionScore) -> schema_types.FunctionScore:
functions = [
Expand Down
15 changes: 15 additions & 0 deletions pymilvus/client/search_result.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ def __init__(
all_scores: List[float],
fields_data: List[schema_pb2.FieldData],
output_fields: List[str],
highlight_results: List[common_pb2.HighlightResult],
pk_name: str,
):
self.ids = all_pks[start:end]
Expand Down Expand Up @@ -93,6 +94,14 @@ def __init__(
else:
msg = f"Unsupported field type: {field_data.type}"
raise MilvusException(msg)

if len(highlight_results) > 0:
for i, hit in enumerate(top_k_res):
hit["highlight"] = {
result.field_name: list(result.datas[i + start].fragments)
for result in highlight_results
}

super().__init__(top_k_res)

def __str__(self) -> str:
Expand Down Expand Up @@ -331,9 +340,11 @@ def _parse_search_result_data(
all_scores,
res.fields_data,
res.output_fields,
res.highlight_results,
_pk_name,
)
)

nq_thres += topk
return data

Expand Down Expand Up @@ -723,6 +734,10 @@ def score(self) -> float:
"""Alias of distance, will be deprecated soon"""
return self.distance

@property
def highlight(self) -> Dict[str, Any]:
return self.data.get("highlight")

@property
def fields(self) -> Dict[str, Any]:
"""Patch for orm, will be deprecated soon"""
Expand Down
5 changes: 5 additions & 0 deletions pymilvus/client/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,11 @@ class FunctionType(IntEnum):
RERANK = 3


class HighlightType(IntEnum):
LEXICAL = 0
SEMANTIC = 1


class RangeType(IntEnum):
LT = 0 # less than
LTE = 1 # less than or equal
Expand Down
24 changes: 12 additions & 12 deletions pymilvus/grpc_gen/common_pb2.py

Large diffs are not rendered by default.

6 changes: 0 additions & 6 deletions pymilvus/grpc_gen/common_pb2.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -355,9 +355,6 @@ class ObjectPrivilege(int, metaclass=_enum_type_wrapper.EnumTypeWrapper):
PrivilegeAddFileResource: _ClassVar[ObjectPrivilege]
PrivilegeRemoveFileResource: _ClassVar[ObjectPrivilege]
PrivilegeListFileResources: _ClassVar[ObjectPrivilege]
PrivilegeAddCollectionFunction: _ClassVar[ObjectPrivilege]
PrivilegeAlterCollectionFunction: _ClassVar[ObjectPrivilege]
PrivilegeDropCollectionFunction: _ClassVar[ObjectPrivilege]
PrivilegeUpdateReplicateConfiguration: _ClassVar[ObjectPrivilege]

class StateCode(int, metaclass=_enum_type_wrapper.EnumTypeWrapper):
Expand Down Expand Up @@ -706,9 +703,6 @@ PrivilegeAddCollectionField: ObjectPrivilege
PrivilegeAddFileResource: ObjectPrivilege
PrivilegeRemoveFileResource: ObjectPrivilege
PrivilegeListFileResources: ObjectPrivilege
PrivilegeAddCollectionFunction: ObjectPrivilege
PrivilegeAlterCollectionFunction: ObjectPrivilege
PrivilegeDropCollectionFunction: ObjectPrivilege
PrivilegeUpdateReplicateConfiguration: ObjectPrivilege
Initializing: StateCode
Healthy: StateCode
Expand Down
954 changes: 477 additions & 477 deletions pymilvus/grpc_gen/milvus_pb2.py

Large diffs are not rendered by default.

8 changes: 6 additions & 2 deletions pymilvus/grpc_gen/milvus_pb2.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ class ListAliasesResponse(_message.Message):
def __init__(self, status: _Optional[_Union[_common_pb2.Status, _Mapping]] = ..., db_name: _Optional[str] = ..., collection_name: _Optional[str] = ..., aliases: _Optional[_Iterable[str]] = ...) -> None: ...

class CreateCollectionRequest(_message.Message):
__slots__ = ("base", "db_name", "collection_name", "schema", "shards_num", "consistency_level", "properties", "num_partitions")
__slots__ = ("base", "db_name", "collection_name", "schema", "shards_num", "consistency_level", "properties", "num_partitions", "external_source", "external_spec")
BASE_FIELD_NUMBER: _ClassVar[int]
DB_NAME_FIELD_NUMBER: _ClassVar[int]
COLLECTION_NAME_FIELD_NUMBER: _ClassVar[int]
Expand All @@ -167,6 +167,8 @@ class CreateCollectionRequest(_message.Message):
CONSISTENCY_LEVEL_FIELD_NUMBER: _ClassVar[int]
PROPERTIES_FIELD_NUMBER: _ClassVar[int]
NUM_PARTITIONS_FIELD_NUMBER: _ClassVar[int]
EXTERNAL_SOURCE_FIELD_NUMBER: _ClassVar[int]
EXTERNAL_SPEC_FIELD_NUMBER: _ClassVar[int]
base: _common_pb2.MsgBase
db_name: str
collection_name: str
Expand All @@ -175,7 +177,9 @@ class CreateCollectionRequest(_message.Message):
consistency_level: _common_pb2.ConsistencyLevel
properties: _containers.RepeatedCompositeFieldContainer[_common_pb2.KeyValuePair]
num_partitions: int
def __init__(self, base: _Optional[_Union[_common_pb2.MsgBase, _Mapping]] = ..., db_name: _Optional[str] = ..., collection_name: _Optional[str] = ..., schema: _Optional[bytes] = ..., shards_num: _Optional[int] = ..., consistency_level: _Optional[_Union[_common_pb2.ConsistencyLevel, str]] = ..., properties: _Optional[_Iterable[_Union[_common_pb2.KeyValuePair, _Mapping]]] = ..., num_partitions: _Optional[int] = ...) -> None: ...
external_source: str
external_spec: str
def __init__(self, base: _Optional[_Union[_common_pb2.MsgBase, _Mapping]] = ..., db_name: _Optional[str] = ..., collection_name: _Optional[str] = ..., schema: _Optional[bytes] = ..., shards_num: _Optional[int] = ..., consistency_level: _Optional[_Union[_common_pb2.ConsistencyLevel, str]] = ..., properties: _Optional[_Iterable[_Union[_common_pb2.KeyValuePair, _Mapping]]] = ..., num_partitions: _Optional[int] = ..., external_source: _Optional[str] = ..., external_spec: _Optional[str] = ...) -> None: ...

class DropCollectionRequest(_message.Message):
__slots__ = ("base", "db_name", "collection_name")
Expand Down
Loading