From bbf05ea2e28422154852107f6bd785fd0e8fb801 Mon Sep 17 00:00:00 2001 From: omdxp Date: Sun, 22 Dec 2024 19:35:04 +0100 Subject: [PATCH 1/5] feat: update search endpoint to use POST method with request body and improve search functionality --- api/src/search/controller.ts | 11 ++++++----- api/src/search/service.ts | 16 +++++++++++----- api/src/search/types.ts | 11 ++++++++++- 3 files changed, 27 insertions(+), 11 deletions(-) diff --git a/api/src/search/controller.ts b/api/src/search/controller.ts index 0bbb74e0..c5368b23 100644 --- a/api/src/search/controller.ts +++ b/api/src/search/controller.ts @@ -1,6 +1,6 @@ -import { Controller, Get } from "routing-controllers"; +import { Controller, Post, Body, ContentType } from "routing-controllers"; -import { GetSearchResponse } from "./types"; +import { SearchRequest, SearchResponse } from "./types"; import { SearchService } from "./service"; import { Service } from "typedi"; @@ -9,9 +9,10 @@ import { Service } from "typedi"; export class SearchController { constructor(private readonly searchService: SearchService) {} - @Get("/") - public async search(): Promise { - const searchResults = await this.searchService.search("test"); + @Post("/") + @ContentType("application/json") + public async search(@Body({ required: true }) req: SearchRequest): Promise { + const searchResults = await this.searchService.search(req.query, req.limit); return { searchResults, }; diff --git a/api/src/search/service.ts b/api/src/search/service.ts index 9bb147e6..a625b05c 100644 --- a/api/src/search/service.ts +++ b/api/src/search/service.ts @@ -25,11 +25,17 @@ export class SearchService { }); } - public search = async (query: string): Promise => { - this.logger.info({ message: `Searching for ${query}` }); - return { - results: [], - }; + public search = async (q: string, limit?: number): Promise => { + this.logger.info({ message: `Searching for "${q}" in all indexes` }); + const searchResults = await this.meilisearch.multiSearch({ + queries: [ + { indexUid: "project", q, limit }, + { indexUid: "contribution", q, limit }, + { indexUid: "contributor", q, limit }, + ], + }); + this.logger.info({ message: `Found ${searchResults.results.length} results` }); + return searchResults as SearchResults; }; public upsert = async (index: SearchType, data: T): Promise => { diff --git a/api/src/search/types.ts b/api/src/search/types.ts index 46f846fd..60abb8a3 100644 --- a/api/src/search/types.ts +++ b/api/src/search/types.ts @@ -3,8 +3,17 @@ import { ContributorEntity } from "@dzcode.io/models/dist/contributor"; import { GeneralResponse } from "src/app/types"; import { MultiSearchResponse } from "meilisearch"; import { ProjectEntity } from "@dzcode.io/models/dist/project"; +import { IsNotEmpty, IsPositive, IsString } from "class-validator"; -export interface GetSearchResponse extends GeneralResponse { +export class SearchRequest { + @IsString() + @IsNotEmpty() + query!: string; + + @IsPositive() + limit?: number = 5; +} +export interface SearchResponse extends GeneralResponse { searchResults: SearchResults; } From 25eba3a92c239b95a1a7059b7a53fd88f606e0fb Mon Sep 17 00:00:00 2001 From: omdxp Date: Sun, 22 Dec 2024 19:47:04 +0100 Subject: [PATCH 2/5] refactor: remove logging of search results count in SearchService --- api/src/search/service.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/api/src/search/service.ts b/api/src/search/service.ts index a625b05c..1c743c77 100644 --- a/api/src/search/service.ts +++ b/api/src/search/service.ts @@ -34,7 +34,6 @@ export class SearchService { { indexUid: "contributor", q, limit }, ], }); - this.logger.info({ message: `Found ${searchResults.results.length} results` }); return searchResults as SearchResults; }; From 20b92eed34b701d760a4b6d8a4ea34554430bc33 Mon Sep 17 00:00:00 2001 From: omdxp Date: Sun, 22 Dec 2024 20:29:51 +0100 Subject: [PATCH 3/5] feat: enhance search functionality by specifying attributes to retrieve for each index --- api/src/search/service.ts | 11 ++++++++--- api/src/search/types.ts | 1 + 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/api/src/search/service.ts b/api/src/search/service.ts index 1c743c77..b72b9acb 100644 --- a/api/src/search/service.ts +++ b/api/src/search/service.ts @@ -29,9 +29,14 @@ export class SearchService { this.logger.info({ message: `Searching for "${q}" in all indexes` }); const searchResults = await this.meilisearch.multiSearch({ queries: [ - { indexUid: "project", q, limit }, - { indexUid: "contribution", q, limit }, - { indexUid: "contributor", q, limit }, + { indexUid: "project", q, limit, attributesToRetrieve: ["id", "name"] }, + { + indexUid: "contribution", + q, + limit, + attributesToRetrieve: ["id", "title", "type", "activityCount"], + }, + { indexUid: "contributor", q, limit, attributesToRetrieve: ["id", "name", "avatarUrl"] }, ], }); return searchResults as SearchResults; diff --git a/api/src/search/types.ts b/api/src/search/types.ts index 60abb8a3..cc71d7b2 100644 --- a/api/src/search/types.ts +++ b/api/src/search/types.ts @@ -13,6 +13,7 @@ export class SearchRequest { @IsPositive() limit?: number = 5; } + export interface SearchResponse extends GeneralResponse { searchResults: SearchResults; } From fbb1812df075b6180fbddb10863f15da1aa82a91 Mon Sep 17 00:00:00 2001 From: omdxp Date: Sun, 22 Dec 2024 20:43:06 +0100 Subject: [PATCH 4/5] feat: change search endpoint to use GET method with query parameters --- api/src/search/controller.ts | 9 ++++----- api/src/search/types.ts | 2 +- 2 files changed, 5 insertions(+), 6 deletions(-) diff --git a/api/src/search/controller.ts b/api/src/search/controller.ts index c5368b23..eedb627e 100644 --- a/api/src/search/controller.ts +++ b/api/src/search/controller.ts @@ -1,6 +1,6 @@ -import { Controller, Post, Body, ContentType } from "routing-controllers"; +import { Controller, Get, QueryParams } from "routing-controllers"; -import { SearchRequest, SearchResponse } from "./types"; +import { SearchQuery, SearchResponse } from "./types"; import { SearchService } from "./service"; import { Service } from "typedi"; @@ -9,9 +9,8 @@ import { Service } from "typedi"; export class SearchController { constructor(private readonly searchService: SearchService) {} - @Post("/") - @ContentType("application/json") - public async search(@Body({ required: true }) req: SearchRequest): Promise { + @Get("/") + public async search(@QueryParams({ required: true }) req: SearchQuery): Promise { const searchResults = await this.searchService.search(req.query, req.limit); return { searchResults, diff --git a/api/src/search/types.ts b/api/src/search/types.ts index cc71d7b2..d0186246 100644 --- a/api/src/search/types.ts +++ b/api/src/search/types.ts @@ -5,7 +5,7 @@ import { MultiSearchResponse } from "meilisearch"; import { ProjectEntity } from "@dzcode.io/models/dist/project"; import { IsNotEmpty, IsPositive, IsString } from "class-validator"; -export class SearchRequest { +export class SearchQuery { @IsString() @IsNotEmpty() query!: string; From 5481dada12c6c01244f4cb12b403629ebf2730be Mon Sep 17 00:00:00 2001 From: omdxp Date: Sun, 22 Dec 2024 21:06:00 +0100 Subject: [PATCH 5/5] chore: address some comments --- api/src/search/controller.ts | 2 +- api/src/search/types.ts | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/api/src/search/controller.ts b/api/src/search/controller.ts index eedb627e..1af74d21 100644 --- a/api/src/search/controller.ts +++ b/api/src/search/controller.ts @@ -10,7 +10,7 @@ export class SearchController { constructor(private readonly searchService: SearchService) {} @Get("/") - public async search(@QueryParams({ required: true }) req: SearchQuery): Promise { + public async search(@QueryParams() req: SearchQuery): Promise { const searchResults = await this.searchService.search(req.query, req.limit); return { searchResults, diff --git a/api/src/search/types.ts b/api/src/search/types.ts index d0186246..e2b752e1 100644 --- a/api/src/search/types.ts +++ b/api/src/search/types.ts @@ -11,7 +11,7 @@ export class SearchQuery { query!: string; @IsPositive() - limit?: number = 5; + limit: number = 5; } export interface SearchResponse extends GeneralResponse {