From 72843203556467a318b2f28776f05bd5bc75efc8 Mon Sep 17 00:00:00 2001 From: EYHN Date: Thu, 12 Jun 2025 16:43:55 +0900 Subject: [PATCH] fix(nbstore): fix search slow (#12800) ## Summary by CodeRabbit - **Bug Fixes** - Improved search results by ensuring links to the current document are properly excluded from related document and database searches. - Enhanced session management in quick search by ensuring all active sessions are cleaned up before processing new search results. --- .../docs-search/services/docs-search.ts | 80 ++++++++----------- .../quicksearch/entities/quick-search.ts | 1 + 2 files changed, 35 insertions(+), 46 deletions(-) diff --git a/packages/frontend/core/src/modules/docs-search/services/docs-search.ts b/packages/frontend/core/src/modules/docs-search/services/docs-search.ts index 78dac29f6b..51f5992149 100644 --- a/packages/frontend/core/src/modules/docs-search/services/docs-search.ts +++ b/packages/frontend/core/src/modules/docs-search/services/docs-search.ts @@ -240,18 +240,6 @@ export class DocsSearchService extends Service { field: 'refDocId', match: docId, }, - // Ignore if it is a link to the current document. - { - type: 'boolean', - occur: 'must_not', - queries: [ - { - type: 'match', - field: 'docId', - match: docId, - }, - ], - }, ], }, 'docId', @@ -282,6 +270,11 @@ export class DocsSearchService extends Service { this.docsService.list.doc$(bucket.key).value?.title$.value ?? ''; + if (bucket.key === docId) { + // Ignore if it is a link to the current document. + return []; + } + return bucket.hits.nodes.map(node => { const blockId = node.fields.blockId ?? ''; const markdownPreview = node.fields.markdownPreview ?? ''; @@ -359,18 +352,6 @@ export class DocsSearchService extends Service { field: 'parentFlavour', match: 'affine:database', }, - // Ignore if it is a link to the current document. - { - type: 'boolean', - occur: 'must_not', - queries: [ - { - type: 'match', - field: 'docId', - match: docId, - }, - ], - }, ], }, { @@ -382,29 +363,36 @@ export class DocsSearchService extends Service { ) .pipe( map(({ nodes }) => { - return nodes.map(node => { - const additional = - typeof node.fields.additional === 'string' - ? node.fields.additional - : node.fields.additional[0]; + return nodes + .map(node => { + if (node.fields.docId === docId) { + // Ignore if it is a link to the current document. + return null; + } - return { - docId: - typeof node.fields.docId === 'string' - ? node.fields.docId - : node.fields.docId[0], - rowId: - typeof node.fields.blockId === 'string' - ? node.fields.blockId - : node.fields.blockId[0], - databaseBlockId: - typeof node.fields.parentBlockId === 'string' - ? node.fields.parentBlockId - : node.fields.parentBlockId[0], - databaseName: DatabaseAdditionalSchema.safeParse(additional).data - ?.databaseName as string | undefined, - }; - }); + const additional = + typeof node.fields.additional === 'string' + ? node.fields.additional + : node.fields.additional[0]; + + return { + docId: + typeof node.fields.docId === 'string' + ? node.fields.docId + : node.fields.docId[0], + rowId: + typeof node.fields.blockId === 'string' + ? node.fields.blockId + : node.fields.blockId[0], + databaseBlockId: + typeof node.fields.parentBlockId === 'string' + ? node.fields.parentBlockId + : node.fields.parentBlockId[0], + databaseName: DatabaseAdditionalSchema.safeParse(additional) + .data?.databaseName as string | undefined, + }; + }) + .filter((item): item is NonNullable => item !== null); }) ); } diff --git a/packages/frontend/core/src/modules/quicksearch/entities/quick-search.ts b/packages/frontend/core/src/modules/quicksearch/entities/quick-search.ts index 19752c473d..5b8d73bb58 100644 --- a/packages/frontend/core/src/modules/quicksearch/entities/quick-search.ts +++ b/packages/frontend/core/src/modules/quicksearch/entities/quick-search.ts @@ -106,6 +106,7 @@ export class QuickSearch extends Entity { submit(result: QuickSearchItem | null) { if (this.state$.value?.callback) { + this.state$.value.sessions.forEach(session => session.dispose?.()); this.state$.value.callback(result); } this.state$.next(null);