diff --git a/src/common/kiwix/reader.cpp b/src/common/kiwix/reader.cpp index 90af5fca4..18352ea63 100644 --- a/src/common/kiwix/reader.cpp +++ b/src/common/kiwix/reader.cpp @@ -61,8 +61,8 @@ namespace kiwix { string Reader::getRandomPageUrl() { zim::size_type idx = this->firstArticleOffset + (zim::size_type)((double)rand() / ((double)RAND_MAX + 1) * this->articleCount); - zim::Article article = zimFileHandler->getArticle(idx); + return article.getLongUrl().c_str(); } diff --git a/src/common/kiwix/searcher.cpp b/src/common/kiwix/searcher.cpp index f59056a54..0c5158b4b 100644 --- a/src/common/kiwix/searcher.cpp +++ b/src/common/kiwix/searcher.cpp @@ -3,67 +3,20 @@ namespace kiwix { /* Constructor */ - Searcher::Searcher(const string &xapianDirectoryPath) - : stemmer(Xapian::Stem("english")) { - this->openDatabase(xapianDirectoryPath); - } - - /* Destructor */ - Searcher::~Searcher() { - } - - /* Open Xapian readable database */ - void Searcher::openDatabase(const string &directoryPath) { - this->readableDatabase = Xapian::Database(directoryPath); - } - - /* Close Xapian writable database */ - void Searcher::closeDatabase() { - return; + Searcher::Searcher() { } /* Search strings in the database */ - void Searcher::search(string search, const unsigned int resultsCount, bool verbose) { + void Searcher::search(std::string &search, const unsigned int resultsCount, bool verbose) { - /* Reset the results */ - this->results.clear(); - this->resultOffset = this->results.begin(); - - /* Create the query */ - Xapian::QueryParser queryParser; - Xapian::Query query = queryParser.parse_query(removeAccents(search)); - - /* Create the enquire object */ - Xapian::Enquire enquire(this->readableDatabase); - enquire.set_query(query); + this->reset(); if (verbose == true) { - cout << "Performing query `" << - query.get_description() << "'" << endl; + cout << "Performing query `" << search << "'" << endl; } - /* Get the results */ - Xapian::MSet matches = enquire.get_mset(0, resultsCount); - - Xapian::MSetIterator i; - for (i = matches.begin(); i != matches.end(); ++i) { - Xapian::Document doc = i.get_document(); - - Result result; - result.url = doc.get_data(); - result.title = doc.get_value(0); - result.score = i.get_percent(); - - this->results.push_back(result); - - if (verbose == true) { - cout << "Document ID " << *i << " \t"; - cout << i.get_percent() << "% "; - cout << "\t[" << doc.get_data() << "] - " << doc.get_value(0) << endl; - } - } - - /* Set the cursor to the begining */ + searchInIndex(search, resultsCount); + this->resultOffset = this->results.begin(); return; diff --git a/src/common/kiwix/searcher.h b/src/common/kiwix/searcher.h index ebbd4d408..8f58213c4 100644 --- a/src/common/kiwix/searcher.h +++ b/src/common/kiwix/searcher.h @@ -9,7 +9,6 @@ #include #include #include -#include #include using namespace std; @@ -26,21 +25,18 @@ namespace kiwix { class Searcher { public: - Searcher(const string &xapianDirectoryPath); - ~Searcher(); + Searcher(); - Xapian::Database readableDatabase; - Xapian::Stem stemmer; - std::vector results; - std::vector::iterator resultOffset; - - void search(string search, const unsigned int resultsCount, bool verbose=false); + void search(std::string &search, const unsigned int resultsCount, bool verbose=false); bool getNextResult(string &url, string &title, unsigned int &score); - void closeDatabase(); void reset(); protected: - void openDatabase(const string &xapianDirectoryPath); + virtual void closeIndex() = 0; + virtual void searchInIndex(string &search, const unsigned int resultsCount) = 0; + + std::vector results; + std::vector::iterator resultOffset; }; } diff --git a/src/common/kiwix/xapianSearcher.cpp b/src/common/kiwix/xapianSearcher.cpp new file mode 100644 index 000000000..d49000360 --- /dev/null +++ b/src/common/kiwix/xapianSearcher.cpp @@ -0,0 +1,49 @@ +#include "xapianSearcher.h" + +namespace kiwix { + + /* Constructor */ + XapianSearcher::XapianSearcher(const string &xapianDirectoryPath) + : Searcher(), + stemmer(Xapian::Stem("english")) { + this->openIndex(xapianDirectoryPath); + } + + /* Open Xapian readable database */ + void XapianSearcher::openIndex(const string &directoryPath) { + this->readableDatabase = Xapian::Database(directoryPath); + } + + /* Close Xapian writable database */ + void XapianSearcher::closeIndex() { + return; + } + + /* Search strings in the database */ + void XapianSearcher::searchInIndex(string &search, const unsigned int resultsCount) { + /* Create the query */ + Xapian::QueryParser queryParser; + Xapian::Query query = queryParser.parse_query(removeAccents(search)); + + /* Create the enquire object */ + Xapian::Enquire enquire(this->readableDatabase); + enquire.set_query(query); + + /* Get the results */ + Xapian::MSet matches = enquire.get_mset(0, resultsCount); + + Xapian::MSetIterator i; + for (i = matches.begin(); i != matches.end(); ++i) { + Xapian::Document doc = i.get_document(); + + Result result; + result.url = doc.get_data(); + result.title = doc.get_value(0); + result.score = i.get_percent(); + + this->results.push_back(result); + } + + return; + } +} diff --git a/src/common/kiwix/xapianSearcher.h b/src/common/kiwix/xapianSearcher.h new file mode 100644 index 000000000..e5200d62c --- /dev/null +++ b/src/common/kiwix/xapianSearcher.h @@ -0,0 +1,28 @@ +#ifndef KIWIX_XAPIAN_SEARCHER_H +#define KIWIX_XAPIAN_SEARCHER_H + +#include +#include "searcher.h" + +using namespace std; + +namespace kiwix { + + class XapianSearcher : public Searcher { + + public: + XapianSearcher(const string &xapianDirectoryPath); + + void searchInIndex(string &search, const unsigned int resultsCount); + + protected: + void closeIndex(); + void openIndex(const string &xapianDirectoryPath); + + Xapian::Database readableDatabase; + Xapian::Stem stemmer; + }; + +} + +#endif