Skip to content

Commit

Permalink
localized search endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
ZibanPirate committed Jan 2, 2025
1 parent e6a4702 commit ded91cb
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 6 deletions.
2 changes: 1 addition & 1 deletion api/src/search/controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export class SearchController {

@Get("/")
public async search(@QueryParams() req: SearchQuery): Promise<SearchResponse> {
const searchResults = await this.searchService.search(req.query, req.limit);
const searchResults = await this.searchService.search(req.query, req.lang, req.limit);
return {
searchResults,
};
Expand Down
30 changes: 26 additions & 4 deletions api/src/search/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { ConfigService } from "src/config/service";
import { LoggerService } from "src/logger/service";
import { MeiliSearch } from "meilisearch";
import { Service } from "typedi";
import { LanguageCode } from "@dzcode.io/utils/dist/language";

@Service()
export class SearchService {
Expand All @@ -25,20 +26,41 @@ export class SearchService {
});
}

public search = async (q: string, limit?: number): Promise<SearchResults> => {
public search = async (q: string, lang: LanguageCode, limit?: number): Promise<SearchResults> => {
// TODO-ZM: only fetch Ids from search db, then query actually entities from their respective repositories
this.logger.info({ message: `Searching for "${q}" in all indexes` });
const searchResults = await this.meilisearch.multiSearch({
queries: [
{ indexUid: "project", q, limit, attributesToRetrieve: ["id", "name"] },
{ indexUid: "project", q, limit, attributesToRetrieve: ["id", `name_${lang}`] },
{
indexUid: "contribution",
q,
limit,
attributesToRetrieve: ["id", "title", "type", "activityCount", "url"],
attributesToRetrieve: ["id", `title_${lang}`, "type", "activityCount", "url"],
},
{
indexUid: "contributor",
q,
limit,
attributesToRetrieve: ["id", `name_${lang}`, "avatarUrl"],
},
{ indexUid: "contributor", q, limit, attributesToRetrieve: ["id", "name", "avatarUrl"] },
],
});

searchResults.results.forEach((result) => {
result.hits.forEach((hit) => {
if (hit[`name_${lang}`]) {
hit.name = hit[`name_${lang}`];
delete hit[`name_${lang}`];
}

if (hit[`title_${lang}`]) {
hit.title = hit[`title_${lang}`];
delete hit[`title_${lang}`];
}
});
});

return searchResults as SearchResults;
};

Expand Down
3 changes: 2 additions & 1 deletion api/src/search/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ 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";
import { LanguageQuery } from "src/_utils/language";

export class SearchQuery {
export class SearchQuery extends LanguageQuery {
@IsString()
@IsNotEmpty()
query!: string;
Expand Down

0 comments on commit ded91cb

Please sign in to comment.