Skip to content

Commit a0b5a11

Browse files
authored
new: add collection metadata and tests to local mode (#1089)
* new: add collection metadata and tests to local mode * fix: regen async client
1 parent c89f615 commit a0b5a11

File tree

5 files changed

+80
-6
lines changed

5 files changed

+80
-6
lines changed

qdrant_client/local/async_qdrant_local.py

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -885,14 +885,20 @@ async def update_collection(
885885
self,
886886
collection_name: str,
887887
sparse_vectors_config: Optional[Mapping[str, types.SparseVectorParams]] = None,
888+
metadata: Optional[types.Payload] = None,
888889
**kwargs: Any,
889890
) -> bool:
890891
_collection = self._get_collection(collection_name)
892+
updated = False
891893
if sparse_vectors_config is not None:
892894
for vector_name, vector_params in sparse_vectors_config.items():
893895
_collection.update_sparse_vectors_config(vector_name, vector_params)
894-
return True
895-
return False
896+
updated = True
897+
if metadata is not None:
898+
_collection.config.metadata.update(metadata)
899+
updated = True
900+
self._save()
901+
return updated
896902

897903
def _collection_path(self, collection_name: str) -> Optional[str]:
898904
if self.persistent:
@@ -924,6 +930,7 @@ async def create_collection(
924930
] = None,
925931
init_from: Optional[types.InitFrom] = None,
926932
sparse_vectors_config: Optional[Mapping[str, types.SparseVectorParams]] = None,
933+
metadata: Optional[types.Payload] = None,
927934
**kwargs: Any,
928935
) -> bool:
929936
if self.closed:
@@ -942,7 +949,9 @@ async def create_collection(
942949
os.makedirs(collection_path, exist_ok=True)
943950
collection = LocalCollection(
944951
rest_models.CreateCollection(
945-
vectors=vectors_config or {}, sparse_vectors=sparse_vectors_config
952+
vectors=vectors_config or {},
953+
sparse_vectors=sparse_vectors_config,
954+
metadata=metadata,
946955
),
947956
location=collection_path,
948957
force_disable_check_same_thread=self.force_disable_check_same_thread,

qdrant_client/local/local_collection.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2786,6 +2786,7 @@ def info(self) -> models.CollectionInfo:
27862786
max_optimization_threads=1,
27872787
),
27882788
quantization_config=None,
2789+
metadata=self.config.metadata,
27892790
),
27902791
)
27912792

qdrant_client/local/qdrant_local.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -954,16 +954,22 @@ def update_collection(
954954
self,
955955
collection_name: str,
956956
sparse_vectors_config: Optional[Mapping[str, types.SparseVectorParams]] = None,
957+
metadata: Optional[types.Payload] = None,
957958
**kwargs: Any,
958959
) -> bool:
959960
_collection = self._get_collection(collection_name)
960-
961+
updated = False
961962
if sparse_vectors_config is not None:
962963
for vector_name, vector_params in sparse_vectors_config.items():
963964
_collection.update_sparse_vectors_config(vector_name, vector_params)
965+
updated = True
964966

965-
return True
966-
return False
967+
if metadata is not None:
968+
_collection.config.metadata.update(metadata)
969+
updated = True
970+
971+
self._save()
972+
return updated
967973

968974
def _collection_path(self, collection_name: str) -> Optional[str]:
969975
if self.persistent:
@@ -996,6 +1002,7 @@ def create_collection(
9961002
] = None,
9971003
init_from: Optional[types.InitFrom] = None,
9981004
sparse_vectors_config: Optional[Mapping[str, types.SparseVectorParams]] = None,
1005+
metadata: Optional[types.Payload] = None,
9991006
**kwargs: Any,
10001007
) -> bool:
10011008
if self.closed:
@@ -1019,6 +1026,7 @@ def create_collection(
10191026
rest_models.CreateCollection(
10201027
vectors=vectors_config or {},
10211028
sparse_vectors=sparse_vectors_config,
1029+
metadata=metadata,
10221030
),
10231031
location=collection_path,
10241032
force_disable_check_same_thread=self.force_disable_check_same_thread,

tests/test_local_persistence.py

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,3 +150,54 @@ def test_local_sparse_persistence(add_dense_to_config):
150150
client = qdrant_client.QdrantClient(path=tmpdir)
151151
assert client.count(default_collection_name).count == 10
152152
assert client.count("example_2").count == 10
153+
154+
155+
def test_update_persisence():
156+
collection_name = "update_persisence"
157+
with tempfile.TemporaryDirectory() as tmpdir:
158+
client = qdrant_client.QdrantClient(path=tmpdir)
159+
160+
if client.collection_exists(collection_name):
161+
client.delete_collection(collection_name)
162+
163+
client.create_collection(
164+
collection_name,
165+
vectors_config={"dense": rest.VectorParams(size=20, distance=rest.Distance.COSINE)},
166+
sparse_vectors_config={
167+
"text": rest.SparseVectorParams(),
168+
},
169+
metadata={"important": "meta information"},
170+
)
171+
172+
original_collection_info = client.get_collection(collection_name)
173+
174+
assert original_collection_info.config.params.sparse_vectors["text"].modifier is None
175+
assert original_collection_info.config.metadata == {"important": "meta information"}
176+
177+
client.update_collection(
178+
collection_name,
179+
sparse_vectors_config={"text": rest.SparseVectorParams(modifier=rest.Modifier.IDF)},
180+
metadata={"not_important": "missing"},
181+
)
182+
updated_collection_info = client.get_collection(collection_name)
183+
assert (
184+
updated_collection_info.config.params.sparse_vectors["text"].modifier
185+
== rest.Modifier.IDF
186+
)
187+
assert updated_collection_info.config.metadata == {
188+
"important": "meta information",
189+
"not_important": "missing",
190+
}
191+
client.close()
192+
del client
193+
194+
client = qdrant_client.QdrantClient(path=tmpdir)
195+
persisted_collection_info = client.get_collection(collection_name)
196+
assert (
197+
persisted_collection_info.config.params.sparse_vectors["text"].modifier
198+
== rest.Modifier.IDF
199+
)
200+
assert persisted_collection_info.config.metadata == {
201+
"important": "meta information",
202+
"not_important": "missing",
203+
}

tests/test_qdrant_client.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1183,6 +1183,11 @@ def test_points_crud(prefer_grpc):
11831183
collection_info = client.get_collection(COLLECTION_NAME)
11841184
assert collection_info.config.metadata == collection_metadata
11851185

1186+
new_metadata = {"due_date": "12.12.2222"}
1187+
client.update_collection(COLLECTION_NAME, metadata=new_metadata)
1188+
updated_collection_info = client.get_collection(COLLECTION_NAME)
1189+
assert updated_collection_info.config.metadata == {**collection_metadata, **new_metadata}
1190+
11861191
# Create a single point
11871192
client.upsert(
11881193
collection_name=COLLECTION_NAME,

0 commit comments

Comments
 (0)