refactor: extract get search index method
This commit is contained in:
@@ -36,8 +36,7 @@ export class SearchController {
|
||||
}
|
||||
}
|
||||
|
||||
@Get('/')
|
||||
// TODO: now only support search by algolia
|
||||
@Get('/algolia')
|
||||
async search(@Query() query: SearchDto) {
|
||||
return this.searchService.searchAlgolia(query)
|
||||
}
|
||||
|
||||
@@ -82,6 +82,19 @@ export class SearchService {
|
||||
)
|
||||
}
|
||||
|
||||
public async getAlgoliaSearchIndex() {
|
||||
const { algoliaSearchOptions } = await this.configs.waitForConfigReady()
|
||||
if (!algoliaSearchOptions.enable) {
|
||||
throw new BadRequestException('algolia not enable.')
|
||||
}
|
||||
const client = algoliasearch(
|
||||
algoliaSearchOptions.appId,
|
||||
algoliaSearchOptions.apiKey,
|
||||
)
|
||||
const index = client.initIndex(algoliaSearchOptions.indexName)
|
||||
return index
|
||||
}
|
||||
|
||||
async searchAlgolia(searchOption: SearchDto): Promise<
|
||||
| SearchResponse<{
|
||||
id: string
|
||||
@@ -98,17 +111,8 @@ export class SearchService {
|
||||
}>
|
||||
})
|
||||
> {
|
||||
const { algoliaSearchOptions } = await this.configs.waitForConfigReady()
|
||||
if (!algoliaSearchOptions.enable) {
|
||||
throw new BadRequestException('algolia not enable.')
|
||||
}
|
||||
|
||||
const { keyword, page, size } = searchOption
|
||||
const client = algoliasearch(
|
||||
algoliaSearchOptions.appId,
|
||||
algoliaSearchOptions.apiKey,
|
||||
)
|
||||
const index = client.initIndex(algoliaSearchOptions.indexName)
|
||||
const { keyword, size, page } = searchOption
|
||||
const index = await this.getAlgoliaSearchIndex()
|
||||
const search = await index.search<{
|
||||
id: string
|
||||
text: string
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
import { forwardRef, Inject, Injectable, Logger } from '@nestjs/common'
|
||||
import { Cron, CronExpression } from '@nestjs/schedule'
|
||||
import algoliasearch from 'algoliasearch'
|
||||
import COS from 'cos-nodejs-sdk-v5'
|
||||
import dayjs from 'dayjs'
|
||||
import { existsSync, readFileSync, rmSync, writeFileSync } from 'fs'
|
||||
@@ -22,6 +21,7 @@ import { ConfigsService } from '~/modules/configs/configs.service'
|
||||
import { NoteService } from '~/modules/note/note.service'
|
||||
import { PageService } from '~/modules/page/page.service'
|
||||
import { PostService } from '~/modules/post/post.service'
|
||||
import { SearchService } from '~/modules/search/search.service'
|
||||
import { getRedisKey } from '~/utils/redis.util'
|
||||
import { CacheService } from '../cache/cache.service'
|
||||
import { HttpService } from './helper.http.service'
|
||||
@@ -47,6 +47,8 @@ export class CronService {
|
||||
|
||||
@Inject(forwardRef(() => PageService))
|
||||
private readonly pageService: PageService,
|
||||
@Inject(forwardRef(() => SearchService))
|
||||
private readonly searchService: SearchService,
|
||||
) {
|
||||
this.logger = new Logger(CronService.name)
|
||||
}
|
||||
@@ -255,80 +257,76 @@ export class CronService {
|
||||
@Cron(CronExpression.EVERY_DAY_AT_NOON, { name: 'pushToAlgoliaSearch' })
|
||||
@CronDescription('推送到 Algolia Search')
|
||||
async pushToAlgoliaSearch() {
|
||||
const { algoliaSearchOptions: configs } =
|
||||
await this.configs.waitForConfigReady()
|
||||
if (configs.enable) {
|
||||
this.logger.log('--> 开始推送到 Algolia')
|
||||
const client = algoliasearch(configs.appId, configs.apiKey)
|
||||
const index = client.initIndex(configs.indexName)
|
||||
const documents: Record<'title' | 'text' | 'type' | 'id', string>[] = []
|
||||
const combineDocuments = await Promise.all([
|
||||
this.postService.model
|
||||
.find({ hide: false }, 'title text')
|
||||
.lean()
|
||||
.then((list) => {
|
||||
return list.map((data) => {
|
||||
Reflect.set(data, 'objectID', data._id)
|
||||
Reflect.deleteProperty(data, '_id')
|
||||
return {
|
||||
...data,
|
||||
type: 'post',
|
||||
}
|
||||
})
|
||||
}),
|
||||
this.pageService.model
|
||||
.find({}, 'title text')
|
||||
.lean()
|
||||
.then((list) => {
|
||||
return list.map((data) => {
|
||||
Reflect.set(data, 'objectID', data._id)
|
||||
Reflect.deleteProperty(data, '_id')
|
||||
return {
|
||||
...data,
|
||||
type: 'page',
|
||||
}
|
||||
})
|
||||
}),
|
||||
this.noteService.model
|
||||
.find(
|
||||
{
|
||||
hide: false,
|
||||
$or: [
|
||||
{ password: undefined },
|
||||
{ password: null },
|
||||
{ password: { $exists: false } },
|
||||
],
|
||||
},
|
||||
'title text nid',
|
||||
)
|
||||
.lean()
|
||||
.then((list) => {
|
||||
return list.map((data) => {
|
||||
const id = data.nid.toString()
|
||||
Reflect.set(data, 'objectID', data._id)
|
||||
Reflect.deleteProperty(data, '_id')
|
||||
Reflect.deleteProperty(data, 'nid')
|
||||
return {
|
||||
...data,
|
||||
type: 'note',
|
||||
id,
|
||||
}
|
||||
})
|
||||
}),
|
||||
])
|
||||
combineDocuments.forEach((documents_: any) => {
|
||||
documents.push(...documents_)
|
||||
const index = await this.searchService.getAlgoliaSearchIndex()
|
||||
|
||||
this.logger.log('--> 开始推送到 Algolia')
|
||||
const documents: Record<'title' | 'text' | 'type' | 'id', string>[] = []
|
||||
const combineDocuments = await Promise.all([
|
||||
this.postService.model
|
||||
.find({ hide: false }, 'title text')
|
||||
.lean()
|
||||
.then((list) => {
|
||||
return list.map((data) => {
|
||||
Reflect.set(data, 'objectID', data._id)
|
||||
Reflect.deleteProperty(data, '_id')
|
||||
return {
|
||||
...data,
|
||||
type: 'post',
|
||||
}
|
||||
})
|
||||
}),
|
||||
this.pageService.model
|
||||
.find({}, 'title text')
|
||||
.lean()
|
||||
.then((list) => {
|
||||
return list.map((data) => {
|
||||
Reflect.set(data, 'objectID', data._id)
|
||||
Reflect.deleteProperty(data, '_id')
|
||||
return {
|
||||
...data,
|
||||
type: 'page',
|
||||
}
|
||||
})
|
||||
}),
|
||||
this.noteService.model
|
||||
.find(
|
||||
{
|
||||
hide: false,
|
||||
$or: [
|
||||
{ password: undefined },
|
||||
{ password: null },
|
||||
{ password: { $exists: false } },
|
||||
],
|
||||
},
|
||||
'title text nid',
|
||||
)
|
||||
.lean()
|
||||
.then((list) => {
|
||||
return list.map((data) => {
|
||||
const id = data.nid.toString()
|
||||
Reflect.set(data, 'objectID', data._id)
|
||||
Reflect.deleteProperty(data, '_id')
|
||||
Reflect.deleteProperty(data, 'nid')
|
||||
return {
|
||||
...data,
|
||||
type: 'note',
|
||||
id,
|
||||
}
|
||||
})
|
||||
}),
|
||||
])
|
||||
combineDocuments.forEach((documents_: any) => {
|
||||
documents.push(...documents_)
|
||||
})
|
||||
try {
|
||||
// await index.clearObjects()
|
||||
await index.saveObjects(documents, {
|
||||
autoGenerateObjectIDIfNotExist: false,
|
||||
})
|
||||
try {
|
||||
// await index.clearObjects()
|
||||
await index.saveObjects(documents, {
|
||||
autoGenerateObjectIDIfNotExist: false,
|
||||
})
|
||||
this.logger.log('--> 推送到 algoliasearch 成功')
|
||||
} catch (err) {
|
||||
Logger.error('algolia推送错误', 'AlgoliaSearch')
|
||||
throw err
|
||||
}
|
||||
this.logger.log('--> 推送到 algoliasearch 成功')
|
||||
} catch (err) {
|
||||
Logger.error('algolia推送错误', 'AlgoliaSearch')
|
||||
throw err
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -4,6 +4,7 @@ import { AggregateModule } from '~/modules/aggregate/aggregate.module'
|
||||
import { NoteModule } from '~/modules/note/note.module'
|
||||
import { PageModule } from '~/modules/page/page.module'
|
||||
import { PostModule } from '~/modules/post/post.module'
|
||||
import { SearchModule } from '~/modules/search/search.module'
|
||||
import { CountingService } from './helper.counting.service'
|
||||
import { CronService } from './helper.cron.service'
|
||||
import { EmailService } from './helper.email.service'
|
||||
@@ -32,6 +33,7 @@ const providers: Provider<any>[] = [
|
||||
forwardRef(() => PostModule),
|
||||
forwardRef(() => NoteModule),
|
||||
forwardRef(() => PageModule),
|
||||
forwardRef(() => SearchModule),
|
||||
],
|
||||
providers: providers,
|
||||
exports: providers,
|
||||
|
||||
Reference in New Issue
Block a user