diff --git a/apps/core/src/app.module.ts b/apps/core/src/app.module.ts index c8d434d9..25b3852b 100644 --- a/apps/core/src/app.module.ts +++ b/apps/core/src/app.module.ts @@ -56,8 +56,6 @@ import { ServerlessModule } from './modules/serverless/serverless.module' import { SitemapModule } from './modules/sitemap/sitemap.module' import { SnippetModule } from './modules/snippet/snippet.module' import { SubscribeModule } from './modules/subscribe/subscribe.module' -import { SyncUpdateModule } from './modules/sync-update/sync-update.module' -import { SyncModule } from './modules/sync/sync.module' import { TopicModule } from './modules/topic/topic.module' import { UpdateModule } from './modules/update/update.module' import { UserModule } from './modules/user/user.module' @@ -100,8 +98,6 @@ import { RedisModule } from './processors/redis/redis.module' RecentlyModule, SayModule, SearchModule, - SyncModule, - SyncUpdateModule, ServerlessModule, ServerTimeModule, SitemapModule, diff --git a/apps/core/src/modules/search/search.controller.ts b/apps/core/src/modules/search/search.controller.ts index 90eca210..e131e87d 100644 --- a/apps/core/src/modules/search/search.controller.ts +++ b/apps/core/src/modules/search/search.controller.ts @@ -1,6 +1,16 @@ -import { BadRequestException, Get, Param, Post, Query } from '@nestjs/common' +import { FastifyReply } from 'fastify' + +import { + BadRequestException, + Get, + Param, + Post, + Query, + Res, +} from '@nestjs/common' import { ApiController } from '~/common/decorators/api-controller.decorator' +import { Auth } from '~/common/decorators/auth.decorator' import { HttpCache } from '~/common/decorators/cache.decorator' import { HTTPDecorators } from '~/common/decorators/http.decorator' import { IsMaster } from '~/common/decorators/role.decorator' @@ -39,7 +49,17 @@ export class SearchController { } @Post('/algolia/push') + @Auth() async pushAlgoliaAllManually() { return this.searchService.pushAllToAlgoliaSearch() } + + @Get('/algolia/import-json') + @Auth() + async getAlgoliaIndexJsonFile(@Res() res: FastifyReply) { + const documents = await this.searchService.buildAlgoliaIndexData() + res.header('Content-Type', 'application/json') + res.header('Content-Disposition', 'attachment; filename=algolia-index.json') + res.send(JSON.stringify(documents)) + } } diff --git a/apps/core/src/modules/search/search.service.ts b/apps/core/src/modules/search/search.service.ts index be9dce80..9fd5ac37 100644 --- a/apps/core/src/modules/search/search.service.ts +++ b/apps/core/src/modules/search/search.service.ts @@ -206,6 +206,26 @@ export class SearchService { const index = await this.getAlgoliaSearchIndex() this.logger.log('--> 开始推送到 Algolia') + + const documents = await this.buildAlgoliaIndexData() + try { + await Promise.all([ + index.replaceAllObjects(documents, { + autoGenerateObjectIDIfNotExist: false, + }), + index.setSettings({ + attributesToHighlight: ['text', 'title'], + }), + ]) + + this.logger.log('--> 推送到 algoliasearch 成功') + } catch (err) { + Logger.error('algolia 推送错误', 'AlgoliaSearch') + throw err + } + } + + async buildAlgoliaIndexData() { const documents: Record<'title' | 'text' | 'type' | 'id', string>[] = [] const combineDocuments = await Promise.all([ this.postService.model @@ -267,21 +287,8 @@ export class SearchService { combineDocuments.forEach((documents_: any) => { documents.push(...documents_) }) - try { - await Promise.all([ - index.replaceAllObjects(documents, { - autoGenerateObjectIDIfNotExist: false, - }), - index.setSettings({ - attributesToHighlight: ['text', 'title'], - }), - ]) - this.logger.log('--> 推送到 algoliasearch 成功') - } catch (err) { - Logger.error('algolia 推送错误', 'AlgoliaSearch') - throw err - } + return documents } @OnEvent(BusinessEvents.POST_CREATE)