Add comprehensive Bible search feature

- Implement backend search engine with indexing and relevance scoring
- Add search API endpoints (/api/search and /api/search/suggestions)
- Create SearchComponent with modal and page views
- Add search button to header navigation
- Support real-time search with debouncing
- Include context verses and search term highlighting
- Add book filtering and mobile-responsive design
- Integrate with existing routing and navigation system
This commit is contained in:
Ryderjj89
2025-09-15 17:30:00 -04:00
parent ebca91591c
commit f5639af27f
5 changed files with 642 additions and 2 deletions

View File

@@ -5,6 +5,7 @@ const path = require('path');
const fs = require('fs').promises;
const { configureAuth, requireAuth, optionalAuth } = require('./auth');
const { preferencesOps, favoritesOps } = require('./database');
const BibleSearchEngine = require('./search');
const app = express();
const PORT = process.env.PORT || 3000;
@@ -34,6 +35,9 @@ app.use(express.static(path.join(__dirname, '../../frontend/build')));
// Bible data directory
const BIBLE_DATA_DIR = path.join(__dirname, '../../bible-data');
// Initialize search engine
const searchEngine = new BibleSearchEngine(BIBLE_DATA_DIR);
// Helper function to read markdown files
async function readMarkdownFile(filePath) {
try {
@@ -131,6 +135,53 @@ app.get('/books/:book/:chapter', async (req, res) => {
}
});
// Search routes
app.get('/api/search', async (req, res) => {
try {
const { q: query, book, limit, context } = req.query;
if (!query || query.trim().length < 2) {
return res.status(400).json({ error: 'Query must be at least 2 characters long' });
}
const options = {
bookFilter: book || null,
limit: parseInt(limit) || 50,
includeContext: context !== 'false',
contextSize: 2
};
const results = await searchEngine.search(query, options);
res.json({
query,
results,
total: results.length,
hasMore: results.length === options.limit
});
} catch (error) {
console.error('Search error:', error);
res.status(500).json({ error: 'Search failed. Please try again.' });
}
});
app.get('/api/search/suggestions', async (req, res) => {
try {
const { q: query, limit } = req.query;
if (!query || query.trim().length < 2) {
return res.json({ suggestions: [] });
}
const suggestions = await searchEngine.getSearchSuggestions(query, parseInt(limit) || 10);
res.json({ suggestions });
} catch (error) {
console.error('Search suggestions error:', error);
res.status(500).json({ error: 'Failed to get search suggestions' });
}
});
// User preferences routes
app.get('/api/preferences', requireAuth, (req, res) => {
preferencesOps.getPreferences(req.user.id, (err, preferences) => {