Skip to content

Commit

Permalink
Multivalue support for book query
Browse files Browse the repository at this point in the history
Adds support for putting multiple `book` query parameter in catalog/search
  • Loading branch information
juuz0 committed Aug 15, 2022
1 parent 59c85ef commit c2272b5
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 19 deletions.
8 changes: 4 additions & 4 deletions include/library.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ enum supportedListMode {
class Filter {
public: // types
using Tags = std::vector<std::string>;
using FileNames = std::vector<std::string>;

private: // data
uint64_t activeFilters;
Expand All @@ -67,7 +68,7 @@ class Filter {
std::string _query;
bool _queryIsPartial;
std::string _name;
std::string _fileName;
FileNames _fileNames;

public: // functions
Filter();
Expand Down Expand Up @@ -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; }
Expand All @@ -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;
Expand Down
30 changes: 16 additions & 14 deletions src/library.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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<Xapian::Query> 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)
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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;
}

Expand All @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion src/server/internalServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down

0 comments on commit c2272b5

Please sign in to comment.