|
| 1 | +"""Unit tests: BM25 search operators are wired into the gRPC request and version-gated. |
| 2 | +
|
| 3 | +``BM25Operator.and_cross()`` maps to the ``OPERATOR_AND_CROSS`` enum added in Weaviate 1.39.0 and |
| 4 | +backported to 1.38.8 and 1.37.15, so the client must reject it on servers outside those ranges |
| 5 | +rather than send an enum value the server will not understand. |
| 6 | +""" |
| 7 | + |
| 8 | +from typing import Optional |
| 9 | + |
| 10 | +import pytest |
| 11 | + |
| 12 | +from weaviate.classes.query import BM25Operator |
| 13 | +from weaviate.collections.classes.grpc import BM25OperatorOptions |
| 14 | +from weaviate.collections.grpc.aggregate import _AggregateGRPC |
| 15 | +from weaviate.collections.grpc.query import _QueryGRPC |
| 16 | +from weaviate.exceptions import WeaviateUnsupportedFeatureError |
| 17 | +from weaviate.proto.v1 import base_search_pb2 |
| 18 | +from weaviate.util import _ServerVersion |
| 19 | + |
| 20 | +_SUPPORTED = ["1.37.15", "1.38.8", "1.39.0", "1.40.0"] |
| 21 | +_UNSUPPORTED = ["1.36.9", "1.37.14", "1.38.0", "1.38.7"] |
| 22 | + |
| 23 | + |
| 24 | +def _query(version: str = "1.39.0") -> _QueryGRPC: |
| 25 | + return _QueryGRPC( |
| 26 | + weaviate_version=_ServerVersion.from_string(version), |
| 27 | + name="Dummy", |
| 28 | + tenant=None, |
| 29 | + consistency_level=None, |
| 30 | + validate_arguments=True, |
| 31 | + uses_125_api=True, |
| 32 | + uses_127_api=True, |
| 33 | + ) |
| 34 | + |
| 35 | + |
| 36 | +def _aggregate(version: str = "1.39.0") -> _AggregateGRPC: |
| 37 | + return _AggregateGRPC( |
| 38 | + weaviate_version=_ServerVersion.from_string(version), |
| 39 | + name="Dummy", |
| 40 | + tenant=None, |
| 41 | + consistency_level=None, |
| 42 | + validate_arguments=True, |
| 43 | + ) |
| 44 | + |
| 45 | + |
| 46 | +def _aggregate_hybrid( |
| 47 | + builder: _AggregateGRPC, operator: Optional[BM25OperatorOptions] |
| 48 | +) -> base_search_pb2.Hybrid: |
| 49 | + return builder.hybrid( |
| 50 | + query="banana two", |
| 51 | + alpha=0.0, |
| 52 | + vector=None, |
| 53 | + properties=None, |
| 54 | + target_vector=None, |
| 55 | + bm25_operator=operator, |
| 56 | + aggregations=[], |
| 57 | + filters=None, |
| 58 | + group_by=None, |
| 59 | + limit=None, |
| 60 | + object_limit=None, |
| 61 | + objects_count=False, |
| 62 | + ).hybrid |
| 63 | + |
| 64 | + |
| 65 | +@pytest.mark.parametrize( |
| 66 | + "operator,expected,minimum_or_tokens_match", |
| 67 | + [ |
| 68 | + (BM25Operator.and_(), base_search_pb2.SearchOperatorOptions.OPERATOR_AND, 0), |
| 69 | + (BM25Operator.or_(minimum_match=2), base_search_pb2.SearchOperatorOptions.OPERATOR_OR, 2), |
| 70 | + ( |
| 71 | + BM25Operator.and_cross(), |
| 72 | + base_search_pb2.SearchOperatorOptions.OPERATOR_AND_CROSS, |
| 73 | + 0, |
| 74 | + ), |
| 75 | + ], |
| 76 | +) |
| 77 | +def test_bm25_operator_wired_into_request( |
| 78 | + operator: BM25OperatorOptions, |
| 79 | + expected: "base_search_pb2.SearchOperatorOptions.Operator", |
| 80 | + minimum_or_tokens_match: int, |
| 81 | +) -> None: |
| 82 | + search_operator = ( |
| 83 | + _query().bm25(query="banana two", operator=operator).bm25_search.search_operator |
| 84 | + ) |
| 85 | + assert search_operator.operator == expected |
| 86 | + assert search_operator.minimum_or_tokens_match == minimum_or_tokens_match |
| 87 | + |
| 88 | + |
| 89 | +@pytest.mark.parametrize( |
| 90 | + "operator,expected", |
| 91 | + [ |
| 92 | + (BM25Operator.and_(), base_search_pb2.SearchOperatorOptions.OPERATOR_AND), |
| 93 | + (BM25Operator.or_(minimum_match=2), base_search_pb2.SearchOperatorOptions.OPERATOR_OR), |
| 94 | + (BM25Operator.and_cross(), base_search_pb2.SearchOperatorOptions.OPERATOR_AND_CROSS), |
| 95 | + ], |
| 96 | +) |
| 97 | +def test_hybrid_bm25_operator_wired_into_request( |
| 98 | + operator: BM25OperatorOptions, expected: "base_search_pb2.SearchOperatorOptions.Operator" |
| 99 | +) -> None: |
| 100 | + req = _query().hybrid(query="banana two", alpha=0.0, bm25_operator=operator) |
| 101 | + assert req.hybrid_search.bm25_search_operator.operator == expected |
| 102 | + |
| 103 | + |
| 104 | +def test_no_operator_leaves_search_operator_unset() -> None: |
| 105 | + assert not _query().bm25(query="banana two").bm25_search.HasField("search_operator") |
| 106 | + req = _query().hybrid(query="banana two", alpha=0.0) |
| 107 | + assert not req.hybrid_search.HasField("bm25_search_operator") |
| 108 | + |
| 109 | + |
| 110 | +@pytest.mark.parametrize("version", _SUPPORTED) |
| 111 | +def test_and_cross_allowed_on_supported_versions(version: str) -> None: |
| 112 | + req = _query(version).bm25(query="banana two", operator=BM25Operator.and_cross()) |
| 113 | + assert ( |
| 114 | + req.bm25_search.search_operator.operator |
| 115 | + == base_search_pb2.SearchOperatorOptions.OPERATOR_AND_CROSS |
| 116 | + ) |
| 117 | + |
| 118 | + |
| 119 | +@pytest.mark.parametrize("version", _UNSUPPORTED) |
| 120 | +def test_and_cross_rejected_on_unsupported_versions(version: str) -> None: |
| 121 | + with pytest.raises(WeaviateUnsupportedFeatureError) as e: |
| 122 | + _query(version).bm25(query="banana two", operator=BM25Operator.and_cross()) |
| 123 | + assert "1.37.15 or 1.38.8 or 1.39.0" in str(e.value) |
| 124 | + assert version in str(e.value) |
| 125 | + |
| 126 | + |
| 127 | +@pytest.mark.parametrize("version", _UNSUPPORTED) |
| 128 | +def test_hybrid_and_cross_rejected_on_unsupported_versions(version: str) -> None: |
| 129 | + with pytest.raises(WeaviateUnsupportedFeatureError): |
| 130 | + _query(version).hybrid( |
| 131 | + query="banana two", alpha=0.0, bm25_operator=BM25Operator.and_cross() |
| 132 | + ) |
| 133 | + |
| 134 | + |
| 135 | +@pytest.mark.parametrize("version", _UNSUPPORTED) |
| 136 | +def test_aggregate_hybrid_and_cross_rejected_on_unsupported_versions(version: str) -> None: |
| 137 | + with pytest.raises(WeaviateUnsupportedFeatureError): |
| 138 | + _aggregate_hybrid(_aggregate(version), BM25Operator.and_cross()) |
| 139 | + |
| 140 | + |
| 141 | +def test_aggregate_hybrid_and_cross_allowed_on_supported_version() -> None: |
| 142 | + hybrid = _aggregate_hybrid(_aggregate("1.39.0"), BM25Operator.and_cross()) |
| 143 | + assert ( |
| 144 | + hybrid.bm25_search_operator.operator |
| 145 | + == base_search_pb2.SearchOperatorOptions.OPERATOR_AND_CROSS |
| 146 | + ) |
| 147 | + |
| 148 | + |
| 149 | +@pytest.mark.parametrize("version", _UNSUPPORTED) |
| 150 | +@pytest.mark.parametrize("operator", [BM25Operator.and_(), BM25Operator.or_(minimum_match=1), None]) |
| 151 | +def test_other_operators_unaffected_by_gate( |
| 152 | + version: str, operator: Optional[BM25OperatorOptions] |
| 153 | +) -> None: |
| 154 | + _query(version).bm25(query="banana two", operator=operator) |
| 155 | + _query(version).hybrid(query="banana two", alpha=0.0, bm25_operator=operator) |
0 commit comments