From 88e93e8dd476533ecfa4048b69ec7ef5205d4abd Mon Sep 17 00:00:00 2001 From: fei <204683769+feiiiiii5@users.noreply.github.com> Date: Sat, 7 Nov 2026 00:14:33 +0800 Subject: [PATCH 1/2] fix(config): preserve distance in exported collection schemas --- mock_tests/test_config_distance.py | 128 +++++++++++++++++++++++++ weaviate/collections/classes/config.py | 6 ++ 2 files changed, 134 insertions(+) create mode 100644 mock_tests/test_config_distance.py diff --git a/mock_tests/test_config_distance.py b/mock_tests/test_config_distance.py new file mode 100644 index 000000000..a657c68c0 --- /dev/null +++ b/mock_tests/test_config_distance.py @@ -0,0 +1,128 @@ +import copy +import json +from typing import Any, Dict, List + +import pytest +from pytest_httpserver import HTTPServer +from werkzeug import Request, Response + +import weaviate +from mock_tests.conftest import MOCK_IP, MOCK_PORT, MOCK_PORT_GRPC + + +HNSW_CONFIG = { + "distance": "dot", + "skip": False, + "cleanupIntervalSeconds": 300, + "maxConnections": 64, + "efConstruction": 128, + "ef": -1, + "dynamicEfMin": 100, + "dynamicEfMax": 500, + "dynamicEfFactor": 8, + "vectorCacheMaxObjects": 1000000000000, + "flatSearchCutoff": 40000, +} +FLAT_CONFIG = {"distance": "dot", "vectorCacheMaxObjects": 1000000000000} +INDEX_CONFIGS = { + "hnsw": HNSW_CONFIG, + "flat": FLAT_CONFIG, + "hfresh": { + "distance": "dot", + "maxPostingSizeKB": 48, + "replicas": 4, + "searchProbe": 256, + }, + "dynamic": { + "distance": "dot", + "threshold": 10000, + "hnsw": HNSW_CONFIG, + "flat": FLAT_CONFIG, + }, +} + + +@pytest.fixture(params=INDEX_CONFIGS) +def index_type(request: pytest.FixtureRequest) -> str: + return request.param + + +@pytest.fixture(params=[False, True], ids=["legacy", "named"]) +def named(request: pytest.FixtureRequest) -> bool: + return request.param + + +@pytest.fixture +def schema(index_type: str, named: bool) -> Dict[str, Any]: + index = { + "vectorIndexType": index_type, + "vectorIndexConfig": copy.deepcopy(INDEX_CONFIGS[index_type]), + } + return { + "class": "DistanceSource", + "properties": [], + "invertedIndexConfig": { + "bm25": {"b": 0.75, "k1": 1.2}, + "cleanupIntervalSeconds": 60, + "stopwords": {"preset": "en", "additions": None, "removals": None}, + }, + "replicationConfig": {"factor": 1}, + "shardingConfig": { + "virtualPerPhysical": 128, + "desiredCount": 1, + "actualCount": 1, + "desiredVirtualCount": 128, + "actualVirtualCount": 128, + "key": "_id", + "strategy": "hash", + "function": "murmur3", + }, + **( + {"vectorConfig": {"custom": {"vectorizer": {"none": {}}, **index}}} + if named + else {"vectorizer": "none", **index} + ), + } + + +@pytest.mark.parametrize("from_config", [False, True], ids=["from_dict", "from_config"]) +def test_exported_config_preserves_distance_in_create_request( + weaviate_mock: HTTPServer, schema: Dict[str, Any], named: bool, from_config: bool +) -> None: + bodies: List[Dict[str, Any]] = [] + + def capture_create(request: Request) -> Response: + body = request.get_json() + bodies.append(body) + return Response(json.dumps({"class": body["class"]}), content_type="application/json") + + weaviate_mock.expect_request("/v1/schema/DistanceSource", method="GET").respond_with_json( + schema + ) + weaviate_mock.expect_request("/v1/schema", method="POST").respond_with_handler(capture_create) + + with weaviate.connect_to_local( + port=MOCK_PORT, host=MOCK_IP, grpc_port=MOCK_PORT_GRPC, skip_init_checks=True + ) as client: + # The unmodified wire schema is a control for the public dictionary creation path. + client.collections.create_from_dict(schema) + assert bodies[0] == schema + + config = client.collections.use("DistanceSource").config.get() + config.name = "DistanceCopy" + if from_config: + client.collections.create_from_config(config) + else: + client.collections.create_from_dict(config.to_dict()) + + weaviate_mock.check_assertions() + assert len(bodies) == 2 + assert bodies[1]["class"] == "DistanceCopy" + posted = bodies[1]["vectorConfig"]["custom"] if named else bodies[1] + index = posted["vectorIndexConfig"] + assert index.get("distance") == "dot", index + assert "distanceMetric" not in index + if posted["vectorIndexType"] == "dynamic": + for child in ["hnsw", "flat"]: + assert index[child].get("distance") == "dot", index[child] + assert "distanceMetric" not in index[child] diff --git a/weaviate/collections/classes/config.py b/weaviate/collections/classes/config.py index 81456001f..159eae408 100644 --- a/weaviate/collections/classes/config.py +++ b/weaviate/collections/classes/config.py @@ -2129,6 +2129,7 @@ class _VectorIndexConfig(_ConfigBase): def to_dict(self) -> Dict[str, Any]: out = super().to_dict() + out["distance"] = out.pop("distanceMetric") if isinstance(self.quantizer, _PQConfig): out["pq"] = {**out.pop("quantizer"), "enabled": True} elif isinstance(self.quantizer, _BQConfig): @@ -2213,6 +2214,11 @@ class _VectorIndexConfigDynamic(_ConfigBase): def vector_index_type() -> str: return VectorIndexType.DYNAMIC.value + def to_dict(self) -> Dict[str, Any]: + out = super().to_dict() + out["distance"] = out.pop("distanceMetric") + return out + VectorIndexConfigDynamic = _VectorIndexConfigDynamic From 658c9708f6d096de13210ff7f371836bbf7065f6 Mon Sep 17 00:00:00 2001 From: Chen Yufeiyang Date: Sat, 14 Nov 2026 14:35:25 +0800 Subject: [PATCH 2/2] test(config): cover non-default distances in export round-trips --- integration/test_client.py | 20 +++- integration/test_collection_hfresh.py | 2 +- mock_tests/test_config_distance.py | 128 -------------------------- 3 files changed, 20 insertions(+), 130 deletions(-) delete mode 100644 mock_tests/test_config_distance.py diff --git a/integration/test_client.py b/integration/test_client.py index 3560cbbb7..3a31cd7b9 100644 --- a/integration/test_client.py +++ b/integration/test_client.py @@ -14,6 +14,7 @@ GenerativeSearches, Property, ReferenceProperty, + VectorDistances, Vectorizers, _CollectionConfig, ) @@ -302,7 +303,24 @@ def test_create_export_and_recreate_named_vectors( source_properties=["name"], vectorize_collection_name=False, ), - Configure.NamedVectors.none("custom", vector_index_config=Configure.VectorIndex.flat()), + Configure.NamedVectors.none( + "custom", + vector_index_config=Configure.VectorIndex.flat( + distance_metric=VectorDistances.DOT + ), + ), + Configure.NamedVectors.none( + "hnsw", + vector_index_config=Configure.VectorIndex.hnsw( + distance_metric=VectorDistances.DOT + ), + ), + Configure.NamedVectors.none( + "dynamic", + vector_index_config=Configure.VectorIndex.dynamic( + distance_metric=VectorDistances.DOT + ), + ), ], ) conf = col.config.get() diff --git a/integration/test_collection_hfresh.py b/integration/test_collection_hfresh.py index a692c0a5f..d17e51d2f 100644 --- a/integration/test_collection_hfresh.py +++ b/integration/test_collection_hfresh.py @@ -19,7 +19,7 @@ def test_collection_config_hfresh(collection_factory: CollectionFactory) -> None collection = collection_factory( vector_index_config=Configure.VectorIndex.hfresh( - distance_metric=VectorDistances.COSINE, + distance_metric=VectorDistances.DOT, max_posting_size_kb=1024, replicas=2, search_probe=50, diff --git a/mock_tests/test_config_distance.py b/mock_tests/test_config_distance.py deleted file mode 100644 index a657c68c0..000000000 --- a/mock_tests/test_config_distance.py +++ /dev/null @@ -1,128 +0,0 @@ -import copy -import json -from typing import Any, Dict, List - -import pytest -from pytest_httpserver import HTTPServer -from werkzeug import Request, Response - -import weaviate -from mock_tests.conftest import MOCK_IP, MOCK_PORT, MOCK_PORT_GRPC - - -HNSW_CONFIG = { - "distance": "dot", - "skip": False, - "cleanupIntervalSeconds": 300, - "maxConnections": 64, - "efConstruction": 128, - "ef": -1, - "dynamicEfMin": 100, - "dynamicEfMax": 500, - "dynamicEfFactor": 8, - "vectorCacheMaxObjects": 1000000000000, - "flatSearchCutoff": 40000, -} -FLAT_CONFIG = {"distance": "dot", "vectorCacheMaxObjects": 1000000000000} -INDEX_CONFIGS = { - "hnsw": HNSW_CONFIG, - "flat": FLAT_CONFIG, - "hfresh": { - "distance": "dot", - "maxPostingSizeKB": 48, - "replicas": 4, - "searchProbe": 256, - }, - "dynamic": { - "distance": "dot", - "threshold": 10000, - "hnsw": HNSW_CONFIG, - "flat": FLAT_CONFIG, - }, -} - - -@pytest.fixture(params=INDEX_CONFIGS) -def index_type(request: pytest.FixtureRequest) -> str: - return request.param - - -@pytest.fixture(params=[False, True], ids=["legacy", "named"]) -def named(request: pytest.FixtureRequest) -> bool: - return request.param - - -@pytest.fixture -def schema(index_type: str, named: bool) -> Dict[str, Any]: - index = { - "vectorIndexType": index_type, - "vectorIndexConfig": copy.deepcopy(INDEX_CONFIGS[index_type]), - } - return { - "class": "DistanceSource", - "properties": [], - "invertedIndexConfig": { - "bm25": {"b": 0.75, "k1": 1.2}, - "cleanupIntervalSeconds": 60, - "stopwords": {"preset": "en", "additions": None, "removals": None}, - }, - "replicationConfig": {"factor": 1}, - "shardingConfig": { - "virtualPerPhysical": 128, - "desiredCount": 1, - "actualCount": 1, - "desiredVirtualCount": 128, - "actualVirtualCount": 128, - "key": "_id", - "strategy": "hash", - "function": "murmur3", - }, - **( - {"vectorConfig": {"custom": {"vectorizer": {"none": {}}, **index}}} - if named - else {"vectorizer": "none", **index} - ), - } - - -@pytest.mark.parametrize("from_config", [False, True], ids=["from_dict", "from_config"]) -def test_exported_config_preserves_distance_in_create_request( - weaviate_mock: HTTPServer, schema: Dict[str, Any], named: bool, from_config: bool -) -> None: - bodies: List[Dict[str, Any]] = [] - - def capture_create(request: Request) -> Response: - body = request.get_json() - bodies.append(body) - return Response(json.dumps({"class": body["class"]}), content_type="application/json") - - weaviate_mock.expect_request("/v1/schema/DistanceSource", method="GET").respond_with_json( - schema - ) - weaviate_mock.expect_request("/v1/schema", method="POST").respond_with_handler(capture_create) - - with weaviate.connect_to_local( - port=MOCK_PORT, host=MOCK_IP, grpc_port=MOCK_PORT_GRPC, skip_init_checks=True - ) as client: - # The unmodified wire schema is a control for the public dictionary creation path. - client.collections.create_from_dict(schema) - assert bodies[0] == schema - - config = client.collections.use("DistanceSource").config.get() - config.name = "DistanceCopy" - if from_config: - client.collections.create_from_config(config) - else: - client.collections.create_from_dict(config.to_dict()) - - weaviate_mock.check_assertions() - assert len(bodies) == 2 - assert bodies[1]["class"] == "DistanceCopy" - posted = bodies[1]["vectorConfig"]["custom"] if named else bodies[1] - index = posted["vectorIndexConfig"] - assert index.get("distance") == "dot", index - assert "distanceMetric" not in index - if posted["vectorIndexType"] == "dynamic": - for child in ["hnsw", "flat"]: - assert index[child].get("distance") == "dot", index[child] - assert "distanceMetric" not in index[child]