Files
immich/server/src/queries/search.repository.sql

271 lines
5.8 KiB
MySQL
Raw Normal View History

-- NOTE: This file is auto generated by ./sql-generator
-- SearchRepository.searchMetadata
2025-01-09 11:15:41 -05:00
select
"assets".*
from
"assets"
inner join "exif" on "assets"."id" = "exif"."assetId"
where
"assets"."fileCreatedAt" >= $1
and "exif"."lensModel" = $2
and "assets"."ownerId" = any ($3::uuid[])
2025-01-09 11:15:41 -05:00
and "assets"."isFavorite" = $4
and "assets"."isArchived" = $5
and "assets"."deletedAt" is null
order by
"assets"."fileCreatedAt" desc
limit
$6
offset
$7
-- SearchRepository.searchRandom
2025-01-09 11:15:41 -05:00
(
select
"assets".*
from
"assets"
inner join "exif" on "assets"."id" = "exif"."assetId"
where
"assets"."fileCreatedAt" >= $1
and "exif"."lensModel" = $2
and "assets"."ownerId" = any ($3::uuid[])
2025-01-09 11:15:41 -05:00
and "assets"."isFavorite" = $4
and "assets"."isArchived" = $5
and "assets"."deletedAt" is null
and "assets"."id" < $6
order by
random()
2025-01-09 11:15:41 -05:00
limit
$7
)
union all
(
select
"assets".*
from
"assets"
inner join "exif" on "assets"."id" = "exif"."assetId"
where
"assets"."fileCreatedAt" >= $8
and "exif"."lensModel" = $9
and "assets"."ownerId" = any ($10::uuid[])
2025-01-09 11:15:41 -05:00
and "assets"."isFavorite" = $11
and "assets"."isArchived" = $12
and "assets"."deletedAt" is null
and "assets"."id" > $13
order by
random()
2025-01-09 11:15:41 -05:00
limit
$14
)
limit
$15
-- SearchRepository.searchSmart
2025-01-09 11:15:41 -05:00
select
"assets".*
from
"assets"
inner join "exif" on "assets"."id" = "exif"."assetId"
inner join "smart_search" on "assets"."id" = "smart_search"."assetId"
where
"assets"."fileCreatedAt" >= $1
and "exif"."lensModel" = $2
and "assets"."ownerId" = any ($3::uuid[])
2025-01-09 11:15:41 -05:00
and "assets"."isFavorite" = $4
and "assets"."isArchived" = $5
and "assets"."deletedAt" is null
order by
smart_search.embedding <=> $6
2025-01-09 11:15:41 -05:00
limit
$7
offset
$8
feat(server): near-duplicate detection (#8228) * duplicate detection job, entity, config * queueing * job panel, update api * use embedding in db instead of fetching * disable concurrency * only queue visible assets * handle multiple duplicateIds * update concurrent queue check * add provider * add web placeholder, server endpoint, migration, various fixes * update sql * select embedding by default * rename variable * simplify * remove separate entity, handle re-running with different threshold, set default back to 0.02 * fix tests * add tests * add index to entity * formatting * update asset mock * fix `upsertJobStatus` signature * update sql * formatting * default to 0.03 * optimize clustering * use asset's `duplicateId` if present * update sql * update tests * expose admin setting * refactor * formatting * skip if ml is disabled * debug trash e2e * remove from web * remove from sidebar * test if ml is disabled * update sql * separate duplicate detection from clip in config, disable by default for now * fix doc * lower minimum `maxDistance` * update api * Add and Use Duplicate Detection Feature Flag (#9364) * Add Duplicate Detection Flag * Use Duplicate Detection Flag * Attempt Fixes for Failing Checks * lower minimum `maxDistance` * fix tests --------- Co-authored-by: mertalev <101130780+mertalev@users.noreply.github.com> * chore: fixes and additions after rebase * chore: update api (remove new Role enum) * fix: left join smart search so getAll works without machine learning * test: trash e2e go back to checking length of assets is zero * chore: regen api after rebase * test: fix tests after rebase * redundant join --------- Co-authored-by: Nicholas Flamy <30300649+NicholasFlamy@users.noreply.github.com> Co-authored-by: Zack Pollard <zackpollard@ymail.com> Co-authored-by: Zack Pollard <zack@futo.org>
2024-05-16 13:08:37 -04:00
-- SearchRepository.searchDuplicates
2025-01-09 11:15:41 -05:00
with
"cte" as (
select
"assets"."id" as "assetId",
"assets"."duplicateId",
smart_search.embedding <=> $1 as "distance"
2025-01-09 11:15:41 -05:00
from
"assets"
inner join "smart_search" on "assets"."id" = "smart_search"."assetId"
where
"assets"."ownerId" = any ($2::uuid[])
2025-01-09 11:15:41 -05:00
and "assets"."deletedAt" is null
and "assets"."isVisible" = $3
and "assets"."type" = $4
and "assets"."id" != $5::uuid
order by
smart_search.embedding <=> $6
2025-01-09 11:15:41 -05:00
limit
$7
feat(server): near-duplicate detection (#8228) * duplicate detection job, entity, config * queueing * job panel, update api * use embedding in db instead of fetching * disable concurrency * only queue visible assets * handle multiple duplicateIds * update concurrent queue check * add provider * add web placeholder, server endpoint, migration, various fixes * update sql * select embedding by default * rename variable * simplify * remove separate entity, handle re-running with different threshold, set default back to 0.02 * fix tests * add tests * add index to entity * formatting * update asset mock * fix `upsertJobStatus` signature * update sql * formatting * default to 0.03 * optimize clustering * use asset's `duplicateId` if present * update sql * update tests * expose admin setting * refactor * formatting * skip if ml is disabled * debug trash e2e * remove from web * remove from sidebar * test if ml is disabled * update sql * separate duplicate detection from clip in config, disable by default for now * fix doc * lower minimum `maxDistance` * update api * Add and Use Duplicate Detection Feature Flag (#9364) * Add Duplicate Detection Flag * Use Duplicate Detection Flag * Attempt Fixes for Failing Checks * lower minimum `maxDistance` * fix tests --------- Co-authored-by: mertalev <101130780+mertalev@users.noreply.github.com> * chore: fixes and additions after rebase * chore: update api (remove new Role enum) * fix: left join smart search so getAll works without machine learning * test: trash e2e go back to checking length of assets is zero * chore: regen api after rebase * test: fix tests after rebase * redundant join --------- Co-authored-by: Nicholas Flamy <30300649+NicholasFlamy@users.noreply.github.com> Co-authored-by: Zack Pollard <zackpollard@ymail.com> Co-authored-by: Zack Pollard <zack@futo.org>
2024-05-16 13:08:37 -04:00
)
2025-01-09 11:15:41 -05:00
select
*
from
"cte"
where
"cte"."distance" <= $8
feat(server): near-duplicate detection (#8228) * duplicate detection job, entity, config * queueing * job panel, update api * use embedding in db instead of fetching * disable concurrency * only queue visible assets * handle multiple duplicateIds * update concurrent queue check * add provider * add web placeholder, server endpoint, migration, various fixes * update sql * select embedding by default * rename variable * simplify * remove separate entity, handle re-running with different threshold, set default back to 0.02 * fix tests * add tests * add index to entity * formatting * update asset mock * fix `upsertJobStatus` signature * update sql * formatting * default to 0.03 * optimize clustering * use asset's `duplicateId` if present * update sql * update tests * expose admin setting * refactor * formatting * skip if ml is disabled * debug trash e2e * remove from web * remove from sidebar * test if ml is disabled * update sql * separate duplicate detection from clip in config, disable by default for now * fix doc * lower minimum `maxDistance` * update api * Add and Use Duplicate Detection Feature Flag (#9364) * Add Duplicate Detection Flag * Use Duplicate Detection Flag * Attempt Fixes for Failing Checks * lower minimum `maxDistance` * fix tests --------- Co-authored-by: mertalev <101130780+mertalev@users.noreply.github.com> * chore: fixes and additions after rebase * chore: update api (remove new Role enum) * fix: left join smart search so getAll works without machine learning * test: trash e2e go back to checking length of assets is zero * chore: regen api after rebase * test: fix tests after rebase * redundant join --------- Co-authored-by: Nicholas Flamy <30300649+NicholasFlamy@users.noreply.github.com> Co-authored-by: Zack Pollard <zackpollard@ymail.com> Co-authored-by: Zack Pollard <zack@futo.org>
2024-05-16 13:08:37 -04:00
-- SearchRepository.searchFaces
2025-01-09 11:15:41 -05:00
with
"cte" as (
select
"asset_faces"."id",
"asset_faces"."personId",
face_search.embedding <=> $1 as "distance"
2025-01-09 11:15:41 -05:00
from
"asset_faces"
inner join "assets" on "assets"."id" = "asset_faces"."assetId"
inner join "face_search" on "face_search"."faceId" = "asset_faces"."id"
where
"assets"."ownerId" = any ($2::uuid[])
2025-01-09 11:15:41 -05:00
and "assets"."deletedAt" is null
order by
face_search.embedding <=> $3
2025-01-09 11:15:41 -05:00
limit
$4
)
2025-01-09 11:15:41 -05:00
select
*
from
"cte"
where
"cte"."distance" <= $5
-- SearchRepository.searchPlaces
2025-01-09 11:15:41 -05:00
select
*
from
"geodata_places"
where
f_unaccent (name) %>> f_unaccent ($1)
2025-01-09 11:15:41 -05:00
or f_unaccent ("admin2Name") %>> f_unaccent ($2)
or f_unaccent ("admin1Name") %>> f_unaccent ($3)
or f_unaccent ("alternateNames") %>> f_unaccent ($4)
order by
coalesce(f_unaccent (name) <->>> f_unaccent ($5), 0.1) + coalesce(
f_unaccent ("admin2Name") <->>> f_unaccent ($6),
0.1
2025-01-09 11:15:41 -05:00
) + coalesce(
f_unaccent ("admin1Name") <->>> f_unaccent ($7),
0.1
2025-01-09 11:15:41 -05:00
) + coalesce(
f_unaccent ("alternateNames") <->>> f_unaccent ($8),
0.1
2025-01-09 11:15:41 -05:00
)
limit
$9
-- SearchRepository.getAssetsByCity
2025-01-09 11:15:41 -05:00
with recursive
"cte" as (
(
2025-01-09 11:15:41 -05:00
select
"city",
"assetId"
2025-01-09 11:15:41 -05:00
from
"exif"
inner join "assets" on "assets"."id" = "exif"."assetId"
where
"assets"."ownerId" = any ($1::uuid[])
2025-01-09 11:15:41 -05:00
and "assets"."isVisible" = $2
and "assets"."isArchived" = $3
and "assets"."type" = $4
and "assets"."deletedAt" is null
order by
"city"
limit
$5
)
union all
(
select
"l"."city",
"l"."assetId"
from
"cte"
inner join lateral (
select
"city",
"assetId"
from
"exif"
inner join "assets" on "assets"."id" = "exif"."assetId"
where
"assets"."ownerId" = any ($6::uuid[])
2025-01-09 11:15:41 -05:00
and "assets"."isVisible" = $7
and "assets"."isArchived" = $8
and "assets"."type" = $9
and "assets"."deletedAt" is null
and "exif"."city" > "cte"."city"
order by
"city"
limit
$10
) as "l" on true
)
)
2025-01-09 11:15:41 -05:00
select
"assets".*,
to_jsonb("exif") as "exifInfo"
from
"assets"
inner join "exif" on "assets"."id" = "exif"."assetId"
inner join "cte" on "assets"."id" = "cte"."assetId"
order by
"exif"."city"
-- SearchRepository.getStates
2025-01-09 11:15:41 -05:00
select distinct
on ("state") "state"
from
"exif"
inner join "assets" on "assets"."id" = "exif"."assetId"
where
"ownerId" = any ($1::uuid[])
2025-01-09 11:15:41 -05:00
and "isVisible" = $2
and "deletedAt" is null
and "state" is not null
-- SearchRepository.getCities
2025-01-09 11:15:41 -05:00
select distinct
on ("city") "city"
from
"exif"
inner join "assets" on "assets"."id" = "exif"."assetId"
where
"ownerId" = any ($1::uuid[])
2025-01-09 11:15:41 -05:00
and "isVisible" = $2
and "deletedAt" is null
and "city" is not null
-- SearchRepository.getCameraMakes
2025-01-09 11:15:41 -05:00
select distinct
on ("make") "make"
from
"exif"
inner join "assets" on "assets"."id" = "exif"."assetId"
where
"ownerId" = any ($1::uuid[])
2025-01-09 11:15:41 -05:00
and "isVisible" = $2
and "deletedAt" is null
and "make" is not null
-- SearchRepository.getCameraModels
2025-01-09 11:15:41 -05:00
select distinct
on ("model") "model"
from
"exif"
inner join "assets" on "assets"."id" = "exif"."assetId"
where
"ownerId" = any ($1::uuid[])
2025-01-09 11:15:41 -05:00
and "isVisible" = $2
and "deletedAt" is null
and "model" is not null