@@ -120,6 +120,7 @@ def __init__(self, using="default", index=None, doc_type=None, extra=None):
120
120
121
121
self ._doc_type = []
122
122
self ._doc_type_map = {}
123
+ self ._collapse = {}
123
124
if isinstance (doc_type , (tuple , list )):
124
125
self ._doc_type .extend (doc_type )
125
126
elif isinstance (doc_type , collections .abc .Mapping ):
@@ -293,6 +294,7 @@ def _clone(self):
293
294
s = self .__class__ (
294
295
using = self ._using , index = self ._index , doc_type = self ._doc_type
295
296
)
297
+ s ._collapse = self ._collapse .copy ()
296
298
s ._doc_type_map = self ._doc_type_map .copy ()
297
299
s ._extra = self ._extra .copy ()
298
300
s ._params = self ._params .copy ()
@@ -318,6 +320,7 @@ def __init__(self, **kwargs):
318
320
319
321
self .aggs = AggsProxy (self )
320
322
self ._sort = []
323
+ self ._collapse = {}
321
324
self ._source = None
322
325
self ._highlight = {}
323
326
self ._highlight_opts = {}
@@ -568,6 +571,27 @@ def sort(self, *keys):
568
571
s ._sort .append (k )
569
572
return s
570
573
574
+ def collapse (self , field = None , inner_hits = None , max_concurrent_group_searches = None ):
575
+ """
576
+ Add collapsing information to the search request.
577
+ If called without providing ``field``, it will remove all collapse
578
+ requirements, otherwise it will replace them with the provided
579
+ arguments.
580
+ The API returns a copy of the Search object and can thus be chained.
581
+ """
582
+ s = self ._clone ()
583
+ s ._collapse = {}
584
+
585
+ if field is None :
586
+ return s
587
+
588
+ s ._collapse ["field" ] = field
589
+ if inner_hits :
590
+ s ._collapse ["inner_hits" ] = inner_hits
591
+ if max_concurrent_group_searches :
592
+ s ._collapse ["max_concurrent_group_searches" ] = max_concurrent_group_searches
593
+ return s
594
+
571
595
def highlight_options (self , ** kwargs ):
572
596
"""
573
597
Update the global highlighting options used for this request. For
@@ -663,6 +687,9 @@ def to_dict(self, count=False, **kwargs):
663
687
if self ._sort :
664
688
d ["sort" ] = self ._sort
665
689
690
+ if self ._collapse :
691
+ d ["collapse" ] = self ._collapse
692
+
666
693
d .update (recursive_to_dict (self ._extra ))
667
694
668
695
if self ._source not in (None , {}):
0 commit comments