Setting for fuzziness

This commit is contained in:
Simon Cambier
2023-06-22 19:47:00 +02:00
parent be2a724c0c
commit 34ef17b4ed
2 changed files with 46 additions and 4 deletions

View File

@@ -188,12 +188,26 @@ export class Omnisearch {
logDebug('Starting search for', query)
let fuzziness: number
switch (settings.fuzziness) {
case '0':
fuzziness = 0
break
case '1':
fuzziness = 0.1
break
default:
fuzziness = 0.2
break
}
let results = this.minisearch.search(query.segmentsToStr(), {
prefix: term => term.length >= options.prefixLength,
// length <= 3: no fuzziness
// length <= 5: fuzziness of 10%
// length > 5: fuzziness of 20%
fuzzy: term => (term.length <= 3 ? 0 : term.length <= 5 ? 0.1 : 0.2),
fuzzy: term =>
term.length <= 3 ? 0 : term.length <= 5 ? fuzziness / 2 : fuzziness,
combineWith: 'AND',
boost: {
basename: settings.weightBasename,
@@ -216,7 +230,6 @@ export class Omnisearch {
ext.startsWith(e.startsWith('.') ? e : '.' + e)
)
})
console.log(query.query.ext, results.length)
}
// Filter query results that match the path

View File

@@ -7,7 +7,11 @@ import {
} from 'obsidian'
import { writable } from 'svelte/store'
import { database } from './database'
import { K_DISABLE_OMNISEARCH, getTextExtractor, isCacheEnabled } from './globals'
import {
K_DISABLE_OMNISEARCH,
getTextExtractor,
isCacheEnabled,
} from './globals'
import type OmnisearchPlugin from './main'
interface WeightingSettings {
@@ -49,6 +53,7 @@ export interface OmnisearchSettings extends WeightingSettings {
splitCamelCase: boolean
openInNewPane: boolean
verboseLogging: boolean
fuzziness: '0' | '1' | '2'
}
/**
@@ -259,6 +264,29 @@ export class SettingsTab extends PluginSettingTab {
})
)
// Fuzziness
new Setting(containerEl)
.setName('Fuzziness')
.setDesc(
"Define the level of fuzziness for the search. The higher the fuzziness, the more results you'll get."
)
.addDropdown(dropdown =>
dropdown
.addOptions({
0: 'Exact match',
1: 'Not too fuzzy',
2: 'Fuzzy enough',
})
.setValue(settings.fuzziness)
.onChange(async v => {
if (!['0', '1', '2'].includes(v)) {
v = '2'
}
settings.fuzziness = v as '0' | '1' | '2'
await saveSettings(this.plugin)
})
)
//#endregion Behavior
//#region User Interface
@@ -467,6 +495,7 @@ export const DEFAULT_SETTINGS: OmnisearchSettings = {
highlight: true,
showPreviousQueryResults: true,
simpleSearch: false,
fuzziness: '0',
weightBasename: 3,
weightDirectory: 2,
@@ -491,4 +520,4 @@ export async function saveSettings(plugin: Plugin): Promise<void> {
export function isPluginDisabled(): boolean {
return app.loadLocalStorage(K_DISABLE_OMNISEARCH) == '1'
}
}