Complete 'All Versions' search functionality - search across all Bible translations and default to 'all' on version selector page
This commit is contained in:
@@ -225,7 +225,45 @@ app.get('/api/search', async (req, res) => {
|
||||
return res.status(400).json({ error: 'Query must be at least 2 characters long' });
|
||||
}
|
||||
|
||||
// Get the appropriate search engine for the version
|
||||
const options = {
|
||||
bookFilter: book || null,
|
||||
limit: parseInt(limit) || 50,
|
||||
includeContext: context !== 'false',
|
||||
contextSize: 2
|
||||
};
|
||||
|
||||
let results = [];
|
||||
let searchVersion = version;
|
||||
|
||||
if (version === 'all') {
|
||||
// Search across all available versions
|
||||
const allResults = [];
|
||||
const searchEngines = [
|
||||
{ engine: esvSearchEngine, version: 'esv' },
|
||||
{ engine: nkjvSearchEngine, version: 'nkjv' },
|
||||
{ engine: nltSearchEngine, version: 'nlt' },
|
||||
{ engine: csbSearchEngine, version: 'csb' }
|
||||
].filter(item => item.engine); // Only include engines that are available
|
||||
|
||||
for (const { engine, version: engineVersion } of searchEngines) {
|
||||
try {
|
||||
const versionResults = await engine.search(query, { ...options, limit: Math.ceil(options.limit / searchEngines.length) });
|
||||
// Add version info to each result
|
||||
const resultsWithVersion = versionResults.map(result => ({ ...result, searchVersion: engineVersion }));
|
||||
allResults.push(...resultsWithVersion);
|
||||
} catch (error) {
|
||||
console.log(`Search failed for ${engineVersion}:`, error.message);
|
||||
}
|
||||
}
|
||||
|
||||
// Sort by relevance and limit total results
|
||||
results = allResults
|
||||
.sort((a, b) => b.relevance - a.relevance)
|
||||
.slice(0, options.limit);
|
||||
|
||||
searchVersion = 'all';
|
||||
} else {
|
||||
// Search in specific version
|
||||
let searchEngine;
|
||||
if (version === 'esv' && esvSearchEngine) {
|
||||
searchEngine = esvSearchEngine;
|
||||
@@ -237,21 +275,15 @@ app.get('/api/search', async (req, res) => {
|
||||
searchEngine = nkjvSearchEngine; // default fallback
|
||||
}
|
||||
|
||||
const options = {
|
||||
bookFilter: book || null,
|
||||
limit: parseInt(limit) || 50,
|
||||
includeContext: context !== 'false',
|
||||
contextSize: 2
|
||||
};
|
||||
|
||||
const results = await searchEngine.search(query, options);
|
||||
results = await searchEngine.search(query, options);
|
||||
}
|
||||
|
||||
res.json({
|
||||
query,
|
||||
results,
|
||||
total: results.length,
|
||||
hasMore: results.length === options.limit,
|
||||
version
|
||||
version: searchVersion
|
||||
});
|
||||
} catch (error) {
|
||||
console.error('Search error:', error);
|
||||
|
||||
@@ -22,8 +22,9 @@ const SearchComponent: React.FC<SearchComponentProps> = ({
|
||||
onClose,
|
||||
isModal = false
|
||||
}) => {
|
||||
// Default to ESV if no version is selected, otherwise use the current version
|
||||
const defaultVersion = initialVersion || 'esv';
|
||||
// Default to "all" if no version is selected (on version selector page),
|
||||
// otherwise use the current version
|
||||
const defaultVersion = initialVersion || 'all';
|
||||
const [query, setQuery] = useState('');
|
||||
const [results, setResults] = useState<SearchResult[]>([]);
|
||||
const [loading, setLoading] = useState(false);
|
||||
@@ -85,9 +86,12 @@ const SearchComponent: React.FC<SearchComponentProps> = ({
|
||||
// Handle result click
|
||||
const handleResultClick = (result: SearchResult) => {
|
||||
const urlBookName = getBookUrlName(formatBookName(result.book));
|
||||
// For "all versions" search, use the specific version from the result
|
||||
// For single version search, use the selected version
|
||||
const resultVersion = (result as any).searchVersion || selectedVersion;
|
||||
// Navigate to chapter with verse hash to scroll directly to the verse
|
||||
// Include the version in the URL to ensure we navigate to the correct translation
|
||||
const url = `/version/${selectedVersion}/book/${urlBookName}/chapter/${result.chapter}#verse-${result.verse}`;
|
||||
const url = `/version/${resultVersion}/book/${urlBookName}/chapter/${result.chapter}#verse-${result.verse}`;
|
||||
navigate(url);
|
||||
if (onClose) onClose();
|
||||
};
|
||||
@@ -169,6 +173,7 @@ const SearchComponent: React.FC<SearchComponentProps> = ({
|
||||
bg-white dark:bg-gray-700 text-gray-900 dark:text-gray-100
|
||||
focus:ring-2 focus:ring-blue-500 focus:border-transparent"
|
||||
>
|
||||
<option value="all">All Versions</option>
|
||||
<option value="esv">ESV - English Standard Version</option>
|
||||
<option value="nkjv">NKJV - New King James Version</option>
|
||||
<option value="nlt">NLT - New Living Translation</option>
|
||||
|
||||
Reference in New Issue
Block a user