Document Indexing & Retrieval System
Add documents to a local index and search them instantly.
Indexed Documents
No documents have been indexed yet.
Search Results
Enter a query to see search results.
Please enter a search query.
'; return; } const queryTokens = tokenize(query); if (queryTokens.length === 0) { searchResultsDiv.innerHTML = 'Your search query contained only common words. Please try more specific terms.
'; return; } const results = documentIndex.filter(doc => { // Find documents where at least one query token is present in the document's tokens return queryTokens.some(queryToken => doc.tokens.has(queryToken)); }); displaySearchResults(results, query); } /** * Displays the search results in the UI, with highlighting. * @param {object[]} results - The array of document objects that matched. * @param {string} query - The original search query for highlighting. */ function displaySearchResults(results, query) { if (results.length === 0) { searchResultsDiv.innerHTML = 'No documents found matching your query.
'; return; } searchResultsDiv.innerHTML = ''; results.forEach(doc => { const resultElement = document.createElement('div'); resultElement.className = 'p-4 border border-gray-200 rounded-lg'; // Create a regex to highlight all occurrences of the query words, case-insensitively const highlightRegex = new RegExp(`(${query.split(/\s+/).join('|')})`, 'gi'); const highlightedContent = doc.content.replace(highlightRegex, '$1'); const highlightedTitle = doc.title.replace(highlightRegex, '$1'); resultElement.innerHTML = `${highlightedTitle}
${highlightedContent}
`; searchResultsDiv.appendChild(resultElement); }); } // --- Event Listeners --- if (addDocBtn) addDocBtn.addEventListener('click', handleAddDocument); if (searchBtn) searchBtn.addEventListener('click', handleSearch); if (searchQueryInput) { searchQueryInput.addEventListener('keyup', (event) => { if (event.key === 'Enter') { handleSearch(); } }); } });