Complete 'All Versions' search functionality - search across all Bible translations and default to 'all' on version selector page
This commit is contained in:
@@ -225,18 +225,6 @@ app.get('/api/search', async (req, res) => {
|
|||||||
return res.status(400).json({ error: 'Query must be at least 2 characters long' });
|
return res.status(400).json({ error: 'Query must be at least 2 characters long' });
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get the appropriate search engine for the version
|
|
||||||
let searchEngine;
|
|
||||||
if (version === 'esv' && esvSearchEngine) {
|
|
||||||
searchEngine = esvSearchEngine;
|
|
||||||
} else if (version === 'nlt' && nltSearchEngine) {
|
|
||||||
searchEngine = nltSearchEngine;
|
|
||||||
} else if (version === 'csb' && csbSearchEngine) {
|
|
||||||
searchEngine = csbSearchEngine;
|
|
||||||
} else {
|
|
||||||
searchEngine = nkjvSearchEngine; // default fallback
|
|
||||||
}
|
|
||||||
|
|
||||||
const options = {
|
const options = {
|
||||||
bookFilter: book || null,
|
bookFilter: book || null,
|
||||||
limit: parseInt(limit) || 50,
|
limit: parseInt(limit) || 50,
|
||||||
@@ -244,14 +232,58 @@ app.get('/api/search', async (req, res) => {
|
|||||||
contextSize: 2
|
contextSize: 2
|
||||||
};
|
};
|
||||||
|
|
||||||
const results = await searchEngine.search(query, options);
|
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;
|
||||||
|
} else if (version === 'nlt' && nltSearchEngine) {
|
||||||
|
searchEngine = nltSearchEngine;
|
||||||
|
} else if (version === 'csb' && csbSearchEngine) {
|
||||||
|
searchEngine = csbSearchEngine;
|
||||||
|
} else {
|
||||||
|
searchEngine = nkjvSearchEngine; // default fallback
|
||||||
|
}
|
||||||
|
|
||||||
|
results = await searchEngine.search(query, options);
|
||||||
|
}
|
||||||
|
|
||||||
res.json({
|
res.json({
|
||||||
query,
|
query,
|
||||||
results,
|
results,
|
||||||
total: results.length,
|
total: results.length,
|
||||||
hasMore: results.length === options.limit,
|
hasMore: results.length === options.limit,
|
||||||
version
|
version: searchVersion
|
||||||
});
|
});
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('Search error:', error);
|
console.error('Search error:', error);
|
||||||
|
|||||||
@@ -22,8 +22,9 @@ const SearchComponent: React.FC<SearchComponentProps> = ({
|
|||||||
onClose,
|
onClose,
|
||||||
isModal = false
|
isModal = false
|
||||||
}) => {
|
}) => {
|
||||||
// Default to ESV if no version is selected, otherwise use the current version
|
// Default to "all" if no version is selected (on version selector page),
|
||||||
const defaultVersion = initialVersion || 'esv';
|
// otherwise use the current version
|
||||||
|
const defaultVersion = initialVersion || 'all';
|
||||||
const [query, setQuery] = useState('');
|
const [query, setQuery] = useState('');
|
||||||
const [results, setResults] = useState<SearchResult[]>([]);
|
const [results, setResults] = useState<SearchResult[]>([]);
|
||||||
const [loading, setLoading] = useState(false);
|
const [loading, setLoading] = useState(false);
|
||||||
@@ -85,9 +86,12 @@ const SearchComponent: React.FC<SearchComponentProps> = ({
|
|||||||
// Handle result click
|
// Handle result click
|
||||||
const handleResultClick = (result: SearchResult) => {
|
const handleResultClick = (result: SearchResult) => {
|
||||||
const urlBookName = getBookUrlName(formatBookName(result.book));
|
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
|
// 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
|
// 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);
|
navigate(url);
|
||||||
if (onClose) onClose();
|
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
|
bg-white dark:bg-gray-700 text-gray-900 dark:text-gray-100
|
||||||
focus:ring-2 focus:ring-blue-500 focus:border-transparent"
|
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="esv">ESV - English Standard Version</option>
|
||||||
<option value="nkjv">NKJV - New King James Version</option>
|
<option value="nkjv">NKJV - New King James Version</option>
|
||||||
<option value="nlt">NLT - New Living Translation</option>
|
<option value="nlt">NLT - New Living Translation</option>
|
||||||
|
|||||||
Reference in New Issue
Block a user