From 023b34df112d61bc0883d15d9de5ceeb3f97d959 Mon Sep 17 00:00:00 2001 From: nafu-at Date: Thu, 22 Aug 2024 17:15:10 +0900 Subject: [PATCH 1/2] =?UTF-8?q?fix(search):=20elasticsearch=E3=81=AB?= =?UTF-8?q?=E7=99=BB=E9=8C=B2=E3=81=95=E3=82=8C=E3=81=9F=E3=83=8E=E3=83=BC?= =?UTF-8?q?=E3=83=88=E3=81=8C=E5=89=8A=E9=99=A4=E3=81=95=E3=82=8C=E3=81=9F?= =?UTF-8?q?=E5=A0=B4=E5=90=88=E3=81=ABunindex=E3=81=95=E3=82=8C=E3=81=AA?= =?UTF-8?q?=E3=81=84=E3=81=AE=E3=82=92=E4=BF=AE=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packages/backend/src/core/SearchService.ts | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/packages/backend/src/core/SearchService.ts b/packages/backend/src/core/SearchService.ts index e20ed06a04c1..0e773e6e8ac2 100644 --- a/packages/backend/src/core/SearchService.ts +++ b/packages/backend/src/core/SearchService.ts @@ -172,6 +172,7 @@ export class SearchService { if (note.text == null && note.cw == null) return; if (!['home', 'public'].includes(note.visibility)) return; + const createdAt = this.idService.parse(note.id).date; if (this.meilisearch) { switch (this.meilisearchIndexScope) { case 'global': @@ -190,7 +191,7 @@ export class SearchService { await this.meilisearchNoteIndex?.addDocuments([{ id: note.id, - createdAt: this.idService.parse(note.id).date.getTime(), + createdAt: createdAt.getTime(), userId: note.userId, userHost: note.userHost, channelId: note.channelId, @@ -202,7 +203,7 @@ export class SearchService { }); } else if (this.elasticsearch) { const body = { - createdAt: this.idService.parse(note.id).date.getTime(), + createdAt: createdAt.getTime(), userId: note.userId, userHost: note.userHost, channelId: note.channelId, @@ -211,11 +212,11 @@ export class SearchService { tags: note.tags, }; await this.elasticsearch.index({ - index: this.elasticsearchNoteIndex + `-${new Date().toISOString().slice(0, 7).replace(/-/g, '')}` as string, + index: this.elasticsearchNoteIndex + `-${createdAt.toISOString().slice(0, 7).replace(/-/g, '')}` as string, id: note.id, body: body, }).catch((error) => { - console.error(error); + this.logger.error(error); }); } } @@ -226,6 +227,13 @@ export class SearchService { if (this.meilisearch) { 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, '')}` as string, + id: note.id, + }).catch((error) => { + this.logger.error(error); + }); } } From 4331d0bd433b48e7ee44b98649d1130fca06384e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=9A=90=E6=9C=88=E3=81=AA=E3=81=B5=20=28Nafu=20Satsuki?= =?UTF-8?q?=29?= Date: Thu, 22 Aug 2024 22:26:51 +0900 Subject: [PATCH 2/2] Apply suggestions from code review MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: まっちゃとーにゅ <17376330+u1-liquid@users.noreply.github.com> --- packages/backend/src/core/SearchService.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/backend/src/core/SearchService.ts b/packages/backend/src/core/SearchService.ts index 0e773e6e8ac2..adc21781dc41 100644 --- a/packages/backend/src/core/SearchService.ts +++ b/packages/backend/src/core/SearchService.ts @@ -212,7 +212,7 @@ export class SearchService { tags: note.tags, }; await this.elasticsearch.index({ - index: this.elasticsearchNoteIndex + `-${createdAt.toISOString().slice(0, 7).replace(/-/g, '')}` as string, + index: `${this.elasticsearchNoteIndex}-${createdAt.toISOString().slice(0, 7).replace(/-/g, '')}`, id: note.id, body: body, }).catch((error) => { @@ -229,7 +229,7 @@ export class SearchService { 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, '')}` as string, + index: `${this.elasticsearchNoteIndex}-${this.idService.parse(note.id).date.toISOString().slice(0, 7).replace(/-/g, '')}`, id: note.id, }).catch((error) => { this.logger.error(error);