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

View File

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