@@ -174,14 +174,21 @@ export class CollectionManager {
174174
175175 public async search < R = AnyObject > ( query : SearchParams , init ?: ClientRequestInit ) : Promise < SearchResult < R > > {
176176 const start = Date . now ( )
177- const { datasourceIDs, indexes, ...restQuery } = query
177+ const { datasourceIDs, indexes, groupBy, ...restQuery } = query
178+
179+ // Extract sortBy from groupBy (client-side only, not sent to backend)
180+ const groupsSortBy = groupBy ?. sortBy
181+ const groupByForApi = groupBy
182+ ? { properties : groupBy . properties , max_results : groupBy . max_results }
183+ : undefined
178184
179185 const result = await this . client . request < Omit < SearchResult < R > , 'elapsed' > > ( {
180186 path : `/v1/collections/${ this . collectionID } /search` ,
181187 body : {
182188 userID : this . profile ?. getUserId ( ) || undefined ,
183189 ...restQuery , // restQuery can override `userID`
184190 indexes : datasourceIDs || indexes ,
191+ groupBy : groupByForApi ,
185192 } ,
186193 method : 'POST' ,
187194 params : undefined ,
@@ -190,6 +197,15 @@ export class CollectionManager {
190197 target : 'reader' ,
191198 } )
192199
200+ // Sort groups by score of first element if requested
201+ if ( groupsSortBy === 'score' && result . groups ) {
202+ result . groups . sort ( ( a , b ) => {
203+ const scoreA = a . result [ 0 ] ?. score ?? 0
204+ const scoreB = b . result [ 0 ] ?. score ?? 0
205+ return scoreB - scoreA // Descending order
206+ } )
207+ }
208+
193209 const elapsed = Date . now ( ) - start
194210
195211 return {
0 commit comments