From c89bd49973c3c3afe73306339b83112d8b0e811a Mon Sep 17 00:00:00 2001 From: Debanjum Singh Solanky Date: Tue, 21 Nov 2023 01:21:59 -0800 Subject: [PATCH 1/2] Fix ranking search results on Obsidian It's reversed since score of entries is now a distance metric on Khoj server. So lesser distance is better. Previously higher score was better --- src/interface/obsidian/src/search_modal.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/interface/obsidian/src/search_modal.ts b/src/interface/obsidian/src/search_modal.ts index e841360e..51870934 100644 --- a/src/interface/obsidian/src/search_modal.ts +++ b/src/interface/obsidian/src/search_modal.ts @@ -106,7 +106,7 @@ export class KhojSearchModal extends SuggestModal { // Combine markdown and PDF results and sort them by score let results = mdData.concat(pdfData) - .sort((a: any, b: any) => b.score - a.score) + .sort((a: any, b: any) => a.score - b.score) .map((result: any) => { return { entry: result.entry, file: result.file } as SearchResult; }) this.query = query; From 645fd966343e6f90e4858846b3ed33dc4fead7ab Mon Sep 17 00:00:00 2001 From: Debanjum Singh Solanky Date: Tue, 21 Nov 2023 11:19:33 -0800 Subject: [PATCH 2/2] Search across all content types from Khoj Obsidian client Previously it was only searching for PDF and Markdown files. This was meant to show only content from current vault as results. But it has not scaled well as other clients also allow syncing PDF and markdown files now. So remove this content type filter for now. A proper solution would limit by using file/dir filters on server or client side. --- src/interface/obsidian/src/search_modal.ts | 19 +++++-------------- 1 file changed, 5 insertions(+), 14 deletions(-) diff --git a/src/interface/obsidian/src/search_modal.ts b/src/interface/obsidian/src/search_modal.ts index 51870934..7e97d1ea 100644 --- a/src/interface/obsidian/src/search_modal.ts +++ b/src/interface/obsidian/src/search_modal.ts @@ -87,27 +87,18 @@ export class KhojSearchModal extends SuggestModal { } async getSuggestions(query: string): Promise { - // Query Khoj backend for search results + // Setup Query Khoj backend for search results let encodedQuery = encodeURIComponent(query); let searchUrl = `${this.setting.khojUrl}/api/search?q=${encodedQuery}&n=${this.setting.resultsCount}&r=${this.rerank}&client=obsidian`; let headers = { 'Authorization': `Bearer ${this.setting.khojApiKey}` } - // Get search results for markdown and pdf files - let mdResponse = await request({ url: `${searchUrl}&t=markdown`, headers: headers }); - let pdfResponse = await request({ url: `${searchUrl}&t=pdf`, headers: headers }); + // Get search results from Khoj backend + let response = await request({ url: `${searchUrl}`, headers: headers }); // Parse search results - let mdData = JSON.parse(mdResponse) + let results = JSON.parse(response) .filter((result: any) => !this.find_similar_notes || !result.additional.file.endsWith(this.app.workspace.getActiveFile()?.path)) - .map((result: any) => { return { entry: result.entry, score: result.score, file: result.additional.file }; }); - let pdfData = JSON.parse(pdfResponse) - .filter((result: any) => !this.find_similar_notes || !result.additional.file.endsWith(this.app.workspace.getActiveFile()?.path)) - .map((result: any) => { return { entry: `## ${result.additional.compiled}`, score: result.score, file: result.additional.file } as SearchResult; }) - - // Combine markdown and PDF results and sort them by score - let results = mdData.concat(pdfData) - .sort((a: any, b: any) => a.score - b.score) - .map((result: any) => { return { entry: result.entry, file: result.file } as SearchResult; }) + .map((result: any) => { return { entry: result.entry, file: result.additional.file } as SearchResult; }); this.query = query; return results;