From ac2ebcee37a9f18ac5aedf27cb7a776fb820e8cb Mon Sep 17 00:00:00 2001 From: Steven Massaro Date: Thu, 16 Apr 2026 10:36:05 -0400 Subject: [PATCH] chore: improve randomness of /search/random endpoint (#27531) --- server/src/queries/search.repository.sql | 55 ++++++-------------- server/src/repositories/search.repository.ts | 17 ++---- 2 files changed, 18 insertions(+), 54 deletions(-) diff --git a/server/src/queries/search.repository.sql b/server/src/queries/search.repository.sql index 701d30fa58..3e75d88af8 100644 --- a/server/src/queries/search.repository.sql +++ b/server/src/queries/search.repository.sql @@ -35,47 +35,22 @@ where and "asset"."deletedAt" is null -- SearchRepository.searchRandom -( - select - "asset".* - from - "asset" - inner join "asset_exif" on "asset"."id" = "asset_exif"."assetId" - where - "asset"."visibility" = $1 - and "asset"."fileCreatedAt" >= $2 - and "asset_exif"."lensModel" = $3 - and "asset"."ownerId" = any ($4::uuid[]) - and "asset"."isFavorite" = $5 - and "asset"."deletedAt" is null - and "asset"."id" < $6 - order by - random() - limit - $7 -) -union all -( - select - "asset".* - from - "asset" - inner join "asset_exif" on "asset"."id" = "asset_exif"."assetId" - where - "asset"."visibility" = $8 - and "asset"."fileCreatedAt" >= $9 - and "asset_exif"."lensModel" = $10 - and "asset"."ownerId" = any ($11::uuid[]) - and "asset"."isFavorite" = $12 - and "asset"."deletedAt" is null - and "asset"."id" > $13 - order by - random() - limit - $14 -) +select + "asset".* +from + "asset" + inner join "asset_exif" on "asset"."id" = "asset_exif"."assetId" +where + "asset"."visibility" = $1 + and "asset"."fileCreatedAt" >= $2 + and "asset_exif"."lensModel" = $3 + and "asset"."ownerId" = any ($4::uuid[]) + and "asset"."isFavorite" = $5 + and "asset"."deletedAt" is null +order by + random() limit - $15 + $6 -- SearchRepository.searchLargeAssets select diff --git a/server/src/repositories/search.repository.ts b/server/src/repositories/search.repository.ts index 171102a660..6f03c80ce1 100644 --- a/server/src/repositories/search.repository.ts +++ b/server/src/repositories/search.repository.ts @@ -1,9 +1,7 @@ import { Injectable } from '@nestjs/common'; import { Kysely, OrderByDirection, Selectable, ShallowDehydrateObject, sql } from 'kysely'; import { InjectKysely } from 'nestjs-kysely'; -import { randomUUID } from 'node:crypto'; import { DummyValue, GenerateSql } from 'src/decorators'; -import { MapAsset } from 'src/dtos/asset-response.dto'; import { AssetStatus, AssetType, AssetVisibility, VectorIndex } from 'src/enum'; import { probes } from 'src/repositories/database.repository'; import { DB } from 'src/schema'; @@ -236,20 +234,11 @@ export class SearchRepository { ], }) async searchRandom(size: number, options: AssetSearchOptions) { - const uuid = randomUUID(); - const builder = searchAssetBuilder(this.db, options); - const lessThan = builder + return searchAssetBuilder(this.db, options) .selectAll('asset') - .where('asset.id', '<', uuid) .orderBy(sql`random()`) - .limit(size); - const greaterThan = builder - .selectAll('asset') - .where('asset.id', '>', uuid) - .orderBy(sql`random()`) - .limit(size); - const { rows } = await sql`${lessThan} union all ${greaterThan} limit ${size}`.execute(this.db); - return rows; + .limit(size) + .execute(); } @GenerateSql({