diff --git a/apps/core/src/modules/configs/configs.default.ts b/apps/core/src/modules/configs/configs.default.ts index 007a11fc504..c39cf4248a6 100644 --- a/apps/core/src/modules/configs/configs.default.ts +++ b/apps/core/src/modules/configs/configs.default.ts @@ -51,7 +51,13 @@ export const generateDefaultConfig: () => IConfig = () => ({ secretKey: null!, }, baiduSearchOptions: { enable: false, token: null! }, - algoliaSearchOptions: { enable: false, apiKey: '', appId: '', indexName: '' }, + algoliaSearchOptions: { + enable: false, + apiKey: '', + appId: '', + indexName: '', + maxTruncateSize: 10000, + }, adminExtra: { enableAdminProxy: true, diff --git a/apps/core/src/modules/configs/configs.dto.ts b/apps/core/src/modules/configs/configs.dto.ts index 669fdba4b4a..d78e529f3ac 100644 --- a/apps/core/src/modules/configs/configs.dto.ts +++ b/apps/core/src/modules/configs/configs.dto.ts @@ -3,13 +3,16 @@ import { ArrayUnique, IsBoolean, IsEmail, + isInt, IsInt, IsIP, IsNotEmpty, + IsNumber, IsObject, IsOptional, IsString, IsUrl, + Min, ValidateNested, } from 'class-validator' import { JSONSchema } from 'class-validator-jsonschema' @@ -229,6 +232,15 @@ export class AlgoliaSearchOptionsDto { @IsOptional() @JSONSchemaPlainField('IndexName') indexName?: string + + @IsInt() + @Min(100) + @IsOptional() + @JSONSchemaPlainField('最大文档大小', { + description: + 'Algolia 文档大小限制,单位为字节,免费版本为 10K, 填写为 10000', + }) + maxTruncateSize?: number } @JSONSchema({ title: '后台附加设置' }) diff --git a/apps/core/src/modules/search/search.service.ts b/apps/core/src/modules/search/search.service.ts index 67fdad09902..15d3a55926f 100644 --- a/apps/core/src/modules/search/search.service.ts +++ b/apps/core/src/modules/search/search.service.ts @@ -285,9 +285,13 @@ export class SearchService { }), ]) + const { algoliaSearchOptions } = await this.configs.waitForConfigReady() + return combineDocuments .flat() - .map((item) => adjustObjectSizeEfficiently(item)) + .map((item) => + adjustObjectSizeEfficiently(item, algoliaSearchOptions.maxTruncateSize), + ) } @OnEvent(BusinessEvents.POST_CREATE) @@ -298,17 +302,22 @@ export class SearchService { if (!data) return this.executeAlgoliaSearchOperationIfEnabled(async (index) => { + const { algoliaSearchOptions } = await this.configs.waitForConfigReady() + this.logger.log( `detect post created or update, save to algolia, data id:${data.id}`, ) await index.saveObject( - adjustObjectSizeEfficiently({ - ...omit(data, '_id'), - objectID: data.id, - id: data.id, + adjustObjectSizeEfficiently( + { + ...omit(data, '_id'), + objectID: data.id, + id: data.id, - type: 'post', - }), + type: 'post', + }, + algoliaSearchOptions.maxTruncateSize, + ), { autoGenerateObjectIDIfNotExist: false, }, @@ -328,15 +337,18 @@ export class SearchService { this.logger.log( `detect post created or update, save to algolia, data id:${data.id}`, ) - await index.saveObject( - adjustObjectSizeEfficiently({ - ...omit(data, '_id'), - objectID: data.id, - - id: data.id, + const { algoliaSearchOptions } = await this.configs.waitForConfigReady() - type: 'note', - }), + await index.saveObject( + adjustObjectSizeEfficiently( + { + ...omit(data, '_id'), + objectID: data.id, + id: data.id, + type: 'note', + }, + algoliaSearchOptions.maxTruncateSize, + ), { autoGenerateObjectIDIfNotExist: false, }, @@ -367,7 +379,7 @@ export class SearchService { } } -const MAX_SIZE_IN_BYTES = 100_000 +const MAX_SIZE_IN_BYTES = 10_000 function adjustObjectSizeEfficiently( originalObject: T, maxSizeInBytes: number = MAX_SIZE_IN_BYTES,