mirror of
https://github.com/samsonjs/immich.git
synced 2026-04-27 15:07:45 +00:00
feat: view similar photos (#21108)
* Enable filteing by example * Drop `@GenerateSql` for `getEmbedding`? * Improve error message * PR Feedback * Sort en.json * Add SQL * Fix lint * Drop test that is no longer valid * Fix i18n file sorting * Fix TS error * Add a `requireAccess` before pulling the embedding * Fix decorators * Run `make open-api` --------- Co-authored-by: Alex <alex.tran1502@gmail.com>
This commit is contained in:
parent
bf6211776f
commit
37a79292c0
12 changed files with 73 additions and 23 deletions
|
|
@ -1557,6 +1557,7 @@
|
||||||
"purchase_server_description_2": "Supporter status",
|
"purchase_server_description_2": "Supporter status",
|
||||||
"purchase_server_title": "Server",
|
"purchase_server_title": "Server",
|
||||||
"purchase_settings_server_activated": "The server product key is managed by the admin",
|
"purchase_settings_server_activated": "The server product key is managed by the admin",
|
||||||
|
"query_asset_id": "Query Asset ID",
|
||||||
"queue_status": "Queuing {count}/{total}",
|
"queue_status": "Queuing {count}/{total}",
|
||||||
"rating": "Star rating",
|
"rating": "Star rating",
|
||||||
"rating_clear": "Clear rating",
|
"rating_clear": "Clear rating",
|
||||||
|
|
@ -2077,6 +2078,7 @@
|
||||||
"view_next_asset": "View next asset",
|
"view_next_asset": "View next asset",
|
||||||
"view_previous_asset": "View previous asset",
|
"view_previous_asset": "View previous asset",
|
||||||
"view_qr_code": "View QR code",
|
"view_qr_code": "View QR code",
|
||||||
|
"view_similar_photos": "View similar photos",
|
||||||
"view_stack": "View Stack",
|
"view_stack": "View Stack",
|
||||||
"view_user": "View User",
|
"view_user": "View User",
|
||||||
"viewer_remove_from_stack": "Remove from Stack",
|
"viewer_remove_from_stack": "Remove from Stack",
|
||||||
|
|
|
||||||
BIN
mobile/openapi/lib/model/smart_search_dto.dart
generated
BIN
mobile/openapi/lib/model/smart_search_dto.dart
generated
Binary file not shown.
|
|
@ -14571,6 +14571,10 @@
|
||||||
"query": {
|
"query": {
|
||||||
"type": "string"
|
"type": "string"
|
||||||
},
|
},
|
||||||
|
"queryAssetId": {
|
||||||
|
"format": "uuid",
|
||||||
|
"type": "string"
|
||||||
|
},
|
||||||
"rating": {
|
"rating": {
|
||||||
"maximum": 5,
|
"maximum": 5,
|
||||||
"minimum": -1,
|
"minimum": -1,
|
||||||
|
|
@ -14638,9 +14642,6 @@
|
||||||
"type": "boolean"
|
"type": "boolean"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"required": [
|
|
||||||
"query"
|
|
||||||
],
|
|
||||||
"type": "object"
|
"type": "object"
|
||||||
},
|
},
|
||||||
"SourceType": {
|
"SourceType": {
|
||||||
|
|
|
||||||
|
|
@ -1014,7 +1014,8 @@ export type SmartSearchDto = {
|
||||||
model?: string | null;
|
model?: string | null;
|
||||||
page?: number;
|
page?: number;
|
||||||
personIds?: string[];
|
personIds?: string[];
|
||||||
query: string;
|
query?: string;
|
||||||
|
queryAssetId?: string;
|
||||||
rating?: number;
|
rating?: number;
|
||||||
size?: number;
|
size?: number;
|
||||||
state?: string | null;
|
state?: string | null;
|
||||||
|
|
|
||||||
|
|
@ -128,12 +128,6 @@ describe(SearchController.name, () => {
|
||||||
await request(ctx.getHttpServer()).post('/search/smart');
|
await request(ctx.getHttpServer()).post('/search/smart');
|
||||||
expect(ctx.authenticate).toHaveBeenCalled();
|
expect(ctx.authenticate).toHaveBeenCalled();
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should require a query', async () => {
|
|
||||||
const { status, body } = await request(ctx.getHttpServer()).post('/search/smart').send({});
|
|
||||||
expect(status).toBe(400);
|
|
||||||
expect(body).toEqual(errorDto.badRequest(['query should not be empty', 'query must be a string']));
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('GET /search/explore', () => {
|
describe('GET /search/explore', () => {
|
||||||
|
|
|
||||||
|
|
@ -199,7 +199,12 @@ export class StatisticsSearchDto extends BaseSearchDto {
|
||||||
export class SmartSearchDto extends BaseSearchWithResultsDto {
|
export class SmartSearchDto extends BaseSearchWithResultsDto {
|
||||||
@IsString()
|
@IsString()
|
||||||
@IsNotEmpty()
|
@IsNotEmpty()
|
||||||
query!: string;
|
@Optional()
|
||||||
|
query?: string;
|
||||||
|
|
||||||
|
@ValidateUUID({ optional: true })
|
||||||
|
@Optional()
|
||||||
|
queryAssetId?: string;
|
||||||
|
|
||||||
@IsString()
|
@IsString()
|
||||||
@IsNotEmpty()
|
@IsNotEmpty()
|
||||||
|
|
|
||||||
|
|
@ -123,6 +123,14 @@ offset
|
||||||
$8
|
$8
|
||||||
commit
|
commit
|
||||||
|
|
||||||
|
-- SearchRepository.getEmbedding
|
||||||
|
select
|
||||||
|
*
|
||||||
|
from
|
||||||
|
"smart_search"
|
||||||
|
where
|
||||||
|
"assetId" = $1
|
||||||
|
|
||||||
-- SearchRepository.searchFaces
|
-- SearchRepository.searchFaces
|
||||||
begin
|
begin
|
||||||
set
|
set
|
||||||
|
|
|
||||||
|
|
@ -293,6 +293,13 @@ export class SearchRepository {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@GenerateSql({
|
||||||
|
params: [DummyValue.UUID],
|
||||||
|
})
|
||||||
|
async getEmbedding(assetId: string) {
|
||||||
|
return this.db.selectFrom('smart_search').selectAll().where('assetId', '=', assetId).executeTakeFirst();
|
||||||
|
}
|
||||||
|
|
||||||
@GenerateSql({
|
@GenerateSql({
|
||||||
params: [
|
params: [
|
||||||
{
|
{
|
||||||
|
|
|
||||||
|
|
@ -18,7 +18,7 @@ import {
|
||||||
SmartSearchDto,
|
SmartSearchDto,
|
||||||
StatisticsSearchDto,
|
StatisticsSearchDto,
|
||||||
} from 'src/dtos/search.dto';
|
} from 'src/dtos/search.dto';
|
||||||
import { AssetOrder, AssetVisibility } from 'src/enum';
|
import { AssetOrder, AssetVisibility, Permission } from 'src/enum';
|
||||||
import { BaseService } from 'src/services/base.service';
|
import { BaseService } from 'src/services/base.service';
|
||||||
import { requireElevatedPermission } from 'src/utils/access';
|
import { requireElevatedPermission } from 'src/utils/access';
|
||||||
import { getMyPartnerIds } from 'src/utils/asset.util';
|
import { getMyPartnerIds } from 'src/utils/asset.util';
|
||||||
|
|
@ -113,8 +113,10 @@ export class SearchService extends BaseService {
|
||||||
}
|
}
|
||||||
|
|
||||||
const userIds = this.getUserIdsToSearch(auth);
|
const userIds = this.getUserIdsToSearch(auth);
|
||||||
|
let embedding;
|
||||||
|
if (dto.query) {
|
||||||
const key = machineLearning.clip.modelName + dto.query + dto.language;
|
const key = machineLearning.clip.modelName + dto.query + dto.language;
|
||||||
let embedding = this.embeddingCache.get(key);
|
embedding = this.embeddingCache.get(key);
|
||||||
if (!embedding) {
|
if (!embedding) {
|
||||||
embedding = await this.machineLearningRepository.encodeText(machineLearning.urls, dto.query, {
|
embedding = await this.machineLearningRepository.encodeText(machineLearning.urls, dto.query, {
|
||||||
modelName: machineLearning.clip.modelName,
|
modelName: machineLearning.clip.modelName,
|
||||||
|
|
@ -122,6 +124,17 @@ export class SearchService extends BaseService {
|
||||||
});
|
});
|
||||||
this.embeddingCache.set(key, embedding);
|
this.embeddingCache.set(key, embedding);
|
||||||
}
|
}
|
||||||
|
} else if (dto.queryAssetId) {
|
||||||
|
await this.requireAccess({ auth, permission: Permission.AssetRead, ids: [dto.queryAssetId] });
|
||||||
|
const getEmbeddingResponse = await this.searchRepository.getEmbedding(dto.queryAssetId);
|
||||||
|
const assetEmbedding = getEmbeddingResponse?.embedding;
|
||||||
|
if (!assetEmbedding) {
|
||||||
|
throw new BadRequestException(`Asset ${dto.queryAssetId} has no embedding`);
|
||||||
|
}
|
||||||
|
embedding = assetEmbedding;
|
||||||
|
} else {
|
||||||
|
throw new BadRequestException('Either `query` or `queryAssetId` must be set');
|
||||||
|
}
|
||||||
const page = dto.page ?? 1;
|
const page = dto.page ?? 1;
|
||||||
const size = dto.size || 100;
|
const size = dto.size || 100;
|
||||||
const { hasNextPage, items } = await this.searchRepository.searchSmart(
|
const { hasNextPage, items } = await this.searchRepository.searchSmart(
|
||||||
|
|
|
||||||
|
|
@ -22,6 +22,7 @@
|
||||||
import ButtonContextMenu from '$lib/components/shared-components/context-menu/button-context-menu.svelte';
|
import ButtonContextMenu from '$lib/components/shared-components/context-menu/button-context-menu.svelte';
|
||||||
import MenuOption from '$lib/components/shared-components/context-menu/menu-option.svelte';
|
import MenuOption from '$lib/components/shared-components/context-menu/menu-option.svelte';
|
||||||
import { AppRoute } from '$lib/constants';
|
import { AppRoute } from '$lib/constants';
|
||||||
|
import { featureFlags } from '$lib/stores/server-config.store';
|
||||||
import { user } from '$lib/stores/user.store';
|
import { user } from '$lib/stores/user.store';
|
||||||
import { photoZoomState } from '$lib/stores/zoom-image.store';
|
import { photoZoomState } from '$lib/stores/zoom-image.store';
|
||||||
import { getAssetJobName, getSharedLink } from '$lib/utils';
|
import { getAssetJobName, getSharedLink } from '$lib/utils';
|
||||||
|
|
@ -41,6 +42,7 @@
|
||||||
import {
|
import {
|
||||||
mdiAlertOutline,
|
mdiAlertOutline,
|
||||||
mdiCogRefreshOutline,
|
mdiCogRefreshOutline,
|
||||||
|
mdiCompare,
|
||||||
mdiContentCopy,
|
mdiContentCopy,
|
||||||
mdiDatabaseRefreshOutline,
|
mdiDatabaseRefreshOutline,
|
||||||
mdiDotsVertical,
|
mdiDotsVertical,
|
||||||
|
|
@ -98,6 +100,7 @@
|
||||||
let isOwner = $derived($user && asset.ownerId === $user?.id);
|
let isOwner = $derived($user && asset.ownerId === $user?.id);
|
||||||
let showDownloadButton = $derived(sharedLink ? sharedLink.allowDownload : !asset.isOffline);
|
let showDownloadButton = $derived(sharedLink ? sharedLink.allowDownload : !asset.isOffline);
|
||||||
let isLocked = $derived(asset.visibility === AssetVisibility.Locked);
|
let isLocked = $derived(asset.visibility === AssetVisibility.Locked);
|
||||||
|
let smartSearchEnabled = $derived($featureFlags.loaded && $featureFlags.smartSearch);
|
||||||
|
|
||||||
// $: showEditorButton =
|
// $: showEditorButton =
|
||||||
// isOwner &&
|
// isOwner &&
|
||||||
|
|
@ -225,6 +228,13 @@
|
||||||
text={$t('view_in_timeline')}
|
text={$t('view_in_timeline')}
|
||||||
/>
|
/>
|
||||||
{/if}
|
{/if}
|
||||||
|
{#if !asset.isArchived && !asset.isTrashed && smartSearchEnabled}
|
||||||
|
<MenuOption
|
||||||
|
icon={mdiCompare}
|
||||||
|
onClick={() => goto(`${AppRoute.SEARCH}?query={"queryAssetId":"${stack?.primaryAssetId ?? asset.id}"}`)}
|
||||||
|
text={$t('view_similar_photos')}
|
||||||
|
/>
|
||||||
|
{/if}
|
||||||
{/if}
|
{/if}
|
||||||
|
|
||||||
{#if !asset.isTrashed}
|
{#if !asset.isTrashed}
|
||||||
|
|
|
||||||
|
|
@ -64,8 +64,16 @@
|
||||||
return validQueryTypes.has(storedQueryType) ? storedQueryType : QueryType.SMART;
|
return validQueryTypes.has(storedQueryType) ? storedQueryType : QueryType.SMART;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
let query = '';
|
||||||
|
if ('query' in searchQuery && searchQuery.query) {
|
||||||
|
query = searchQuery.query;
|
||||||
|
}
|
||||||
|
if ('originalFileName' in searchQuery && searchQuery.originalFileName) {
|
||||||
|
query = searchQuery.originalFileName;
|
||||||
|
}
|
||||||
|
|
||||||
let filter: SearchFilter = $state({
|
let filter: SearchFilter = $state({
|
||||||
query: 'query' in searchQuery ? searchQuery.query : searchQuery.originalFileName || '',
|
query,
|
||||||
queryType: defaultQueryType(),
|
queryType: defaultQueryType(),
|
||||||
personIds: new SvelteSet('personIds' in searchQuery ? searchQuery.personIds : []),
|
personIds: new SvelteSet('personIds' in searchQuery ? searchQuery.personIds : []),
|
||||||
tagIds:
|
tagIds:
|
||||||
|
|
|
||||||
|
|
@ -68,7 +68,7 @@
|
||||||
|
|
||||||
const assetInteraction = new AssetInteraction();
|
const assetInteraction = new AssetInteraction();
|
||||||
|
|
||||||
type SearchTerms = MetadataSearchDto & Pick<SmartSearchDto, 'query'>;
|
type SearchTerms = MetadataSearchDto & Pick<SmartSearchDto, 'query' | 'queryAssetId'>;
|
||||||
let searchQuery = $derived(page.url.searchParams.get(QueryParameter.QUERY));
|
let searchQuery = $derived(page.url.searchParams.get(QueryParameter.QUERY));
|
||||||
let smartSearchEnabled = $derived($featureFlags.loaded && $featureFlags.smartSearch);
|
let smartSearchEnabled = $derived($featureFlags.loaded && $featureFlags.smartSearch);
|
||||||
let terms = $derived(searchQuery ? JSON.parse(searchQuery) : {});
|
let terms = $derived(searchQuery ? JSON.parse(searchQuery) : {});
|
||||||
|
|
@ -164,7 +164,7 @@
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const { albums, assets } =
|
const { albums, assets } =
|
||||||
'query' in searchDto && smartSearchEnabled
|
('query' in searchDto || 'queryAssetId' in searchDto) && smartSearchEnabled
|
||||||
? await searchSmart({ smartSearchDto: searchDto })
|
? await searchSmart({ smartSearchDto: searchDto })
|
||||||
: await searchAssets({ metadataSearchDto: searchDto });
|
: await searchAssets({ metadataSearchDto: searchDto });
|
||||||
|
|
||||||
|
|
@ -210,6 +210,7 @@
|
||||||
tagIds: $t('tags'),
|
tagIds: $t('tags'),
|
||||||
originalFileName: $t('file_name'),
|
originalFileName: $t('file_name'),
|
||||||
description: $t('description'),
|
description: $t('description'),
|
||||||
|
queryAssetId: $t('query_asset_id'),
|
||||||
};
|
};
|
||||||
return keyMap[key] || key;
|
return keyMap[key] || key;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue