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 { writable } from 'svelte/store'
import { Notice } from 'obsidian' import { Notice } from 'obsidian'
let previousResults: ResultNote[] = []
const tokenize = (text: string): string[] => { const tokenize = (text: string): string[] => {
const tokens = text.split(SPACE_OR_PUNCTUATION) const tokens = text.split(SPACE_OR_PUNCTUATION)
const chsSegmenter = (app as any).plugins.plugins['cm-chs-patch'] const chsSegmenter = (app as any).plugins.plugins['cm-chs-patch']
@@ -95,15 +97,13 @@ export class SearchEngine {
*/ */
public async search( public async search(
query: Query, query: Query,
options = { fuzzy: 0.1, prefix: false } options = { prefix: true }
): Promise<SearchResult[]> { ): Promise<SearchResult[]> {
if (!query.segmentsToStr()) return [] if (!query.segmentsToStr()) return []
let results = this.minisearch.search(query.segmentsToStr(), { let results = this.minisearch.search(query.segmentsToStr(), {
prefix: term => { prefix: term => options.prefix || term.length > 3,
return options.prefix || term.length > 4 fuzzy: 0.2,
},
fuzzy: options.fuzzy,
combineWith: 'AND', combineWith: 'AND',
boost: { boost: {
basename: settings.weightBasename, basename: settings.weightBasename,
@@ -190,13 +190,13 @@ export class SearchEngine {
options?: Partial<{ singleFilePath: string | null }> options?: Partial<{ singleFilePath: string | null }>
): Promise<ResultNote[]> { ): Promise<ResultNote[]> {
// Get the raw results // Get the raw results
let results = await this.search(query) let results: SearchResult[]
if (results.length == 0) { if (settings.simpleSearch) {
if (settings.retryWhenZeroResult) { results = await this.search(query)
results = await this.search(query, { fuzzy: 0.2, prefix: true }) } else {
results = await this.search(query, { prefix: true })
} }
} if (!results.length) return previousResults
if (!results.length) return []
// Extract tags from the query // Extract tags from the query
const tags = query.segments const tags = query.segments
@@ -223,7 +223,7 @@ export class SearchEngine {
} }
// Map the raw results to get usable suggestions // Map the raw results to get usable suggestions
return results.map(result => { const resultNotes = results.map(result => {
let note = cacheManager.getLiveDocument(result.id) let note = cacheManager.getLiveDocument(result.id)
if (!note) { if (!note) {
// throw new Error(`Omnisearch - Note "${result.id}" not indexed`) // throw new Error(`Omnisearch - Note "${result.id}" not indexed`)
@@ -267,6 +267,8 @@ export class SearchEngine {
} }
return resultNote return resultNote
}) })
previousResults = resultNotes
return resultNotes
} }
// #region Read/write minisearch index // #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. */ /** Key for the welcome message when Obsidian is updated. A message is only shown once. */
welcomeMessage: string welcomeMessage: string
/** If a query returns 0 result, try again with more relax conditions */ /** 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() const diacriticsDesc = new DocumentFragment()
diacriticsDesc.createSpan({}, span => { 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/> 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) new Setting(containerEl)
@@ -133,13 +134,14 @@ export class SettingsTab extends PluginSettingTab {
// Ignore diacritics // Ignore diacritics
new Setting(containerEl) new Setting(containerEl)
.setName('Retry queries that return zero result') .setName('Simpler search')
.setDesc( .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 => .addToggle(toggle =>
toggle.setValue(settings.retryWhenZeroResult).onChange(async v => { toggle.setValue(settings.simpleSearch).onChange(async v => {
settings.retryWhenZeroResult = v settings.simpleSearch = v
await saveSettings(this.plugin) await saveSettings(this.plugin)
}) })
) )
@@ -345,7 +347,7 @@ export const DEFAULT_SETTINGS: OmnisearchSettings = {
showExcerpt: true, showExcerpt: true,
showCreateButton: false, showCreateButton: false,
showPreviousQueryResults: true, showPreviousQueryResults: true,
retryWhenZeroResult: true, simpleSearch: false,
weightBasename: 2, weightBasename: 2,
weightH1: 1.5, weightH1: 1.5,