[search] Added an option to disable suggests.

This commit is contained in:
Yuri Gorshenin 2016-03-02 15:41:02 +03:00 committed by Sergey Yershov
parent a7600f33a1
commit dcc24d01d6
11 changed files with 96 additions and 29 deletions

View file

@ -187,6 +187,7 @@ extern "C"
params.SetInputLocale(ReplaceDeprecatedLanguageCode(jni::ToNativeString(env, lang)));
params.SetForceSearch(true);
params.SetMode(search::Mode::World);
params.DisableSuggests();
params.m_onResults = bind(&OnResults, _1, timestamp, false /* isMapAndTable */, false /* hasPosition */, 0.0, 0.0);
g_framework->NativeFramework()->Search(params);

View file

@ -134,6 +134,7 @@ using namespace storage;
{
__weak auto weakSelf = self;
m_searchParams.SetMode(search::Mode::World);
m_searchParams.DisableSuggests();
m_searchParams.m_onResults = ^(search::Results const & results)
{
__strong auto self = weakSelf;

View file

@ -77,29 +77,40 @@ inline shared_ptr<trie::Iterator<ValueList<TValue>>> MoveTrieIteratorToString(
namespace
{
bool CheckMatchString(strings::UniChar const * rootPrefix,
size_t rootPrefixSize,
strings::UniString & s)
bool CheckMatchString(strings::UniChar const * rootPrefix, size_t rootPrefixSize,
strings::UniString & s, bool prefix)
{
if (rootPrefixSize == 0)
return true;
if (prefix && s.size() < rootPrefixSize &&
StartsWith(rootPrefix, rootPrefix + rootPrefixSize, s.begin(), s.end()))
{
if (rootPrefixSize > 0)
{
if (s.size() < rootPrefixSize ||
!StartsWith(s.begin(), s.end(), rootPrefix, rootPrefix + rootPrefixSize))
return false;
s = strings::UniString(s.begin() + rootPrefixSize, s.end());
}
// In the case of prefix match query may be a prefix of the root
// label string. In this case we continue processing as if the
// string is equal to root label.
s.clear();
return true;
}
if (s.size() >= rootPrefixSize &&
StartsWith(s.begin(), s.end(), rootPrefix, rootPrefix + rootPrefixSize))
{
// In both (prefix and not-prefix) cases when string has root label
// as a prefix, we continue processing.
s = strings::UniString(s.begin() + rootPrefixSize, s.end());
return true;
}
return false;
}
} // namespace
template <typename TValue, typename TF>
void FullMatchInTrie(trie::Iterator<ValueList<TValue>> const & trieRoot,
strings::UniChar const * rootPrefix, size_t rootPrefixSize,
strings::UniString s, TF & f)
{
if (!CheckMatchString(rootPrefix, rootPrefixSize, s))
if (!CheckMatchString(rootPrefix, rootPrefixSize, s, false /* prefix */))
return;
size_t symbolsMatched = 0;
@ -125,7 +136,7 @@ void PrefixMatchInTrie(trie::Iterator<ValueList<TValue>> const & trieRoot,
strings::UniChar const * rootPrefix, size_t rootPrefixSize,
strings::UniString s, TF & f)
{
if (!CheckMatchString(rootPrefix, rootPrefixSize, s))
if (!CheckMatchString(rootPrefix, rootPrefixSize, s, true /* prefix */))
return;
using TIterator = trie::Iterator<ValueList<TValue>>;

View file

@ -8,7 +8,11 @@
namespace search
{
SearchParams::SearchParams()
: m_searchRadiusM(-1.0), m_mode(Mode::Everywhere), m_forceSearch(false), m_validPos(false)
: m_searchRadiusM(-1.0)
, m_mode(Mode::Everywhere)
, m_forceSearch(false)
, m_validPos(false)
, m_suggestsEnabled(true)
{
}

View file

@ -31,21 +31,25 @@ namespace search
inline Mode GetMode() const { return m_mode; }
void SetPosition(double lat, double lon);
bool IsValidPosition() const { return m_validPos; }
bool IsSearchAroundPosition() const
inline bool IsValidPosition() const { return m_validPos; }
inline bool IsSearchAroundPosition() const
{
return (m_searchRadiusM > 0 && IsValidPosition());
}
void SetSearchRadiusMeters(double radiusM) { m_searchRadiusM = radiusM; }
inline void SetSearchRadiusMeters(double radiusM) { m_searchRadiusM = radiusM; }
bool GetSearchRect(m2::RectD & rect) const;
/// @param[in] locale can be "fr", "en-US", "ru_RU" etc.
void SetInputLocale(string const & locale) { m_inputLocale = locale; }
inline void SetInputLocale(string const & locale) { m_inputLocale = locale; }
inline void DisableSuggests() { m_suggestsEnabled = false; }
inline void EnableSuggests() { m_suggestsEnabled = true; }
inline bool SuggestsEnabled() const { return m_suggestsEnabled; }
bool IsEqualCommon(SearchParams const & rhs) const;
void Clear() { m_query.clear(); }
inline void Clear() { m_query.clear(); }
public:
TOnStarted m_onStarted;
@ -61,6 +65,8 @@ namespace search
private:
double m_searchRadiusM;
Mode m_mode;
bool m_forceSearch, m_validPos;
bool m_forceSearch;
bool m_validPos;
bool m_suggestsEnabled;
};
} // namespace search

View file

@ -294,6 +294,7 @@ void Engine::DoSearch(SearchParams const & params, m2::RectD const & viewport,
processor.SetPosition(viewport.Center());
processor.SetMode(params.GetMode());
processor.EnableSuggests(params.SuggestsEnabled());
// This flag is needed for consistency with old search algorithm
// only. It will be gone when we remove old search code.

View file

@ -335,3 +335,30 @@ UNIT_CLASS_TEST(SearchQueryV2Test, SearchByName)
TEST(ResultsMatch("london", search::Mode::Everywhere, rules), ());
}
}
UNIT_CLASS_TEST(SearchQueryV2Test, DisableSuggests)
{
TestCity london1(m2::PointD(1, 1), "London", "en", 100 /* rank */);
TestCity london2(m2::PointD(-1, -1), "London", "en", 100 /* rank */);
auto worldId = BuildMwm("testWorld", feature::DataHeader::world, [&](TestMwmBuilder & builder)
{
builder.Add(london1);
builder.Add(london2);
});
RegisterCountry("Wonderland", m2::RectD(m2::PointD(-2, -2), m2::PointD(2, 2)));
SetViewport(m2::RectD(m2::PointD(0.5, 0.5), m2::PointD(1.5, 1.5)));
{
search::SearchParams params;
params.m_query = "londo";
params.m_inputLocale = "en";
params.SetMode(search::Mode::World);
params.DisableSuggests();
TestSearchRequest request(m_engine, params, m_viewport);
request.Wait();
TRules rules = {ExactMatch(worldId, london1), ExactMatch(worldId, london2)};
TEST(MatchResults(m_engine, rules, request.Results()), ());
}
}

View file

@ -204,6 +204,7 @@ Query::Query(Index & index, CategoriesHolder const & categories, vector<Suggest>
, m_position(0, 0)
, m_mode(Mode::Everywhere)
, m_worldSearch(true)
, m_suggestsEnabled(true)
, m_keepHouseNumberInQuery(false)
{
// Results queue's initialization.
@ -916,7 +917,7 @@ void Query::GetSuggestion(string const & name, string & suggest) const
template <class T>
void Query::ProcessSuggestions(vector<T> & vec, Results & res) const
{
if (m_prefix.empty())
if (m_prefix.empty() || !m_suggestsEnabled)
return;
int added = 0;
@ -1729,7 +1730,7 @@ void Query::SearchInMwms(TMWMVector const & mwmsInfo, SearchQueryParams const &
void Query::SuggestStrings(Results & res)
{
if (m_prefix.empty())
if (m_prefix.empty() || !m_suggestsEnabled)
return;
int8_t arrLocales[3];
int const localesCount = GetCategoryLocales(arrLocales);

View file

@ -91,6 +91,7 @@ public:
inline void SetMode(Mode mode) { m_mode = mode; }
inline void SetSearchInWorld(bool b) { m_worldSearch = b; }
inline void EnableSuggests(bool enabled) { m_suggestsEnabled = enabled; }
/// Suggestions language code, not the same as we use in mwm data
int8_t m_inputLocaleCode, m_currentLocaleCode;
@ -243,6 +244,7 @@ protected:
m2::PointD m_position;
Mode m_mode;
bool m_worldSearch;
bool m_suggestsEnabled;
Retrieval m_retrieval;
/// @name Get ranking params.

View file

@ -1,6 +1,5 @@
#include "search/search_tests_support/test_search_request.hpp"
#include "search/params.hpp"
#include "search/search_tests_support/test_search_engine.hpp"
#include "base/logging.hpp"
@ -11,14 +10,19 @@ namespace tests_support
{
TestSearchRequest::TestSearchRequest(TestSearchEngine & engine, string const & query,
string const & locale, Mode mode, m2::RectD const & viewport)
: m_done(false)
{
search::SearchParams params;
SearchParams params;
params.m_query = query;
params.m_inputLocale = locale;
params.m_onStarted = bind(&TestSearchRequest::OnStarted, this);
params.m_onResults = bind(&TestSearchRequest::OnResults, this, _1);
params.SetMode(mode);
SetUpCallbacks(params);
engine.Search(params, viewport);
}
TestSearchRequest::TestSearchRequest(TestSearchEngine & engine, SearchParams params,
m2::RectD const & viewport)
{
SetUpCallbacks(params);
engine.Search(params, viewport);
}
@ -45,6 +49,12 @@ vector<search::Result> const & TestSearchRequest::Results() const
return m_results;
}
void TestSearchRequest::SetUpCallbacks(SearchParams & params)
{
params.m_onStarted = bind(&TestSearchRequest::OnStarted, this);
params.m_onResults = bind(&TestSearchRequest::OnResults, this, _1);
}
void TestSearchRequest::OnStarted()
{
lock_guard<mutex> lock(m_mu);

View file

@ -27,6 +27,8 @@ public:
TestSearchRequest(TestSearchEngine & engine, string const & query, string const & locale,
Mode mode, m2::RectD const & viewport);
TestSearchRequest(TestSearchEngine & engine, SearchParams params, m2::RectD const & viewport);
void Wait();
// Call these functions only after call to Wait().
@ -34,6 +36,7 @@ public:
vector<search::Result> const & Results() const;
private:
void SetUpCallbacks(SearchParams & params);
void OnStarted();
void OnResults(search::Results const & results);
@ -41,7 +44,7 @@ private:
mutable mutex m_mu;
vector<search::Result> m_results;
bool m_done;
bool m_done = false;
my::Timer m_timer;
steady_clock::duration m_startTime;