generated from obsidianmd/obsidian-sample-plugin
-
-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
09d491b
commit d5aafa3
Showing
2 changed files
with
49 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import BM25TextSearch from "wink-bm25-text-search"; | ||
|
||
class BM25Singleton { | ||
private static instance: BM25TextSearch | null = null; | ||
|
||
/** | ||
* Returns a singleton instance of BM25TextSearch. | ||
* Initializes the index if not already initialized. | ||
* @param folders Array of folder names | ||
* @returns BM25TextSearch instance | ||
*/ | ||
static getInstance(folders: string[]): BM25TextSearch { | ||
if (!BM25Singleton.instance) { | ||
BM25Singleton.instance = BM25TextSearch({ | ||
fieldsToIndex: ['folder'], | ||
}); | ||
folders.forEach(folder => { | ||
BM25Singleton.instance!.addDoc({ folder }, folder); | ||
}); | ||
BM25Singleton.instance.finalize(); | ||
} | ||
return BM25Singleton.instance; | ||
} | ||
|
||
/** | ||
* Resets the BM25 index. | ||
* Call this method if the folder list changes. | ||
* @param folders New array of folder names | ||
*/ | ||
static resetInstance(folders: string[]) { | ||
BM25Singleton.instance = BM25TextSearch({ | ||
fieldsToIndex: ['folder'], | ||
}); | ||
folders.forEach(folder => { | ||
BM25Singleton.instance!.addDoc({ folder }, folder); | ||
}); | ||
BM25Singleton.instance.finalize(); | ||
} | ||
} | ||
|
||
export default BM25Singleton; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters