Use the new search API in zimlib.

We do not use xapian anymore. This is all handled by zimlib.
This commit is contained in:
Matthieu Gautier 2017-04-07 00:49:12 +02:00
parent 37f29da63e
commit 5ca419bee7
5 changed files with 109 additions and 39 deletions

View File

@ -5,10 +5,6 @@ headers = [
'searcher.h' 'searcher.h'
] ]
if xapian_dep.found()
headers += ['xapianSearcher.h']
endif
install_headers(headers, subdir:'kiwix') install_headers(headers, subdir:'kiwix')
install_headers( install_headers(

View File

@ -35,30 +35,31 @@
using namespace std; using namespace std;
class Result
{
public:
virtual ~Result() {};
virtual std::string get_url() = 0;
virtual std::string get_title() = 0;
virtual int get_score() = 0;
virtual std::string get_snippet() = 0;
virtual int get_wordCount() = 0;
virtual int get_size() = 0;
};
namespace kiwix { namespace kiwix {
class Reader;
class Result {
public:
virtual ~Result() {};
virtual std::string get_url() = 0;
virtual std::string get_title() = 0;
virtual int get_score() = 0;
virtual std::string get_snippet() = 0;
virtual int get_wordCount() = 0;
virtual int get_size() = 0;
};
struct SearcherInternal;
class Searcher { class Searcher {
public: public:
Searcher(); Searcher(Reader* reader);
virtual ~Searcher(); ~Searcher();
void search(std::string &search, unsigned int resultStart, void search(std::string &search, unsigned int resultStart,
unsigned int resultEnd, const bool verbose=false); unsigned int resultEnd, const bool verbose=false);
virtual Result* getNextResult() = 0; Result* getNextResult();
virtual void restart_search() = 0; void restart_search();
unsigned int getEstimatedResultCount(); unsigned int getEstimatedResultCount();
bool setProtocolPrefix(const std::string prefix); bool setProtocolPrefix(const std::string prefix);
bool setSearchProtocolPrefix(const std::string prefix); bool setSearchProtocolPrefix(const std::string prefix);
@ -71,10 +72,12 @@ namespace kiwix {
protected: protected:
std::string beautifyInteger(const unsigned int number); std::string beautifyInteger(const unsigned int number);
virtual void closeIndex() = 0; void closeIndex() ;
virtual void searchInIndex(string &search, const unsigned int resultStart, void searchInIndex(string &search, const unsigned int resultStart,
const unsigned int resultEnd, const bool verbose=false) = 0; const unsigned int resultEnd, const bool verbose=false);
Reader* reader;
SearcherInternal* internal;
std::string searchPattern; std::string searchPattern;
std::string protocolPrefix; std::string protocolPrefix;
std::string searchProtocolPrefix; std::string searchProtocolPrefix;

View File

@ -61,9 +61,7 @@ else
endif endif
endif endif
xapian_dep = dependency('xapian-core', required:false) all_deps = [thread_dep, libicu_dep, libzim_dep, pugixml_dep]
all_deps = [thread_dep, libicu_dep, libzim_dep, xapian_dep, pugixml_dep]
if has_ctpp2_dep if has_ctpp2_dep
all_deps += [ctpp2_dep] all_deps += [ctpp2_dep]
endif endif
@ -80,9 +78,6 @@ subdir('static')
subdir('src') subdir('src')
pkg_requires = ['libzim', 'icu-i18n', 'pugixml'] pkg_requires = ['libzim', 'icu-i18n', 'pugixml']
if xapian_dep.found()
pkg_requires += ['xapian-core']
endif
extra_libs = [] extra_libs = []
extra_cflags = '' extra_cflags = ''

View File

@ -8,16 +8,10 @@ kiwix_sources = [
'common/regexTools.cpp', 'common/regexTools.cpp',
'common/stringTools.cpp', 'common/stringTools.cpp',
'common/networkTools.cpp', 'common/networkTools.cpp',
'common/otherTools.cpp', 'common/otherTools.cpp'
'xapian/htmlparse.cc',
'xapian/myhtmlparse.cc'
] ]
kiwix_sources += lib_resources kiwix_sources += lib_resources
if xapian_dep.found()
kiwix_sources += ['xapianSearcher.cpp']
endif
if get_option('android') if get_option('android')
subdir('android') subdir('android')
endif endif

View File

@ -18,8 +18,11 @@
*/ */
#include "searcher.h" #include "searcher.h"
#include "reader.h"
#include "kiwixlib-resources.h" #include "kiwixlib-resources.h"
#include <zim/search.h>
#ifdef ENABLE_CTPP2 #ifdef ENABLE_CTPP2
#include <ctpp2/CDT.hpp> #include <ctpp2/CDT.hpp>
#include <ctpp2/CTPP2FileLogger.hpp> #include <ctpp2/CTPP2FileLogger.hpp>
@ -32,8 +35,39 @@ using namespace CTPP;
namespace kiwix { namespace kiwix {
class _Result : public Result {
public:
_Result(Searcher* searcher, zim::Search::iterator& iterator);
virtual ~_Result() {};
virtual std::string get_url();
virtual std::string get_title();
virtual int get_score();
virtual std::string get_snippet();
virtual int get_wordCount();
virtual int get_size();
private:
Searcher* searcher;
zim::Search::iterator iterator;
};
struct SearcherInternal {
const zim::Search *_search;
zim::Search::iterator current_iterator;
SearcherInternal() : _search(NULL) {}
~SearcherInternal() {
if ( _search != NULL )
delete _search;
}
};
/* Constructor */ /* Constructor */
Searcher::Searcher() : Searcher::Searcher(Reader* reader) :
reader(reader),
internal(new SearcherInternal()),
searchPattern(""), searchPattern(""),
protocolPrefix("zim://"), protocolPrefix("zim://"),
searchProtocolPrefix("search://?"), searchProtocolPrefix("search://?"),
@ -47,7 +81,9 @@ namespace kiwix {
} }
/* Destructor */ /* Destructor */
Searcher::~Searcher() {} Searcher::~Searcher() {
delete internal;
}
/* Search strings in the database */ /* Search strings in the database */
void Searcher::search(std::string &search, unsigned int resultStart, void Searcher::search(std::string &search, unsigned int resultStart,
@ -80,12 +116,28 @@ namespace kiwix {
this->resultStart = resultStart; this->resultStart = resultStart;
this->resultEnd = resultEnd; this->resultEnd = resultEnd;
string unaccentedSearch = removeAccents(search); string unaccentedSearch = removeAccents(search);
searchInIndex(unaccentedSearch, resultStart, resultEnd, verbose); internal->_search = this->reader->getZimFileHandler()->search(unaccentedSearch, resultStart, resultEnd);
internal->current_iterator = internal->_search->begin();
this->estimatedResultCount = internal->_search->get_matches_estimated();
} }
return; return;
} }
void Searcher::restart_search() {
internal->current_iterator = internal->_search->begin();
}
Result* Searcher::getNextResult() {
if (internal->current_iterator != internal->_search->end()) {
Result* result = new _Result(this, internal->current_iterator);
internal->current_iterator++;
return result;
}
return NULL;
}
/* Reset the results */ /* Reset the results */
void Searcher::reset() { void Searcher::reset() {
this->estimatedResultCount = 0; this->estimatedResultCount = 0;
@ -112,6 +164,36 @@ namespace kiwix {
this->contentHumanReadableId = contentHumanReadableId; this->contentHumanReadableId = contentHumanReadableId;
} }
_Result::_Result(Searcher* searcher, zim::Search::iterator& iterator):
searcher(searcher),
iterator(iterator)
{
}
std::string _Result::get_url() {
return iterator.get_url();
}
std::string _Result::get_title() {
return iterator.get_title();
}
int _Result::get_score() {
return iterator.get_score();
}
std::string _Result::get_snippet() {
return iterator.get_snippet();
}
int _Result::get_size() {
return iterator.get_size();
}
int _Result::get_wordCount() {
return iterator.get_wordCount();
}
#ifdef ENABLE_CTPP2 #ifdef ENABLE_CTPP2
string Searcher::getHtml() { string Searcher::getHtml() {