Algolia Search Integration
This documentation site uses Algolia DocSearch to provide powerful, fast search functionality with AI-powered conversational search via Ask AI.
Features
- Fast, relevant search - Algolia's search engine returns results in milliseconds
- Ask AI - Conversational search powered by AI for natural language queries
- Contextual filtering - Results automatically filtered by section and category
- Keyboard shortcuts - Press
Ctrl+K(orCmd+Kon Mac) to open search - API-based indexing - Content is pushed via API, bypassing the need for crawlers
- Dark mode support - Search UI matches the site's theme
How It Works
Indexing Process
- Build Time: When
npm run buildcompletes, thepostbuildscript runs automatically - Content Extraction: The script reads all HTML files from the build directory
- Data Preparation: Extracts titles, content, headings, and metadata
- API Push: Sends records to Algolia via the Admin API
- Index Update: Algolia replaces all records with the new content
This approach works even though the production site is protected by Cloudflare Access, since we're pushing content directly via API rather than relying on a crawler.
Configuration
Environment Variables
The following environment variables are required:
# Algolia Search Configuration
ALGOLIA_APP_ID=OMNGM7GBAZ
ALGOLIA_SEARCH_API_KEY=f14b4b7b1389d1c28f199ed742b2b120 # Public, safe to commit
ALGOLIA_ADMIN_API_KEY=5d5aeac178410c1c738f1a1e2957aa18 # Secret, never commit
ALGOLIA_INDEX_NAME=PacingDocs1Index
ALGOLIA_ASSISTANT_ID=uS0228m9DgOG
Local Development: Add these to .env in the workspace root (gitignored)
Production: Add these to Cloudflare Pages environment variables in the dashboard
Docusaurus Config
The Algolia configuration in docusaurus.config.ts:
algolia: {
appId: 'OMNGM7GBAZ',
apiKey: 'f14b4b7b1389d1c28f199ed742b2b120', // Public search-only key
indexName: 'PacingDocs1Index',
contextualSearch: true,
askAi: {
assistantId: 'uS0228m9DgOG',
indexName: 'PacingDocs1Index',
apiKey: 'f14b4b7b1389d1c28f199ed742b2b120',
appId: 'OMNGM7GBAZ',
suggestedQuestions: true,
},
searchPagePath: 'search',
},
Using Search
Basic Search
- Open search: Click the search bar in the navbar or press
Ctrl+K(Windows/Linux) orCmd+K(Mac) - Type your query: Start typing what you're looking for
- Navigate results: Use arrow keys to navigate, Enter to open, Esc to close
- View full results: Click "See all X results" to view the dedicated search page
Ask AI
- Open search: Use the search bar or keyboard shortcut
- Switch to Ask AI: Click the "Ask AI" tab in the search modal
- Ask a question: Type a natural language question (e.g., "How do I set up tracking for a new client?")
- Get AI answer: The AI will provide a contextual answer based on the documentation
- View sources: Click on suggested pages to read the full documentation
Search Tips
- Be specific: "GTM server-side tracking" is better than "tracking"
- Use keywords: Search for tool names, feature names, or key concepts
- Try variations: If you don't find what you need, try alternative terms
- Check filters: Results are automatically filtered by section, but you can adjust this
Manual Indexing
To manually trigger indexing without rebuilding:
cd docusaurus
npm run index-algolia
This is useful when:
- Testing indexing changes
- The automatic postbuild hook failed
- You want to verify content is indexed correctly
Troubleshooting
Search Returns No Results
Possible causes:
- Content hasn't been indexed yet (wait for first build/deploy)
- Index was cleared and needs repopulating
- Search query is too specific or contains typos
Solution:
- Check the Algolia dashboard to verify records exist
- Trigger a manual indexing:
npm run index-algolia - Verify environment variables are set correctly
Ask AI Not Working
Possible causes:
- Assistant ID is incorrect or missing
- Algolia Ask AI feature not enabled in dashboard
- Network/API issues
Solution:
- Verify
ALGOLIA_ASSISTANT_IDis set correctly - Check Algolia dashboard that Ask AI is enabled
- Check browser console for errors
Indexing Fails in Production
Possible causes:
ALGOLIA_ADMIN_API_KEYnot set in Cloudflare Pages- Network issues during build
- Build directory not found
Solution:
- Check Cloudflare Pages environment variables
- Review build logs for specific error messages
- Ensure
postbuildscript is running
Stale Search Results
Possible causes:
- Recent changes haven't been deployed yet
- Cache needs clearing
- Old records still in index
Solution:
- Wait for deployment to complete
- Clear browser cache
- Trigger manual reindex if needed
Index Configuration
The indexing script configures the Algolia index with:
{
searchableAttributes: [
'title', // Page title (highest priority)
'hierarchy.lvl0', // Section (e.g., "Guides", "Tools")
'hierarchy.lvl1', // Category (e.g., "Marketing", "Website")
'hierarchy.lvl2', // Subcategory
'content', // Page content (lowest priority)
],
attributesForFaceting: [
'type', // docs vs blog
'hierarchy.lvl0', // Section filtering
],
customRanking: [
'desc(type)', // Docs ranked higher than blog
],
}
This ensures:
- Titles match first, then headings, then content
- Results are relevant to the current section
- Documentation pages rank higher than blog posts
Security Notes
- Search API Key is safe to commit - it's read-only and public
- Admin API Key is secret - never commit it, only in environment variables
- Keys are stored securely in Cloudflare Pages (encrypted)
.envfile is gitignored by default
Performance
- Index size: ~50-100 records per build (depends on content)
- Search speed: < 50ms average response time
- Indexing time: ~5-10 seconds during build
- API calls: 1 index replacement per build (~$0.00 cost)
Related Documentation
Implementation Details
Files involved:
docusaurus.config.ts- Algolia configurationscripts/index-algolia.cjs- Indexing scriptpackage.json- Postbuild hooksrc/css/custom.css- Search styling.env- Environment variables (local)
Last updated: 2026-01-07