Skip to content

Commit

Permalink
Merge pull request #618 from dzcode-io/using-meilisearch-client
Browse files Browse the repository at this point in the history
  • Loading branch information
omdxp authored Dec 20, 2024
2 parents 4f20866 + 512aa88 commit 1b9d79e
Show file tree
Hide file tree
Showing 8 changed files with 49 additions and 4 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ coverage
api/oracle-cloud/build
api/fetch_cache
api/postgres_db
api/meilisearch_db
api/nodemon.json

# web
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ npm run dev:all

- For api server go to <http://localhost:7070>
- For web server go to <http://localhost:8080>
- For search server go to <http://localhost:7700>

**Note**

Expand Down
2 changes: 1 addition & 1 deletion api/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ services:
ports:
- "7700:7700"
volumes:
- ./meilisearch_db:/data.ms
- ./meilisearch_db:/meili_data
environment:
MEILI_NO_ANALYTICS: true
MEILI_MASTER_KEY: "default"
9 changes: 9 additions & 0 deletions api/oracle-cloud/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,12 @@ services:
environment:
POSTGRES_HOST_AUTH_METHOD: trust
POSTGRES_DB: db
meilisearch:
image: getmeili/meilisearch:latest
ports:
- "7700:7700"
volumes:
- /home/ubuntu/app-data/api/meilisearch_db:/meili_data
environment:
MEILI_NO_ANALYTICS: true
MEILI_MASTER_KEY: "default"
2 changes: 1 addition & 1 deletion api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@
"lint:prettier": "prettier --config ../packages/tooling/.prettierrc --ignore-path ../packages/tooling/.prettierignore --log-level warn",
"lint:ts-prune": "tsx ../packages/tooling/setup-ts-prune.ts && ts-prune --error",
"lint:tsc": "tspc --noEmit",
"start": "wait-port postgres:5432 && delay 2 && node dist/app/index.js",
"start": "wait-port postgres:5432 && wait-port meilisearch:7700 && delay 2 && node dist/app/index.js",
"start:dev": "tsx ../packages/tooling/nodemon.ts \"@dzcode.io/api\" && npm-run-all --parallel start:nodemon db:server",
"start:nodemon": "wait-port localhost:5432 && delay 2 && nodemon dist/app/index.js",
"test": "npm run build && npm run test:alone",
Expand Down
5 changes: 5 additions & 0 deletions api/src/app/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import { PostgresService } from "src/postgres/service";
import { ProjectController } from "src/project/controller";
import { RobotsController } from "./middlewares/robots";
import { SearchController } from "src/search/controller";
import { SearchService } from "src/search/service";
import { SecurityMiddleware } from "./middlewares/security";
import { fsConfig } from "@dzcode.io/utils/dist/config";

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

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

// Initialize Search Service
const searchService = Container.get(SearchService);
await searchService.ensureIndexes();

// Add crons to DI container
const CronServices = [DigestCron];
CronServices.forEach((service) => Container.get(service));
Expand Down
31 changes: 30 additions & 1 deletion api/src/search/service.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { SearchItem, SearchType } from "./types";

import { ConfigService } from "src/config/service";
import { LoggerService } from "src/logger/service";
import { MeiliSearch } from "meilisearch";
import { SearchItem } from "./types";
import { Service } from "typedi";

@Service()
Expand All @@ -11,17 +12,45 @@ export class SearchService {
private readonly configService: ConfigService,
private readonly logger: LoggerService,
) {
this.logger.info({ message: "Initializing MeiliSearch client" });
const { MEILISEARCH_URL, MEILISEARCH_MASTER_KEY } =
this.configService.env();

this.meilisearch = new MeiliSearch({
host: MEILISEARCH_URL,
apiKey: MEILISEARCH_MASTER_KEY,
});
this.logger.info({
message: `MeiliSearch client initialized with url ${MEILISEARCH_URL}`,
});
}

public search = async (query: string): Promise<SearchItem[]> => {
this.logger.info({ message: `Searching for ${query}` });
return [];
};

public index = async (
index: SearchType,
data: SearchItem[],
): Promise<void> => {
this.logger.info({
message: `Indexing ${data.length} items in ${index}`,
});
await this.meilisearch.index(index).addDocuments(data);
this.logger.info({
message: `Indexed ${data.length} items in ${index}`,
});
};

public ensureIndexes = async (): Promise<void> => {
await this.meilisearch.createIndex("project");
this.logger.info({ message: "project index created" });

await this.meilisearch.createIndex("contribution");
this.logger.info({ message: "contribution index created" });

await this.meilisearch.createIndex("contributor");
this.logger.info({ message: "contributor index created" });
};
}
2 changes: 1 addition & 1 deletion api/src/search/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,4 @@ export interface SearchItem {
type: SearchType;
}

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

0 comments on commit 1b9d79e

Please sign in to comment.