mirror of https://github.com/kiwix/libkiwix.git
Use selectBooks in handle_search
This commit is contained in:
parent
76d5fafb72
commit
39d0a56be8
|
@ -181,20 +181,12 @@ Library::BookIdSet InternalServer::selectBooks(const RequestContext& request) co
|
|||
return Library::BookIdSet(id_vec.begin(), id_vec.end());
|
||||
}
|
||||
|
||||
SearchInfo::SearchInfo(const std::string& pattern)
|
||||
: pattern(pattern),
|
||||
geoQuery()
|
||||
{}
|
||||
|
||||
SearchInfo::SearchInfo(const std::string& pattern, GeoQuery geoQuery)
|
||||
: pattern(pattern),
|
||||
geoQuery(geoQuery)
|
||||
{}
|
||||
|
||||
SearchInfo::SearchInfo(const RequestContext& request)
|
||||
: pattern(request.get_optional_param<std::string>("pattern", "")),
|
||||
geoQuery()
|
||||
SearchInfo InternalServer::getSearchInfo(const RequestContext& request) const
|
||||
{
|
||||
auto bookIds = selectBooks(request);
|
||||
auto pattern = request.get_optional_param<std::string>("pattern", "");
|
||||
GeoQuery geoQuery;
|
||||
|
||||
/* Retrive geo search */
|
||||
try {
|
||||
auto latitude = request.get_argument<float>("latitude");
|
||||
|
@ -208,12 +200,15 @@ SearchInfo::SearchInfo(const RequestContext& request)
|
|||
throw std::invalid_argument("No query provided.");
|
||||
}
|
||||
|
||||
try {
|
||||
auto content_vector = request.get_arguments("content");
|
||||
bookNames = std::set<std::string>(content_vector.begin(), content_vector.end());
|
||||
} catch (const std::out_of_range&) {}
|
||||
return SearchInfo(pattern, geoQuery, bookIds);
|
||||
}
|
||||
|
||||
SearchInfo::SearchInfo(const std::string& pattern, GeoQuery geoQuery, const Library::BookIdSet& bookIds)
|
||||
: pattern(pattern),
|
||||
geoQuery(geoQuery),
|
||||
bookIds(bookIds)
|
||||
{}
|
||||
|
||||
zim::Query SearchInfo::getZimQuery(bool verbose) const {
|
||||
zim::Query query;
|
||||
if (verbose) {
|
||||
|
@ -666,7 +661,8 @@ std::unique_ptr<Response> InternalServer::handle_search(const RequestContext& re
|
|||
}
|
||||
|
||||
try {
|
||||
auto searchInfo = SearchInfo(request);
|
||||
auto searchInfo = getSearchInfo(request);
|
||||
auto bookIds = searchInfo.getBookIds();
|
||||
|
||||
/* Make the search */
|
||||
// Try to get a search from the searchInfo, else build it
|
||||
|
@ -674,21 +670,6 @@ std::unique_ptr<Response> InternalServer::handle_search(const RequestContext& re
|
|||
try {
|
||||
search = searchCache.getOrPut(searchInfo,
|
||||
[=](){
|
||||
Library::BookIdSet bookIds;
|
||||
if(!searchInfo.bookNames.empty()) {
|
||||
for(const auto& bookName: searchInfo.bookNames) {
|
||||
try {
|
||||
auto bookId = mp_nameMapper->getIdForName(bookName);
|
||||
bookIds.insert(bookId);
|
||||
} catch(const std::out_of_range&) {
|
||||
throw std::invalid_argument("The requested book doesn't exist.");
|
||||
}
|
||||
}
|
||||
} else {
|
||||
for (auto& bookId: mp_library->filter(kiwix::Filter().local(true).valid(true))) {
|
||||
bookIds.insert(bookId);
|
||||
}
|
||||
}
|
||||
auto searcher = mp_library->getSearcherByIds(bookIds);
|
||||
return make_shared<zim::Search>(searcher->search(searchInfo.getZimQuery(m_verbose.load())));
|
||||
}
|
||||
|
@ -702,9 +683,9 @@ std::unique_ptr<Response> InternalServer::handle_search(const RequestContext& re
|
|||
"404-page-heading",
|
||||
cssUrl);
|
||||
response += nonParameterizedMessage("no-search-results");
|
||||
if(searchInfo.bookNames.size() == 1) {
|
||||
auto bookName =*searchInfo.bookNames.begin();
|
||||
auto bookId = mp_nameMapper->getIdForName(bookName);
|
||||
if(bookIds.size() == 1) {
|
||||
auto bookId = *bookIds.begin();
|
||||
auto bookName = mp_nameMapper->getNameForId(bookId);
|
||||
response += TaskbarInfo(bookName, mp_library->getArchiveById(bookId).get());
|
||||
}
|
||||
return response;
|
||||
|
@ -736,9 +717,9 @@ std::unique_ptr<Response> InternalServer::handle_search(const RequestContext& re
|
|||
renderer.setSearchProtocolPrefix(m_root + "/search");
|
||||
renderer.setPageLength(pageLength);
|
||||
auto response = ContentResponse::build(*this, renderer.getHtml(), "text/html; charset=utf-8");
|
||||
if(searchInfo.bookNames.size() == 1) {
|
||||
auto bookName = *searchInfo.bookNames.begin();
|
||||
auto bookId = mp_nameMapper->getIdForName(bookName);
|
||||
if(bookIds.size() == 1) {
|
||||
auto bookId = *bookIds.begin();
|
||||
auto bookName = mp_nameMapper->getNameForId(bookId);
|
||||
response->set_taskbar(bookName, mp_library->getArchiveById(bookId).get());
|
||||
}
|
||||
return std::move(response);
|
||||
|
|
|
@ -68,22 +68,21 @@ struct GeoQuery {
|
|||
|
||||
class SearchInfo {
|
||||
public:
|
||||
SearchInfo(const std::string& pattern);
|
||||
SearchInfo(const std::string& pattern, GeoQuery geoQuery);
|
||||
SearchInfo(const RequestContext& request);
|
||||
SearchInfo(const std::string& pattern, GeoQuery geoQuery, const Library::BookIdSet& bookIds);
|
||||
|
||||
zim::Query getZimQuery(bool verbose) const;
|
||||
const Library::BookIdSet& getBookIds() const { return bookIds; }
|
||||
|
||||
friend bool operator<(const SearchInfo& l, const SearchInfo& r)
|
||||
{
|
||||
return std::tie(l.bookNames, l.pattern, l.geoQuery)
|
||||
< std::tie(r.bookNames, r.pattern, r.geoQuery); // keep the same order
|
||||
return std::tie(l.bookIds, l.pattern, l.geoQuery)
|
||||
< std::tie(r.bookIds, r.pattern, r.geoQuery); // keep the same order
|
||||
}
|
||||
|
||||
public: //data
|
||||
std::string pattern;
|
||||
GeoQuery geoQuery;
|
||||
std::set<std::string> bookNames;
|
||||
Library::BookIdSet bookIds;
|
||||
};
|
||||
|
||||
|
||||
|
@ -150,6 +149,7 @@ class InternalServer {
|
|||
bool etag_not_needed(const RequestContext& r) const;
|
||||
ETag get_matching_if_none_match_etag(const RequestContext& request) const;
|
||||
Library::BookIdSet selectBooks(const RequestContext& r) const;
|
||||
SearchInfo getSearchInfo(const RequestContext& r) const;
|
||||
|
||||
private: // data
|
||||
std::string m_addr;
|
||||
|
|
Loading…
Reference in New Issue