1+ from enum import Enum
12from typing import Any , Dict , List , Literal , Optional , Union
23
34try :
@@ -35,7 +36,7 @@ def query_string(self) -> str:
3536 def scorer (self , scorer : str ) -> "HybridSearchQuery" :
3637 """
3738 Scoring algorithm for text search query.
38- Allowed values are "TFIDF" or "BM25"
39+ Allowed values are "TFIDF", "DISMAX", "DOCSCORE", "BM25", etc.
3940 """
4041 self ._scorer = scorer
4142 return self
@@ -56,12 +57,17 @@ def get_args(self) -> List[str]:
5657 return args
5758
5859
60+ class VectorSearchMethods (Enum ):
61+ KNN = "KNN"
62+ RANGE = "RANGE"
63+
64+
5965class HybridVsimQuery :
6066 def __init__ (
6167 self ,
6268 vector_field_name : str ,
6369 vector_data : Union [bytes , str ],
64- vsim_search_method : Optional [str ] = None ,
70+ vsim_search_method : Optional [VectorSearchMethods ] = None ,
6571 vsim_search_method_params : Optional [Dict [str , Any ]] = None ,
6672 filter : Optional ["Filter" ] = None ,
6773 ) -> None :
@@ -70,11 +76,22 @@ def __init__(
7076
7177 Args:
7278 vector_field_name: Vector field name.
79+
7380 vector_data: Vector data for the search.
81+
7482 vsim_search_method: Search method that will be used for the vsim search.
75- Allowed values are "KNN" or "RANGE".
83+
7684 vsim_search_method_params: Search method parameters. Use the param names
77- for keys and the values for the values. Example: {"K": 10, "EF_RUNTIME": 100}.
85+ for keys and the values for the values.
86+ Example for KNN: {"K": 10, "EF_RUNTIME": 100}
87+ where K is mandatory and defines the number of results
88+ and EF_RUNTIME is optional and definesthe exploration factor.
89+ Example for RANGE: {"RADIUS": 10, "EPSILON": 0.1}
90+ where RADIUS is mandatory and defines the radius of the search
91+ and EPSILON is optional and defines the accuracy of the search.
92+ For both KNN and RANGE, the following parameter is optional:
93+ YIELD_SCORE_AS: The name of the field to yield the calculated score as.
94+
7895 filter: If defined, a filter will be applied on the vsim query results.
7996 """
8097 self ._vector_field = vector_field_name
@@ -95,7 +112,7 @@ def vector_data(self) -> Union[bytes, str]:
95112
96113 def vsim_method_params (
97114 self ,
98- method : str ,
115+ method : VectorSearchMethods ,
99116 ** kwargs ,
100117 ) -> "HybridVsimQuery" :
101118 """
@@ -106,7 +123,7 @@ def vsim_method_params(
106123 kwargs: Search method parameters. Use the param names for keys and the
107124 values for the values. Example: {"K": 10, "EF_RUNTIME": 100}.
108125 """
109- vsim_method_params : List [Union [str , int ]] = [method ]
126+ vsim_method_params : List [Union [str , int ]] = [method . value ]
110127 if kwargs :
111128 vsim_method_params .append (len (kwargs .items ()) * 2 )
112129 for key , value in kwargs .items ():
@@ -158,40 +175,44 @@ def get_args(self) -> List[str]:
158175 return args
159176
160177
178+ class CombinationMethods (Enum ):
179+ RRF = "RRF"
180+ LINEAR = "LINEAR"
181+
182+
183+ class CombineResultsMethod :
184+ def __init__ (self , method : CombinationMethods , ** kwargs ) -> None :
185+ """
186+ Create a new combine results method object.
187+
188+ Args:
189+ method: The combine method to use - RRF or LINEAR.
190+ kwargs: Additional combine parameters.
191+ """
192+ self ._method = method
193+ self ._kwargs = kwargs
194+
195+ def get_args (self ) -> List [Union [str , int ]]:
196+ args : List [Union [str , int ]] = ["COMBINE" , self ._method .value ]
197+ if self ._kwargs :
198+ args .append (len (self ._kwargs .items ()) * 2 )
199+ for key , value in self ._kwargs .items ():
200+ args .extend ((key , value ))
201+ return args
202+
203+
161204class HybridPostProcessingConfig :
162205 def __init__ (self ) -> None :
163206 """
164207 Create a new hybrid post processing configuration object.
165208 """
166- self ._combine = []
167209 self ._load_fields = []
168210 self ._groupby = []
169211 self ._apply = []
170212 self ._sortby_fields = []
171213 self ._filter = None
172214 self ._limit = None
173215
174- def combine (
175- self ,
176- method : Literal ["RRF" , "LINEAR" ],
177- ** kwargs ,
178- ) -> Self :
179- """
180- Add combine parameters to the query.
181-
182- Args:
183- method: The combine method to use - RRF or LINEAR.
184- kwargs: Additional combine parameters.
185- """
186- self ._combine : List [Union [str , int ]] = [method ]
187-
188- self ._combine .append (len (kwargs ) * 2 )
189-
190- for key , value in kwargs .items ():
191- self ._combine .extend ([key , value ])
192-
193- return self
194-
195216 def load (self , * fields : str ) -> Self :
196217 """
197218 Add load parameters to the query.
@@ -267,8 +288,6 @@ def limit(self, offset: int, num: int) -> Self:
267288
268289 def build_args (self ) -> List [str ]:
269290 args = []
270- if self ._combine :
271- args .extend (("COMBINE" , * self ._combine ))
272291 if self ._load_fields :
273292 fields_str = " " .join (self ._load_fields )
274293 fields = fields_str .split (" " )
0 commit comments