API endpoint for searching file content?

(I’m resurrecting this thread from the dead, but I wanted to add the answer for any other Google searchers like me.)

There’s a great plugin which will let you perform advanced full-text searches via JS/API:

If you don’t want to use the above plugin for whatever reason, here’s sample code which will perform an vanilla Obsidian search and then return a list of files which match the query.

const query = 'some query'

// Perform the search
app.internalPlugins.plugins['global-search'].instance.openGlobalSearch(query)
const searchLeaf = app.workspace.getLeavesOfType('search')[0]
const search = await searchLeaf.open(searchLeaf.view)
const rawSearchResult = await new Promise(resolve => setTimeout(() => {
    resolve(search.dom.resultDomLookup)
}, 300)) // the delay here was specified in 'obsidian-text-expand' plugin; I assume they had a reason

const files = Array.from(rawSearchResult.keys())

console.log(files.map(x => x.path))
5 Likes