mirror of
https://github.com/immich-app/immich.git
synced 2025-05-31 20:25:32 -04:00
fix(server): /search/random API returns same assets every call (#15682)
* Fix for server searchRandom function not returning random results * Fix lint
This commit is contained in:
parent
f780a56e24
commit
206412267a
@ -36,7 +36,7 @@ offset
|
|||||||
and "assets"."deletedAt" is null
|
and "assets"."deletedAt" is null
|
||||||
and "assets"."id" < $6
|
and "assets"."id" < $6
|
||||||
order by
|
order by
|
||||||
"assets"."id"
|
random()
|
||||||
limit
|
limit
|
||||||
$7
|
$7
|
||||||
)
|
)
|
||||||
@ -56,7 +56,7 @@ union all
|
|||||||
and "assets"."deletedAt" is null
|
and "assets"."deletedAt" is null
|
||||||
and "assets"."id" > $13
|
and "assets"."id" > $13
|
||||||
order by
|
order by
|
||||||
"assets"."id"
|
random()
|
||||||
limit
|
limit
|
||||||
$14
|
$14
|
||||||
)
|
)
|
||||||
|
@ -72,8 +72,14 @@ export class SearchRepository implements ISearchRepository {
|
|||||||
async searchRandom(size: number, options: AssetSearchOptions): Promise<AssetEntity[]> {
|
async searchRandom(size: number, options: AssetSearchOptions): Promise<AssetEntity[]> {
|
||||||
const uuid = randomUUID();
|
const uuid = randomUUID();
|
||||||
const builder = searchAssetBuilder(this.db, options);
|
const builder = searchAssetBuilder(this.db, options);
|
||||||
const lessThan = builder.where('assets.id', '<', uuid).orderBy('assets.id').limit(size);
|
const lessThan = builder
|
||||||
const greaterThan = builder.where('assets.id', '>', uuid).orderBy('assets.id').limit(size);
|
.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);
|
const { rows } = await sql`${lessThan} union all ${greaterThan} limit ${size}`.execute(this.db);
|
||||||
return rows as any as AssetEntity[];
|
return rows as any as AssetEntity[];
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user