diff --git a/layouts/_default/search.html b/layouts/_default/search.html
index 841cd4276baa..176bf4d64a6e 100644
--- a/layouts/_default/search.html
+++ b/layouts/_default/search.html
@@ -72,8 +72,9 @@
{{ .Title }}
bubbles: true,
cancelable: true,
});
- // Trigger the input event for the search input
+ // Dispatch input event and call manual search
searchInput.dispatchEvent(event);
+ await onPageSearch({ target: { value: query } });
searchInput.focus();
});
@@ -84,6 +85,30 @@ {{ .Title }}
async function onPageSearch(e) {
pagefind.init();
const query = e.target.value;
+ const terms = query.trim().split(/\s+/);
+
+ // Search each term individually
+ const allResults = [];
+
+ for (let term of terms) {
+ const result = await pagefind.search(term);
+ if (result) {
+ allResults.push(...result.results);
+ }
+ }
+
+ // Deduplicate results by URL (some results match multiple terms)
+ const resultMap = new Map();
+ for (const r of allResults) {
+ if (!resultMap.has(r.url)) {
+ resultMap.set(r.url, r);
+ }
+ }
+
+ const dedupedResults = [...resultMap.values()];
+ const fullResultsData = await Promise.all(
+ dedupedResults.map((r) => r.data()),
+ );
// Set the query parameter in the URL
const params = new URLSearchParams(document.location.search);
@@ -92,7 +117,7 @@ {{ .Title }}
// Default the current page to 1
let currentPage = 1;
- // Check if the page parameter exists
+ // Check if the page parameter exists
const page = params.get("page");
// Calculate the range start based on the page parameter
if (page) {
@@ -101,72 +126,63 @@ {{ .Title }}
const rangeStart = (currentPage - 1) * 10;
const rangeEnd = rangeStart + 10;
- // Execute the search
- const search = await pagefind.debouncedSearch(query);
- // If no search results are found, exit
- if (search === null) {
+ // total number of results
+ const resultsLength = fullResultsData.length;
+ // Slice the results based on the range
+ const resultsData = fullResultsData.slice(rangeStart, rangeEnd);
+
+ // If the range does not have any results, display a message
+ if (resultsData.length === 0) {
+ searchPageResults.innerHTML = `No results found
`;
return;
+ }
+ // Add an index to the results, for heap tracking
+ const results = resultsData.map((item, index) => ({
+ ...item,
+ index: rangeStart + index + 1,
+ }));
+
+ // If the query is not empty, display the search results container
+ if (query) {
+ searchPageResults.classList.remove("hidden");
} else {
- // total number of results
- const resultsLength = search.results.length;
- // Get the data for the search results
- // Slice the results based on the range start + 10
- const resultsData = await Promise.all(
- search.results.slice(rangeStart, rangeEnd).map((r) => r.data()),
- );
- // If the range does not have any results, display a message
- if (resultsData.length === 0) {
- searchPageResults.innerHTML = `No results found
`;
- return;
- }
- // Add an index to the results, for heap tracking
- const results = resultsData.map((item, index) => ({
- ...item,
- index: index + 1,
- }));
-
- // If the query is not empty, display the search results container
- if (query) {
- searchPageResults.classList.remove("hidden");
- } else {
- searchPageResults.classList.add("hidden");
- }
+ searchPageResults.classList.add("hidden");
+ }
- // Generate the search results HTML
- let resultsHTML = `${resultsLength} results
`;
-
- // Map results to HTML
- resultsHTML += results
- .map((item) => {
- return ``;
- })
- .join("");
- // If the results length is greater than 10, display links to show more results
- if (resultsLength > 10) {
- resultsHTML += `
`;
- resultsHTML += ``;
- for (let i = 1; i <= resultsLength / 10; i++) {
- if (i == currentPage) {
- resultsHTML += `-
-
-
`;
- } else {
- resultsHTML += `-
-
-
`;
- }
+ // Generate the search results HTML
+ let resultsHTML = `${resultsLength} results
`;
+
+ // Map results to HTML
+ resultsHTML += results
+ .map((item) => {
+ return ``;
+ })
+ .join("");
+ // If the results length is greater than 10, display links to show more results
+ if (resultsLength > 10) {
+ resultsHTML += `
`;
+ resultsHTML += ``;
+ for (let i = 1; i <= resultsLength / 10; i++) {
+ if (i == currentPage) {
+ resultsHTML += `-
+
+
`;
+ } else {
+ resultsHTML += `-
+
+
`;
}
- resultsHTML += `
`;
}
-
- searchPageResults.innerHTML = resultsHTML;
+ resultsHTML += `
`;
}
+
+ searchPageResults.innerHTML = resultsHTML;
}
searchPageInput.addEventListener("input", (e) => onPageSearch(e));