Merge pull request #597 from kiwix/fix_get_results

This commit is contained in:
Matthieu Gautier 2021-08-04 15:57:58 +02:00 committed by GitHub
commit b8aee8a42c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 47 additions and 32 deletions

View File

@ -85,12 +85,12 @@ class Searcher
* *
* @param search The search query. * @param search The search query.
* @param resultStart the start offset of the search results (used for pagination). * @param resultStart the start offset of the search results (used for pagination).
* @param resultEnd the end offset of the search results (used for pagination). * @param maxResultCount Maximum results to get from start (used for pagination).
* @param verbose print some info on stdout if true. * @param verbose print some info on stdout if true.
*/ */
void search(const std::string& search, void search(const std::string& search,
unsigned int resultStart, unsigned int resultStart,
unsigned int resultEnd, unsigned int maxResultCount,
const bool verbose = false); const bool verbose = false);
/** /**
@ -104,12 +104,12 @@ class Searcher
* @param longitude The longitude of the center point. * @param longitude The longitude of the center point.
* @param distance The radius of the disc. * @param distance The radius of the disc.
* @param resultStart the start offset of the search results (used for pagination). * @param resultStart the start offset of the search results (used for pagination).
* @param resultEnd the end offset of the search results (used for pagination). * @param maxResultCount Maximum number of results to get from start (used for pagination).
* @param verbose print some info on stdout if true. * @param verbose print some info on stdout if true.
*/ */
void geo_search(float latitude, float longitude, float distance, void geo_search(float latitude, float longitude, float distance,
unsigned int resultStart, unsigned int resultStart,
unsigned int resultEnd, unsigned int maxResultCount,
const bool verbose = false); const bool verbose = false);
/** /**
@ -148,14 +148,14 @@ class Searcher
zim::SearchResultSet getSearchResultSet(); zim::SearchResultSet getSearchResultSet();
unsigned int getResultStart() { return resultStart; } unsigned int getResultStart() { return resultStart; }
unsigned int getResultEnd() { return resultEnd; } unsigned int getMaxResultCount() { return maxResultCount; }
protected: protected:
std::string beautifyInteger(const unsigned int number); std::string beautifyInteger(const unsigned int number);
void closeIndex(); void closeIndex();
void searchInIndex(string& search, void searchInIndex(string& search,
const unsigned int resultStart, const unsigned int resultStart,
const unsigned int resultEnd, const unsigned int maxResultCount,
const bool verbose = false); const bool verbose = false);
std::vector<Reader*> readers; std::vector<Reader*> readers;
@ -163,7 +163,7 @@ class Searcher
std::string searchPattern; std::string searchPattern;
unsigned int estimatedResultCount; unsigned int estimatedResultCount;
unsigned int resultStart; unsigned int resultStart;
unsigned int resultEnd; unsigned int maxResultCount;
private: private:
void reset(); void reset();

View File

@ -67,7 +67,7 @@ Searcher::Searcher()
: searchPattern(""), : searchPattern(""),
estimatedResultCount(0), estimatedResultCount(0),
resultStart(0), resultStart(0),
resultEnd(0) maxResultCount(0)
{ {
loadICUExternalTables(); loadICUExternalTables();
} }
@ -95,7 +95,7 @@ Reader* Searcher::get_reader(int readerIndex)
/* Search strings in the database */ /* Search strings in the database */
void Searcher::search(const std::string& search, void Searcher::search(const std::string& search,
unsigned int resultStart, unsigned int resultStart,
unsigned int resultEnd, unsigned int maxResultCount,
const bool verbose) const bool verbose)
{ {
this->reset(); this->reset();
@ -106,9 +106,9 @@ void Searcher::search(const std::string& search,
this->searchPattern = search; this->searchPattern = search;
this->resultStart = resultStart; this->resultStart = resultStart;
this->resultEnd = resultEnd; this->maxResultCount = maxResultCount;
/* Try to find results */ /* Try to find results */
if (resultStart != resultEnd) { if (maxResultCount != 0) {
/* Perform the search */ /* Perform the search */
string unaccentedSearch = removeAccents(search); string unaccentedSearch = removeAccents(search);
std::vector<zim::Archive> archives; std::vector<zim::Archive> archives;
@ -123,7 +123,7 @@ void Searcher::search(const std::string& search,
query.setQuery(unaccentedSearch, false); query.setQuery(unaccentedSearch, false);
query.setVerbose(verbose); query.setVerbose(verbose);
zim::Search search = searcher.search(query); zim::Search search = searcher.search(query);
internal.reset(new SearcherInternal(search.getResults(resultStart, resultEnd))); internal.reset(new SearcherInternal(search.getResults(resultStart, maxResultCount)));
this->estimatedResultCount = search.getEstimatedMatches(); this->estimatedResultCount = search.getEstimatedMatches();
} }
@ -133,7 +133,7 @@ void Searcher::search(const std::string& search,
void Searcher::geo_search(float latitude, float longitude, float distance, void Searcher::geo_search(float latitude, float longitude, float distance,
unsigned int resultStart, unsigned int resultStart,
unsigned int resultEnd, unsigned int maxResultCount,
const bool verbose) const bool verbose)
{ {
this->reset(); this->reset();
@ -147,10 +147,10 @@ void Searcher::geo_search(float latitude, float longitude, float distance,
oss << "Articles located less than " << distance << " meters of " << latitude << ";" << longitude; oss << "Articles located less than " << distance << " meters of " << latitude << ";" << longitude;
this->searchPattern = oss.str(); this->searchPattern = oss.str();
this->resultStart = resultStart; this->resultStart = resultStart;
this->resultEnd = resultEnd; this->maxResultCount = maxResultCount;
/* Try to find results */ /* Try to find results */
if (resultStart == resultEnd) { if (maxResultCount == 0) {
return; return;
} }
@ -165,7 +165,7 @@ void Searcher::geo_search(float latitude, float longitude, float distance,
query.setQuery("", false); query.setQuery("", false);
query.setGeorange(latitude, longitude, distance); query.setGeorange(latitude, longitude, distance);
zim::Search search = searcher.search(query); zim::Search search = searcher.search(query);
internal.reset(new SearcherInternal(search.getResults(resultStart, resultEnd))); internal.reset(new SearcherInternal(search.getResults(resultStart, maxResultCount)));
this->estimatedResultCount = search.getEstimatedMatches(); this->estimatedResultCount = search.getEstimatedMatches();
} }
@ -206,7 +206,7 @@ void Searcher::suggestions(std::string& searchPattern, const bool verbose)
this->searchPattern = searchPattern; this->searchPattern = searchPattern;
this->resultStart = 0; this->resultStart = 0;
this->resultEnd = 10; this->maxResultCount = 10;
string unaccentedSearch = removeAccents(searchPattern); string unaccentedSearch = removeAccents(searchPattern);
std::vector<zim::Archive> archives; std::vector<zim::Archive> archives;
@ -219,7 +219,7 @@ void Searcher::suggestions(std::string& searchPattern, const bool verbose)
query.setVerbose(verbose); query.setVerbose(verbose);
query.setQuery(unaccentedSearch, true); query.setQuery(unaccentedSearch, true);
zim::Search search = searcher.search(query); zim::Search search = searcher.search(query);
internal.reset(new SearcherInternal(search.getResults(resultStart, resultEnd))); internal.reset(new SearcherInternal(search.getResults(resultStart, maxResultCount)));
this->estimatedResultCount = search.getEstimatedMatches(); this->estimatedResultCount = search.getEstimatedMatches();
} }

View File

@ -589,8 +589,6 @@ std::unique_ptr<Response> InternalServer::handle_search(const RequestContext& re
pageLength = 25; pageLength = 25;
} }
auto end = start + pageLength;
/* Get the results */ /* Get the results */
try { try {
zim::Query query; zim::Query query;
@ -615,7 +613,7 @@ std::unique_ptr<Response> InternalServer::handle_search(const RequestContext& re
} }
zim::Search search = searcher->search(query); zim::Search search = searcher->search(query);
SearchRenderer renderer(search.getResults(start, end), mp_nameMapper, start, SearchRenderer renderer(search.getResults(start, pageLength), mp_nameMapper, start,
search.getEstimatedMatches()); search.getEstimatedMatches());
renderer.setSearchPattern(patternString); renderer.setSearchPattern(patternString);
renderer.setSearchContent(bookName); renderer.setSearchContent(bookName);

View File

@ -22,4 +22,21 @@ TEST(Searcher, search) {
ASSERT_EQ(result->get_title(), "Wikibooks"); ASSERT_EQ(result->get_title(), "Wikibooks");
} }
TEST(Searcher, incrementalRange) {
// Attempt to get 50 results in steps of 5
zim::Archive archive("./test/zimfile.zim");
zim::Searcher ftsearcher(archive);
zim::Query query;
query.setQuery("ray", false);
auto search = ftsearcher.search(query);
int suggCount = 0;
for (int i = 0; i < 10; i++) {
auto srs = search.getResults(i*5, 5); // get 5 results
ASSERT_EQ(srs.size(), 5);
suggCount += srs.size();
}
ASSERT_EQ(suggCount, 50);
}
} }