feat(server): provide the ability to search archived photos (#6332)

* Feat: provide the ability to search archived photos

Adds a query parameter (`searchArchived`) to the search URL parameters
to allow the results to contain archived photos.

* chore: rename includeArchived => withArchived

* chore: open api

---------

Co-authored-by: Jason Rasmussen <jrasm91@gmail.com>
This commit is contained in:
Steven Carter 2024-01-17 21:08:00 -05:00 committed by GitHub
parent f0b328fb6b
commit d4146e3e6d
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
11 changed files with 100 additions and 16 deletions

View file

@ -9,6 +9,7 @@ export interface EmbeddingSearch {
embedding: Embedding;
numResults: number;
maxDistance?: number;
withArchived?: boolean;
}
export interface ISmartInfoRepository {

View file

@ -32,6 +32,11 @@ export class SearchDto {
@Optional()
@Transform(toBoolean)
motion?: boolean;
@IsBoolean()
@Optional()
@Transform(toBoolean)
withArchived?: boolean;
}
export class SearchPeopleDto {

View file

@ -114,6 +114,39 @@ describe(SearchService.name, () => {
expect(smartInfoMock.searchCLIP).not.toHaveBeenCalled();
});
it('should search archived photos if `withArchived` option is true', async () => {
const dto: SearchDto = { q: 'test query', clip: true, withArchived: true };
const embedding = [1, 2, 3];
smartInfoMock.searchCLIP.mockResolvedValueOnce([assetStub.image]);
machineMock.encodeText.mockResolvedValueOnce(embedding);
partnerMock.getAll.mockResolvedValueOnce([]);
const expectedResponse = {
albums: {
total: 0,
count: 0,
items: [],
facets: [],
},
assets: {
total: 1,
count: 1,
items: [mapAsset(assetStub.image)],
facets: [],
},
};
const result = await sut.search(authStub.user1, dto);
expect(result).toEqual(expectedResponse);
expect(smartInfoMock.searchCLIP).toHaveBeenCalledWith({
userIds: [authStub.user1.user.id],
embedding,
numResults: 100,
withArchived: true,
});
expect(assetMock.searchMetadata).not.toHaveBeenCalled();
});
it('should search by CLIP if `clip` option is true', async () => {
const dto: SearchDto = { q: 'test query', clip: true };
const embedding = [1, 2, 3];
@ -142,6 +175,7 @@ describe(SearchService.name, () => {
userIds: [authStub.user1.user.id],
embedding,
numResults: 100,
withArchived: false,
});
expect(assetMock.searchMetadata).not.toHaveBeenCalled();
});

View file

@ -67,6 +67,7 @@ export class SearchService {
}
const strategy = dto.clip ? SearchStrategy.CLIP : SearchStrategy.TEXT;
const userIds = await this.getUserIdsToSearch(auth);
const withArchived = dto.withArchived || false;
let assets: AssetEntity[] = [];
@ -77,7 +78,12 @@ export class SearchService {
{ text: query },
machineLearning.clip,
);
assets = await this.smartInfoRepository.searchCLIP({ userIds: userIds, embedding, numResults: 100 });
assets = await this.smartInfoRepository.searchCLIP({
userIds: userIds,
embedding,
numResults: 100,
withArchived,
});
break;
case SearchStrategy.TEXT:
assets = await this.assetRepository.searchMetadata(query, userIds, { numResults: 250 });

View file

@ -43,7 +43,7 @@ export class SmartInfoRepository implements ISmartInfoRepository {
@GenerateSql({
params: [{ userIds: [DummyValue.UUID], embedding: Array.from({ length: 512 }, Math.random), numResults: 100 }],
})
async searchCLIP({ userIds, embedding, numResults }: EmbeddingSearch): Promise<AssetEntity[]> {
async searchCLIP({ userIds, embedding, numResults, withArchived }: EmbeddingSearch): Promise<AssetEntity[]> {
if (!isValidInteger(numResults, { min: 1 })) {
throw new Error(`Invalid value for 'numResults': ${numResults}`);
}
@ -52,12 +52,18 @@ export class SmartInfoRepository implements ISmartInfoRepository {
await this.assetRepository.manager.transaction(async (manager) => {
await manager.query(`SET LOCAL vectors.k = '${numResults}'`);
await manager.query(`SET LOCAL vectors.enable_prefilter = on`);
results = await manager
const query = manager
.createQueryBuilder(AssetEntity, 'a')
.innerJoin('a.smartSearch', 's')
.where('a.ownerId IN (:...userIds )')
.andWhere('a.isVisible = true')
.andWhere('a.isArchived = false')
.andWhere('a.isVisible = true');
if (!withArchived) {
query.andWhere('a.isArchived = false');
}
results = await query
.andWhere('a.fileCreatedAt < NOW()')
.leftJoinAndSelect('a.exifInfo', 'e')
.orderBy('s.embedding <=> :embedding')