mirror of
https://github.com/khoj-ai/khoj.git
synced 2025-02-17 08:04:21 +00:00
Add ability to search for similar notes in Khoj Obsidian
- Hide input field on init, Trigger search on opening modal in similar notes mode - Set input to current markdown file and get similar notes to it - Enable rerank when searching for similar notes - Filter out current note from similar note search results
This commit is contained in:
parent
ffaef92476
commit
39a18e2080
2 changed files with 37 additions and 1 deletions
|
@ -21,6 +21,17 @@ export default class Khoj extends Plugin {
|
|||
}
|
||||
});
|
||||
|
||||
// Add a similar notes command
|
||||
this.addCommand({
|
||||
id: 'similar',
|
||||
name: 'Find Similar Notes',
|
||||
checkCallback: (checking) => {
|
||||
if (!checking && this.settings.connectedToBackend)
|
||||
new KhojModal(this.app, this.settings, true).open();
|
||||
return this.settings.connectedToBackend;
|
||||
}
|
||||
});
|
||||
|
||||
// Create an icon in the left ribbon.
|
||||
this.addRibbonIcon('search', 'Khoj', (_: MouseEvent) => {
|
||||
// Called when the user clicks the icon.
|
||||
|
|
|
@ -9,10 +9,15 @@ export interface SearchResult {
|
|||
export class KhojModal extends SuggestModal<SearchResult> {
|
||||
setting: KhojSetting;
|
||||
rerank: boolean = false;
|
||||
find_similar_notes: boolean;
|
||||
|
||||
constructor(app: App, setting: KhojSetting) {
|
||||
constructor(app: App, setting: KhojSetting, find_similar_notes: boolean = false) {
|
||||
super(app);
|
||||
this.setting = setting;
|
||||
this.find_similar_notes = find_similar_notes;
|
||||
|
||||
// Hide input element in Similar Notes mode
|
||||
this.inputEl.hidden = this.find_similar_notes;
|
||||
|
||||
// Register Modal Keybindings to Rerank Results
|
||||
this.scope.register(['Mod'], 'Enter', async () => {
|
||||
|
@ -49,6 +54,25 @@ export class KhojModal extends SuggestModal<SearchResult> {
|
|||
this.setPlaceholder('Search with Khoj 🦅...');
|
||||
}
|
||||
|
||||
async onOpen() {
|
||||
if (this.find_similar_notes) {
|
||||
// If markdown file is currently active
|
||||
let file = this.app.workspace.getActiveFile();
|
||||
if (file && file.extension === 'md') {
|
||||
// Enable rerank of search results
|
||||
this.rerank = true
|
||||
// Set contents of active markdown file to input element
|
||||
this.inputEl.value = await this.app.vault.read(file);
|
||||
// Trigger search to get and render similar notes from khoj backend
|
||||
this.inputEl.dispatchEvent(new Event('input'));
|
||||
this.rerank = false
|
||||
}
|
||||
else {
|
||||
this.resultContainerEl.setText('Cannot find similar notes for non-markdown files');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async getSuggestions(query: string): Promise<SearchResult[]> {
|
||||
// Query Khoj backend for search results
|
||||
let encodedQuery = encodeURIComponent(query);
|
||||
|
@ -56,6 +80,7 @@ export class KhojModal extends SuggestModal<SearchResult> {
|
|||
let results = await request(searchUrl)
|
||||
.then(response => JSON.parse(response))
|
||||
.then(data => data
|
||||
.filter((result: any) => !this.find_similar_notes || !result.additional.file.endsWith(this.app.workspace.getActiveFile()?.path))
|
||||
.map((result: any) => { return { entry: result.entry, file: result.additional.file } as SearchResult; }));
|
||||
|
||||
return results;
|
||||
|
|
Loading…
Add table
Reference in a new issue