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

@ -225,7 +225,7 @@ export class SearchService {
await this.elasticsearch.index({
index: `${this.elasticsearchNoteIndex}-${createdAt.toISOString().slice(0, 7).replace(/-/g, '')}`,
id: note.id,
body: body,
document: body,
}).catch((error: any) => {
this.logger.error(error);
});
@ -237,7 +237,7 @@ export class SearchService {
// if (!['home', 'public'].includes(note.visibility)) return;
if (this.meilisearch) {
this.meilisearchNoteIndex?.deleteDocument(note.id);
await this.meilisearchNoteIndex?.deleteDocument(note.id);
} else if (this.elasticsearch) {
await this.elasticsearch.delete({
index: `${this.elasticsearchNoteIndex}-${this.idService.parse(note.id).date.toISOString().slice(0, 7).replace(/-/g, '')}`,
@ -248,19 +248,38 @@ export class SearchService {
}
}
@bindThis
public async unindexAllNotes(): Promise<void> {
if (this.meilisearch) {
await this.meilisearchNoteIndex?.deleteAllDocuments();
} else if (this.elasticsearch) {
await this.elasticsearch.deleteByQuery({
index: this.elasticsearchNoteIndex + '*' as string,
query: {
match_all: {},
},
}).catch((error) => {
this.logger.error(error);
});
}
}
@bindThis
private async filter(me: MiUser | null, note: MiNote): Promise<boolean> {
const [
userIdsWhoMeMuting,
userIdsWhoMeBlocking,
userIdsWhoBlockingMe,
] = me ? await Promise.all([
this.cacheService.userMutingsCache.fetch(me.id),
this.cacheService.userBlockingCache.fetch(me.id),
this.cacheService.userBlockedCache.fetch(me.id),
]) : [new Set<string>(), new Set<string>()];
]) : [new Set<string>(), new Set<string>(), new Set<string>()];
if (me && isUserRelated(note, userIdsWhoBlockingMe)) return false;
if (me && isUserRelated(note, userIdsWhoMeBlocking)) return false;
if (me && isUserRelated(note, userIdsWhoMeMuting)) return false;
if (['followers', 'specified'].includes(note.visibility)) {
if (!me) return false;
if (me == null) return false;
if (note.visibility === 'followers') {
const relationship = await this.userEntityService.getRelation(me.id, note.userId);
if (relationship.isFollowing) return true;
@ -358,25 +377,14 @@ export class SearchService {
const noteIds = res.hits.hits.map((hit: any) => hit._id);
if (noteIds.length === 0) return [];
const [
userIdsWhoMeMuting,
userIdsWhoMeBlocking,
userIdsWhoBlockingMe,
] = me ? await Promise.all([
this.cacheService.userMutingsCache.fetch(me.id),
this.cacheService.userBlockingCache.fetch(me.id),
this.cacheService.userBlockedCache.fetch(me.id),
]) : [new Set<string>(), new Set<string>(), new Set<string>()];
const notes = (await this.notesRepository.findBy({
const notes = await this.notesRepository.findBy({
id: In(noteIds),
})).filter(note => {
if (me && isUserRelated(note, userIdsWhoBlockingMe)) return false;
if (me && isUserRelated(note, userIdsWhoMeBlocking)) return false;
if (me && isUserRelated(note, userIdsWhoMeMuting)) return false;
return true;
});
return notes.sort((a, b) => a.id > b.id ? -1 : 1);
const promises = notes.map(async note => ({ note: note, result: (await this.filter(me, note)) }));
const data = await Promise.all(promises);
const dataFilter = data.filter(d => d.result);
const filteredNotes = dataFilter.map(d => d.note);
return filteredNotes.sort((a, b) => a.id > b.id ? -1 : 1);
} else {
const query = this.queryService.makePaginationQuery(this.notesRepository.createQueryBuilder('note'), pagination.sinceId, pagination.untilId);