mirror of
https://github.com/khoaliber/khoj.git
synced 2026-03-07 05:40:17 +00:00
Fix ranking search results on Obsidian (#560)
This bug was causing the search results on the Obsidian client to be shown in the reverse order of their actual relevance. It reversed since entry scores returned by Khoj server are a distance metric since the move to Postgres. So lesser distance is better. Previously higher score was better.
This commit is contained in:
@@ -87,27 +87,18 @@ export class KhojSearchModal extends SuggestModal<SearchResult> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async getSuggestions(query: string): Promise<SearchResult[]> {
|
async getSuggestions(query: string): Promise<SearchResult[]> {
|
||||||
// Query Khoj backend for search results
|
// Setup Query Khoj backend for search results
|
||||||
let encodedQuery = encodeURIComponent(query);
|
let encodedQuery = encodeURIComponent(query);
|
||||||
let searchUrl = `${this.setting.khojUrl}/api/search?q=${encodedQuery}&n=${this.setting.resultsCount}&r=${this.rerank}&client=obsidian`;
|
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}` }
|
let headers = { 'Authorization': `Bearer ${this.setting.khojApiKey}` }
|
||||||
|
|
||||||
// Get search results for markdown and pdf files
|
// Get search results from Khoj backend
|
||||||
let mdResponse = await request({ url: `${searchUrl}&t=markdown`, headers: headers });
|
let response = await request({ url: `${searchUrl}`, headers: headers });
|
||||||
let pdfResponse = await request({ url: `${searchUrl}&t=pdf`, headers: headers });
|
|
||||||
|
|
||||||
// Parse search results
|
// 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))
|
.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 }; });
|
.map((result: any) => { return { entry: result.entry, file: result.additional.file } as SearchResult; });
|
||||||
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) => b.score - a.score)
|
|
||||||
.map((result: any) => { return { entry: result.entry, file: result.file } as SearchResult; })
|
|
||||||
|
|
||||||
this.query = query;
|
this.query = query;
|
||||||
return results;
|
return results;
|
||||||
|
|||||||
Reference in New Issue
Block a user