Allow multiple category support

Created a generic function multipleQuery which takes:
1. string (representing a comma separated list)
2. param (the value to query on using Xapian).

Category and language query will use this function.
This commit is contained in:
Nikhil Tanwar 2023-07-23 18:57:42 +05:30
parent 9c91fc7369
commit c938101c70
2 changed files with 25 additions and 14 deletions

View File

@ -105,6 +105,12 @@ class Filter {
Filter& acceptTags(const Tags& tags); Filter& acceptTags(const Tags& tags);
Filter& rejectTags(const Tags& tags); Filter& rejectTags(const Tags& tags);
/**
* Set the filter to only accept books in the specified category.
*
* Multiple categories can be specified as a comma-separated list (in
* which case a book in any of those categories will match).
*/
Filter& category(std::string category); Filter& category(std::string category);
/** /**

View File

@ -549,25 +549,30 @@ Xapian::Query nameQuery(const std::string& name)
return Xapian::Query("XN" + normalizeText(name)); return Xapian::Query("XN" + normalizeText(name));
} }
Xapian::Query categoryQuery(const std::string& category) Xapian::Query multipleParamQuery(const std::string& commaSeparatedList, const std::string& prefix)
{ {
return Xapian::Query("XC" + normalizeText(category)); Xapian::Query q;
bool firstIteration = true;
for ( const auto& elem : kiwix::split(commaSeparatedList, ",") ) {
const Xapian::Query singleQuery(prefix + normalizeText(elem));
if ( firstIteration ) {
q = singleQuery;
firstIteration = false;
} else {
q = Xapian::Query(Xapian::Query::OP_OR, q, singleQuery);
}
}
return q;
}
Xapian::Query categoryQuery(const std::string& commaSeparatedCategoryList)
{
return multipleParamQuery(commaSeparatedCategoryList, "XC");
} }
Xapian::Query langQuery(const std::string& commaSeparatedLanguageList) Xapian::Query langQuery(const std::string& commaSeparatedLanguageList)
{ {
Xapian::Query q; return multipleParamQuery(commaSeparatedLanguageList, "L");
bool firstIteration = true;
for ( const auto& lang : kiwix::split(commaSeparatedLanguageList, ",") ) {
const Xapian::Query singleLangQuery("L" + normalizeText(lang));
if ( firstIteration ) {
q = singleLangQuery;
firstIteration = false;
} else {
q = Xapian::Query(Xapian::Query::OP_OR, q, singleLangQuery);
}
}
return q;
} }
Xapian::Query publisherQuery(const std::string& publisher) Xapian::Query publisherQuery(const std::string& publisher)