Fixed embeds injection in results

This commit is contained in:
Simon Cambier
2024-10-08 17:13:04 +02:00
parent 24ee6675c4
commit a4352c365c
2 changed files with 23 additions and 11 deletions

View File

@@ -1,4 +1,4 @@
import { TFile } from 'obsidian' import { normalizePath, TFile } from 'obsidian'
import type { IndexedDocument } from './globals' import type { IndexedDocument } from './globals'
import { import {
extractHeadingsFromCache, extractHeadingsFromCache,
@@ -49,6 +49,7 @@ export class CacheManager {
console.warn(`Omnisearch: Error while adding "${path}" to live cache`, e) console.warn(`Omnisearch: Error while adding "${path}" to live cache`, e)
// Shouldn't be needed, but... // Shouldn't be needed, but...
this.removeFromLiveCache(path) this.removeFromLiveCache(path)
// TODO: increment errors counter
} }
} }
@@ -101,6 +102,7 @@ export class CacheManager {
private async getAndMapIndexedDocument( private async getAndMapIndexedDocument(
path: string path: string
): Promise<IndexedDocument> { ): Promise<IndexedDocument> {
path = normalizePath(path)
const app = this.plugin.app const app = this.plugin.app
const file = app.vault.getAbstractFileByPath(path) const file = app.vault.getAbstractFileByPath(path)
if (!file) throw new Error(`Invalid file path: "${path}"`) if (!file) throw new Error(`Invalid file path: "${path}"`)

View File

@@ -174,7 +174,7 @@ export class SearchEngine {
tokenize: text => [text], tokenize: text => [text],
}) })
logDebug('Found', results.length, 'results') logDebug(`Found ${results.length} results`, results)
// Filter query results to only keep files that match query.query.ext (if any) // Filter query results to only keep files that match query.query.ext (if any)
if (query.query.ext?.length) { if (query.query.ext?.length) {
@@ -295,6 +295,8 @@ export class SearchEngine {
// Sort results and keep the 50 best // Sort results and keep the 50 best
results = results.sort((a, b) => b.score - a.score).slice(0, 50) results = results.sort((a, b) => b.score - a.score).slice(0, 50)
logDebug('Filtered results:', results)
if (results.length) logDebug('First result:', results[0]) if (results.length) logDebug('First result:', results[0])
const documents = await Promise.all( const documents = await Promise.all(
@@ -380,24 +382,29 @@ export class SearchEngine {
) )
// Inject embeds for images, documents, and PDFs // Inject embeds for images, documents, and PDFs
for (let i = 0; i < documents.length; i++) { let total = documents.length
for (let i = 0; i < total; i++) {
const doc = documents[i] const doc = documents[i]
if (!doc) continue
const embeds = this.plugin.embedsRepository const embeds = this.plugin.embedsRepository
.getEmbeds(doc.path) .getEmbeds(doc.path)
// Limit to 5 embeds
.slice(0, this.plugin.settings.maxEmbeds) .slice(0, this.plugin.settings.maxEmbeds)
// Inject embeds in the results
for (const embed of embeds) { for (const embed of embeds) {
// Inject the embed in the content after index i total++
documents[++i] = await this.plugin.cacheManager.getDocument(embed) const newDoc = await this.plugin.cacheManager.getDocument(embed)
results[i] = { documents.splice(i + 1, 0, newDoc)
id: documents[i].path, results.splice(i + 1, 0, {
id: newDoc.path,
score: 0, score: 0,
terms: [], terms: [],
queryTerms: [], queryTerms: [],
match: {}, match: {},
isEmbed: true, isEmbed: true,
} })
// console.log(documents[i]) i++ // Increment i to skip the newly inserted document
} }
} }
@@ -436,7 +443,7 @@ export class SearchEngine {
foundWords, foundWords,
query query
) )
logDebug(`Matches for ${note.basename}`, matches) logDebug(`Matches for note "${note.path}"`, matches)
const resultNote: ResultNote = { const resultNote: ResultNote = {
score: result.score, score: result.score,
foundWords, foundWords,
@@ -446,6 +453,9 @@ export class SearchEngine {
} }
return resultNote return resultNote
}) })
logDebug('Suggestions:', resultNotes)
return resultNotes return resultNotes
} }