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
|
class Searcher
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
|
Searcher();
|
||||||
Searcher(const string& xapianDirectoryPath, Reader* reader);
|
Searcher(const string& xapianDirectoryPath, Reader* reader);
|
||||||
~Searcher();
|
~Searcher();
|
||||||
|
|
||||||
|
void add_reader(Reader* reader, const std::string& humanReaderName);
|
||||||
void search(std::string& search,
|
void search(std::string& search,
|
||||||
unsigned int resultStart,
|
unsigned int resultStart,
|
||||||
unsigned int resultEnd,
|
unsigned int resultEnd,
|
||||||
|
@ -84,7 +86,8 @@ class Searcher
|
||||||
const unsigned int resultEnd,
|
const unsigned int resultEnd,
|
||||||
const bool verbose = false);
|
const bool verbose = false);
|
||||||
|
|
||||||
Reader* reader;
|
std::vector<Reader*> readers;
|
||||||
|
std::vector<std::string> humanReaderNames;
|
||||||
SearcherInternal* internal;
|
SearcherInternal* internal;
|
||||||
std::string searchPattern;
|
std::string searchPattern;
|
||||||
std::string protocolPrefix;
|
std::string protocolPrefix;
|
||||||
|
|
|
@ -490,7 +490,15 @@ JNIEXPORT jboolean JNICALL Java_org_kiwix_kiwixlib_JNIKiwix_loadFulltextIndex(
|
||||||
if (searcher != NULL) {
|
if (searcher != NULL) {
|
||||||
delete searcher;
|
delete searcher;
|
||||||
}
|
}
|
||||||
|
if (!reader || !reader->hasFulltextIndex()) {
|
||||||
|
// Use old API (no embedded full text index).
|
||||||
searcher = new kiwix::Searcher(cPath, reader);
|
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 (...) {
|
} catch (...) {
|
||||||
searcher = NULL;
|
searcher = NULL;
|
||||||
retVal = JNI_FALSE;
|
retVal = JNI_FALSE;
|
||||||
|
|
|
@ -74,8 +74,7 @@ struct SearcherInternal {
|
||||||
|
|
||||||
/* Constructor */
|
/* Constructor */
|
||||||
Searcher::Searcher(const string& xapianDirectoryPath, Reader* reader)
|
Searcher::Searcher(const string& xapianDirectoryPath, Reader* reader)
|
||||||
: reader(reader),
|
: internal(new SearcherInternal()),
|
||||||
internal(new SearcherInternal()),
|
|
||||||
searchPattern(""),
|
searchPattern(""),
|
||||||
protocolPrefix("zim://"),
|
protocolPrefix("zim://"),
|
||||||
searchProtocolPrefix("search://?"),
|
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 */
|
/* Destructor */
|
||||||
Searcher::~Searcher()
|
Searcher::~Searcher()
|
||||||
{
|
{
|
||||||
delete internal;
|
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 */
|
/* Search strings in the database */
|
||||||
void Searcher::search(std::string& search,
|
void Searcher::search(std::string& search,
|
||||||
unsigned int resultStart,
|
unsigned int resultStart,
|
||||||
|
@ -135,8 +155,15 @@ void Searcher::search(std::string& search,
|
||||||
this->estimatedResultCount
|
this->estimatedResultCount
|
||||||
= internal->_xapianSearcher->results.get_matches_estimated();
|
= internal->_xapianSearcher->results.get_matches_estimated();
|
||||||
} else {
|
} else {
|
||||||
internal->_search = this->reader->getZimFileHandler()->search(
|
std::vector<const zim::File*> zims;
|
||||||
unaccentedSearch, resultStart, resultEnd);
|
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();
|
internal->current_iterator = internal->_search->begin();
|
||||||
this->estimatedResultCount = internal->_search->get_matches_estimated();
|
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. */
|
* We do not support that. */
|
||||||
this->estimatedResultCount = 0;
|
this->estimatedResultCount = 0;
|
||||||
} else {
|
} else {
|
||||||
internal->_search = this->reader->getZimFileHandler()->suggestions(
|
std::vector<const zim::File*> zims;
|
||||||
unaccentedSearch, resultStart, resultEnd);
|
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();
|
internal->current_iterator = internal->_search->begin();
|
||||||
this->estimatedResultCount = internal->_search->get_matches_estimated();
|
this->estimatedResultCount = internal->_search->get_matches_estimated();
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue