+ factorization of the Searcher class to avoid multiple backends (preparation work for CluceneSearcher class and CluceneAccessor XPCOM)

This commit is contained in:
kelson42 2010-11-06 11:47:13 +00:00
parent e48d5b5a24
commit 92aec4e612
5 changed files with 91 additions and 65 deletions

View File

@ -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();
}

View File

@ -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;

View File

@ -9,7 +9,6 @@
#include <locale>
#include <cctype>
#include <vector>
#include <xapian.h>
#include <unaccent.h>
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<Result> results;
std::vector<Result>::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<Result> results;
std::vector<Result>::iterator resultOffset;
};
}

View File

@ -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;
}
}

View File

@ -0,0 +1,28 @@
#ifndef KIWIX_XAPIAN_SEARCHER_H
#define KIWIX_XAPIAN_SEARCHER_H
#include <xapian.h>
#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