@@ -229,28 +229,41 @@ def __post_init__(self):
229229
230230
231231@dataclass
232- class HybridSearchNamespace :
233- """Namespace containing all parameters for hybrid search operations.
232+ class HybridSearchParameters :
233+ """Parameters for hybrid (vector + keyword) search operations.
234234
235235 Args:
236236 vector: Parameters for the vector search component.
237237 keyword: Parameters for the keyword search component.
238- hybrid: Parameters for combining the vector and keyword results.
238+ ranker: Ranker for combining vector and keyword search results.
239+ Example: RRFRanker(k=100).
240+ limit: Maximum number of results to return per query. Defaults to 3 search
241+ results.
242+ kwargs: Optional keyword arguments for additional hybrid search parameters.
243+ Enables forward compatibility.
239244 """
240245 vector : VectorSearchParameters
241246 keyword : KeywordSearchParameters
242- hybrid : HybridSearchParameters
247+ ranker : MilvusBaseRanker
248+ limit : int = 3
249+ kwargs : Dict [str , Any ] = field (default_factory = dict )
243250
244251 def __post_init__ (self ):
245- if not self .vector or not self .keyword or not self . hybrid :
252+ if not self .vector or not self .keyword :
246253 raise ValueError (
247- "Vector, keyword, and hybrid search parameters must be provided for "
254+ "Vector and keyword search parameters must be provided for "
248255 "hybrid search" )
249256
257+ if not self .ranker :
258+ raise ValueError ("Ranker must be provided for hybrid search" )
259+
260+ if self .limit <= 0 :
261+ raise ValueError (f"Search limit must be positive, got { self .limit } " )
262+
250263
251264SearchStrategyType = Union [VectorSearchParameters ,
252265 KeywordSearchParameters ,
253- HybridSearchNamespace ]
266+ HybridSearchParameters ]
254267
255268
256269@dataclass
@@ -315,6 +328,22 @@ class MilvusCollectionLoadParameters:
315328 kwargs : Dict [str , Any ] = field (default_factory = dict )
316329
317330
331+ @dataclass
332+ class MilvusSearchResult :
333+ """Search result from Milvus per chunk.
334+
335+ Args:
336+ id: List of entity IDs returned from the search. Can be either string or
337+ integer IDs.
338+ distance: List of distances/similarity scores for each returned entity.
339+ fields: List of dictionaries containing additional field values for each
340+ entity. Each dictionary corresponds to one returned entity.
341+ """
342+ id : List [Union [str , int ]] = field (default_factory = list )
343+ distance : List [float ] = field (default_factory = list )
344+ fields : List [Dict [str , Any ]] = field (default_factory = list )
345+
346+
318347InputT , OutputT = Union [Chunk , List [Chunk ]], List [Tuple [Chunk , Dict [str , Any ]]]
319348
320349
@@ -343,8 +372,8 @@ def __init__(
343372 self ,
344373 connection_parameters : MilvusConnectionParameters ,
345374 search_parameters : MilvusSearchParameters ,
346- collection_load_parameters : MilvusCollectionLoadParameters ,
347375 * ,
376+ collection_load_parameters : Optional [MilvusCollectionLoadParameters ],
348377 min_batch_size : int = 1 ,
349378 max_batch_size : int = 1000 ,
350379 ** kwargs ):
@@ -360,7 +389,7 @@ def __init__(
360389 milvus_handler = MilvusSearchEnrichmentHandler(
361390 connection_paramters,
362391 search_parameters,
363- collection_load_parameters,
392+ collection_load_parameters=collection_load_parameters ,
364393 min_batch_size=10,
365394 max_batch_size=100)
366395
@@ -371,8 +400,8 @@ def __init__(
371400 search_parameters (MilvusSearchParameters): Configuration for search
372401 operations, including collection name, search strategy, and output
373402 fields.
374- collection_load_parameters (MilvusCollectionLoadParameters): Parameters
375- controlling how collections are loaded into memory, which can
403+ collection_load_parameters (Optional[ MilvusCollectionLoadParameters]):
404+ Parameters controlling how collections are loaded into memory, which can
376405 significantly impact resource usage and performance.
377406 min_batch_size (int): Minimum number of elements to batch together when
378407 querying Milvus. Default is 1 (no batching when max_batch_size is 1).
@@ -390,22 +419,25 @@ def __init__(
390419 self ._connection_parameters = connection_parameters
391420 self ._search_parameters = search_parameters
392421 self ._collection_load_parameters = collection_load_parameters
393- self .kwargs = kwargs
422+ if not self ._collection_load_parameters :
423+ self ._collection_load_parameters = MilvusCollectionLoadParameters ()
394424 self ._batching_kwargs = {
395425 'min_batch_size' : min_batch_size , 'max_batch_size' : max_batch_size
396426 }
427+ self .kwargs = kwargs
397428 self .join_fn = join_fn
398429 self .use_custom_types = True
399430
400431 def __enter__ (self ):
401- connectionParams = unpack_dataclass_with_kwargs (self ._connection_parameters )
402- loadCollectionParams = unpack_dataclass_with_kwargs (
432+ connection_params = unpack_dataclass_with_kwargs (
433+ self ._connection_parameters )
434+ collection_load_params = unpack_dataclass_with_kwargs (
403435 self ._collection_load_parameters )
404- self ._client = MilvusClient (** connectionParams )
436+ self ._client = MilvusClient (** connection_params )
405437 self ._client .load_collection (
406438 collection_name = self .collection_name ,
407439 partition_names = self .partition_names ,
408- ** loadCollectionParams )
440+ ** collection_load_params )
409441
410442 def __call__ (self , request : Union [Chunk , List [Chunk ]], * args ,
411443 ** kwargs ) -> List [Tuple [Chunk , Dict [str , Any ]]]:
@@ -414,18 +446,18 @@ def __call__(self, request: Union[Chunk, List[Chunk]], *args,
414446 return self ._get_call_response (reqs , search_result )
415447
416448 def _search_documents (self , chunks : List [Chunk ]):
417- if isinstance (self .search_strategy , HybridSearchNamespace ):
449+ if isinstance (self .search_strategy , HybridSearchParameters ):
418450 data = self ._get_hybrid_search_data (chunks )
419- hybrid_search_params = unpack_dataclass_with_kwargs (
420- self .search_strategy .hybrid )
421451 return self ._client .hybrid_search (
422452 collection_name = self .collection_name ,
423453 partition_names = self .partition_names ,
424454 output_fields = self .output_fields ,
425455 timeout = self .timeout ,
426456 round_decimal = self .round_decimal ,
427457 reqs = data ,
428- ** hybrid_search_params )
458+ ranker = self .search_strategy .ranker ,
459+ limit = self .search_strategy .limit ,
460+ ** self .search_strategy .kwargs )
429461 elif isinstance (self .search_strategy , VectorSearchParameters ):
430462 data = list (map (self ._get_vector_search_data , chunks ))
431463 vector_search_params = unpack_dataclass_with_kwargs (self .search_strategy )
@@ -497,14 +529,14 @@ def _get_call_response(
497529 for i in range (len (chunks )):
498530 chunk = chunks [i ]
499531 hits : Hits = search_result [i ]
500- result = defaultdict ( list )
532+ result = MilvusSearchResult ( )
501533 for i in range (len (hits )):
502534 hit : Hit = hits [i ]
503535 normalized_fields = self ._normalize_milvus_fields (hit .fields )
504- result [ "id" ] .append (hit .id )
505- result [ " distance" ] .append (hit .distance )
506- result [ " fields" ] .append (normalized_fields )
507- response .append ((chunk , result ))
536+ result . id .append (hit .id )
537+ result . distance .append (hit .distance )
538+ result . fields .append (normalized_fields )
539+ response .append ((chunk , result . __dict__ ))
508540 return response
509541
510542 def _normalize_milvus_fields (self , fields : Dict [str , Any ]):
0 commit comments