From f0e51d038f799a8456d3d84a64e06fbe64495afb Mon Sep 17 00:00:00 2001 From: Alp Date: Mon, 25 Dec 2023 02:38:02 +0300 Subject: [PATCH] Hopefully search is fixed --- app/backend/src/poll/poll.controller.ts | 4 +++- app/backend/src/poll/poll.module.ts | 3 ++- app/backend/src/poll/poll.service.ts | 20 +++++++++++++------- 3 files changed, 18 insertions(+), 9 deletions(-) diff --git a/app/backend/src/poll/poll.controller.ts b/app/backend/src/poll/poll.controller.ts index e3a442b9..745bce60 100644 --- a/app/backend/src/poll/poll.controller.ts +++ b/app/backend/src/poll/poll.controller.ts @@ -63,8 +63,10 @@ export class PollController { @Post('pinecone/search') public async pineconeSearch( @Query('searchQuery') searchQuery: string, + @Req() req: any, ): Promise { - return await this.pollService.searchSemanticPolls(searchQuery); + const userId = req.user?.sub; // Realize that it is not id instead sub. I do not know why but middleware gives this field. + return await this.pollService.searchSemanticPolls(searchQuery, userId); } @UseGuards(AuthGuard, VerificationGuard) diff --git a/app/backend/src/poll/poll.module.ts b/app/backend/src/poll/poll.module.ts index 12c306e9..ca359d0c 100644 --- a/app/backend/src/poll/poll.module.ts +++ b/app/backend/src/poll/poll.module.ts @@ -60,7 +60,7 @@ import { OptionService } from '../option/option.service'; GoogleGenerativeAIEmbeddings, RankingService, VoteService, - OptionService + OptionService, ], exports: [PollService], }) @@ -71,6 +71,7 @@ export class PollModule implements NestModule { .forRoutes( { path: '/poll', method: RequestMethod.GET }, { path: '/poll/:param', method: RequestMethod.GET }, + { path: '/poll/pinecone/search', method: RequestMethod.GET }, ); } } diff --git a/app/backend/src/poll/poll.service.ts b/app/backend/src/poll/poll.service.ts index 629a72ec..d5eababc 100644 --- a/app/backend/src/poll/poll.service.ts +++ b/app/backend/src/poll/poll.service.ts @@ -651,14 +651,20 @@ export class PollService { }); } - public async searchSemanticPolls(query: string): Promise { - let results = await this.pineconeStore.similaritySearchWithScore(query, 5); - results = results + public async searchSemanticPolls( + query: string, + userId?: string | null, + ): Promise { + const polls = await this.pineconeStore.similaritySearchWithScore(query, 5); + const pollIDs = polls .filter((result) => result[1] > 0.7) .map((result) => result[0].metadata.id); - return await this.pollRepository.find({ - where: { id: In(results) }, - relations: ['options', 'tags', 'creator', 'likes', 'comments', 'votes'], - }); + + const results = await Promise.all( + pollIDs.map(async (pollId) => { + return await this.findPollById(pollId, userId); + }), + ); + return results; } }