Skip to content

Commit

Permalink
Allow kiwix-serve to get suggestions of custom range
Browse files Browse the repository at this point in the history
This will allow handle_suggest API to accept two arguments `start` and
`suggestionLength` that will allow handle_suggest to retrieve
suggestions in the given range rather than the default 0-10 range.
  • Loading branch information
maneeshpm authored and kelson42 committed Jul 25, 2021
1 parent a312d22 commit 1bd389c
Showing 1 changed file with 17 additions and 6 deletions.
23 changes: 17 additions & 6 deletions src/server/internalServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -334,15 +334,15 @@ std::unique_ptr<Response> InternalServer::build_homepage(const RequestContext& r

// TODO: retrieve searcher from caching mechanism
SuggestionsList_t getSuggestions(const zim::Archive* const archive,
const std::string& queryString, int suggestionCount)
const std::string& queryString, int start, int end)
{
SuggestionsList_t suggestions;
if (archive->hasTitleIndex()) {
auto searcher = zim::Searcher(*archive);
zim::Query suggestionQuery;
suggestionQuery.setQuery(queryString, true);
auto suggestionSearch = searcher.search(suggestionQuery);
auto suggestionResult = suggestionSearch.getResults(0, suggestionCount);
auto suggestionResult = suggestionSearch.getResults(start, end);

for (auto it = suggestionResult.begin(); it != suggestionResult.end(); it++) {
SuggestionItem suggestion(it.getTitle(), kiwix::normalize(it.getTitle()),
Expand All @@ -353,6 +353,7 @@ SuggestionsList_t getSuggestions(const zim::Archive* const archive,
// TODO: This case should be handled by libzim
std::vector<std::string> variants = getTitleVariants(queryString);
int currCount = 0;
int suggestionCount = end - start;
for (auto it = variants.begin(); it != variants.end() && currCount < suggestionCount; it++) {
for (auto& entry: archive->findByTitle(*it)) {
SuggestionItem suggestion(entry.getTitle(), kiwix::normalize(entry.getTitle()),
Expand Down Expand Up @@ -426,8 +427,6 @@ std::unique_ptr<Response> InternalServer::handle_suggest(const RequestContext& r

std::string content;
std::string mimeType;
unsigned int maxSuggestionCount = 10;
unsigned int suggestionCount = 0;

std::string bookName;
std::string bookId;
Expand All @@ -442,6 +441,19 @@ std::unique_ptr<Response> InternalServer::handle_suggest(const RequestContext& r
return Response::build_404(*this, request, bookName, "");
}

auto start = 0;
try {
start = request.get_argument<unsigned int>("start");
} catch (const std::exception&) {}

unsigned int suggestionLength = 10;
try {
suggestionLength = request.get_argument<unsigned int>("suggestionLength");
} catch (const std::exception&) {}
if (suggestionLength == 0) suggestionLength = 10;

auto end = start + suggestionLength;

if (archive == nullptr) {
return Response::build_404(*this, request, bookName, "");
}
Expand All @@ -455,7 +467,7 @@ std::unique_ptr<Response> InternalServer::handle_suggest(const RequestContext& r
bool first = true;

/* Get the suggestions */
SuggestionsList_t suggestions = getSuggestions(archive.get(), queryString, maxSuggestionCount);
SuggestionsList_t suggestions = getSuggestions(archive.get(), queryString, start, end);
for(auto& suggestion:suggestions) {
MustacheData result;
result.set("label", suggestion.getTitle());
Expand All @@ -470,7 +482,6 @@ std::unique_ptr<Response> InternalServer::handle_suggest(const RequestContext& r
result.set("first", first);
first = false;
results.push_back(result);
suggestionCount++;
}


Expand Down

0 comments on commit 1bd389c

Please sign in to comment.