Tweaking search results - also solves #121

This commit is contained in:
Simon Cambier
2022-11-07 21:54:14 +01:00
parent 466560d4e9
commit 7fd7dc38cf
2 changed files with 23 additions and 19 deletions

View File

@@ -17,6 +17,8 @@ import { cacheManager } from '../cache-manager'
import { writable } from 'svelte/store'
import { Notice } from 'obsidian'
let previousResults: ResultNote[] = []
const tokenize = (text: string): string[] => {
const tokens = text.split(SPACE_OR_PUNCTUATION)
const chsSegmenter = (app as any).plugins.plugins['cm-chs-patch']
@@ -95,15 +97,13 @@ export class SearchEngine {
*/
public async search(
query: Query,
options = { fuzzy: 0.1, prefix: false }
options = { prefix: true }
): Promise<SearchResult[]> {
if (!query.segmentsToStr()) return []
let results = this.minisearch.search(query.segmentsToStr(), {
prefix: term => {
return options.prefix || term.length > 4
},
fuzzy: options.fuzzy,
prefix: term => options.prefix || term.length > 3,
fuzzy: 0.2,
combineWith: 'AND',
boost: {
basename: settings.weightBasename,
@@ -190,13 +190,13 @@ export class SearchEngine {
options?: Partial<{ singleFilePath: string | null }>
): Promise<ResultNote[]> {
// Get the raw results
let results = await this.search(query)
if (results.length == 0) {
if (settings.retryWhenZeroResult) {
results = await this.search(query, { fuzzy: 0.2, prefix: true })
let results: SearchResult[]
if (settings.simpleSearch) {
results = await this.search(query)
} else {
results = await this.search(query, { prefix: true })
}
}
if (!results.length) return []
if (!results.length) return previousResults
// Extract tags from the query
const tags = query.segments
@@ -223,7 +223,7 @@ export class SearchEngine {
}
// Map the raw results to get usable suggestions
return results.map(result => {
const resultNotes = results.map(result => {
let note = cacheManager.getLiveDocument(result.id)
if (!note) {
// throw new Error(`Omnisearch - Note "${result.id}" not indexed`)
@@ -267,6 +267,8 @@ export class SearchEngine {
}
return resultNote
})
previousResults = resultNotes
return resultNotes
}
// #region Read/write minisearch index

View File

@@ -42,7 +42,7 @@ export interface OmnisearchSettings extends WeightingSettings {
/** Key for the welcome message when Obsidian is updated. A message is only shown once. */
welcomeMessage: string
/** If a query returns 0 result, try again with more relax conditions */
retryWhenZeroResult: boolean
simpleSearch: boolean
}
/**
@@ -98,7 +98,8 @@ export class SettingsTab extends PluginSettingTab {
const diacriticsDesc = new DocumentFragment()
diacriticsDesc.createSpan({}, span => {
span.innerHTML = `Normalize diacritics in search terms. Words like "brûlée" or "žluťoučký" will be indexed as "brulee" and "zlutoucky".<br/>
<strong style="color: var(--text-accent)">You probably shouldn't disable this. Needs a restart to fully take effect.</strong>
<strong style="color: var(--text-accent)"><em>You probably shouldn't disable this</em>.<br>
Needs a restart to fully take effect.</strong>
`
})
new Setting(containerEl)
@@ -133,13 +134,14 @@ export class SettingsTab extends PluginSettingTab {
// Ignore diacritics
new Setting(containerEl)
.setName('Retry queries that return zero result')
.setName('Simpler search')
.setDesc(
`When a query returns zero result, Omnisearch will try again (but harder). Disabling this can improve search reactivity.`
`When enabled, Omnisearch is a bit more restrictive when using your query terms as prefixes.
May return less results, but will be quicker. You should enable this if Omnisearch makes Obsidian freeze while searching.`
)
.addToggle(toggle =>
toggle.setValue(settings.retryWhenZeroResult).onChange(async v => {
settings.retryWhenZeroResult = v
toggle.setValue(settings.simpleSearch).onChange(async v => {
settings.simpleSearch = v
await saveSettings(this.plugin)
})
)
@@ -345,7 +347,7 @@ export const DEFAULT_SETTINGS: OmnisearchSettings = {
showExcerpt: true,
showCreateButton: false,
showPreviousQueryResults: true,
retryWhenZeroResult: true,
simpleSearch: false,
weightBasename: 2,
weightH1: 1.5,