From c2272b5656f1883c17bd9f6d6162e639a7ddcff5 Mon Sep 17 00:00:00 2001 From: Nikhil Tanwar <2002nikhiltanwar@gmail.com> Date: Mon, 15 Aug 2022 22:12:47 +0530 Subject: [PATCH] Multivalue support for book query Adds support for putting multiple `book` query parameter in catalog/search --- include/library.h | 8 ++++---- src/library.cpp | 30 ++++++++++++++++-------------- src/server/internalServer.cpp | 2 +- 3 files changed, 21 insertions(+), 19 deletions(-) diff --git a/include/library.h b/include/library.h index 4a1ccb96e..55a46bf1e 100644 --- a/include/library.h +++ b/include/library.h @@ -54,6 +54,7 @@ enum supportedListMode { class Filter { public: // types using Tags = std::vector; + using FileNames = std::vector; private: // data uint64_t activeFilters; @@ -67,7 +68,7 @@ class Filter { std::string _query; bool _queryIsPartial; std::string _name; - std::string _fileName; + FileNames _fileNames; public: // functions Filter(); @@ -113,7 +114,7 @@ class Filter { Filter& maxSize(size_t size); Filter& query(std::string query, bool partial=true); Filter& name(std::string name); - Filter& fileName(std::string fileName); + Filter& fileNames(const FileNames& fileNames); bool hasQuery() const; const std::string& getQuery() const { return _query; } @@ -137,8 +138,7 @@ class Filter { const Tags& getAcceptTags() const { return _acceptTags; } const Tags& getRejectTags() const { return _rejectTags; } - bool hasFileName() const; - const std::string& getFileName() const { return _fileName; } + const FileNames& getFileNames() const { return _fileNames; } private: // functions friend class Library; diff --git a/src/library.cpp b/src/library.cpp index 38e6ec40e..f63547f5d 100644 --- a/src/library.cpp +++ b/src/library.cpp @@ -529,7 +529,7 @@ Xapian::Query buildXapianQueryFromFilterQuery(const Filter& filter) Xapian::Query parseQuery(const std::string& query, const std::string& prefix) { Xapian::QueryParser queryParser; - queryParser.set_default_op(Xapian::Query::OP_OR); + queryParser.set_default_op(Xapian::Query::OP_AND); queryParser.set_stemming_strategy(Xapian::QueryParser::STEM_NONE); const auto flags = 0; const auto q = queryParser.parse_query(normalizeText(query), flags, prefix); @@ -546,9 +546,16 @@ Xapian::Query categoryQuery(const std::string& category) return Xapian::Query("XC" + normalizeText(category)); } -Xapian::Query fileNameQuery(const std::string& fileName) +Xapian::Query fileNamesQuery(const Filter::FileNames& fileNames) { - return parseQuery(fileName, "XF"); + Xapian::Query q = Xapian::Query(std::string()); + std::vector queryVec; + for (const auto& fileName : fileNames) { + queryVec.push_back(parseQuery(fileName, "XF")); + } + Xapian::Query combinedQuery(Xapian::Query::OP_OR, queryVec.begin(), queryVec.end()); + q = Xapian::Query(Xapian::Query::OP_FILTER, q, combinedQuery); + return q; } Xapian::Query langQuery(const std::string& lang) @@ -603,8 +610,8 @@ Xapian::Query buildXapianQuery(const Filter& filter) const auto tq = tagsQuery(filter.getAcceptTags(), filter.getRejectTags()); q = Xapian::Query(Xapian::Query::OP_AND, q, tq);; } - if ( filter.hasFileName() ) { - q = Xapian::Query(Xapian::Query::OP_AND, q, fileNameQuery(filter.getFileName())); + if ( !filter.getFileNames().empty() ) { + q = Xapian::Query(Xapian::Query::OP_AND, q, fileNamesQuery(filter.getFileNames())); } return q; } @@ -755,7 +762,7 @@ enum filterTypes { QUERY = FLAG(12), NAME = FLAG(13), CATEGORY = FLAG(14), - FILENAME = FLAG(15), + FILENAMES = FLAG(15), }; Filter& Filter::local(bool accept) @@ -858,10 +865,10 @@ Filter& Filter::name(std::string name) return *this; } -Filter& Filter::fileName(std::string fileName) +Filter& Filter::fileNames(const FileNames& fileNames) { - _fileName = fileName; - activeFilters |= FILENAME; + _fileNames = fileNames; + activeFilters |= FILENAMES; return *this; } @@ -877,11 +884,6 @@ bool Filter::hasName() const return ACTIVE(NAME); } -bool Filter::hasFileName() const -{ - return ACTIVE(FILENAME); -} - bool Filter::hasCategory() const { return ACTIVE(CATEGORY); diff --git a/src/server/internalServer.cpp b/src/server/internalServer.cpp index 9728f0e75..561ad38dc 100644 --- a/src/server/internalServer.cpp +++ b/src/server/internalServer.cpp @@ -120,7 +120,7 @@ Filter get_search_filter(const RequestContext& request, const std::string& prefi filter.rejectTags(kiwix::split(request.get_argument(prefix+"notag"), ";")); } catch (...) {} try { - filter.fileName(request.get_argument(prefix + "book")); + filter.fileNames(request.get_arguments(prefix + "book")); } catch (...) {} return filter; }