Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: extend smart search, match exif-description too #14380

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions server/src/interfaces/search.interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,10 @@ export interface SearchExifOptions {
state?: string | null;
}

export interface SearchTextOptions {
query?: string;
}

export interface SearchEmbeddingOptions {
embedding: number[];
userIds: string[];
Expand Down Expand Up @@ -140,6 +144,7 @@ export type AssetSearchBuilderOptions = Omit<AssetSearchOptions, 'orderDirection
export type SmartSearchOptions = SearchDateOptions &
SearchEmbeddingOptions &
SearchExifOptions &
SearchTextOptions &
SearchOneToOneRelationOptions &
SearchStatusOptions &
SearchUserIdOptions &
Expand Down
51 changes: 44 additions & 7 deletions server/src/queries/search.repository.sql
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,35 @@ SELECT
"asset"."sidecarPath" AS "asset_sidecarPath",
"asset"."stackId" AS "asset_stackId",
"asset"."duplicateId" AS "asset_duplicateId",
"exif"."assetId" AS "exif_assetId",
"exif"."description" AS "exif_description",
"exif"."exifImageWidth" AS "exif_exifImageWidth",
"exif"."exifImageHeight" AS "exif_exifImageHeight",
"exif"."fileSizeInByte" AS "exif_fileSizeInByte",
"exif"."orientation" AS "exif_orientation",
"exif"."dateTimeOriginal" AS "exif_dateTimeOriginal",
"exif"."modifyDate" AS "exif_modifyDate",
"exif"."timeZone" AS "exif_timeZone",
"exif"."latitude" AS "exif_latitude",
"exif"."longitude" AS "exif_longitude",
"exif"."projectionType" AS "exif_projectionType",
"exif"."city" AS "exif_city",
"exif"."livePhotoCID" AS "exif_livePhotoCID",
"exif"."autoStackId" AS "exif_autoStackId",
"exif"."state" AS "exif_state",
"exif"."country" AS "exif_country",
"exif"."make" AS "exif_make",
"exif"."model" AS "exif_model",
"exif"."lensModel" AS "exif_lensModel",
"exif"."fNumber" AS "exif_fNumber",
"exif"."focalLength" AS "exif_focalLength",
"exif"."iso" AS "exif_iso",
"exif"."exposureTime" AS "exif_exposureTime",
"exif"."profileDescription" AS "exif_profileDescription",
"exif"."colorspace" AS "exif_colorspace",
"exif"."bitsPerSample" AS "exif_bitsPerSample",
"exif"."rating" AS "exif_rating",
"exif"."fps" AS "exif_fps",
"stack"."id" AS "stack_id",
"stack"."ownerId" AS "stack_ownerId",
"stack"."primaryAssetId" AS "stack_primaryAssetId",
Expand Down Expand Up @@ -339,29 +368,37 @@ SELECT
"stackedAssets"."originalFileName" AS "stackedAssets_originalFileName",
"stackedAssets"."sidecarPath" AS "stackedAssets_sidecarPath",
"stackedAssets"."stackId" AS "stackedAssets_stackId",
"stackedAssets"."duplicateId" AS "stackedAssets_duplicateId"
"stackedAssets"."duplicateId" AS "stackedAssets_duplicateId",
"search"."embedding" <= > $1 AS "similarity"
FROM
"assets" "asset"
LEFT JOIN "exif" "exif" ON "exif"."assetId" = "asset"."id"
LEFT JOIN "exif" "exifInfo" ON "exifInfo"."assetId" = "asset"."id"
LEFT JOIN "asset_stack" "stack" ON "stack"."id" = "asset"."stackId"
LEFT JOIN "assets" "stackedAssets" ON "stackedAssets"."stackId" = "stack"."id"
AND ("stackedAssets"."deletedAt" IS NULL)
INNER JOIN "smart_search" "search" ON "search"."assetId" = "asset"."id"
WHERE
(
"asset"."fileCreatedAt" >= $1
AND "exifInfo"."lensModel" = $2
"asset"."fileCreatedAt" >= $2
AND "exifInfo"."lensModel" = $3
AND 1 = 1
AND 1 = 1
AND (
"asset"."isFavorite" = $3
AND "asset"."isArchived" = $4
"asset"."isFavorite" = $4
AND "asset"."isArchived" = $5
)
AND "asset"."ownerId" IN ($5)
AND "asset"."ownerId" IN ($6)
)
AND ("asset"."deletedAt" IS NULL)
ORDER BY
"search"."embedding" <= > $6 ASC
CASE
WHEN to_tsvector('english', COALESCE("exif"."description", '')) @@ plainto_tsquery('english', $7) THEN ts_rank(
to_tsvector('english', COALESCE("exif"."description", '')),
plainto_tsquery('english', $7)
)
ELSE 0
END * 0.7 + COALESCE((1 - ("search"."embedding" <= > $1)), 0) * 0.3 DESC
LIMIT
201
COMMIT
Expand Down
43 changes: 36 additions & 7 deletions server/src/repositories/search.repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ export class SearchRepository implements ISearchRepository {
{
takenAfter: DummyValue.DATE,
embedding: Array.from({ length: 512 }, Math.random),
query: 'beach vacation',
lensModel: DummyValue.STRING,
withStacked: true,
isFavorite: true,
Expand All @@ -124,23 +125,51 @@ export class SearchRepository implements ISearchRepository {
})
async searchSmart(
pagination: SearchPaginationOptions,
{ embedding, userIds, ...options }: SmartSearchOptions,
{ embedding, query, userIds, ...options }: SmartSearchOptions,
): Paginated<AssetEntity> {
let results: PaginationResult<AssetEntity> = { items: [], hasNextPage: false };

await this.assetRepository.manager.transaction(async (manager) => {
let builder = manager.createQueryBuilder(AssetEntity, 'asset');
let builder = manager.createQueryBuilder(AssetEntity, 'asset').leftJoinAndSelect('asset.exifInfo', 'exif');
builder = searchAssetBuilder(builder, options);
builder
.innerJoin('asset.smartSearch', 'search')
.andWhere('asset.ownerId IN (:...userIds )')
.orderBy('search.embedding <=> :embedding')
.setParameters({ userIds, embedding: asVector(embedding) });
builder.andWhere('asset.ownerId IN (:...userIds)');

const parameters: Record<string, any> = { userIds };

if (embedding) {
builder.innerJoin('asset.smartSearch', 'search').addSelect('search.embedding <=> :embedding', 'similarity');
parameters.embedding = asVector(embedding);
}

if (query) {
parameters.query = query;
}

if (query && embedding) {
builder.orderBy(
`
CASE WHEN to_tsvector('english', COALESCE(exif.description, '')) @@ plainto_tsquery('english', :query)
THEN
ts_rank(
to_tsvector('english', COALESCE(exif.description, '')),
plainto_tsquery('english', :query)
)
ELSE 0 END * 0.7 +
COALESCE((1 - (search.embedding <=> :embedding)), 0) * 0.3
`,
'DESC',
);
} else if (embedding) {
builder.orderBy('search.embedding <=> :embedding', 'ASC');
}

builder.setParameters(parameters);

const runtimeConfig = this.getRuntimeConfig(pagination.size);
if (runtimeConfig) {
await manager.query(runtimeConfig);
}

results = await paginatedBuilder<AssetEntity>(builder, {
mode: PaginationMode.LIMIT_OFFSET,
skip: (pagination.page - 1) * pagination.size,
Expand Down
Loading