Skip to content

Commit

Permalink
feat: support algolia search custom truncate size, closed #2271
Browse files Browse the repository at this point in the history
Signed-off-by: Innei <tukon479@gmail.com>
  • Loading branch information
Innei committed Dec 20, 2024
1 parent 838b5b3 commit 6da1c13
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 17 deletions.
8 changes: 7 additions & 1 deletion apps/core/src/modules/configs/configs.default.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,

Expand Down
12 changes: 12 additions & 0 deletions apps/core/src/modules/configs/configs.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -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: '后台附加设置' })
Expand Down
44 changes: 28 additions & 16 deletions apps/core/src/modules/search/search.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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,
},
Expand All @@ -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,
},
Expand Down Expand Up @@ -367,7 +379,7 @@ export class SearchService {
}
}

const MAX_SIZE_IN_BYTES = 100_000
const MAX_SIZE_IN_BYTES = 10_000
function adjustObjectSizeEfficiently<T extends { text: string }>(
originalObject: T,
maxSizeInBytes: number = MAX_SIZE_IN_BYTES,
Expand Down

0 comments on commit 6da1c13

Please sign in to comment.