1
1
from __future__ import annotations
2
2
3
3
from enum import Enum
4
- from typing import (List ,
4
+ from typing import (TYPE_CHECKING ,
5
+ List ,
5
6
Optional ,
6
7
Union )
7
8
8
9
from couchbase ._utils import is_null_or_empty
9
10
from couchbase .exceptions import InvalidArgumentException
10
11
from couchbase .options import VectorSearchOptions
11
12
13
+ if TYPE_CHECKING :
14
+ from couchbase .logic .search_queries import SearchQuery
15
+
12
16
13
17
class VectorQueryCombination (Enum ):
14
18
""" Specifies how multiple vector searches are combined.
@@ -31,6 +35,7 @@ class VectorQuery:
31
35
vector (Union[List[float], str]): The vector to use in the query.
32
36
num_candidates (int, optional): Specifies the number of results returned. If provided, must be greater or equal to 1.
33
37
boost (float, optional): Add boost to query.
38
+ prefilter (`~couchbase.search.SearchQuery`, optional): Specifies a pre-filter to use for the vector query.
34
39
35
40
Raises:
36
41
:class:`~couchbase.exceptions.InvalidArgumentException`: If the vector is not provided.
@@ -46,18 +51,21 @@ def __init__(self,
46
51
vector , # type: Union[List[float], str]
47
52
num_candidates = None , # type: Optional[int]
48
53
boost = None , # type: Optional[float]
54
+ prefilter = None , # type: Optional[SearchQuery]
49
55
):
50
56
if is_null_or_empty (field_name ):
51
57
raise InvalidArgumentException ('Must provide a field name.' )
52
58
self ._field_name = field_name
53
59
self ._vector = None
54
60
self ._vector_base64 = None
55
61
self ._validate_and_set_vector (vector )
56
- self ._num_candidates = self ._boost = None
62
+ self ._num_candidates = self ._boost = self . _prefilter = None
57
63
if num_candidates is not None :
58
64
self .num_candidates = num_candidates
59
65
if boost is not None :
60
66
self .boost = boost
67
+ if prefilter is not None :
68
+ self .prefilter = prefilter
61
69
62
70
@property
63
71
def boost (self ) -> Optional [float ]:
@@ -98,6 +106,23 @@ def num_candidates(self,
98
106
raise InvalidArgumentException ('num_candidates must be >= 1.' )
99
107
self ._num_candidates = value
100
108
109
+ @property
110
+ def prefilter (self ) -> Optional [SearchQuery ]:
111
+ """
112
+ Optional[SearchQuery]: Returns vector query's prefilter query, if it exists.
113
+ """
114
+ return self ._prefilter
115
+
116
+ @prefilter .setter
117
+ def prefilter (self ,
118
+ value # type: SearchQuery
119
+ ):
120
+ # avoid circular import
121
+ from couchbase .logic .search_queries import SearchQuery
122
+ if not isinstance (value , SearchQuery ):
123
+ raise InvalidArgumentException ('prefilter must be a SearchQuery.' )
124
+ self ._prefilter = value
125
+
101
126
@property
102
127
def vector (self ) -> Optional [List [float ]]:
103
128
"""
@@ -138,6 +163,7 @@ def create(cls,
138
163
vector , # type: Union[List[float], str]
139
164
num_candidates = None , # type: Optional[int]
140
165
boost = None , # type: Optional[float]
166
+ prefilter = None , # type: Optional[SearchQuery]
141
167
) -> VectorQuery :
142
168
""" Creates a :class:`~couchbase.vector_search.VectorQuery`.
143
169
@@ -146,6 +172,7 @@ def create(cls,
146
172
vector (Union[List[float], str]): The vector to use in the query.
147
173
num_candidates (int, optional): Specifies the number of results returned. If provided, must be greater or equal to 1.
148
174
boost (float, optional): Add boost to query.
175
+ prefilter (`~couchbase.search.SearchQuery`, optional): Specifies a pre-filter to use for the vector query.
149
176
150
177
Raises:
151
178
:class:`~couchbase.exceptions.InvalidArgumentException`: If the vector is not provided.
@@ -155,7 +182,7 @@ def create(cls,
155
182
Returns:
156
183
:class:`~couchbase.vector_search.VectorQuery`: The created vector query.
157
184
""" # noqa: E501
158
- return cls (field_name , vector , num_candidates = num_candidates , boost = boost )
185
+ return cls (field_name , vector , num_candidates = num_candidates , boost = boost , prefilter = prefilter )
159
186
160
187
161
188
class VectorSearch :
0 commit comments