feat: note reindexing for search service

This commit is contained in:
무라쿠모 2024-09-06 17:02:06 +09:00
parent 243c9c0075
commit 4f8ef63111
No known key found for this signature in database
GPG key ID: 139D6573F92DA9F7
7 changed files with 157 additions and 21 deletions

View file

@ -0,0 +1,77 @@
/*
* SPDX-FileCopyrightText: syuilo and misskey-project
* SPDX-License-Identifier: AGPL-3.0-only
*/
import { Inject, Injectable } from '@nestjs/common';
import { LessThanOrEqual, MoreThan } from 'typeorm';
import { DI } from '@/di-symbols.js';
import type { MiNote, NotesRepository } from '@/models/_.js';
import type Logger from '@/logger.js';
import { bindThis } from '@/decorators.js';
import { SearchService } from '@/core/SearchService.js';
import { QueueLoggerService } from '../QueueLoggerService.js';
import type * as Bull from 'bullmq';
@Injectable()
export class ReindexNotesProcessorService {
private logger: Logger;
constructor(
@Inject(DI.notesRepository)
private notesRepository: NotesRepository,
private searchService: SearchService,
private queueLoggerService: QueueLoggerService,
) {
this.logger = this.queueLoggerService.logger.createSubLogger('clean-remote-files');
}
@bindThis
public async process(job: Bull.Job<Record<string, unknown>>): Promise<void> {
this.logger.info('Removing all indexes from search engine...');
await this.searchService.unindexAllNotes();
this.logger.info('Removed all indexes from search engine.');
this.logger.info('Indexing all notes to search engine...');
const lastNote = await this.notesRepository.findOneOrFail({
order: { id: 'DESC' },
});
let indexedCount = 0;
let cursor: MiNote['id'] | null = null;
while (true) {
const notes = await this.notesRepository.find({
where: {
id: LessThanOrEqual(lastNote.id),
...(cursor ? { id: MoreThan(cursor) } : {}),
},
take: 20,
order: {
id: 'ASC',
},
});
if (notes.length === 0) {
await job.updateProgress(100);
break;
}
cursor = notes[notes.length - 1].id;
await Promise.all(notes.map(note => this.searchService.indexNote(note)));
indexedCount += 20;
const total = await this.notesRepository.countBy({
id: LessThanOrEqual(lastNote.id),
});
job.updateProgress(100 / total * indexedCount);
}
this.logger.succ('Successfully re-indexed all notes to search engine.');
}
}