Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions api/src/search/controller.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Controller, Get } from "routing-controllers";
import { Controller, Get, QueryParams } from "routing-controllers";

import { GetSearchResponse } from "./types";
import { SearchQuery, SearchResponse } from "./types";
import { SearchService } from "./service";
import { Service } from "typedi";

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

@Get("/")
public async search(): Promise<GetSearchResponse> {
const searchResults = await this.searchService.search("test");
public async search(@QueryParams() req: SearchQuery): Promise<SearchResponse> {
const searchResults = await this.searchService.search(req.query, req.limit);
return {
searchResults,
};
Expand Down
20 changes: 15 additions & 5 deletions api/src/search/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,21 @@ export class SearchService {
});
}

public search = async (query: string): Promise<SearchResults> => {
this.logger.info({ message: `Searching for ${query}` });
return {
results: [],
};
public search = async (q: string, limit?: number): Promise<SearchResults> => {
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: "contribution",
q,
limit,
attributesToRetrieve: ["id", "title", "type", "activityCount"],
},
{ indexUid: "contributor", q, limit, attributesToRetrieve: ["id", "name", "avatarUrl"] },
],
});
return searchResults as SearchResults;
};

public upsert = async <T extends BaseEntity>(index: SearchType, data: T): Promise<void> => {
Expand Down
12 changes: 11 additions & 1 deletion api/src/search/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,18 @@ 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 SearchQuery {
@IsString()
@IsNotEmpty()
query!: string;

@IsPositive()
limit: number = 5;
}

export interface SearchResponse extends GeneralResponse {
searchResults: SearchResults;
}

Expand Down
Loading