Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add autocomplete suggestions feature in search page #1030

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

Yash-1511
Copy link

File Path Autocompletion in Search

Closes #1024

Description

Added file path autocompletion to enhance the search experience in Khoj. Users can now easily search for specific files by typing "file:" followed by the file name, with real-time suggestions appearing as they type.

Changes

  • Added new API endpoint /api/file-suggestions to get file path suggestions
  • Enhanced search UI with dropdown suggestions for file paths
  • Implemented debounced search to optimize API calls
  • Added keyboard (Enter) and mouse click support for selecting suggestions

Features

  • Type "file:" to trigger file path suggestions
  • Real-time filtering of suggestions as you type
  • Top 10 alphabetically sorted suggestions
  • Case-insensitive matching
  • Keyboard and mouse interaction support
  • Clear visual feedback with a dropdown UI

Screenshots

[Add screenshots showing the file path autocompletion in action]

Testing

  1. Type "file:" in the search box
    • Verify that suggestions dropdown appears
  2. Type a partial file name
    • Verify that suggestions are filtered in real-time
  3. Select a suggestion using mouse click
    • Verify that the search is performed with the selected file
  4. Select a suggestion using Enter key
    • Verify that the search is performed with the selected file
  5. Type an invalid file prefix
    • Verify that no suggestions are shown
  6. Type without "file:" prefix
    • Verify that no file suggestions appear

Dependencies

No new dependencies were added.

Notes

  • The feature is designed to be non-intrusive, only showing suggestions when explicitly typing "file:"
  • Suggestions are limited to 10 items to maintain performance and usability
  • The implementation follows Khoj's existing UI patterns and styling

Copy link
Member

@debanjum debanjum left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @Yash-1511 , thanks for the useful PR. I've left some comments, feel free to ask any clarifications if required. Looking forward to get this PR in soon!

Comment on lines +250 to +252
suggestionsTimeoutRef.current = setTimeout(() => {
getFileSuggestions(value);
}, 100);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we get all files indexed by user once on search page load (instead of every keystroke)? This would avoid unnecessary API and DB calls to server and hopefully make file filter suggestions faster too

return;
}

setFocusSearchResult(null);
const filePrefix = query.substring(5).trim(); // Remove "file:" prefix
const apiUrl = `/api/file-suggestions?q=${encodeURIComponent(filePrefix)}`;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we just call the GET /api/content/computer API endpoint to get all files indexed for the user once? This will reduce API calls and remove the need for the new file suggestions API endpoint. But the sorting and filtering logic that you added to the /api/file-suggestions API endpoint would need to move to the client

if (!searchQuery.trim()) {
function getFileSuggestions(query: string) {
// Get suggestions only if query starts with "file:"
if (!query.toLowerCase().startsWith("file:")) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This limits the file: filters to be only added at the start of the query. This conflicts with the slash commands which are expected to be at start of query currently. So for example, you wouldn't be able to do queries like /notes file:"Notes/Tasks.org".

It'd be good if the file filter can be placed anywhere in query or maybe at the end of query is fine too for now?

Suggested change
if (!query.toLowerCase().startsWith("file:")) {
if (!query.toLowerCase().endsWith("file:")) {

return;
}

setFocusSearchResult(null);
const filePrefix = query.substring(5).trim(); // Remove "file:" prefix
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This logic will need to be updated if file: is a suffix instead of a prefix/infix as suggested in another comment

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Show autocomplete suggestions for File Query Filters on Web App
2 participants