add meili search

This commit is contained in:
liuyi 2025-06-01 22:02:53 +08:00
parent 02ccf58457
commit 5452f890ec
3 changed files with 9 additions and 5 deletions

View File

@ -1,5 +1,5 @@
import { ContentConfig } from '@/modules/content/types'; import { ContentConfig } from '@/modules/content/types';
export const content = (): ContentConfig => ({ export const content = (): ContentConfig => ({
SearchType: 'mysql', SearchType: 'meili',
}); });

View File

@ -158,12 +158,15 @@ export class PostService {
options: FindParams, options: FindParams,
callback?: QueryHook<PostEntity>, callback?: QueryHook<PostEntity>,
) { ) {
const { orderBy, isPublished, category, tag, trashed } = options; const { orderBy, isPublished, category, tag, trashed, search } = options;
if (typeof isPublished === 'boolean') { if (typeof isPublished === 'boolean') {
isPublished isPublished
? qb.where({ publishedAt: Not(IsNull()) }) ? qb.where({ publishedAt: Not(IsNull()) })
: qb.where({ publishedAt: IsNull() }); : qb.where({ publishedAt: IsNull() });
} }
if (!isNil(search)) {
this.buildSearchQuery(qb, search);
}
if (trashed === SelectTrashMode.ALL || trashed === SelectTrashMode.ONLY) { if (trashed === SelectTrashMode.ALL || trashed === SelectTrashMode.ONLY) {
qb.withDeleted(); qb.withDeleted();
if (trashed === SelectTrashMode.ONLY) { if (trashed === SelectTrashMode.ONLY) {

View File

@ -1,4 +1,4 @@
import { ForbiddenException, OnModuleInit } from '@nestjs/common'; import { ForbiddenException, Injectable, OnModuleInit } from '@nestjs/common';
import { isNil, omit } from 'lodash'; import { isNil, omit } from 'lodash';
import { MeiliSearch } from 'meilisearch'; import { MeiliSearch } from 'meilisearch';
@ -13,6 +13,7 @@ import { getSearchData, getSearchItem } from '@/modules/content/utils';
import { SelectTrashMode } from '@/modules/database/constants'; import { SelectTrashMode } from '@/modules/database/constants';
import { MeiliService } from '@/modules/meilisearch/meili.service'; import { MeiliService } from '@/modules/meilisearch/meili.service';
@Injectable()
export class SearchService implements OnModuleInit { export class SearchService implements OnModuleInit {
private index = 'content'; private index = 'content';
@ -29,7 +30,7 @@ export class SearchService implements OnModuleInit {
async onModuleInit(): Promise<any> { async onModuleInit(): Promise<any> {
await this.client.deleteIndex(this.index); 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']); this.client.index(this.index).updateSortableAttributes(['updatedAt', 'commentCount']);
const posts = await this.postRepository.buildBaseQB().withDeleted().getMany(); const posts = await this.postRepository.buildBaseQB().withDeleted().getMany();
await this.client await this.client
@ -47,7 +48,7 @@ export class SearchService implements OnModuleInit {
} }
async search(text: string, param: SearchOption = {}) { 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 limit = isNil(option.limit) || option.limit < 1 ? 1 : option.limit;
const page = isNil(option.page) || option.page < 1 ? 1 : option.page; const page = isNil(option.page) || option.page < 1 ? 1 : option.page;
let filter = ['deletedAt IS NULL']; let filter = ['deletedAt IS NULL'];