Fix search (#271)

Fix search
This commit is contained in:
Matthieu Gautier 2019-09-09 14:59:35 +02:00 committed by GitHub
commit 56f8b7a876
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 57 additions and 54 deletions

View File

@ -96,27 +96,29 @@ std::string SearchRenderer::getHtml()
auto resultEnd = mp_searcher->getResultEnd(); auto resultEnd = mp_searcher->getResultEnd();
auto resultCountPerPage = resultEnd - resultStart; auto resultCountPerPage = resultEnd - resultStart;
auto estimatedResultCount = mp_searcher->getEstimatedResultCount(); auto estimatedResultCount = mp_searcher->getEstimatedResultCount();
auto currentPage = 0U;
unsigned int pageStart auto pageStart = 0U;
= resultStart / resultCountPerPage >= 5 auto pageEnd = 0U;
? resultStart / resultCountPerPage - 4 auto lastPageStart = 0U;
: 0; if (resultCountPerPage) {
unsigned int pageCount currentPage = resultStart/resultCountPerPage;
= estimatedResultCount / resultCountPerPage + 1 - pageStart; pageStart = currentPage > 4 ? currentPage-4 : 0;
pageEnd = currentPage + 5;
if (pageCount > 10) { if (pageEnd > estimatedResultCount / resultCountPerPage) {
pageCount = 10; pageEnd = estimatedResultCount / resultCountPerPage;
} else if (pageCount == 1) { }
pageCount = 0; if (estimatedResultCount > resultCountPerPage) {
lastPageStart = round(estimatedResultCount/resultCountPerPage) * resultCountPerPage;
}
} }
for (unsigned int i = pageStart; i < pageStart + pageCount; i++) { for (unsigned int i = pageStart; i < pageEnd; i++) {
kainjow::mustache::data page; kainjow::mustache::data page;
page.set("label", to_string(i + 1)); page.set("label", to_string(i + 1));
page.set("start", to_string(i * resultCountPerPage)); page.set("start", to_string(i * resultCountPerPage));
page.set("end", to_string((i + 1) * resultCountPerPage)); page.set("end", to_string((i + 1) * resultCountPerPage));
if (i * resultCountPerPage == resultStart) { if (i == currentPage) {
page.set("selected", true); page.set("selected", true);
} }
pages.push_back(page); pages.push_back(page);
@ -128,16 +130,15 @@ std::string SearchRenderer::getHtml()
kainjow::mustache::data allData; kainjow::mustache::data allData;
allData.set("results", results); allData.set("results", results);
allData.set("pages", pages); allData.set("pages", pages);
allData.set("hasResult", estimatedResultCount != 0); allData.set("hasResults", estimatedResultCount != 0);
allData.set("hasPages", pageStart != pageEnd);
allData.set("count", kiwix::beautifyInteger(estimatedResultCount)); allData.set("count", kiwix::beautifyInteger(estimatedResultCount));
allData.set("searchPattern", kiwix::encodeDiples(this->searchPattern)); allData.set("searchPattern", kiwix::encodeDiples(this->searchPattern));
allData.set("searchPatternEncoded", urlEncode(this->searchPattern)); allData.set("searchPatternEncoded", urlEncode(this->searchPattern));
allData.set("resultStart", to_string(resultStart + 1)); allData.set("resultStart", to_string(resultStart + 1));
allData.set("resultEnd", to_string(min(resultEnd, estimatedResultCount))); allData.set("resultEnd", to_string(min(resultEnd, estimatedResultCount)));
allData.set("resultRange", to_string(resultCountPerPage)); allData.set("resultRange", to_string(resultCountPerPage));
allData.set("resultLastPageStart", to_string(estimatedResultCount > resultCountPerPage allData.set("resultLastPageStart", to_string(lastPageStart));
? round(estimatedResultCount / resultCountPerPage) * resultCountPerPage
: 0));
allData.set("lastResult", to_string(estimatedResultCount)); allData.set("lastResult", to_string(estimatedResultCount));
allData.set("protocolPrefix", this->protocolPrefix); allData.set("protocolPrefix", this->protocolPrefix);
allData.set("searchProtocolPrefix", this->searchProtocolPrefix); allData.set("searchProtocolPrefix", this->searchProtocolPrefix);

View File

@ -108,12 +108,12 @@ void Searcher::search(std::string& search,
cout << "Performing query `" << search << "'" << endl; cout << "Performing query `" << search << "'" << endl;
} }
/* Try to find results */
if (resultStart != resultEnd) {
/* Perform the search */
this->searchPattern = search; this->searchPattern = search;
this->resultStart = resultStart; this->resultStart = resultStart;
this->resultEnd = resultEnd; this->resultEnd = resultEnd;
/* Try to find results */
if (resultStart != resultEnd) {
/* Perform the search */
string unaccentedSearch = removeAccents(search); string unaccentedSearch = removeAccents(search);
std::vector<const zim::File*> zims; std::vector<const zim::File*> zims;
for (auto current = this->readers.begin(); current != this->readers.end(); for (auto current = this->readers.begin(); current != this->readers.end();
@ -146,11 +146,6 @@ void Searcher::geo_search(float latitude, float longitude, float distance,
cout << "Performing geo query `" << distance << "&(" << latitude << ";" << longitude << ")'" << endl; cout << "Performing geo query `" << distance << "&(" << latitude << ";" << longitude << ")'" << endl;
} }
/* Try to find results */
if (resultStart == resultEnd) {
return;
}
/* Perform the search */ /* Perform the search */
std::ostringstream oss; std::ostringstream oss;
oss << "Articles located less than " << distance << " meters of " << latitude << ";" << longitude; oss << "Articles located less than " << distance << " meters of " << latitude << ";" << longitude;
@ -158,6 +153,11 @@ void Searcher::geo_search(float latitude, float longitude, float distance,
this->resultStart = resultStart; this->resultStart = resultStart;
this->resultEnd = resultEnd; this->resultEnd = resultEnd;
/* Try to find results */
if (resultStart == resultEnd) {
return;
}
std::vector<const zim::File*> zims; std::vector<const zim::File*> zims;
for (auto current = this->readers.begin(); current != this->readers.end(); for (auto current = this->readers.begin(); current != this->readers.end();
current++) { current++) {

View File

@ -95,7 +95,7 @@
</head> </head>
<body bgcolor="white"> <body bgcolor="white">
<div class="header"> <div class="header">
{{#hasResult}} {{#hasResults}}
Results Results
<b> <b>
{{resultStart}}-{{resultEnd}} {{resultStart}}-{{resultEnd}}
@ -104,10 +104,10 @@
</b> for <b> </b> for <b>
{{searchPattern}} {{searchPattern}}
</b> </b>
{{/hasResult}} {{/hasResults}}
{{^hasResult}} {{^hasResults}}
No results were found for <b>{{searchPattern}}</b> No results were found for <b>{{searchPattern}}</b>
{{/hasResult}} {{/hasResults}}
</div> </div>
<div class="results"> <div class="results">
@ -129,6 +129,7 @@
</div> </div>
<div class="footer"> <div class="footer">
{{#hasPages}}
<ul> <ul>
{{#resultLastPageStart}} {{#resultLastPageStart}}
<li> <li>
@ -153,6 +154,7 @@
</li> </li>
{{/resultLastPageStart}} {{/resultLastPageStart}}
</ul> </ul>
{{/hasPages}}
</div> </div>
</body> </body>
</html> </html>