[search] Exclude whitespaces of search requests on savings.

This commit is contained in:
Lev Dragunov 2015-09-17 12:30:32 +03:00 committed by Alex Zolotarev
parent 1f36eb0c15
commit aded764baa
2 changed files with 28 additions and 1 deletions

View file

@ -8,6 +8,7 @@
#include "coding/write_to_sink.hpp"
#include "base/logging.hpp"
#include "base/string_utils.hpp"
#include "std/limits.hpp"
@ -72,8 +73,17 @@ QuerySaver::QuerySaver()
void QuerySaver::Add(TSearchRequest const & query)
{
TSearchRequest trimmedQuery(query);
strings::Trim(trimmedQuery.first);
strings::Trim(trimmedQuery.second);
auto trimmedComparator = [&trimmedQuery](TSearchRequest request)
{
strings::Trim(request.first);
strings::Trim(request.second);
return trimmedQuery == request;
};
// Remove items if needed.
auto const it = find(m_topQueries.begin(), m_topQueries.end(), query);
auto const it = find_if(m_topQueries.begin(), m_topQueries.end(), trimmedComparator);
if (it != m_topQueries.end())
m_topQueries.erase(it);
else if (m_topQueries.size() >= kMaxSuggestionsCount)

View file

@ -110,4 +110,21 @@ UNIT_TEST(QuerySaverPersistanceStore)
saver.Clear();
}
}
UNIT_TEST(QuerySaverTrimRequestTest)
{
QuerySaver saver;
saver.Clear();
search::QuerySaver::TSearchRequest const rec1("RU_ru" ,"test record1");
search::QuerySaver::TSearchRequest const rec2("RU_ru" ,"test record1 ");
saver.Add(rec1);
saver.Add(rec2);
list<QuerySaver::TSearchRequest> const & result = saver.Get();
TEST_EQUAL(result.size(), 1, ());
TEST_EQUAL(result.front(), rec2, ());
saver.Clear();
}
} // namespace search