search_notes
Search all notes in your Obsidian vault using full-text queries to find relevant information quickly. Filter by folder, adjust case sensitivity, and control result count.
Instructions
Full-text search across all notes in the vault
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| query | Yes | Search query string | |
| caseSensitive | No | Whether the search should be case sensitive | |
| maxResults | No | Maximum number of results to return | |
| folder | No | Limit search to a specific folder |
Implementation Reference
- src/lib/vault.ts:159-222 (handler)The searchNotes function implementation which iterates over notes and finds matching lines based on a query string.
export async function searchNotes( vaultPath: string, query: string, options?: { caseSensitive?: boolean; maxResults?: number; folder?: string; }, ): Promise<SearchResult[]> { const caseSensitive = options?.caseSensitive ?? false; const maxResults = options?.maxResults ?? 100; const notes = await listNotes(vaultPath, options?.folder); const results: SearchResult[] = []; const searchQuery = caseSensitive ? query : query.toLowerCase(); for (const notePath of notes) { if (results.length >= maxResults) break; let content: string; try { content = await readNote(vaultPath, notePath); } catch { console.error(`Failed to read note during search: ${notePath}`); continue; } const lines = content.split("\n"); const matches: SearchMatch[] = []; for (let i = 0; i < lines.length; i++) { const line = lines[i]; const compareLine = caseSensitive ? line : line.toLowerCase(); let startIndex = 0; while (true) { const col = compareLine.indexOf(searchQuery, startIndex); if (col === -1) break; matches.push({ line: i + 1, content: line.trim(), column: col, }); startIndex = col + searchQuery.length; } } if (matches.length > 0) { results.push({ path: resolveVaultPath(vaultPath, notePath), relativePath: notePath, matches, score: matches.length, }); } } // Sort by score descending results.sort((a, b) => b.score - a.score); return results.slice(0, maxResults); }