Algolia Search Implementation
This document outlines the Algolia search implementation for the HelpDesk application.
App id T5WDVP4AC3
API d6720b7b6885eea0800246afc5d3a2dd
Overview
We’ve integrated Algolia search to provide fast and powerful search capabilities for tickets in the HelpDesk application. The implementation includes:
- Ticket indexing in Algolia
- Real-time synchronization between Firestore and Algolia
- Search API endpoint
- Search UI component integrated with the ticket list
- Direct client-side search for static exports
Static Export Support
The search functionality has been configured to work in both regular and static export deployments using a Firebase Cloud Function:
-
Firebase Cloud Function: All search requests now go through a dedicated Firebase Cloud Function endpoint at
/search-tickets. This provides:- Consistent behavior across both regular and static exports
- Enhanced security (Algolia API keys remain on the server)
- User permission enforcement on the server-side
-
Configuration: The Firebase Function URL is configured via the
NEXT_PUBLIC_FIREBASE_FUNCTIONS_URLenvironment variable.
Setup
1. Algolia Account Setup
- Create an Algolia account at https://www.algolia.com/
- Create a new application in your Algolia dashboard
- Get your Application ID, Search API Key, and Admin API Key
2. Environment Variables
Add the following environment variables to your .env.local file:
NEXT_PUBLIC_ALGOLIA_APP_ID=your_algolia_app_idNEXT_PUBLIC_ALGOLIA_SEARCH_API_KEY=your_algolia_search_api_keyALGOLIA_ADMIN_API_KEY=your_algolia_admin_api_key3. Initialize Algolia Indexes
Run the initialization script to set up the Algolia indexes with the correct settings:
node scripts/init-algolia.js4. Sync Existing Tickets
If you have existing tickets in Firestore, sync them to Algolia:
node scripts/sync-tickets.jsImplementation Details
1. Algolia Configuration
The Algolia configuration is defined in src/lib/algolia/algoliaConfig.ts. It sets up:
- Algolia clients for admin and search operations
- Index names and search configurations
- Index settings like searchable attributes, faceting, and ranking
2. Ticket Indexing
Tickets are prepared for indexing in src/lib/algolia/ticketIndexService.ts, which:
- Formats ticket data to optimize for search
- Provides functions for adding, updating, and removing tickets from the index
3. Synchronized Operations
The src/lib/firebase/ticketServiceWithAlgolia.ts file extends the regular ticket service to keep Algolia in sync:
- When tickets are created, they’re added to Algolia
- When tickets are updated, the Algolia index is updated
- When tickets are deleted, they’re removed from Algolia
4. Search API Endpoint
The search API endpoint at src/pages/api/api/search-tickets.ts provides:
- Secure access to search (requires authentication)
- Support for filtering by status, priority, assigned user, etc.
- Text search across ticket title, description, and replies
5. Search UI
The search UI is implemented in two components:
src/components/search/TicketSearch.tsx- The search input and filterssrc/components/tickets/TicketList.tsx- Displays search results
Search Features
The implementation supports:
- Full-text search across ticket title, description, and replies
- Filtering by status, priority, assigned user, and more
- Faceting for quick filtering options
- Highlighting of search matches in results
- Sorting by relevance or recency
Maintenance
To maintain the search functionality:
- Ensure all ticket operations go through the
ticketServiceWithAlgolia.tsservice - Keep Algolia environment variables secure
- Run the sync script if you need to refresh the index
If you make changes to the index structure, you’ll need to:
- Update the index settings in
algoliaConfig.ts - Run the initialization script
- Re-sync the tickets
Deployment Instructions
1. Deploy the Firebase Function
To deploy the search functionality to Firebase:
cd functionsnpm installnpm run deploy2. Update Environment Variables
After deploying, update your .env.local file with the correct Firebase Functions URL:
NEXT_PUBLIC_FIREBASE_FUNCTIONS_URL=https://us-central1-your-project-id.cloudfunctions.net/api3. Algolia Configuration in Functions
Ensure your Firebase Functions environment has the correct Algolia configuration:
firebase functions:config:set algolia.app_id="YOUR_APP_ID" algolia.api_key="YOUR_ADMIN_API_KEY"Security Considerations
The Firebase Function implementation ensures:
- Protected API Keys: Your Algolia admin API key remains secure on the server.
- User Authentication: All search requests are authenticated using Firebase Auth.
- Role-Based Access Control: Search results are filtered based on the user’s role.
- Data Privacy: Users only see tickets they have permission to access.