Skip to content

Commit 1b9d79e

Browse files
authored
Merge pull request #618 from dzcode-io/using-meilisearch-client
2 parents 4f20866 + 512aa88 commit 1b9d79e

File tree

8 files changed

+49
-4
lines changed

8 files changed

+49
-4
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ coverage
1919
api/oracle-cloud/build
2020
api/fetch_cache
2121
api/postgres_db
22+
api/meilisearch_db
2223
api/nodemon.json
2324

2425
# web

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ npm run dev:all
6363

6464
- For api server go to <http://localhost:7070>
6565
- For web server go to <http://localhost:8080>
66+
- For search server go to <http://localhost:7700>
6667

6768
**Note**
6869

api/docker-compose.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ services:
1515
ports:
1616
- "7700:7700"
1717
volumes:
18-
- ./meilisearch_db:/data.ms
18+
- ./meilisearch_db:/meili_data
1919
environment:
2020
MEILI_NO_ANALYTICS: true
2121
MEILI_MASTER_KEY: "default"

api/oracle-cloud/docker-compose.yml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,12 @@ services:
2727
environment:
2828
POSTGRES_HOST_AUTH_METHOD: trust
2929
POSTGRES_DB: db
30+
meilisearch:
31+
image: getmeili/meilisearch:latest
32+
ports:
33+
- "7700:7700"
34+
volumes:
35+
- /home/ubuntu/app-data/api/meilisearch_db:/meili_data
36+
environment:
37+
MEILI_NO_ANALYTICS: true
38+
MEILI_MASTER_KEY: "default"

api/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@
7777
"lint:prettier": "prettier --config ../packages/tooling/.prettierrc --ignore-path ../packages/tooling/.prettierignore --log-level warn",
7878
"lint:ts-prune": "tsx ../packages/tooling/setup-ts-prune.ts && ts-prune --error",
7979
"lint:tsc": "tspc --noEmit",
80-
"start": "wait-port postgres:5432 && delay 2 && node dist/app/index.js",
80+
"start": "wait-port postgres:5432 && wait-port meilisearch:7700 && delay 2 && node dist/app/index.js",
8181
"start:dev": "tsx ../packages/tooling/nodemon.ts \"@dzcode.io/api\" && npm-run-all --parallel start:nodemon db:server",
8282
"start:nodemon": "wait-port localhost:5432 && delay 2 && nodemon dist/app/index.js",
8383
"test": "npm run build && npm run test:alone",

api/src/app/index.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import { PostgresService } from "src/postgres/service";
2525
import { ProjectController } from "src/project/controller";
2626
import { RobotsController } from "./middlewares/robots";
2727
import { SearchController } from "src/search/controller";
28+
import { SearchService } from "src/search/service";
2829
import { SecurityMiddleware } from "./middlewares/security";
2930
import { fsConfig } from "@dzcode.io/utils/dist/config";
3031

@@ -38,6 +39,10 @@ useContainer(Container); // eslint-disable-line react-hooks/rules-of-hooks
3839

3940
const { NODE_ENV, PORT } = Container.get(ConfigService).env();
4041

42+
// Initialize Search Service
43+
const searchService = Container.get(SearchService);
44+
await searchService.ensureIndexes();
45+
4146
// Add crons to DI container
4247
const CronServices = [DigestCron];
4348
CronServices.forEach((service) => Container.get(service));

api/src/search/service.ts

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1+
import { SearchItem, SearchType } from "./types";
2+
13
import { ConfigService } from "src/config/service";
24
import { LoggerService } from "src/logger/service";
35
import { MeiliSearch } from "meilisearch";
4-
import { SearchItem } from "./types";
56
import { Service } from "typedi";
67

78
@Service()
@@ -11,17 +12,45 @@ export class SearchService {
1112
private readonly configService: ConfigService,
1213
private readonly logger: LoggerService,
1314
) {
15+
this.logger.info({ message: "Initializing MeiliSearch client" });
1416
const { MEILISEARCH_URL, MEILISEARCH_MASTER_KEY } =
1517
this.configService.env();
1618

1719
this.meilisearch = new MeiliSearch({
1820
host: MEILISEARCH_URL,
1921
apiKey: MEILISEARCH_MASTER_KEY,
2022
});
23+
this.logger.info({
24+
message: `MeiliSearch client initialized with url ${MEILISEARCH_URL}`,
25+
});
2126
}
2227

2328
public search = async (query: string): Promise<SearchItem[]> => {
2429
this.logger.info({ message: `Searching for ${query}` });
2530
return [];
2631
};
32+
33+
public index = async (
34+
index: SearchType,
35+
data: SearchItem[],
36+
): Promise<void> => {
37+
this.logger.info({
38+
message: `Indexing ${data.length} items in ${index}`,
39+
});
40+
await this.meilisearch.index(index).addDocuments(data);
41+
this.logger.info({
42+
message: `Indexed ${data.length} items in ${index}`,
43+
});
44+
};
45+
46+
public ensureIndexes = async (): Promise<void> => {
47+
await this.meilisearch.createIndex("project");
48+
this.logger.info({ message: "project index created" });
49+
50+
await this.meilisearch.createIndex("contribution");
51+
this.logger.info({ message: "contribution index created" });
52+
53+
await this.meilisearch.createIndex("contributor");
54+
this.logger.info({ message: "contributor index created" });
55+
};
2756
}

api/src/search/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,4 @@ export interface SearchItem {
1010
type: SearchType;
1111
}
1212

13-
type SearchType = "project" | "contribution" | "contributor";
13+
export type SearchType = "project" | "contribution" | "contributor";

0 commit comments

Comments
 (0)