Skip to content

Commit

Permalink
fix(server): /search/random API returns same assets every call (#15682)
Browse files Browse the repository at this point in the history
* Fix for server searchRandom function not returning random results

* Fix lint
  • Loading branch information
sudbrack authored Jan 26, 2025
1 parent f780a56 commit 2064122
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 4 deletions.
4 changes: 2 additions & 2 deletions server/src/queries/search.repository.sql
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ offset
and "assets"."deletedAt" is null
and "assets"."id" < $6
order by
"assets"."id"
random()
limit
$7
)
Expand All @@ -56,7 +56,7 @@ union all
and "assets"."deletedAt" is null
and "assets"."id" > $13
order by
"assets"."id"
random()
limit
$14
)
Expand Down
10 changes: 8 additions & 2 deletions server/src/repositories/search.repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,14 @@ export class SearchRepository implements ISearchRepository {
async searchRandom(size: number, options: AssetSearchOptions): Promise<AssetEntity[]> {
const uuid = randomUUID();
const builder = searchAssetBuilder(this.db, options);
const lessThan = builder.where('assets.id', '<', uuid).orderBy('assets.id').limit(size);
const greaterThan = builder.where('assets.id', '>', uuid).orderBy('assets.id').limit(size);
const lessThan = builder
.where('assets.id', '<', uuid)
.orderBy(sql`random()`)
.limit(size);
const greaterThan = builder
.where('assets.id', '>', uuid)
.orderBy(sql`random()`)
.limit(size);
const { rows } = await sql`${lessThan} union all ${greaterThan} limit ${size}`.execute(this.db);
return rows as any as AssetEntity[];
}
Expand Down

0 comments on commit 2064122

Please sign in to comment.