mirror of https://github.com/kiwix/libkiwix.git
Dummy application of new libzim search API
Didn't take any advantage of the new libzim search API. Just fixed the libkiwix build in the most straightforward way.
This commit is contained in:
parent
5188355878
commit
cd02b4de3b
|
@ -27,7 +27,7 @@
|
||||||
#include <cctype>
|
#include <cctype>
|
||||||
#include <locale>
|
#include <locale>
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <vector>
|
#include <memory>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
#include "tools/pathTools.h"
|
#include "tools/pathTools.h"
|
||||||
#include "tools/stringTools.h"
|
#include "tools/stringTools.h"
|
||||||
|
@ -154,7 +154,7 @@ class Searcher
|
||||||
const bool verbose = false);
|
const bool verbose = false);
|
||||||
|
|
||||||
std::vector<Reader*> readers;
|
std::vector<Reader*> readers;
|
||||||
SearcherInternal* internal;
|
std::unique_ptr<SearcherInternal> internal;
|
||||||
std::string searchPattern;
|
std::string searchPattern;
|
||||||
unsigned int estimatedResultCount;
|
unsigned int estimatedResultCount;
|
||||||
unsigned int resultStart;
|
unsigned int resultStart;
|
||||||
|
|
|
@ -495,14 +495,16 @@ bool Reader::searchSuggestionsSmart(const string& prefix,
|
||||||
bool retVal = false;
|
bool retVal = false;
|
||||||
|
|
||||||
/* Try to search in the title using fulltext search database */
|
/* Try to search in the title using fulltext search database */
|
||||||
auto suggestionSearch = zim::Search(*zimArchive);
|
|
||||||
suggestionSearch.set_query(prefix);
|
|
||||||
suggestionSearch.set_range(0, suggestionsCount);
|
|
||||||
suggestionSearch.set_suggestion_mode(true);
|
|
||||||
|
|
||||||
if (suggestionSearch.get_matches_estimated()) {
|
auto suggestionSearcher = zim::Searcher(*zimArchive);
|
||||||
for (auto current = suggestionSearch.begin();
|
zim::Query suggestionQuery;
|
||||||
current != suggestionSearch.end();
|
suggestionQuery.setQuery(prefix, true);
|
||||||
|
auto suggestionSearch = suggestionSearcher.search(suggestionQuery);
|
||||||
|
|
||||||
|
if (suggestionSearch.getEstimatedMatches()) {
|
||||||
|
const auto suggestions = suggestionSearch.getResults(0, suggestionsCount);
|
||||||
|
for (auto current = suggestions.begin();
|
||||||
|
current != suggestions.end();
|
||||||
current++) {
|
current++) {
|
||||||
std::vector<std::string> suggestion;
|
std::vector<std::string> suggestion;
|
||||||
suggestion.push_back(current->getTitle());
|
suggestion.push_back(current->getTitle());
|
||||||
|
|
|
@ -35,7 +35,7 @@ namespace kiwix
|
||||||
class _Result : public Result
|
class _Result : public Result
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
_Result(zim::Search::iterator& iterator);
|
_Result(zim::SearchResultSet::iterator iterator);
|
||||||
virtual ~_Result(){};
|
virtual ~_Result(){};
|
||||||
|
|
||||||
virtual std::string get_url();
|
virtual std::string get_url();
|
||||||
|
@ -48,26 +48,22 @@ class _Result : public Result
|
||||||
virtual int get_readerIndex();
|
virtual int get_readerIndex();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
zim::Search::iterator iterator;
|
zim::SearchResultSet::iterator iterator;
|
||||||
};
|
};
|
||||||
|
|
||||||
struct SearcherInternal {
|
struct SearcherInternal : zim::SearchResultSet {
|
||||||
const zim::Search* _search;
|
explicit SearcherInternal(const zim::SearchResultSet& srs)
|
||||||
zim::Search::iterator current_iterator;
|
: zim::SearchResultSet(srs)
|
||||||
|
, current_iterator(srs.begin())
|
||||||
SearcherInternal() : _search(NULL) {}
|
|
||||||
~SearcherInternal()
|
|
||||||
{
|
{
|
||||||
if (_search != NULL) {
|
|
||||||
delete _search;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
zim::SearchResultSet::iterator current_iterator;
|
||||||
};
|
};
|
||||||
|
|
||||||
/* Constructor */
|
/* Constructor */
|
||||||
Searcher::Searcher()
|
Searcher::Searcher()
|
||||||
: internal(new SearcherInternal()),
|
: searchPattern(""),
|
||||||
searchPattern(""),
|
|
||||||
estimatedResultCount(0),
|
estimatedResultCount(0),
|
||||||
resultStart(0),
|
resultStart(0),
|
||||||
resultEnd(0)
|
resultEnd(0)
|
||||||
|
@ -78,7 +74,6 @@ Searcher::Searcher()
|
||||||
/* Destructor */
|
/* Destructor */
|
||||||
Searcher::~Searcher()
|
Searcher::~Searcher()
|
||||||
{
|
{
|
||||||
delete internal;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
bool Searcher::add_reader(Reader* reader)
|
bool Searcher::add_reader(Reader* reader)
|
||||||
|
@ -122,13 +117,13 @@ void Searcher::search(const std::string& search,
|
||||||
archives.push_back(*(*current)->getZimArchive());
|
archives.push_back(*(*current)->getZimArchive());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
zim::Search* search = new zim::Search(archives);
|
zim::Searcher searcher(archives);
|
||||||
search->set_verbose(verbose);
|
zim::Query query;
|
||||||
search->set_query(unaccentedSearch);
|
query.setQuery(unaccentedSearch, false);
|
||||||
search->set_range(resultStart, resultEnd);
|
query.setVerbose(verbose);
|
||||||
internal->_search = search;
|
zim::Search search = searcher.search(query);
|
||||||
internal->current_iterator = internal->_search->begin();
|
internal.reset(new SearcherInternal(search.getResults(resultStart, resultEnd)));
|
||||||
this->estimatedResultCount = internal->_search->get_matches_estimated();
|
this->estimatedResultCount = search.getEstimatedMatches();
|
||||||
}
|
}
|
||||||
|
|
||||||
return;
|
return;
|
||||||
|
@ -163,28 +158,28 @@ void Searcher::geo_search(float latitude, float longitude, float distance,
|
||||||
current++) {
|
current++) {
|
||||||
archives.push_back(*(*current)->getZimArchive());
|
archives.push_back(*(*current)->getZimArchive());
|
||||||
}
|
}
|
||||||
zim::Search* search = new zim::Search(archives);
|
zim::Searcher searcher(archives);
|
||||||
search->set_verbose(verbose);
|
zim::Query query;
|
||||||
search->set_query("");
|
query.setVerbose(verbose);
|
||||||
search->set_georange(latitude, longitude, distance);
|
query.setQuery("", false);
|
||||||
search->set_range(resultStart, resultEnd);
|
query.setGeorange(latitude, longitude, distance);
|
||||||
internal->_search = search;
|
zim::Search search = searcher.search(query);
|
||||||
internal->current_iterator = internal->_search->begin();
|
internal.reset(new SearcherInternal(search.getResults(resultStart, resultEnd)));
|
||||||
this->estimatedResultCount = internal->_search->get_matches_estimated();
|
this->estimatedResultCount = search.getEstimatedMatches();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void Searcher::restart_search()
|
void Searcher::restart_search()
|
||||||
{
|
{
|
||||||
if (internal->_search) {
|
if (internal.get()) {
|
||||||
internal->current_iterator = internal->_search->begin();
|
internal->current_iterator = internal->begin();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Result* Searcher::getNextResult()
|
Result* Searcher::getNextResult()
|
||||||
{
|
{
|
||||||
if (internal->_search &&
|
if (internal.get() &&
|
||||||
internal->current_iterator != internal->_search->end()) {
|
internal->current_iterator != internal->end()) {
|
||||||
Result* result = new _Result(internal->current_iterator);
|
Result* result = new _Result(internal->current_iterator);
|
||||||
internal->current_iterator++;
|
internal->current_iterator++;
|
||||||
return result;
|
return result;
|
||||||
|
@ -218,14 +213,13 @@ void Searcher::suggestions(std::string& searchPattern, const bool verbose)
|
||||||
current++) {
|
current++) {
|
||||||
archives.push_back(*(*current)->getZimArchive());
|
archives.push_back(*(*current)->getZimArchive());
|
||||||
}
|
}
|
||||||
zim::Search* search = new zim::Search(archives);
|
zim::Searcher searcher(archives);
|
||||||
search->set_verbose(verbose);
|
zim::Query query;
|
||||||
search->set_query(unaccentedSearch);
|
query.setVerbose(verbose);
|
||||||
search->set_range(resultStart, resultEnd);
|
query.setQuery(unaccentedSearch, true);
|
||||||
search->set_suggestion_mode(true);
|
zim::Search search = searcher.search(query);
|
||||||
internal->_search = search;
|
internal.reset(new SearcherInternal(search.getResults(resultStart, resultEnd)));
|
||||||
internal->current_iterator = internal->_search->begin();
|
this->estimatedResultCount = search.getEstimatedMatches();
|
||||||
this->estimatedResultCount = internal->_search->get_matches_estimated();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Return the result count estimation */
|
/* Return the result count estimation */
|
||||||
|
@ -234,7 +228,7 @@ unsigned int Searcher::getEstimatedResultCount()
|
||||||
return this->estimatedResultCount;
|
return this->estimatedResultCount;
|
||||||
}
|
}
|
||||||
|
|
||||||
_Result::_Result(zim::Search::iterator& iterator)
|
_Result::_Result(zim::SearchResultSet::iterator iterator)
|
||||||
: iterator(iterator)
|
: iterator(iterator)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue