From 97e69713a6fdf09d88ee65ad23c566f85724b8fc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Luc=C3=ADa=20M=2E=20Polis?= Date: Thu, 31 Oct 2024 14:22:54 -0700 Subject: [PATCH] [DOCS-2059] Improve Vectara Search 1.1 Code (#331) --- js/search-specs.js | 29 ++++++++++++++--------------- js/vectara.js | 4 +++- 2 files changed, 17 insertions(+), 16 deletions(-) diff --git a/js/search-specs.js b/js/search-specs.js index d882123a4..f59e06e93 100644 --- a/js/search-specs.js +++ b/js/search-specs.js @@ -17,7 +17,7 @@ let searchWidget = createSearch( ); document.getElementById("search-widget").appendChild(searchWidget); -function successFn(results) { +function successFn(results, query) { hideOverlay(); // Check if results are valid or not empty @@ -26,6 +26,8 @@ function successFn(results) { return; } + query = query || document.querySelector('#search-input').value.trim(); + // Process the results and display them on the page. console.log(results); renderResults(results, "search-widget-results"); @@ -58,7 +60,7 @@ function successFn(results) { }); // Remove not-useful qq CLI boilerplate - document.querySelectorAll('.vuiSearchResult a[href*="qq-cli-command-guide"]').forEach(link => { + document.querySelectorAll('.vuiSearchResult a[href*="qq-cli-command-guide"]').forEach((link) => { // For each of these links, find any parent 'vuiSearchResult' divs const parentDiv = link.closest(".vuiSearchResult"); if (parentDiv) { @@ -79,24 +81,24 @@ function successFn(results) { }); // Encourage users to enter more search terms. - const apologyText1 = "The returned results did not contain sufficient information to be summarized into a useful answer for your query. Please try a different search or restate your query differently."; - const apologyText2 = "The returned search results did not contain sufficient information to be summarized into a useful answer for your query. Please try a different search or restate your query differently."; + const apologyText = /The returned (search )?results did not contain sufficient information to be summarized into a useful answer for your query. Please try a different search or restate your query differently./; const apologyTextElements = document.querySelectorAll(".vuiText--m"); apologyTextElements.forEach(function(element) { - if (element.textContent.includes(apologyText1) || element.textContent.includes(apologyText2)) { - element.textContent = `Here are some search results. To help me write a better summary, enter more search terms or ask me a question!`; + if (apologyText.exec(element.textContent)) { + element.textContent = `Here are some search results about ${query}. To help me write a better summary, enter more search terms or ask me a question!`; } }); // Update history state const state = { results: results, - query: document.getElementById("search-input").value + query: query }; - if (history.state && history.state.results) { - history.replaceState(state, "", "?query=" + encodeURIComponent(state.query)); - } else { + + if (history.state) { history.pushState(state, "", "?query=" + encodeURIComponent(state.query)); + } else { + history.replaceState(state, "", "?query=" + encodeURIComponent(state.query)); } } @@ -107,9 +109,6 @@ function errorFn(err) { } window.addEventListener("popstate", function(event) { - let url = new URL(window.location.href); - let query = url.searchParams.get("query"); - if (event.state && event.state.results) { // Directly render the cached results for query pages renderResults(event.state.results, "search-widget-results"); @@ -126,8 +125,8 @@ window.addEventListener("load", function(e) { // Handle the case where the page is loaded with search results in the history state if (history.state && history.state.results) { // If there are results in the history state, render them - successFn(history.state.results); - } + successFn(history.state.results, history.state.query); + } }); // Expand left sidebar menu on page load diff --git a/js/vectara.js b/js/vectara.js index 39c3a31e2..137cd4bca 100644 --- a/js/vectara.js +++ b/js/vectara.js @@ -167,7 +167,9 @@ console.log("submitFn called with query:", query); console.log(`fetch elapsedTime: ${ ( endTime - startTime ) / 1000 }`); return data; }) - .then(successFn) + .then((results) => { + return successFn(results, query); + }) .catch(function (error) { errorFn(error); pulseLogo.classList.remove("vectara__search_loading");