mirror of https://github.com/kiwix/libkiwix.git
Use the new search API in zimlib.
We do not use xapian anymore. This is all handled by zimlib.
This commit is contained in:
parent
37f29da63e
commit
5ca419bee7
|
@ -5,10 +5,6 @@ headers = [
|
|||
'searcher.h'
|
||||
]
|
||||
|
||||
if xapian_dep.found()
|
||||
headers += ['xapianSearcher.h']
|
||||
endif
|
||||
|
||||
install_headers(headers, subdir:'kiwix')
|
||||
|
||||
install_headers(
|
||||
|
|
|
@ -35,30 +35,31 @@
|
|||
|
||||
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 {
|
||||
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 {
|
||||
|
||||
public:
|
||||
Searcher();
|
||||
virtual ~Searcher();
|
||||
Searcher(Reader* reader);
|
||||
~Searcher();
|
||||
|
||||
void search(std::string &search, unsigned int resultStart,
|
||||
unsigned int resultEnd, const bool verbose=false);
|
||||
virtual Result* getNextResult() = 0;
|
||||
virtual void restart_search() = 0;
|
||||
Result* getNextResult();
|
||||
void restart_search();
|
||||
unsigned int getEstimatedResultCount();
|
||||
bool setProtocolPrefix(const std::string prefix);
|
||||
bool setSearchProtocolPrefix(const std::string prefix);
|
||||
|
@ -71,10 +72,12 @@ namespace kiwix {
|
|||
|
||||
protected:
|
||||
std::string beautifyInteger(const unsigned int number);
|
||||
virtual void closeIndex() = 0;
|
||||
virtual void searchInIndex(string &search, const unsigned int resultStart,
|
||||
const unsigned int resultEnd, const bool verbose=false) = 0;
|
||||
void closeIndex() ;
|
||||
void searchInIndex(string &search, const unsigned int resultStart,
|
||||
const unsigned int resultEnd, const bool verbose=false);
|
||||
|
||||
Reader* reader;
|
||||
SearcherInternal* internal;
|
||||
std::string searchPattern;
|
||||
std::string protocolPrefix;
|
||||
std::string searchProtocolPrefix;
|
||||
|
|
|
@ -61,9 +61,7 @@ else
|
|||
endif
|
||||
endif
|
||||
|
||||
xapian_dep = dependency('xapian-core', required:false)
|
||||
|
||||
all_deps = [thread_dep, libicu_dep, libzim_dep, xapian_dep, pugixml_dep]
|
||||
all_deps = [thread_dep, libicu_dep, libzim_dep, pugixml_dep]
|
||||
if has_ctpp2_dep
|
||||
all_deps += [ctpp2_dep]
|
||||
endif
|
||||
|
@ -80,9 +78,6 @@ subdir('static')
|
|||
subdir('src')
|
||||
|
||||
pkg_requires = ['libzim', 'icu-i18n', 'pugixml']
|
||||
if xapian_dep.found()
|
||||
pkg_requires += ['xapian-core']
|
||||
endif
|
||||
|
||||
extra_libs = []
|
||||
extra_cflags = ''
|
||||
|
|
|
@ -8,16 +8,10 @@ kiwix_sources = [
|
|||
'common/regexTools.cpp',
|
||||
'common/stringTools.cpp',
|
||||
'common/networkTools.cpp',
|
||||
'common/otherTools.cpp',
|
||||
'xapian/htmlparse.cc',
|
||||
'xapian/myhtmlparse.cc'
|
||||
'common/otherTools.cpp'
|
||||
]
|
||||
kiwix_sources += lib_resources
|
||||
|
||||
if xapian_dep.found()
|
||||
kiwix_sources += ['xapianSearcher.cpp']
|
||||
endif
|
||||
|
||||
if get_option('android')
|
||||
subdir('android')
|
||||
endif
|
||||
|
|
|
@ -18,8 +18,11 @@
|
|||
*/
|
||||
|
||||
#include "searcher.h"
|
||||
#include "reader.h"
|
||||
#include "kiwixlib-resources.h"
|
||||
|
||||
#include <zim/search.h>
|
||||
|
||||
#ifdef ENABLE_CTPP2
|
||||
#include <ctpp2/CDT.hpp>
|
||||
#include <ctpp2/CTPP2FileLogger.hpp>
|
||||
|
@ -32,8 +35,39 @@ using namespace CTPP;
|
|||
|
||||
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 */
|
||||
Searcher::Searcher() :
|
||||
Searcher::Searcher(Reader* reader) :
|
||||
reader(reader),
|
||||
internal(new SearcherInternal()),
|
||||
searchPattern(""),
|
||||
protocolPrefix("zim://"),
|
||||
searchProtocolPrefix("search://?"),
|
||||
|
@ -47,7 +81,9 @@ namespace kiwix {
|
|||
}
|
||||
|
||||
/* Destructor */
|
||||
Searcher::~Searcher() {}
|
||||
Searcher::~Searcher() {
|
||||
delete internal;
|
||||
}
|
||||
|
||||
/* Search strings in the database */
|
||||
void Searcher::search(std::string &search, unsigned int resultStart,
|
||||
|
@ -80,12 +116,28 @@ namespace kiwix {
|
|||
this->resultStart = resultStart;
|
||||
this->resultEnd = resultEnd;
|
||||
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;
|
||||
}
|
||||
|
||||
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 */
|
||||
void Searcher::reset() {
|
||||
this->estimatedResultCount = 0;
|
||||
|
@ -112,6 +164,36 @@ namespace kiwix {
|
|||
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
|
||||
|
||||
string Searcher::getHtml() {
|
||||
|
|
Loading…
Reference in New Issue