diff --git a/search/intermediate_result.cpp b/search/intermediate_result.cpp index 219a27070f..cb409027db 100644 --- a/search/intermediate_result.cpp +++ b/search/intermediate_result.cpp @@ -217,7 +217,9 @@ namespace Result PreResult2::GenerateFinalResult( storage::CountryInfoGetter const * pInfo, - CategoriesHolder const * pCat, int8_t lang) const + CategoriesHolder const * pCat, + set const * pTypes, + int8_t lang) const { storage::CountryInfo info; @@ -235,7 +237,7 @@ Result PreResult2::GenerateFinalResult( switch (m_resultType) { case RESULT_FEATURE: - return Result(m_str, info.m_name, info.m_flag, GetFeatureType(pCat, lang) + return Result(m_str, info.m_name, info.m_flag, GetFeatureType(pCat, pTypes, lang) #ifdef DEBUG + ' ' + strings::to_string(static_cast(m_rank)) #endif @@ -357,20 +359,34 @@ string PreResult2::DebugPrint() const return res; } -uint32_t PreResult2::GetBestType() const +uint32_t PreResult2::GetBestType(set const * pPrefferedTypes) const { - /// @todo Need to process all types. + uint32_t t = 0; + + if (pPrefferedTypes) + { + for (size_t i = 0; i < m_types.Size(); ++i) + if (pPrefferedTypes->count(m_types[i]) > 0) + { + t = m_types[i]; + break; + } + } + + if (t == 0) + t = m_types.GetBestType(); - uint32_t t = m_types.GetBestType(); ftype::TruncValue(t, 2); return t; } -string PreResult2::GetFeatureType(CategoriesHolder const * pCat, int8_t lang) const +string PreResult2::GetFeatureType(CategoriesHolder const * pCat, + set const * pTypes, + int8_t lang) const { ASSERT_EQUAL(m_resultType, RESULT_FEATURE, ()); - uint32_t const type = GetBestType(); + uint32_t const type = GetBestType(pTypes); ASSERT_NOT_EQUAL(type, 0, ()); if (pCat) diff --git a/search/intermediate_result.hpp b/search/intermediate_result.hpp index 25aef6a1f0..ea87a2a7f8 100644 --- a/search/intermediate_result.hpp +++ b/search/intermediate_result.hpp @@ -78,8 +78,14 @@ public: // For RESULT_CATEGORY. PreResult2(string const & name, int penalty); + /// @param[in] pInfo Need to get region for result. + /// @param[in] pCat Categories need to display readable type string. + /// @param[in] pTypes Set of preffered types that match input tokens by categories. + /// @param[in] lang Current system language. Result GenerateFinalResult(storage::CountryInfoGetter const * pInfo, - CategoriesHolder const * pCat, int8_t lang) const; + CategoriesHolder const * pCat, + set const * pTypes, + int8_t lang) const; static bool LessRank(PreResult2 const & r1, PreResult2 const & r2); static bool LessDistance(PreResult2 const & r1, PreResult2 const & r2); @@ -114,11 +120,13 @@ private: template friend bool LessViewportDistanceT(T const & r1, T const & r2); template friend bool LessDistanceT(T const & r1, T const & r2); - string GetFeatureType(CategoriesHolder const * pCat, int8_t lang) const; + string GetFeatureType(CategoriesHolder const * pCat, + set const * pTypes, + int8_t lang) const; m2::RectD GetFinalViewport() const; feature::TypesHolder m_types; - uint32_t GetBestType() const; + uint32_t GetBestType(set const * pPrefferedTypes = 0) const; string m_str, m_completionString; diff --git a/search/search_query.cpp b/search/search_query.cpp index f166ce5724..fb2fc37dcf 100644 --- a/search/search_query.cpp +++ b/search/search_query.cpp @@ -474,6 +474,9 @@ void Query::FlushResults(Results & res, void (Results::*pAddFn)(Result const &)) AddPreResult2(maker(*i), indV); } + if (indV.empty()) + return; + RemoveDuplicatingLinear(indV); for (size_t i = 0; i < m_qCount; ++i) @@ -500,6 +503,17 @@ void Query::FlushResults(Results & res, void (Results::*pAddFn)(Result const &)) // sort results according to combined criteria sort(indV.begin(), indV.end()); + // get preffered types to show in results + set prefferedTypes; + if (m_pCategories) + { + for (size_t i = 0; i < m_tokens.size(); ++i) + m_pCategories->ForEachTypeByName(m_tokens[i], MakeInsertFunctor(prefferedTypes)); + + if (!m_prefix.empty()) + m_pCategories->ForEachTypeByName(m_prefix, MakeInsertFunctor(prefferedTypes)); + } + // emit feature results for (size_t i = 0; i < indV.size(); ++i) { @@ -507,7 +521,7 @@ void Query::FlushResults(Results & res, void (Results::*pAddFn)(Result const &)) LOG(LDEBUG, (indV[i])); - (res.*pAddFn)(MakeResult(*(indV[i]))); + (res.*pAddFn)(MakeResult(*(indV[i]), &prefferedTypes)); } } diff --git a/search/search_query.hpp b/search/search_query.hpp index f5b904436b..02fcb1a92c 100644 --- a/search/search_query.hpp +++ b/search/search_query.hpp @@ -131,9 +131,9 @@ private: void GetBestMatchName(FeatureType const & f, uint32_t & penalty, string & name); - inline Result MakeResult(impl::PreResult2 const & r) const + inline Result MakeResult(impl::PreResult2 const & r, set const * pPrefferedTypes = 0) const { - return r.GenerateFinalResult(m_pInfoGetter, m_pCategories, m_currentLang); + return r.GenerateFinalResult(m_pInfoGetter, m_pCategories, pPrefferedTypes, m_currentLang); } Index const * m_pIndex;