Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support insert dict as JSON #1424

Merged
merged 1 commit into from
May 11, 2023
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
9 changes: 9 additions & 0 deletions pymilvus/client/entity_helper.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import ujson
from ..grpc_gen import schema_pb2 as schema_types
from .types import DataType
from ..exceptions import ParamError
Expand Down Expand Up @@ -40,6 +41,12 @@ def entity_to_str_arr(entity, field_info, check=True):
check_str_arr(arr, max_len)
return arr

def entity_to_json_arr(entity):
arr = []
for obj in entity.get("values"):
arr.append(ujson.dumps(obj).encode(Config.EncodeProtocol))

yah01 marked this conversation as resolved.
Show resolved Hide resolved
return arr

# TODO: refactor here.
def entity_to_field_data(entity, field_info):
Expand Down Expand Up @@ -72,6 +79,8 @@ def entity_to_field_data(entity, field_info):
field_data.vectors.binary_vector = b''.join(entity.get("values"))
elif entity_type in (DataType.VARCHAR,):
field_data.scalars.string_data.data.extend(entity_to_str_arr(entity, field_info, True))
elif entity_type in (DataType.JSON,):
field_data.scalars.json_data.data.extend(entity_to_json_arr(entity))
else:
raise ParamError(message=f"UnSupported data type: {entity_type}")

Expand Down
1 change: 1 addition & 0 deletions pymilvus/client/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ class DataType(IntEnum):

STRING = 20
VARCHAR = 21
JSON = 23

BINARY_VECTOR = 100
FLOAT_VECTOR = 101
Expand Down
3 changes: 3 additions & 0 deletions pymilvus/orm/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,9 @@ def infer_dtype_bydata(data):
d_type = infer_dtype_by_scaladata(data)
return d_type

if isinstance(data, dict):
return DataType.JSON

if is_list_like(data) or is_array_like(data):
failed = False
try:
Expand Down