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) {