From 81b8ae2642d91b1f01089f6543e1201d4e872b6f Mon Sep 17 00:00:00 2001 From: vng Date: Tue, 23 Oct 2012 16:34:03 +0300 Subject: [PATCH] Minor changes in search - better check for empty query. --- search/search_engine.cpp | 16 +++++++++---- search/search_query.cpp | 52 ++++++++++++++++++++-------------------- search/search_query.hpp | 16 +++++++++++-- 3 files changed, 52 insertions(+), 32 deletions(-) diff --git a/search/search_engine.cpp b/search/search_engine.cpp index 078c845d04..8993f1a9ac 100644 --- a/search/search_engine.cpp +++ b/search/search_engine.cpp @@ -220,6 +220,8 @@ void Engine::SearchAsync() } // Initialize query. + m_pQuery->Init(); + bool worldSearch = true; if (params.m_validPos) { @@ -244,14 +246,19 @@ void Engine::SearchAsync() if (params.IsLanguageValid()) m_pQuery->SetInputLanguage(params.m_inputLanguageCode); + m_pQuery->SetQuery(params.m_query); + bool const emptyQuery = m_pQuery->IsEmptyQuery(); + Results res; // Call m_pQuery->IsCanceled() everywhere it needed without storing return value. // This flag can be changed from another thread. + m_pQuery->SearchCoordinates(params.m_query, res); + try { - if (params.m_query.empty()) + if (emptyQuery) { // Search for empty query only around viewport. if (params.m_validPos) @@ -260,7 +267,8 @@ void Engine::SearchAsync() for (size_t i = 0; i < ARRAY_SIZE(arrR); ++i) { res.Clear(); - m_pQuery->SearchAllInViewport(GetViewportRect(params.m_lat, params.m_lon, arrR[i]), res, 3*RESULTS_COUNT); + m_pQuery->SearchAllInViewport(GetViewportRect(params.m_lat, params.m_lon, arrR[i]), + res, 3*RESULTS_COUNT); if (m_pQuery->IsCanceled() || res.GetCount() >= 2*RESULTS_COUNT) break; @@ -268,7 +276,7 @@ void Engine::SearchAsync() } } else - m_pQuery->Search(params.m_query, res); + m_pQuery->Search(res); } catch (Query::CancelException const &) { @@ -280,7 +288,7 @@ void Engine::SearchAsync() params.m_callback(res); // Make additional search in whole mwm when not enough results (only for non-empty query). - if (!params.m_query.empty() && !m_pQuery->IsCanceled() && count < RESULTS_COUNT) + if (!emptyQuery && !m_pQuery->IsCanceled() && count < RESULTS_COUNT) { try { diff --git a/search/search_query.cpp b/search/search_query.cpp index 618abaaf18..f6601b1118 100644 --- a/search/search_query.cpp +++ b/search/search_query.cpp @@ -249,11 +249,14 @@ void Query::UpdateViewportOffsets(MWMVectorT const & mwmInfo, m2::RectD const & #endif } -void Query::InitSearch() +void Query::Init() { m_cancel = false; + m_tokens.clear(); m_prefix.clear(); + + ClearQueues(); } void Query::ClearQueues() @@ -262,10 +265,8 @@ void Query::ClearQueues() m_results[i].clear(); } -void Query::Search(string const & query, Results & res) +void Query::SetQuery(string const & query) { - InitSearch(); - // split input query by tokens and prefix search::Delimiters delims; SplitUniString(NormalizeAndSimplifyString(query), MakeBackInsertFunctor(m_tokens), delims); @@ -282,18 +283,21 @@ void Query::Search(string const & query, Results & res) // assign tokens and prefix to scorer m_keywordsScorer.SetKeywords(m_tokens.data(), m_tokens.size(), &m_prefix); +} - ClearQueues(); - - // Match (lat, lon). +void Query::SearchCoordinates(string const & query, Results & res) const +{ + double lat, lon, latPrec, lonPrec; + if (search::MatchLatLon(query, lat, lon, latPrec, lonPrec)) { - double lat, lon, latPrec, lonPrec; - if (search::MatchLatLon(query, lat, lon, latPrec, lonPrec)) - { - //double const precision = 5.0 * max(0.0001, min(latPrec, lonPrec)); // Min 55 meters - res.AddResult(MakeResult(impl::PreResult2(GetViewport(), m_position, lat, lon))); - } + //double const precision = 5.0 * max(0.0001, min(latPrec, lonPrec)); // Min 55 meters + res.AddResult(MakeResult(impl::PreResult2(GetViewport(), m_position, lat, lon))); } +} + +void Query::Search(Results & res) +{ + ClearQueues(); if (m_cancel) return; SuggestStrings(res); @@ -847,7 +851,7 @@ namespace void Query::Params::ProcessAddressTokens() { // 1. Do simple stuff - erase all number tokens. - // Assume that USA street name numbers endswith "st, nd, rd, th" suffixes. + // Assume that USA street name numbers are end with "st, nd, rd, th" suffixes. vector toErase; ForEachToken(DoStoreNumbers(toErase)); @@ -1219,15 +1223,15 @@ namespace impl bool GetBestLocality(Locality & res) { - LOG(LDEBUG, ("Countries before processing = ", m_localities[1])); - LOG(LDEBUG, ("States before processing = ", m_localities[0])); + //LOG(LDEBUG, ("Countries before processing = ", m_localities[1])); + //LOG(LDEBUG, ("States before processing = ", m_localities[0])); // First, prepare countries info for checking locality. vector regions; AddRegions(1, regions); AddRegions(0, regions); - LOG(LDEBUG, ("Regions after processing = ", regions)); + //LOG(LDEBUG, ("Regions after processing = ", regions)); // Get suitable locality. vector & arr = m_localities[2]; @@ -1481,10 +1485,9 @@ void Query::SearchInMWM(Index::MwmLock const & mwmLock, Params const & params, i TrieRootPrefix(*pLangRoot, edge), filter, categoriesHolder, emitter); - LOG(LDEBUG, ("Lang:", - StringUtf8Multilang::GetLangByCode(static_cast(edge[0])), - "Matched: ", - emitter.GetCount())); + LOG(LDEBUG, ("Country", pMwm->m_name, + "Lang", StringUtf8Multilang::GetLangByCode(static_cast(edge[0])), + "Matched", emitter.GetCount())); emitter.Reset(); } @@ -1584,8 +1587,6 @@ void Query::SearchAllInViewport(m2::RectD const & viewport, Results & res, unsig UpdateViewportOffsets(mwmInfo, viewport, offsets); } - InitSearch(); - vector > indV; impl::PreResult2Maker maker(*this); @@ -1624,6 +1625,8 @@ void Query::SearchAllInViewport(m2::RectD const & viewport, Results & res, unsig void Query::SearchAdditional(Results & res) { + ClearQueues(); + string name[2]; // search in mwm with position ... @@ -1648,10 +1651,7 @@ void Query::SearchAdditional(Results & res) string const s = mwmLock.GetCountryName(); if (s == name[0] || s == name[1]) - { - ClearQueues(); SearchInMWM(mwmLock, params); - } } FlushResults(res, &Results::AddResultCheckExisting); diff --git a/search/search_query.hpp b/search/search_query.hpp index 41da7c8514..854f9fccc7 100644 --- a/search/search_query.hpp +++ b/search/search_query.hpp @@ -63,6 +63,8 @@ public: size_t resultsNeeded = 10); ~Query(); + void Init(); + void SetViewport(m2::RectD viewport[], size_t count); static const int empty_pos_value = -1000; @@ -75,9 +77,16 @@ public: void SetInputLanguage(int8_t lang); int8_t GetPrefferedLanguage() const; - void Search(string const & query, Results & res); + void SetQuery(string const & query); + inline bool IsEmptyQuery() const { return (m_prefix.empty() && m_tokens.empty()); } + + /// @name Different search functions. + //@{ + void SearchCoordinates(string const & query, Results & res) const; + void Search(Results & res); void SearchAllInViewport(m2::RectD const & viewport, Results & res, unsigned int resultsNeeded = 30); void SearchAdditional(Results & res); + //@} void ClearCaches(); @@ -85,6 +94,9 @@ public: inline bool IsCanceled() const { return m_cancel; } struct CancelException {}; + /// @name This stuff is public for implementation classes in search_query.cpp + /// Do not use it in client code. + //@{ typedef trie::ValueReader::ValueType TrieValueT; struct Params @@ -114,6 +126,7 @@ public: void FillLanguages(Query const & q); }; + //@} private: friend class impl::FeatureLoader; @@ -121,7 +134,6 @@ private: friend class impl::PreResult2Maker; friend class impl::DoFindLocality; - void InitSearch(); void ClearQueues(); typedef vector MWMVectorT;