Skip to content

Commit 62a8ecb

Browse files
committed
refactor: streamline index setup by introducing upsertIndex method for improved index management
1 parent b49c098 commit 62a8ecb

File tree

2 files changed

+18
-17
lines changed

2 files changed

+18
-17
lines changed

api/src/search/service.ts

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -65,22 +65,23 @@ export class SearchService {
6565
};
6666

6767
private setupIndexes = async (): Promise<void> => {
68-
await this.meilisearch.createIndex("project", {
69-
primaryKey: "id",
70-
});
71-
this.logger.info({ message: "project index created" });
72-
73-
await this.meilisearch.createIndex("contribution", {
74-
primaryKey: "id",
75-
});
76-
this.logger.info({ message: "contribution index created" });
77-
78-
await this.meilisearch.createIndex("contributor", {
79-
primaryKey: "id",
80-
});
81-
this.logger.info({ message: "contributor index created" });
68+
await this.upsertIndex("project");
69+
await this.upsertIndex("contribution");
70+
await this.upsertIndex("contributor");
8271
};
8372

73+
private async upsertIndex(index: SearchType): Promise<void> {
74+
try {
75+
await this.meilisearch.getIndex(index);
76+
this.logger.info({ message: `${index} index already exists` });
77+
} catch {
78+
await this.meilisearch.createIndex(index, {
79+
primaryKey: "id",
80+
});
81+
this.logger.info({ message: `${index} index created` });
82+
}
83+
}
84+
8485
private async updateFilterableAttributes(): Promise<void> {
8586
await this.meilisearch
8687
.index("project")

api/src/search/types.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ export interface GetSearchResponse extends GeneralResponse {
88
searchResults: SearchResults;
99
}
1010

11-
export type SearchResults = MultiSearchResponse<SearchItem>;
12-
13-
type SearchItem = ProjectEntity | ContributionEntity | ContributorEntity;
11+
export type SearchResults = MultiSearchResponse<
12+
ProjectEntity | ContributionEntity | ContributorEntity
13+
>;
1414

1515
export type SearchType = "project" | "contribution" | "contributor";

0 commit comments

Comments
 (0)