return distance in response

This commit is contained in:
mertalev
2025-10-13 08:53:56 -04:00
parent 46869f664d
commit a459632420
11 changed files with 654 additions and 7 deletions

View File

@@ -130,6 +130,7 @@ export type MapAsset = {
tags?: Tag[];
thumbhash: Buffer<ArrayBufferLike> | null;
type: AssetType;
distance?: number;
};
export class AssetStackResponseDto {

View File

@@ -296,12 +296,16 @@ class SearchAlbumResponseDto {
facets!: SearchFacetResponseDto[];
}
class AssetSearchResponseDto extends AssetResponseDto {
distance?: number;
}
class SearchAssetResponseDto {
@ApiProperty({ type: 'integer' })
total!: number;
@ApiProperty({ type: 'integer' })
count!: number;
items!: AssetResponseDto[];
items!: AssetSearchResponseDto[];
facets!: SearchFacetResponseDto[];
nextPage!: string | null;
}

View File

@@ -284,8 +284,9 @@ export class SearchRepository {
await sql`set local vchordrq.probes = ${sql.lit(probes[VectorIndex.Clip])}`.execute(trx);
const items = await searchAssetBuilder(trx, options)
.selectAll('asset')
.select(sql<number>`smart_search.embedding <=> ${options.embedding}`.as('distance'))
.innerJoin('smart_search', 'asset.id', 'smart_search.assetId')
.orderBy(sql`smart_search.embedding <=> ${options.embedding}`)
.orderBy('distance')
.limit(pagination.size + 1)
.offset((pagination.page - 1) * pagination.size)
.execute();

View File

@@ -198,7 +198,7 @@ export class SearchService extends BaseService {
assets: {
total: assets.length,
count: assets.length,
items: assets.map((asset) => mapAsset(asset, options)),
items: assets.map((asset) => ({ ...mapAsset(asset, options), distance: asset.distance })),
facets: [],
nextPage,
},