Skip to content

Commit

Permalink
Dummy application of new libzim search API
Browse files Browse the repository at this point in the history
Didn't take any advantage of the new libzim search API. Just fixed the
libkiwix build in the most straightforward way.
  • Loading branch information
veloman-yunkan committed May 15, 2021
1 parent 5188355 commit 8d68581
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 51 deletions.
2 changes: 1 addition & 1 deletion include/searcher.h
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ class Searcher
const bool verbose = false);

std::vector<Reader*> readers;
SearcherInternal* internal;
std::unique_ptr<SearcherInternal> internal;
std::string searchPattern;
unsigned int estimatedResultCount;
unsigned int resultStart;
Expand Down
18 changes: 10 additions & 8 deletions src/reader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -495,14 +495,16 @@ bool Reader::searchSuggestionsSmart(const string& prefix,
bool retVal = false;

/* 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()) {
for (auto current = suggestionSearch.begin();
current != suggestionSearch.end();

auto suggestionSearcher = zim::Searcher(*zimArchive);
zim::Query suggestionQuery;
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++) {
std::vector<std::string> suggestion;
suggestion.push_back(current->getTitle());
Expand Down
78 changes: 36 additions & 42 deletions src/searcher.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ namespace kiwix
class _Result : public Result
{
public:
_Result(zim::Search::iterator& iterator);
_Result(zim::SearchResultSet::iterator iterator);
virtual ~_Result(){};

virtual std::string get_url();
Expand All @@ -48,26 +48,22 @@ class _Result : public Result
virtual int get_readerIndex();

private:
zim::Search::iterator iterator;
zim::SearchResultSet::iterator iterator;
};

struct SearcherInternal {
const zim::Search* _search;
zim::Search::iterator current_iterator;

SearcherInternal() : _search(NULL) {}
~SearcherInternal()
struct SearcherInternal : zim::SearchResultSet {
explicit SearcherInternal(const zim::SearchResultSet& srs)
: zim::SearchResultSet(srs)
, current_iterator(srs.begin())
{
if (_search != NULL) {
delete _search;
}
}

zim::SearchResultSet::iterator current_iterator;
};

/* Constructor */
Searcher::Searcher()
: internal(new SearcherInternal()),
searchPattern(""),
: searchPattern(""),
estimatedResultCount(0),
resultStart(0),
resultEnd(0)
Expand All @@ -78,7 +74,6 @@ Searcher::Searcher()
/* Destructor */
Searcher::~Searcher()
{
delete internal;
}

bool Searcher::add_reader(Reader* reader)
Expand Down Expand Up @@ -122,13 +117,13 @@ void Searcher::search(const std::string& search,
archives.push_back(*(*current)->getZimArchive());
}
}
zim::Search* search = new zim::Search(archives);
search->set_verbose(verbose);
search->set_query(unaccentedSearch);
search->set_range(resultStart, resultEnd);
internal->_search = search;
internal->current_iterator = internal->_search->begin();
this->estimatedResultCount = internal->_search->get_matches_estimated();
zim::Searcher searcher(archives);
zim::Query query;
query.setQuery(unaccentedSearch, false);
query.setVerbose(verbose);
zim::Search search = searcher.search(query);
internal.reset(new SearcherInternal(search.getResults(resultStart, resultEnd)));
this->estimatedResultCount = search.getEstimatedMatches();
}

return;
Expand Down Expand Up @@ -163,28 +158,28 @@ void Searcher::geo_search(float latitude, float longitude, float distance,
current++) {
archives.push_back(*(*current)->getZimArchive());
}
zim::Search* search = new zim::Search(archives);
search->set_verbose(verbose);
search->set_query("");
search->set_georange(latitude, longitude, distance);
search->set_range(resultStart, resultEnd);
internal->_search = search;
internal->current_iterator = internal->_search->begin();
this->estimatedResultCount = internal->_search->get_matches_estimated();
zim::Searcher searcher(archives);
zim::Query query;
query.setVerbose(verbose);
query.setQuery("", false);
query.setGeorange(latitude, longitude, distance);
zim::Search search = searcher.search(query);
internal.reset(new SearcherInternal(search.getResults(resultStart, resultEnd)));
this->estimatedResultCount = search.getEstimatedMatches();
}


void Searcher::restart_search()
{
if (internal->_search) {
internal->current_iterator = internal->_search->begin();
if (internal.get()) {
internal->current_iterator = internal->begin();
}
}

Result* Searcher::getNextResult()
{
if (internal->_search &&
internal->current_iterator != internal->_search->end()) {
if (internal.get() &&
internal->current_iterator != internal->end()) {
Result* result = new _Result(internal->current_iterator);
internal->current_iterator++;
return result;
Expand Down Expand Up @@ -218,14 +213,13 @@ void Searcher::suggestions(std::string& searchPattern, const bool verbose)
current++) {
archives.push_back(*(*current)->getZimArchive());
}
zim::Search* search = new zim::Search(archives);
search->set_verbose(verbose);
search->set_query(unaccentedSearch);
search->set_range(resultStart, resultEnd);
search->set_suggestion_mode(true);
internal->_search = search;
internal->current_iterator = internal->_search->begin();
this->estimatedResultCount = internal->_search->get_matches_estimated();
zim::Searcher searcher(archives);
zim::Query query;
query.setVerbose(verbose);
query.setQuery(unaccentedSearch, true);
zim::Search search = searcher.search(query);
internal.reset(new SearcherInternal(search.getResults(resultStart, resultEnd)));
this->estimatedResultCount = search.getEstimatedMatches();
}

/* Return the result count estimation */
Expand All @@ -234,7 +228,7 @@ unsigned int Searcher::getEstimatedResultCount()
return this->estimatedResultCount;
}

_Result::_Result(zim::Search::iterator& iterator)
_Result::_Result(zim::SearchResultSet::iterator iterator)
: iterator(iterator)
{
}
Expand Down

0 comments on commit 8d68581

Please sign in to comment.