Skip to content

Commit

Permalink
Initiate full-text search in parallel with title search #940 (#941)
Browse files Browse the repository at this point in the history
  • Loading branch information
Jaifroid authored Dec 19, 2022
1 parent 366c0c1 commit 9e4fd4c
Showing 1 changed file with 13 additions and 12 deletions.
25 changes: 13 additions & 12 deletions www/js/lib/zimArchive.js
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,14 @@ define(['zimfile', 'zimDirEntry', 'util', 'uiUtil', 'utf8'],
// results much more quickly if we do this (and the user can click on a result before the rarer patterns complete)
// NB duplicates are removed before processing search array
var startArray = [];
var dirEntries = [];
search.scanCount = 0;
// Launch a full-text search if possible
if (LZ) that.findDirEntriesFromFullTextSearch(search, dirEntries).then(function (fullTextDirEntries) {
dirEntries = fullTextDirEntries;
search.status = 'complete';
callback(dirEntries, search);
});
// Ensure a search is done on the string exactly as typed
startArray.push(search.prefix);
// Normalize any spacing and make string all lowercase
Expand All @@ -272,21 +280,13 @@ define(['zimfile', 'zimDirEntry', 'util', 'uiUtil', 'utf8'],
)
)
);
var dirEntries = [];
search.scanCount = 0;

function searchNextVariant() {
// If user has initiated a new search, cancel this one
if (search.status === 'cancelled') return callback([], search);
if (prefixVariants.length === 0 || dirEntries.length >= search.size) {
// We have found all the title-search entries we are going to get, so launch full-text search if we are still missing entries
if (LZ) {
return that.findDirEntriesFromFullTextSearch(search, dirEntries).then(function (fullTextDirEntries) {
search.status = 'complete';
callback(fullTextDirEntries, search);
});
}
search.status = 'complete';
// We have found all the title-search entries we are going to get, so indicate search type if we're still searching
if (LZ && search.status !== 'complete') search.type = 'fulltext';
else search.status = 'complete';
return callback(dirEntries, search);
}
// Dynamically populate list of articles
Expand Down Expand Up @@ -401,7 +401,8 @@ define(['zimfile', 'zimDirEntry', 'util', 'uiUtil', 'utf8'],
var cns = this.getContentNamespace();
var that = this;
// We give ourselves an overhead in caclulating the results needed, because full-text search will return some results already found
var resultsNeeded = Math.floor(params.maxSearchResultsSize - dirEntries.length / 2);
// var resultsNeeded = Math.floor(params.maxSearchResultsSize - dirEntries.length / 2);
var resultsNeeded = params.maxSearchResultsSize;
return this.callLibzimWorker({action: "search", text: search.prefix, numResults: resultsNeeded}).then(function (results) {
if (results) {
var dirEntryPaths = [];
Expand Down

0 comments on commit 9e4fd4c

Please sign in to comment.