test: added SearchIndexTestSuite and refined SearchIndex API
This commit is contained in:
@@ -48,11 +48,6 @@ SearchIndex::SearchNode::~SearchNode()
|
||||
{
|
||||
}
|
||||
|
||||
void SearchIndex::SearchNode::clear()
|
||||
{
|
||||
m_nodes.clear();
|
||||
}
|
||||
|
||||
const std::string& SearchIndex::SearchNode::getName() const
|
||||
{
|
||||
return m_name;
|
||||
@@ -85,6 +80,11 @@ Id SearchIndex::SearchNode::getFirstTokenId() const
|
||||
return 0;
|
||||
}
|
||||
|
||||
const std::set<Id>& SearchIndex::SearchNode::getTokenIds() const
|
||||
{
|
||||
return m_tokenIds;
|
||||
}
|
||||
|
||||
void SearchIndex::SearchNode::addTokenId(Id tokenId)
|
||||
{
|
||||
m_tokenIds.insert(tokenId);
|
||||
@@ -166,17 +166,14 @@ std::vector<SearchIndex::SearchMatch> SearchIndex::SearchNode::findFuzzyMatches(
|
||||
FuzzySet ordered(&fncomp);
|
||||
for (std::shared_ptr<SearchNode> n: m_nodes)
|
||||
{
|
||||
FuzzyMap m = n->fuzzyMatches(query, 0, 0, 0);
|
||||
FuzzyMap m = n->fuzzyMatchRecursive(query, 0, 0, 0);
|
||||
ordered.insert(m.begin(), m.end());
|
||||
}
|
||||
|
||||
std::stringstream ss;
|
||||
ss << std::endl << ordered.size() << " matches for \"" << query << "\":" << std::endl;
|
||||
for (FuzzySetIterator it = ordered.begin(); it != ordered.end(); it++)
|
||||
{
|
||||
SearchMatch match = it->second->fuzzyMatchData(query, this);
|
||||
result.push_back(match);
|
||||
match.print(ss);
|
||||
|
||||
if (it->first != match.weight)
|
||||
{
|
||||
@@ -184,12 +181,10 @@ std::vector<SearchIndex::SearchMatch> SearchIndex::SearchNode::findFuzzyMatches(
|
||||
}
|
||||
}
|
||||
|
||||
LOG_INFO(ss.str());
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
SearchIndex::SearchNode::FuzzyMap SearchIndex::SearchNode::fuzzyMatches(
|
||||
SearchIndex::SearchNode::FuzzyMap SearchIndex::SearchNode::fuzzyMatchRecursive(
|
||||
const std::string& query, size_t pos, size_t weight, size_t size) const
|
||||
{
|
||||
FuzzyMap result;
|
||||
@@ -212,7 +207,7 @@ SearchIndex::SearchNode::FuzzyMap SearchIndex::SearchNode::fuzzyMatches(
|
||||
|
||||
for (std::shared_ptr<SearchNode> n: m_nodes)
|
||||
{
|
||||
FuzzyMap m = n->fuzzyMatches(query, pos, weight, size + m_name.size() + SearchIndex::DELIMITER.size());
|
||||
FuzzyMap m = n->fuzzyMatchRecursive(query, pos, weight, size + m_name.size() + SearchIndex::DELIMITER.size());
|
||||
result.insert(m.begin(), m.end());
|
||||
}
|
||||
|
||||
@@ -325,15 +320,9 @@ std::deque<const SearchIndex::SearchNode*> SearchIndex::SearchNode::getNodesToPa
|
||||
std::deque<const SearchIndex::SearchNode*> nodes;
|
||||
|
||||
const SearchNode* node = this;
|
||||
while (node->m_nameId)
|
||||
while (node->m_nameId && node != parent)
|
||||
{
|
||||
nodes.push_front(node);
|
||||
|
||||
if (node == parent)
|
||||
{
|
||||
break;
|
||||
}
|
||||
|
||||
node = node->m_parent;
|
||||
}
|
||||
|
||||
@@ -341,6 +330,17 @@ std::deque<const SearchIndex::SearchNode*> SearchIndex::SearchNode::getNodesToPa
|
||||
}
|
||||
|
||||
|
||||
void SearchIndex::logMatches(const std::vector<SearchIndex::SearchMatch>& matches, const std::string& query)
|
||||
{
|
||||
std::stringstream ss;
|
||||
ss << std::endl << matches.size() << " matches for \"" << query << "\":" << std::endl;
|
||||
for (const SearchIndex::SearchMatch& match : matches)
|
||||
{
|
||||
match.print(ss);
|
||||
}
|
||||
LOG_INFO(ss.str());
|
||||
}
|
||||
|
||||
SearchIndex::SearchIndex()
|
||||
: m_root(nullptr, DELIMITER, 0)
|
||||
{
|
||||
@@ -352,7 +352,7 @@ SearchIndex::~SearchIndex()
|
||||
|
||||
void SearchIndex::clear()
|
||||
{
|
||||
m_root.clear();
|
||||
m_root.m_nodes.clear();
|
||||
}
|
||||
|
||||
SearchIndex::SearchNode* SearchIndex::addNode(const std::string& fullName)
|
||||
@@ -379,32 +379,22 @@ SearchIndex::SearchNode* SearchIndex::getNode(const std::string& fullName) const
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
std::vector<std::string> SearchIndex::findFuzzyMatches(const std::string& query) const
|
||||
std::vector<SearchIndex::SearchMatch> SearchIndex::findFuzzyMatches(const std::string& query) const
|
||||
{
|
||||
std::vector<SearchMatch> matches;
|
||||
std::vector<std::string> pieces = utility::split<std::vector<std::string>>(query, '\"');
|
||||
std::vector<std::string> names = utility::split<std::vector<std::string>>(query, '\"');
|
||||
|
||||
if (pieces.size() == 3 && pieces[0].size() == 0)
|
||||
if (names.size() == 3 && names[0].size() == 0)
|
||||
{
|
||||
SearchNode* node = getNode(pieces[1]);
|
||||
SearchNode* node = getNode(names[1]);
|
||||
if (!node)
|
||||
{
|
||||
LOG_ERROR_STREAM(<< "Couldn't find node with name " << pieces[1] << " in the SearchIndex.");
|
||||
LOG_ERROR_STREAM(<< "Couldn't find node with name " << names[1] << " in the SearchIndex.");
|
||||
}
|
||||
|
||||
matches = node->findFuzzyMatches(pieces[2]);
|
||||
}
|
||||
else
|
||||
{
|
||||
matches = m_root.findFuzzyMatches(query);
|
||||
return node->findFuzzyMatches(names[2]);
|
||||
}
|
||||
|
||||
std::vector<std::string> names;
|
||||
for (const SearchMatch& match : matches)
|
||||
{
|
||||
names.push_back(match.node->getFullName());
|
||||
}
|
||||
return names;
|
||||
return m_root.findFuzzyMatches(query);
|
||||
}
|
||||
|
||||
const std::string SearchIndex::DELIMITER = "::";
|
||||
|
||||
@@ -37,26 +37,27 @@ public:
|
||||
SearchNode(SearchNode* parent, const std::string& name, Id nameId);
|
||||
~SearchNode();
|
||||
|
||||
void clear();
|
||||
|
||||
const std::string& getName() const;
|
||||
std::string getFullName() const;
|
||||
|
||||
Id getNameId() const;
|
||||
|
||||
Id getFirstTokenId() const;
|
||||
const std::set<Id>& getTokenIds() const;
|
||||
void addTokenId(Id tokenId);
|
||||
|
||||
SearchNode* getParent() const;
|
||||
std::deque<SearchIndex::SearchNode*> getParentsWithoutTokenId();
|
||||
|
||||
private:
|
||||
// Accessed by SearchIndex
|
||||
std::shared_ptr<SearchNode> addNodeRecursive(std::deque<Id>* nameIds);
|
||||
std::shared_ptr<SearchNode> getNodeRecursive(std::deque<Id>* nameIds) const;
|
||||
|
||||
std::vector<SearchIndex::SearchMatch> findFuzzyMatches(const std::string& query) const;
|
||||
|
||||
private:
|
||||
FuzzyMap fuzzyMatches(const std::string& query, size_t pos, size_t weight, size_t size) const;
|
||||
friend class SearchIndex;
|
||||
|
||||
FuzzyMap fuzzyMatchRecursive(const std::string& query, size_t pos, size_t weight, size_t size) const;
|
||||
std::pair<size_t, size_t> fuzzyMatch(
|
||||
const std::string query, size_t start, size_t size, std::vector<size_t>* indices = nullptr) const;
|
||||
SearchMatch fuzzyMatchData(const std::string& query, const SearchNode* parent) const;
|
||||
@@ -73,6 +74,8 @@ public:
|
||||
const Id m_nameId;
|
||||
};
|
||||
|
||||
static void logMatches(const std::vector<SearchIndex::SearchMatch>& matches, const std::string& query);
|
||||
|
||||
SearchIndex();
|
||||
virtual ~SearchIndex();
|
||||
|
||||
@@ -81,7 +84,7 @@ public:
|
||||
SearchNode* addNode(const std::string& fullName);
|
||||
SearchNode* getNode(const std::string& fullName) const;
|
||||
|
||||
std::vector<std::string> findFuzzyMatches(const std::string& query) const;
|
||||
std::vector<SearchIndex::SearchMatch> findFuzzyMatches(const std::string& query) const;
|
||||
|
||||
static const std::string DELIMITER;
|
||||
|
||||
|
||||
@@ -338,7 +338,13 @@ std::string Storage::getNameForNodeWithId(Id id) const
|
||||
|
||||
std::vector<std::string> Storage::getNamesForNodesWithNamePrefix(const std::string& prefix) const
|
||||
{
|
||||
return m_index.findFuzzyMatches(prefix);
|
||||
std::vector<std::string> names;
|
||||
std::vector<SearchIndex::SearchMatch> matches = m_index.findFuzzyMatches(prefix);
|
||||
for (const SearchIndex::SearchMatch& match : matches)
|
||||
{
|
||||
names.push_back(match.node->getFullName());
|
||||
}
|
||||
return names;
|
||||
}
|
||||
|
||||
std::vector<Id> Storage::getIdsOfNeighbours(const Id id) const
|
||||
@@ -526,7 +532,7 @@ std::vector<Id> Storage::getTokenIdsForQuery(std::string query) const
|
||||
|
||||
LOG_INFO_STREAM(<< '\n' << tree << '\n' << outGraph);
|
||||
|
||||
m_index.findFuzzyMatches(query);
|
||||
SearchIndex::logMatches(m_index.findFuzzyMatches(query), query);
|
||||
|
||||
return outGraph.getTokenIds();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user