From b9ac7084ac5a3b8828038346c357ab566eec8c65 Mon Sep 17 00:00:00 2001 From: Matthieu Gautier Date: Tue, 14 Nov 2017 17:32:06 +0100 Subject: [PATCH] =?UTF-8?q?Add=20small=20API=C2=A0to=20do=20geo=20query.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This is a small quick and dirty API to do geo query. It is not possible with this API to do a query search and a geo search. It's either one or the other. We should think about a better global API to do searching and provide both of them in the same time (libzim does it). --- include/searcher.h | 4 ++++ src/searcher.cpp | 57 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 61 insertions(+) diff --git a/include/searcher.h b/include/searcher.h index bc3bcbd9d..3a1950cab 100644 --- a/include/searcher.h +++ b/include/searcher.h @@ -67,6 +67,10 @@ class Searcher unsigned int resultStart, unsigned int resultEnd, const bool verbose = false); + void geo_search(float latitude, float longitude, float distance, + unsigned int resultStart, + unsigned int resultEnd, + const bool verbose = false); void suggestions(std::string& search, const bool verbose = false); Result* getNextResult(); void restart_search(); diff --git a/src/searcher.cpp b/src/searcher.cpp index 8c5a27ad8..ca8f8e415 100644 --- a/src/searcher.cpp +++ b/src/searcher.cpp @@ -176,6 +176,63 @@ void Searcher::search(std::string& search, return; } + +void Searcher::geo_search(float latitude, float longitude, float distance, + unsigned int resultStart, + unsigned int resultEnd, + const bool verbose) +{ + this->reset(); + + if (verbose == true) { + cout << "Performing geo query `" << distance << "&(" << latitude << ";" << longitude << ")'" << endl; + } + + /* If resultEnd & resultStart inverted */ + if (resultStart > resultEnd) { + resultEnd += resultStart; + resultStart = resultEnd - resultStart; + resultEnd -= resultStart; + } + + /* Try to find results */ + if (resultStart == resultEnd) { + return; + } + + if (internal->_xapianSearcher) { + return; + } + + /* Avoid big researches */ + this->resultCountPerPage = resultEnd - resultStart; + if (this->resultCountPerPage > MAX_SEARCH_LEN) { + resultEnd = resultStart + MAX_SEARCH_LEN; + this->resultCountPerPage = MAX_SEARCH_LEN; + } + + /* Perform the search */ + std::ostringstream oss; + oss << "Articles located less than " << distance << " meters of " << latitude << ";" << longitude; + this->searchPattern = oss.str(); + this->resultStart = resultStart; + this->resultEnd = resultEnd; + + std::vector zims; + for (auto current = this->readers.begin(); current != this->readers.end(); + current++) { + zims.push_back((*current)->getZimFileHandler()); + } + zim::Search* search = new zim::Search(zims); + 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(); +} + + void Searcher::restart_search() { if (internal->_xapianSearcher) {