Files
qdrant-client/tests/test_local_persistence.py
George a8beff7c96 Drop python3.9 (#1110)
* new: remove vectors_count, update http and grpc models

* fix: update inspection cache

* new: add conversions and update interface

* fix: fix some conversions

* fix: fix typo

* fix: fix isinstance

* fix: regen async

* fix: fix update_filter usage, fix isinstance

* tests: collection metadata test

* fix: address backward compatibility in test

* new: update models, add max payload index count and copy vectors

* fix; update _inspection_cache

* new: add read consistency to count points

* Allow uuids in interface (#1085)

* new: direct uuid support

* tests: add uuid tests

* fix: update inspection cache

* new: add collection metadata and tests to local mode (#1089)

* new: add collection metadata and tests to local mode

* fix: regen async client

* new: implement parametrized rrf in local mode (#1087)

* new: implement parametrized rrf in local mode

* refactoring: use a variable for a magic value

* fix: adjust conversion according to AI

* Update filter (#1090)

* new: add missing update_filter, implement it in local mode

* fix: fix type hint, fix update operation, fix rest uploader, add tests

* fix: fix update filter is None case

* fix: mypy was not a good boy

* Text any filter (#1091)

* new: add match text any local mode

* tests: add match text any tests

* new: update models, remove init_from and locks (#1100)

* new: update models, remove init_from and locks

* deprecate: remove init from tests

* deprecate: remove lock tests

* new: convert ascii_folding

* fix: fix type stub

* new: convert acorn

* new: convert shard key with fallback

* new: update grpcio and grpcio tools in generator (#1106)

* new: update grpcio and grpcio tools in generator

* fix: bind grpcio and tools versions to 1.62.0 in generator

* Remove deprecated methods (#1103)

* deprecate: remove old api methods

* deprecate: remove type stub for removed methods

* deprecate: remove old api methods from test_qdrant_client

* deprecate: replace search with query points in test_in_memory

* deprecate: replace search methods in fastembed mixin with query points

* deprecate: replace old api methods in test async qdrant client

* deprecate: replace search with query points in test delete points

* deprecate: replace discover and context with query points in test_discovery

* deprecate: replace recommend_groups with query_points_groups in test_group_recommend

* deprecate: replace search_groups in test_group_search

* deprecate: replace recommend with query points in test_recommendation

* deprecate: replace search with query points in test search

* deprecate: replace context and discover with query points in test sparse discovery

* deprecate: replace search with query points in test sparse idf search

* deprecate: replace recommend with query points in test sparse recommend

* deprecate: replace search with query points in test sparse search

* deprecate: replace missing search request with query request in qdrant_fastembed

* deprecate: replace search with query points in test multivector search queries

* deprecate: replace upload records with upload points in test_updates

* deprecate: remove redundant structs (#1104)

* deprecate: remove redundant structs

* fix: do not use removed conversions in local mode

* fix: remove redundant conversions, simplify types.QueryRequest

* deprecate: replace old style grpc vector conversion to a new one (#1105)

* deprecate: replace old style grpc vector conversion to a new one

* fix: ignore union attr in conversion

* review fixes

---------

Co-authored-by: generall <andrey@vasnetsov.com>

---------

Co-authored-by: generall <andrey@vasnetsov.com>

---------

Co-authored-by: generall <andrey@vasnetsov.com>

* new: deprecate add, query, query_batch in fastembed mixin (#1102)

* new: deprecate add, query, query_batch in fastembed mixin

* 1.16 -> 1.17

---------

Co-authored-by: generall <andrey@vasnetsov.com>

---------

Co-authored-by: generall <andrey@vasnetsov.com>

* new: yet another update

* new: add initial_state to create shard key (#1109)

* new: drop python3.9, replace union and optional with | where possible

* fix: fix missing type hints, regen async

* fix: remove redundant optional

* fix: fix ai comments

* fix: update type hints from merge

* new: update pyproject and lock

* new: replace optional and union with |

* new: remove optional and union from qdrant local

* new: replace union with | in client classes

* fix: replace remaining union, optional, etc, address review comments

* new: adjust numpy versioning

---------

Co-authored-by: generall <andrey@vasnetsov.com>
2025-12-05 16:32:24 +07:00

203 lines
6.6 KiB
Python

import random
import tempfile
import numpy as np
import pytest
import qdrant_client
import qdrant_client.http.models as rest
from qdrant_client._pydantic_compat import construct
from tests.fixtures.points import generate_random_sparse_vector_list
default_collection_name = "example"
def ingest_dense_vector_data(
vector_size: int = 1500,
path: str | None = None,
collection_name: str = default_collection_name,
):
lines = [x for x in range(10)]
embeddings = np.random.randn(len(lines), vector_size).tolist()
client = qdrant_client.QdrantClient(path=path)
if client.collection_exists(collection_name):
client.delete_collection(collection_name)
client.create_collection(
collection_name,
vectors_config=rest.VectorParams(
size=vector_size,
distance=rest.Distance.COSINE,
),
)
client.upsert(
collection_name=collection_name,
points=construct(
rest.Batch,
ids=random.sample(range(100), len(lines)),
vectors=embeddings,
),
)
def ingest_sparse_vector_data(
vector_count: int = 10,
max_vector_size: int = 100,
path: str | None = None,
collection_name: str = default_collection_name,
add_dense_to_config: bool = False,
):
sparse_vectors = generate_random_sparse_vector_list(vector_count, max_vector_size, 0.2)
client = qdrant_client.QdrantClient(path=path)
if client.collection_exists(collection_name):
client.delete_collection(collection_name)
client.create_collection(
collection_name,
vectors_config={}
if not add_dense_to_config
else rest.VectorParams(size=1500, distance=rest.Distance.COSINE),
sparse_vectors_config={
"text": rest.SparseVectorParams(),
},
)
batch = construct(
rest.Batch,
ids=random.sample(range(100), vector_count),
vectors={"text": sparse_vectors},
)
client.upsert(
collection_name=collection_name,
points=batch,
)
return client
def test_prevent_parallel_access():
with tempfile.TemporaryDirectory() as tmpdir:
_client = qdrant_client.QdrantClient(path=tmpdir)
with pytest.raises(Exception) as e:
_client2 = qdrant_client.QdrantClient(path=tmpdir)
assert "already accessed by another instance" in str(e)
def test_local_dense_persistence():
with tempfile.TemporaryDirectory() as tmpdir:
ingest_dense_vector_data(path=tmpdir)
client = qdrant_client.QdrantClient(path=tmpdir)
assert client.count(default_collection_name).count == 10
del client
ingest_dense_vector_data(path=tmpdir)
client = qdrant_client.QdrantClient(path=tmpdir)
assert client.count(default_collection_name).count == 10
del client
ingest_dense_vector_data(path=tmpdir)
ingest_dense_vector_data(path=tmpdir, collection_name="example_2")
client = qdrant_client.QdrantClient(path=tmpdir)
assert client.count(default_collection_name).count == 10
assert client.count("example_2").count == 10
@pytest.mark.parametrize("add_dense_to_config", [True, False])
def test_local_sparse_persistence(add_dense_to_config):
with tempfile.TemporaryDirectory() as tmpdir:
client = ingest_sparse_vector_data(path=tmpdir, add_dense_to_config=add_dense_to_config)
assert client.count(default_collection_name).count == 10
(post_result, _) = client.scroll(
collection_name=default_collection_name,
limit=10,
with_vectors=True,
)
del client
client = qdrant_client.QdrantClient(path=tmpdir)
(pre_result, _) = client.scroll(
collection_name=default_collection_name,
limit=10,
with_vectors=True,
)
for i in range(len(pre_result)):
assert pre_result[i].vector["text"] == post_result[i].vector["text"]
assert len(pre_result[i].vector["text"].indices) > 0
assert len(pre_result[i].vector["text"].values) > 0
assert len(pre_result[i].vector["text"].indices) == len(
pre_result[i].vector["text"].values
)
del client
ingest_sparse_vector_data(path=tmpdir)
client = qdrant_client.QdrantClient(path=tmpdir)
assert client.count(default_collection_name).count == 10
del client
ingest_sparse_vector_data(path=tmpdir)
ingest_sparse_vector_data(path=tmpdir, collection_name="example_2")
client = qdrant_client.QdrantClient(path=tmpdir)
assert client.count(default_collection_name).count == 10
assert client.count("example_2").count == 10
def test_update_persisence():
collection_name = "update_persisence"
with tempfile.TemporaryDirectory() as tmpdir:
client = qdrant_client.QdrantClient(path=tmpdir)
if client.collection_exists(collection_name):
client.delete_collection(collection_name)
client.create_collection(
collection_name,
vectors_config={"dense": rest.VectorParams(size=20, distance=rest.Distance.COSINE)},
sparse_vectors_config={
"text": rest.SparseVectorParams(),
},
metadata={"important": "meta information"},
)
original_collection_info = client.get_collection(collection_name)
assert original_collection_info.config.params.sparse_vectors["text"].modifier is None
assert original_collection_info.config.metadata == {"important": "meta information"}
client.update_collection(
collection_name,
sparse_vectors_config={"text": rest.SparseVectorParams(modifier=rest.Modifier.IDF)},
metadata={"not_important": "missing"},
)
updated_collection_info = client.get_collection(collection_name)
assert (
updated_collection_info.config.params.sparse_vectors["text"].modifier
== rest.Modifier.IDF
)
assert updated_collection_info.config.metadata == {
"important": "meta information",
"not_important": "missing",
}
client.close()
del client
client = qdrant_client.QdrantClient(path=tmpdir)
persisted_collection_info = client.get_collection(collection_name)
assert (
persisted_collection_info.config.params.sparse_vectors["text"].modifier
== rest.Modifier.IDF
)
assert persisted_collection_info.config.metadata == {
"important": "meta information",
"not_important": "missing",
}