Enabled smart mode of suggestions

This commit is contained in:
Veloman Yunkan 2025-05-30 12:33:05 +04:00
parent 222e4396c7
commit f8fc60ed2f
4 changed files with 32 additions and 15 deletions

View File

@ -775,6 +775,7 @@ std::unique_ptr<Response> InternalServer::handle_suggest(const RequestContext& r
}
const auto queryString = request.get_optional_param("term", std::string());
const auto mode = request.get_optional_param("mode", std::string());
const auto start = request.get_optional_param<unsigned int>("start", 0);
unsigned int count = request.get_optional_param<unsigned int>("count", 10);
if (count == 0) {
@ -793,13 +794,17 @@ std::unique_ptr<Response> InternalServer::handle_suggest(const RequestContext& r
);
const auto lock(searcher->getLock());
auto search = searcher->suggest(queryString);
auto srs = search.getResults(start, count);
for(auto& suggestion: srs) {
results.add(suggestion);
if ( start == 0 && mode == "smart") {
for(const auto& suggestion: search.getSmartSuggestions(count)) {
results.add(suggestion);
}
} else {
for(const auto& suggestion: search.getResults(start, count)) {
results.add(suggestion);
}
}
/* Propose the fulltext search if possible */
if (archive->hasFulltextIndex()) {
results.addFTSearchSuggestion(request.get_user_language(), queryString);

View File

@ -384,9 +384,13 @@ void kiwix::Suggestions::add(const zim::SuggestionItem& suggestion)
: suggestion.getTitle();
result.set("label", escapeForJSON(label, DONT_ESCAPE_QUOTE));
result.set("value", escapeForJSON(suggestion.getTitle(), DONT_ESCAPE_QUOTE));
result.set("kind", "path");
result.set("path", escapeForJSON(suggestion.getPath(), DONT_ESCAPE_QUOTE));
if ( suggestion.getPath().empty() ) {
result.set("kind", "modifiedquery");
} else {
result.set("kind", "path");
result.set("value", escapeForJSON(suggestion.getTitle(), DONT_ESCAPE_QUOTE));
result.set("path", escapeForJSON(suggestion.getPath(), DONT_ESCAPE_QUOTE));
}
result.set("first", m_data.is_empty_list());
m_data.push_back(result);
}

View File

@ -102,7 +102,7 @@ function suggestionsApiURL()
{
const uriEncodedBookName = encodeURIComponent(currentBook);
const userLang = viewerState.uiLanguage;
return `${root}/suggest?userlang=${userLang}&content=${uriEncodedBookName}`;
return `${root}/suggest?userlang=${userLang}&mode=smart&content=${uriEncodedBookName}`;
}
function setTitle(element, text) {
@ -186,9 +186,13 @@ function closeSuggestions() {
}
}
function setSearchQuery(text) {
document.getElementById("kiwixsearchbox").value = text;
}
function updateSearchBoxForLocationChange() {
closeSuggestions();
document.getElementById("kiwixsearchbox").value = getSearchPattern();
setSearchQuery(getSearchPattern());
}
function updateSearchBoxForBookChange() {
@ -464,7 +468,8 @@ function setupSuggestions() {
resultItem: {
element: (item, data) => {
const uriEncodedBookName = encodeURIComponent(currentBook);
let url;
const linkText = htmlDecode(data.value.label);
let url, modifiedQuery;
if (data.value.kind == "path") {
// The double quote and backslash symbols are included in the list
// of special symbols to URI-encode so that the resulting URL can
@ -473,15 +478,18 @@ function setupSuggestions() {
const path = htmlDecode(data.value.path);
const quasiUriEncodedPath = quasiUriEncode(path, '#?"\\');
url = `/content/${uriEncodedBookName}/${quasiUriEncodedPath}`;
} else {
} else if (data.value.kind == "pattern") {
const pattern = encodeURIComponent(htmlDecode(data.value.value));
url = `/search?content=${uriEncodedBookName}&pattern=${pattern}`;
} else { // data.value.kind == "modifiedquery"
modifiedQuery = htmlDecode(linkText);
}
// url can't contain any double quote and/or backslash symbols
// since they should have been URI-encoded. Therefore putting it
// inside double quotes should result in valid javascript.
const jsAction = `gotoUrl("${url}")`;
const linkText = htmlDecode(data.value.label);
const jsAction = url
? `gotoUrl("${url}")`
: `setSearchQuery("${modifiedQuery}")`;
item.innerHTML = makeJSLink(jsAction, linkText, 'class="suggest"');
},
highlight: "autoComplete_highlight",

View File

@ -77,7 +77,7 @@ const ResourceCollection resources200Compressible{
{ DYNAMIC_CONTENT, "/ROOT%23%3F/skin/taskbar.css" },
{ STATIC_CONTENT, "/ROOT%23%3F/skin/taskbar.css?cacheid=80d56607" },
{ DYNAMIC_CONTENT, "/ROOT%23%3F/skin/viewer.js" },
{ STATIC_CONTENT, "/ROOT%23%3F/skin/viewer.js?cacheid=7f05bf6c" },
{ STATIC_CONTENT, "/ROOT%23%3F/skin/viewer.js?cacheid=f8c5f4bf" },
{ DYNAMIC_CONTENT, "/ROOT%23%3F/skin/fonts/Poppins.ttf" },
{ STATIC_CONTENT, "/ROOT%23%3F/skin/fonts/Poppins.ttf?cacheid=af705837" },
{ DYNAMIC_CONTENT, "/ROOT%23%3F/skin/fonts/Roboto.ttf" },
@ -333,7 +333,7 @@ R"EXPECTEDRESULT( <link type="text/css" href="./skin/kiwix.css?cacheid=3948b8
<script type="text/javascript" src="./skin/polyfills.js?cacheid=a0e0343d"></script>
<script type="module" src="./skin/i18n.js?cacheid=e9a10ac1" defer></script>
<script type="text/javascript" src="./skin/languages.js?cacheid=a83f0e13" defer></script>
<script type="text/javascript" src="./skin/viewer.js?cacheid=7f05bf6c" defer></script>
<script type="text/javascript" src="./skin/viewer.js?cacheid=f8c5f4bf" defer></script>
<script type="text/javascript" src="./skin/autoComplete/autoComplete.min.js?cacheid=1191aaaf"></script>
const blankPageUrl = root + "/skin/blank.html?cacheid=6b1fa032";
<label for="kiwix_button_show_toggle"><img src="./skin/caret.png?cacheid=22b942b4" alt=""></label>