From 5452f890ece467dca82608a5f936ce3ac13e1273 Mon Sep 17 00:00:00 2001 From: liuyi Date: Sun, 1 Jun 2025 22:02:53 +0800 Subject: [PATCH] add meili search --- src/config/content.config.ts | 2 +- src/modules/content/services/post.service.ts | 5 ++++- src/modules/content/services/search.service.ts | 7 ++++--- 3 files changed, 9 insertions(+), 5 deletions(-) diff --git a/src/config/content.config.ts b/src/config/content.config.ts index 1687e32..266da1a 100644 --- a/src/config/content.config.ts +++ b/src/config/content.config.ts @@ -1,5 +1,5 @@ import { ContentConfig } from '@/modules/content/types'; export const content = (): ContentConfig => ({ - SearchType: 'mysql', + SearchType: 'meili', }); diff --git a/src/modules/content/services/post.service.ts b/src/modules/content/services/post.service.ts index 49bb9f1..85d86e6 100644 --- a/src/modules/content/services/post.service.ts +++ b/src/modules/content/services/post.service.ts @@ -158,12 +158,15 @@ export class PostService { options: FindParams, callback?: QueryHook, ) { - const { orderBy, isPublished, category, tag, trashed } = options; + const { orderBy, isPublished, category, tag, trashed, search } = options; if (typeof isPublished === 'boolean') { isPublished ? qb.where({ publishedAt: Not(IsNull()) }) : qb.where({ publishedAt: IsNull() }); } + if (!isNil(search)) { + this.buildSearchQuery(qb, search); + } if (trashed === SelectTrashMode.ALL || trashed === SelectTrashMode.ONLY) { qb.withDeleted(); if (trashed === SelectTrashMode.ONLY) { diff --git a/src/modules/content/services/search.service.ts b/src/modules/content/services/search.service.ts index 9538a38..73a0e4b 100644 --- a/src/modules/content/services/search.service.ts +++ b/src/modules/content/services/search.service.ts @@ -1,4 +1,4 @@ -import { ForbiddenException, OnModuleInit } from '@nestjs/common'; +import { ForbiddenException, Injectable, OnModuleInit } from '@nestjs/common'; import { isNil, omit } from 'lodash'; import { MeiliSearch } from 'meilisearch'; @@ -13,6 +13,7 @@ import { getSearchData, getSearchItem } from '@/modules/content/utils'; import { SelectTrashMode } from '@/modules/database/constants'; import { MeiliService } from '@/modules/meilisearch/meili.service'; +@Injectable() export class SearchService implements OnModuleInit { private index = 'content'; @@ -29,7 +30,7 @@ export class SearchService implements OnModuleInit { async onModuleInit(): Promise { await this.client.deleteIndex(this.index); - this.client.index(this.index).updateFilterableAttributes(['deleteAt', 'publishedAt']); + this.client.index(this.index).updateFilterableAttributes(['deletedAt', 'publishedAt']); this.client.index(this.index).updateSortableAttributes(['updatedAt', 'commentCount']); const posts = await this.postRepository.buildBaseQB().withDeleted().getMany(); await this.client @@ -47,7 +48,7 @@ export class SearchService implements OnModuleInit { } async search(text: string, param: SearchOption = {}) { - const option = { page: 1, limit: 10, trashed: SelectTrashMode.NONE, ...param }; + const option = { page: 1, limit: 10, trashed: SelectTrashMode.ONLY, ...param }; const limit = isNil(option.limit) || option.limit < 1 ? 1 : option.limit; const page = isNil(option.page) || option.page < 1 ? 1 : option.page; let filter = ['deletedAt IS NULL'];