forked from immich-app/immich
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(server): separate face search relation (immich-app#10371)
* wip * various fixes * new migration * fix test * add face search entity, update sql * update e2e * set storage to external
- Loading branch information
Showing
12 changed files
with
130 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import { AssetFaceEntity } from 'src/entities/asset-face.entity'; | ||
import { asVector } from 'src/utils/database'; | ||
import { Column, Entity, Index, JoinColumn, OneToOne, PrimaryColumn } from 'typeorm'; | ||
|
||
@Entity('face_search', { synchronize: false }) | ||
export class FaceSearchEntity { | ||
@OneToOne(() => AssetFaceEntity, { onDelete: 'CASCADE', nullable: true }) | ||
@JoinColumn({ name: 'faceId', referencedColumnName: 'id' }) | ||
face?: AssetFaceEntity; | ||
|
||
@PrimaryColumn() | ||
faceId!: string; | ||
|
||
@Index('face_index', { synchronize: false }) | ||
@Column({ | ||
type: 'float4', | ||
array: true, | ||
transformer: { from: (v) => JSON.parse(v), to: (v) => asVector(v) }, | ||
}) | ||
embedding!: number[]; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
54 changes: 54 additions & 0 deletions
54
server/src/migrations/1718486162779-AddFaceSearchRelation.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import { getVectorExtension } from 'src/database.config'; | ||
import { DatabaseExtension } from 'src/interfaces/database.interface'; | ||
import { MigrationInterface, QueryRunner } from 'typeorm'; | ||
|
||
export class AddFaceSearchRelation1718486162779 implements MigrationInterface { | ||
public async up(queryRunner: QueryRunner): Promise<void> { | ||
if (getVectorExtension() === DatabaseExtension.VECTORS) { | ||
await queryRunner.query(`SET search_path TO "$user", public, vectors`); | ||
await queryRunner.query(`SET vectors.pgvector_compatibility=on`); | ||
} | ||
|
||
await queryRunner.query(` | ||
CREATE TABLE face_search ( | ||
"faceId" uuid PRIMARY KEY REFERENCES asset_faces(id) ON DELETE CASCADE, | ||
embedding vector(512) NOT NULL )`); | ||
|
||
await queryRunner.query(`ALTER TABLE face_search ALTER COLUMN embedding SET STORAGE EXTERNAL`); | ||
await queryRunner.query(`ALTER TABLE smart_search ALTER COLUMN embedding SET STORAGE EXTERNAL`); | ||
|
||
await queryRunner.query(` | ||
INSERT INTO face_search("faceId", embedding) | ||
SELECT id, embedding | ||
FROM asset_faces faces`); | ||
|
||
await queryRunner.query(`ALTER TABLE asset_faces DROP COLUMN "embedding"`); | ||
|
||
await queryRunner.query(` | ||
CREATE INDEX face_index ON face_search | ||
USING hnsw (embedding vector_cosine_ops) | ||
WITH (ef_construction = 300, m = 16)`); | ||
} | ||
|
||
public async down(queryRunner: QueryRunner): Promise<void> { | ||
if (getVectorExtension() === DatabaseExtension.VECTORS) { | ||
await queryRunner.query(`SET search_path TO "$user", public, vectors`); | ||
await queryRunner.query(`SET vectors.pgvector_compatibility=on`); | ||
} | ||
|
||
await queryRunner.query(`ALTER TABLE asset_faces ADD COLUMN "embedding" vector(512)`); | ||
await queryRunner.query(`ALTER TABLE face_search ALTER COLUMN embedding SET STORAGE DEFAULT`); | ||
await queryRunner.query(`ALTER TABLE smart_search ALTER COLUMN embedding SET STORAGE DEFAULT`); | ||
await queryRunner.query(` | ||
UPDATE asset_faces | ||
SET embedding = fs.embedding | ||
FROM face_search fs | ||
WHERE id = fs."faceId"`); | ||
await queryRunner.query(`DROP TABLE face_search`); | ||
|
||
await queryRunner.query(` | ||
CREATE INDEX face_index ON asset_faces | ||
USING hnsw (embedding vector_cosine_ops) | ||
WITH (ef_construction = 300, m = 16)`); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.