@@ -318,8 +318,9 @@ def __init__(self, **kwargs):
318
318
319
319
self .aggs = AggsProxy (self )
320
320
self ._sort = []
321
- self ._collapse = {}
322
321
self ._knn = []
322
+ self ._rank = {}
323
+ self ._collapse = {}
323
324
self ._source = None
324
325
self ._highlight = {}
325
326
self ._highlight_opts = {}
@@ -408,6 +409,7 @@ def _clone(self):
408
409
409
410
s ._response_class = self ._response_class
410
411
s ._knn = [knn .copy () for knn in self ._knn ]
412
+ s ._rank = self ._rank .copy ()
411
413
s ._collapse = self ._collapse .copy ()
412
414
s ._sort = self ._sort [:]
413
415
s ._source = copy .copy (self ._source ) if self ._source is not None else None
@@ -451,6 +453,8 @@ def update_from_dict(self, d):
451
453
self ._knn = d .pop ("knn" )
452
454
if isinstance (self ._knn , dict ):
453
455
self ._knn = [self ._knn ]
456
+ if "rank" in d :
457
+ self ._rank = d .pop ("rank" )
454
458
if "collapse" in d :
455
459
self ._collapse = d .pop ("collapse" )
456
460
if "sort" in d :
@@ -558,6 +562,27 @@ def knn(
558
562
s ._knn [- 1 ]["similarity" ] = similarity
559
563
return s
560
564
565
+ def rank (self , rrf = None ):
566
+ """
567
+ Defines a method for combining and ranking results sets from a combination
568
+ of searches. Requires a minimum of 2 results sets.
569
+
570
+ :arg rrf: Set to ``True`` or an options dictionary to set the rank method to reciprocal rank fusion (RRF).
571
+
572
+ Example::
573
+ s = Search()
574
+ s = s.query('match', content='search text')
575
+ s = s.knn(field='embedding', k=5, num_candidates=10, query_vector=vector)
576
+ s = s.rank(rrf=True)
577
+
578
+ Note: This option is in technical preview and may change in the future. The syntax will likely change before GA.
579
+ """
580
+ s = self ._clone ()
581
+ s ._rank = {}
582
+ if rrf is not None and rrf is not False :
583
+ s ._rank ["rrf" ] = {} if rrf is True else rrf
584
+ return s
585
+
561
586
def source (self , fields = None , ** kwargs ):
562
587
"""
563
588
Selectively control how the _source field is returned.
@@ -747,6 +772,9 @@ def to_dict(self, count=False, **kwargs):
747
772
else :
748
773
d ["knn" ] = self ._knn
749
774
775
+ if self ._rank :
776
+ d ["rank" ] = self ._rank
777
+
750
778
# count request doesn't care for sorting and other things
751
779
if not count :
752
780
if self .post_filter :
0 commit comments