Skip to content

Commit 9988863

Browse files
authored
Merge pull request #623 from dzcode-io/search-endpoint
Search Endpoint
2 parents 30b94a4 + 5481dad commit 9988863

File tree

3 files changed

+30
-10
lines changed

3 files changed

+30
-10
lines changed

api/src/search/controller.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import { Controller, Get } from "routing-controllers";
1+
import { Controller, Get, QueryParams } from "routing-controllers";
22

3-
import { GetSearchResponse } from "./types";
3+
import { SearchQuery, SearchResponse } from "./types";
44
import { SearchService } from "./service";
55
import { Service } from "typedi";
66

@@ -10,8 +10,8 @@ export class SearchController {
1010
constructor(private readonly searchService: SearchService) {}
1111

1212
@Get("/")
13-
public async search(): Promise<GetSearchResponse> {
14-
const searchResults = await this.searchService.search("test");
13+
public async search(@QueryParams() req: SearchQuery): Promise<SearchResponse> {
14+
const searchResults = await this.searchService.search(req.query, req.limit);
1515
return {
1616
searchResults,
1717
};

api/src/search/service.ts

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,21 @@ export class SearchService {
2525
});
2626
}
2727

28-
public search = async (query: string): Promise<SearchResults> => {
29-
this.logger.info({ message: `Searching for ${query}` });
30-
return {
31-
results: [],
32-
};
28+
public search = async (q: string, limit?: number): Promise<SearchResults> => {
29+
this.logger.info({ message: `Searching for "${q}" in all indexes` });
30+
const searchResults = await this.meilisearch.multiSearch({
31+
queries: [
32+
{ indexUid: "project", q, limit, attributesToRetrieve: ["id", "name"] },
33+
{
34+
indexUid: "contribution",
35+
q,
36+
limit,
37+
attributesToRetrieve: ["id", "title", "type", "activityCount"],
38+
},
39+
{ indexUid: "contributor", q, limit, attributesToRetrieve: ["id", "name", "avatarUrl"] },
40+
],
41+
});
42+
return searchResults as SearchResults;
3343
};
3444

3545
public upsert = async <T extends BaseEntity>(index: SearchType, data: T): Promise<void> => {

api/src/search/types.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,18 @@ import { ContributorEntity } from "@dzcode.io/models/dist/contributor";
33
import { GeneralResponse } from "src/app/types";
44
import { MultiSearchResponse } from "meilisearch";
55
import { ProjectEntity } from "@dzcode.io/models/dist/project";
6+
import { IsNotEmpty, IsPositive, IsString } from "class-validator";
67

7-
export interface GetSearchResponse extends GeneralResponse {
8+
export class SearchQuery {
9+
@IsString()
10+
@IsNotEmpty()
11+
query!: string;
12+
13+
@IsPositive()
14+
limit: number = 5;
15+
}
16+
17+
export interface SearchResponse extends GeneralResponse {
818
searchResults: SearchResults;
919
}
1020

0 commit comments

Comments
 (0)