From e4c4b53fcdcf733b6e91982b3f7593a89348b06f Mon Sep 17 00:00:00 2001 From: Alex Tran Date: Tue, 26 Jul 2022 13:43:12 -0500 Subject: [PATCH] Added imageName as searchable text on database --- .../libs/database/src/entities/exif.entity.ts | 7 +-- ...470248-AddExifImageNameAsSearchableText.ts | 50 +++++++++++++++++++ 2 files changed, 54 insertions(+), 3 deletions(-) create mode 100644 server/libs/database/src/migrations/1658860470248-AddExifImageNameAsSearchableText.ts diff --git a/server/libs/database/src/entities/exif.entity.ts b/server/libs/database/src/entities/exif.entity.ts index 26cabf8e1..5cca53689 100644 --- a/server/libs/database/src/entities/exif.entity.ts +++ b/server/libs/database/src/entities/exif.entity.ts @@ -74,7 +74,7 @@ export class ExifEntity { @JoinColumn({ name: 'assetId', referencedColumnName: 'id' }) asset?: ExifEntity; - @Index("exif_text_searchable", { synchronize: false }) + @Index('exif_text_searchable', { synchronize: false }) @Column({ type: 'tsvector', generatedType: 'STORED', @@ -83,9 +83,10 @@ export class ExifEntity { COALESCE(model, '') || ' ' || COALESCE(orientation, '') || ' ' || COALESCE("lensModel", '') || ' ' || + COALESCE("imageName", '') || ' ' || COALESCE("city", '') || ' ' || COALESCE("state", '') || ' ' || - COALESCE("country", ''))` + COALESCE("country", ''))`, }) - exifTextSearchableColumn!: string + exifTextSearchableColumn!: string; } diff --git a/server/libs/database/src/migrations/1658860470248-AddExifImageNameAsSearchableText.ts b/server/libs/database/src/migrations/1658860470248-AddExifImageNameAsSearchableText.ts new file mode 100644 index 000000000..3dfc655fc --- /dev/null +++ b/server/libs/database/src/migrations/1658860470248-AddExifImageNameAsSearchableText.ts @@ -0,0 +1,50 @@ +import { MigrationInterface, QueryRunner } from 'typeorm'; + +export class AddExifImageNameAsSearchableText1658860470248 implements MigrationInterface { + name = 'AddExifImageNameAsSearchableText1658860470248'; + + public async up(queryRunner: QueryRunner): Promise { + await queryRunner.query(`ALTER TABLE "exif" DROP COLUMN "exifTextSearchableColumn"`); + await queryRunner.query( + `DELETE FROM "typeorm_metadata" WHERE "type" = $1 AND "name" = $2 AND "database" = $3 AND "schema" = $4 AND "table" = $5`, + ['GENERATED_COLUMN', 'exifTextSearchableColumn', 'immich', 'public', 'exif'], + ); + await queryRunner.query(`ALTER TABLE "exif" ADD "exifTextSearchableColumn" tsvector GENERATED ALWAYS AS (TO_TSVECTOR('english', + COALESCE(make, '') || ' ' || + COALESCE(model, '') || ' ' || + COALESCE(orientation, '') || ' ' || + COALESCE("lensModel", '') || ' ' || + COALESCE("imageName", '') || ' ' || + COALESCE("city", '') || ' ' || + COALESCE("state", '') || ' ' || + COALESCE("country", ''))) STORED`); + await queryRunner.query( + `DELETE FROM "typeorm_metadata" WHERE "type" = $1 AND "name" = $2 AND "database" = $3 AND "schema" = $4 AND "table" = $5`, + ['GENERATED_COLUMN', 'exifTextSearchableColumn', 'immich', 'public', 'exif'], + ); + await queryRunner.query( + `INSERT INTO "typeorm_metadata"("database", "schema", "table", "type", "name", "value") VALUES ($1, $2, $3, $4, $5, $6)`, + [ + 'immich', + 'public', + 'exif', + 'GENERATED_COLUMN', + 'exifTextSearchableColumn', + "TO_TSVECTOR('english',\n COALESCE(make, '') || ' ' ||\n COALESCE(model, '') || ' ' ||\n COALESCE(orientation, '') || ' ' ||\n COALESCE(\"lensModel\", '') || ' ' ||\n COALESCE(\"imageName\", '') || ' ' ||\n COALESCE(\"city\", '') || ' ' ||\n COALESCE(\"state\", '') || ' ' ||\n COALESCE(\"country\", ''))", + ], + ); + } + + public async down(queryRunner: QueryRunner): Promise { + await queryRunner.query( + `DELETE FROM "typeorm_metadata" WHERE "type" = $1 AND "name" = $2 AND "database" = $3 AND "schema" = $4 AND "table" = $5`, + ['GENERATED_COLUMN', 'exifTextSearchableColumn', 'immich', 'public', 'exif'], + ); + await queryRunner.query(`ALTER TABLE "exif" DROP COLUMN "exifTextSearchableColumn"`); + await queryRunner.query( + `INSERT INTO "typeorm_metadata"("database", "schema", "table", "type", "name", "value") VALUES ($1, $2, $3, $4, $5, $6)`, + ['immich', 'public', 'exif', 'GENERATED_COLUMN', 'exifTextSearchableColumn', ''], + ); + await queryRunner.query(`ALTER TABLE "exif" ADD "exifTextSearchableColumn" tsvector NOT NULL`); + } +}