mirror of https://github.com/kiwix/libkiwix.git
Check if a valuemaps metadata is available in the database and use it.
This way, we do not make assumption of where the values are stored.
This commit is contained in:
parent
83d27255cf
commit
9be2abedf3
|
@ -22,14 +22,18 @@
|
|||
|
||||
#include <xapian.h>
|
||||
#include "searcher.h"
|
||||
#include <map>
|
||||
#include <string>
|
||||
|
||||
using namespace std;
|
||||
|
||||
namespace kiwix {
|
||||
|
||||
class XapianSearcher;
|
||||
|
||||
class XapianResult : public Result {
|
||||
public:
|
||||
XapianResult(Xapian::MSetIterator& iterator);
|
||||
XapianResult(XapianSearcher* searcher, Xapian::MSetIterator& iterator);
|
||||
virtual ~XapianResult() {};
|
||||
|
||||
virtual std::string get_url();
|
||||
|
@ -40,6 +44,7 @@ namespace kiwix {
|
|||
virtual int get_size();
|
||||
|
||||
private:
|
||||
XapianSearcher* searcher;
|
||||
Xapian::MSetIterator iterator;
|
||||
Xapian::Document document;
|
||||
};
|
||||
|
@ -51,7 +56,7 @@ namespace kiwix {
|
|||
};
|
||||
|
||||
class XapianSearcher : public Searcher {
|
||||
|
||||
friend class XapianResult;
|
||||
public:
|
||||
XapianSearcher(const string &xapianDirectoryPath);
|
||||
virtual ~XapianSearcher() {};
|
||||
|
@ -68,6 +73,7 @@ namespace kiwix {
|
|||
Xapian::Stem stemmer;
|
||||
Xapian::MSet results;
|
||||
Xapian::MSetIterator current_result;
|
||||
std::map<std::string, int> valuesmap;
|
||||
};
|
||||
|
||||
}
|
||||
|
|
|
@ -25,8 +25,21 @@
|
|||
#include <sys/types.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include <vector>
|
||||
|
||||
namespace kiwix {
|
||||
|
||||
std::map<std::string, int> read_valuesmap(const std::string &s) {
|
||||
std::map<std::string, int> result;
|
||||
std::vector<std::string> elems = split(s, ";");
|
||||
for( auto elem: elems)
|
||||
{
|
||||
std::vector<std::string> tmp_elems = split(elem, ":");
|
||||
result.insert( std::pair<std::string, int>(tmp_elems[0], atoi(tmp_elems[1].c_str())) );
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
/* Constructor */
|
||||
XapianSearcher::XapianSearcher(const string &xapianDirectoryPath)
|
||||
: Searcher(),
|
||||
|
@ -49,6 +62,7 @@ namespace kiwix {
|
|||
} catch (...) {
|
||||
this->readableDatabase = Xapian::Database(directoryPath);
|
||||
}
|
||||
this->valuesmap = read_valuesmap(this->readableDatabase.get_metadata("valuesmap"));
|
||||
}
|
||||
|
||||
/* Close Xapian writable database */
|
||||
|
@ -78,7 +92,7 @@ namespace kiwix {
|
|||
/* Get next result */
|
||||
Result* XapianSearcher::getNextResult() {
|
||||
if (this->current_result != this->results.end()) {
|
||||
XapianResult* result = new XapianResult(this->current_result);
|
||||
XapianResult* result = new XapianResult(this, this->current_result);
|
||||
this->current_result++;
|
||||
return result;
|
||||
}
|
||||
|
@ -89,7 +103,8 @@ namespace kiwix {
|
|||
this->current_result = this->results.begin();
|
||||
}
|
||||
|
||||
XapianResult::XapianResult(Xapian::MSetIterator& iterator):
|
||||
XapianResult::XapianResult(XapianSearcher* searcher, Xapian::MSetIterator& iterator):
|
||||
searcher(searcher),
|
||||
iterator(iterator),
|
||||
document(iterator.get_document())
|
||||
{
|
||||
|
@ -100,7 +115,16 @@ namespace kiwix {
|
|||
}
|
||||
|
||||
std::string XapianResult::get_title() {
|
||||
return document.get_value(0);
|
||||
if ( searcher->valuesmap.empty() )
|
||||
{
|
||||
/* This is the old legacy version. Guess and try */
|
||||
return document.get_value(0);
|
||||
}
|
||||
else if ( searcher->valuesmap.find("title") != searcher->valuesmap.end() )
|
||||
{
|
||||
return document.get_value(searcher->valuesmap["title"]);
|
||||
}
|
||||
return "";
|
||||
}
|
||||
|
||||
int XapianResult::get_score() {
|
||||
|
@ -108,15 +132,44 @@ namespace kiwix {
|
|||
}
|
||||
|
||||
std::string XapianResult::get_snippet() {
|
||||
return document.get_value(1);
|
||||
if ( searcher->valuesmap.empty() )
|
||||
{
|
||||
/* This is the old legacy version. Guess and try */
|
||||
return document.get_value(1);
|
||||
}
|
||||
else if ( searcher->valuesmap.find("snippet") != searcher->valuesmap.end() )
|
||||
{
|
||||
return document.get_value(searcher->valuesmap["snippet"]);
|
||||
}
|
||||
return "";
|
||||
}
|
||||
|
||||
int XapianResult::get_size() {
|
||||
return document.get_value(2).empty() == true ? -1 : atoi(document.get_value(2).c_str());
|
||||
if ( searcher->valuesmap.empty() )
|
||||
{
|
||||
/* This is the old legacy version. Guess and try */
|
||||
return document.get_value(2).empty() == true ? -1 : atoi(document.get_value(2).c_str());
|
||||
}
|
||||
else if ( searcher->valuesmap.find("size") != searcher->valuesmap.end() )
|
||||
{
|
||||
return atoi(document.get_value(searcher->valuesmap["size"]).c_str());
|
||||
}
|
||||
/* The size is never used. Do we really want to get the content and
|
||||
calculate the size ? */
|
||||
return -1;
|
||||
}
|
||||
|
||||
int XapianResult::get_wordCount() {
|
||||
return document.get_value(3).empty() == true ? -1 : atoi(document.get_value(3).c_str());
|
||||
if ( searcher->valuesmap.empty() )
|
||||
{
|
||||
/* This is the old legacy version. Guess and try */
|
||||
return document.get_value(3).empty() == true ? -1 : atoi(document.get_value(3).c_str());
|
||||
}
|
||||
else if ( searcher->valuesmap.find("wordcount") != searcher->valuesmap.end() )
|
||||
{
|
||||
return atoi(document.get_value(searcher->valuesmap["wordcount"]).c_str());
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
} // Kiwix namespace
|
||||
|
|
Loading…
Reference in New Issue