mirror of https://github.com/kiwix/libkiwix.git
Support multi-zims search in kiwix-lib.
All the code was already in zimlib. It is mainly a update of the code using zimlib. No JNI change for now to not break the API.
This commit is contained in:
parent
3991e648ed
commit
9cc329dbd2
|
@ -56,9 +56,11 @@ struct SearcherInternal;
|
|||
class Searcher
|
||||
{
|
||||
public:
|
||||
Searcher();
|
||||
Searcher(const string& xapianDirectoryPath, Reader* reader);
|
||||
~Searcher();
|
||||
|
||||
void add_reader(Reader* reader, const std::string& humanReaderName);
|
||||
void search(std::string& search,
|
||||
unsigned int resultStart,
|
||||
unsigned int resultEnd,
|
||||
|
@ -84,7 +86,8 @@ class Searcher
|
|||
const unsigned int resultEnd,
|
||||
const bool verbose = false);
|
||||
|
||||
Reader* reader;
|
||||
std::vector<Reader*> readers;
|
||||
std::vector<std::string> humanReaderNames;
|
||||
SearcherInternal* internal;
|
||||
std::string searchPattern;
|
||||
std::string protocolPrefix;
|
||||
|
|
|
@ -490,7 +490,15 @@ JNIEXPORT jboolean JNICALL Java_org_kiwix_kiwixlib_JNIKiwix_loadFulltextIndex(
|
|||
if (searcher != NULL) {
|
||||
delete searcher;
|
||||
}
|
||||
searcher = new kiwix::Searcher(cPath, reader);
|
||||
if (!reader || !reader->hasFulltextIndex()) {
|
||||
// Use old API (no embedded full text index).
|
||||
searcher = new kiwix::Searcher(cPath, reader);
|
||||
} else {
|
||||
// Use the new API. We don't care about the human readable name as
|
||||
// we don't use it (in android).
|
||||
searcher = new kiwix::Searcher();
|
||||
searcher->add_reader(reader, "");
|
||||
}
|
||||
} catch (...) {
|
||||
searcher = NULL;
|
||||
retVal = JNI_FALSE;
|
||||
|
|
|
@ -74,8 +74,7 @@ struct SearcherInternal {
|
|||
|
||||
/* Constructor */
|
||||
Searcher::Searcher(const string& xapianDirectoryPath, Reader* reader)
|
||||
: reader(reader),
|
||||
internal(new SearcherInternal()),
|
||||
: internal(new SearcherInternal()),
|
||||
searchPattern(""),
|
||||
protocolPrefix("zim://"),
|
||||
searchProtocolPrefix("search://?"),
|
||||
|
@ -91,11 +90,32 @@ Searcher::Searcher(const string& xapianDirectoryPath, Reader* reader)
|
|||
}
|
||||
}
|
||||
|
||||
Searcher::Searcher()
|
||||
: internal(new SearcherInternal()),
|
||||
searchPattern(""),
|
||||
protocolPrefix("zim://"),
|
||||
searchProtocolPrefix("search://?"),
|
||||
resultCountPerPage(0),
|
||||
estimatedResultCount(0),
|
||||
resultStart(0),
|
||||
resultEnd(0)
|
||||
{
|
||||
template_ct2 = RESOURCE::results_ct2;
|
||||
loadICUExternalTables();
|
||||
}
|
||||
|
||||
/* Destructor */
|
||||
Searcher::~Searcher()
|
||||
{
|
||||
delete internal;
|
||||
}
|
||||
|
||||
void Searcher::add_reader(Reader* reader, const std::string& humanReadableName)
|
||||
{
|
||||
this->readers.push_back(reader);
|
||||
this->humanReaderNames.push_back(humanReadableName);
|
||||
}
|
||||
|
||||
/* Search strings in the database */
|
||||
void Searcher::search(std::string& search,
|
||||
unsigned int resultStart,
|
||||
|
@ -135,8 +155,15 @@ void Searcher::search(std::string& search,
|
|||
this->estimatedResultCount
|
||||
= internal->_xapianSearcher->results.get_matches_estimated();
|
||||
} else {
|
||||
internal->_search = this->reader->getZimFileHandler()->search(
|
||||
unaccentedSearch, resultStart, resultEnd);
|
||||
std::vector<const zim::File*> zims;
|
||||
for (auto current = this->readers.begin(); current != this->readers.end();
|
||||
current++) {
|
||||
zims.push_back((*current)->getZimFileHandler());
|
||||
}
|
||||
zim::Search* search = new zim::Search(zims);
|
||||
search->set_query(unaccentedSearch);
|
||||
search->set_range(resultStart, resultEnd);
|
||||
internal->_search = search;
|
||||
internal->current_iterator = internal->_search->begin();
|
||||
this->estimatedResultCount = internal->_search->get_matches_estimated();
|
||||
}
|
||||
|
@ -192,8 +219,16 @@ void Searcher::suggestions(std::string& search, const bool verbose)
|
|||
* We do not support that. */
|
||||
this->estimatedResultCount = 0;
|
||||
} else {
|
||||
internal->_search = this->reader->getZimFileHandler()->suggestions(
|
||||
unaccentedSearch, resultStart, resultEnd);
|
||||
std::vector<const zim::File*> zims;
|
||||
for (auto current = this->readers.begin(); current != this->readers.end();
|
||||
current++) {
|
||||
zims.push_back((*current)->getZimFileHandler());
|
||||
}
|
||||
zim::Search* search = new zim::Search(zims);
|
||||
search->set_query(unaccentedSearch);
|
||||
search->set_range(resultStart, resultEnd);
|
||||
search->set_suggestion_mode(true);
|
||||
internal->_search = search;
|
||||
internal->current_iterator = internal->_search->begin();
|
||||
this->estimatedResultCount = internal->_search->get_matches_estimated();
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue