Update milvus_multitenancy.py (#25857)

This commit is contained in:
Classic298 2026-06-17 03:07:27 +02:00 committed by GitHub
parent 819ad1d904
commit d501e3d6b5
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -31,10 +31,15 @@ from pymilvus import (
connections,
utility,
)
from pymilvus.exceptions import MilvusException
log = logging.getLogger(__name__)
RESOURCE_ID_FIELD = 'resource_id'
# Milvus VARCHAR hard cap for the `text` field (see _create_shared_collection).
# Chunks longer than this are truncated before insert so one oversized chunk
# can't fail the whole batch (and leave the file with zero embeddings).
MILVUS_TEXT_MAX_LENGTH = 65535
# Milvus expressions are SQL-like strings with no parameterized-query API;
# values get interpolated into single-quoted literals. Reject anything that
@ -169,17 +174,34 @@ class MilvusClient(VectorDBBase):
self._ensure_collection(mt_collection, dimension)
collection = Collection(mt_collection)
entities = [
{
'id': item['id'],
'vector': item['vector'],
'text': item['text'],
'metadata': item['metadata'],
RESOURCE_ID_FIELD: resource_id,
}
for item in items
]
collection.insert(entities)
entities = []
for item in items:
text = item['text'] or ''
if len(text) > MILVUS_TEXT_MAX_LENGTH:
log.warning(
f'Milvus: truncating text id={item["id"]} '
f'{len(text)}->{MILVUS_TEXT_MAX_LENGTH} chars '
f'(collection={mt_collection}, resource_id={resource_id})'
)
text = text[:MILVUS_TEXT_MAX_LENGTH]
entities.append(
{
'id': item['id'],
'vector': item['vector'],
'text': text,
'metadata': item['metadata'],
RESOURCE_ID_FIELD: resource_id,
}
)
try:
collection.insert(entities)
except MilvusException as e:
log.error(
f'Milvus insert failed (collection={mt_collection}, '
f'resource_id={resource_id}, items={len(entities)}): {e}'
)
raise
def search(
self,