Updates to EntrySearcher

* Only search custom named attributes
* Search attribute values in addition to keys
* Optimize search process
This commit is contained in:
Jonathan White 2019-02-22 17:17:51 -05:00
parent 5cf50d9fae
commit d0d1b25e5c
5 changed files with 48 additions and 21 deletions

View file

@ -32,21 +32,38 @@ QList<Entry*> EntrySearcher::search(const QString& searchString, const Group* ba
{
Q_ASSERT(baseGroup);
parseSearchTerms(searchString);
return repeat(baseGroup, forceSearch);
}
QList<Entry*> EntrySearcher::repeat(const Group* baseGroup, bool forceSearch)
{
Q_ASSERT(baseGroup);
QList<Entry*> results;
for (const auto group : baseGroup->groupsRecursive(true)) {
if (forceSearch || group->resolveSearchingEnabled()) {
results.append(searchEntries(searchString, group->entries()));
for (auto* entry : group->entries()) {
if (searchEntryImpl(entry)) {
results.append(entry);
}
}
}
}
return results;
}
QList<Entry*> EntrySearcher::searchEntries(const QString& searchString, const QList<Entry*>& entries)
{
parseSearchTerms(searchString);
return repeatEntries(entries);
}
QList<Entry*> EntrySearcher::repeatEntries(const QList<Entry*>& entries)
{
QList<Entry*> results;
for (Entry* entry : entries) {
if (searchEntryImpl(searchString, entry)) {
for (auto* entry : entries) {
if (searchEntryImpl(entry)) {
results.append(entry);
}
}
@ -63,16 +80,15 @@ bool EntrySearcher::isCaseSensitive()
return m_caseSensitive;
}
bool EntrySearcher::searchEntryImpl(const QString& searchString, Entry* entry)
bool EntrySearcher::searchEntryImpl(Entry* entry)
{
// Pre-load in case they are needed
auto attributes = QStringList(entry->attributes()->keys());
auto attributes_keys = entry->attributes()->customKeys();
auto attributes = QStringList(attributes_keys + entry->attributes()->values(attributes_keys));
auto attachments = QStringList(entry->attachments()->keys());
bool found;
auto searchTerms = parseSearchTerms(searchString);
for (const auto& term : searchTerms) {
for (const auto& term : m_searchTerms) {
switch (term->field) {
case Field::Title:
found = term->regex.match(entry->resolvePlaceholder(entry->title())).hasMatch();
@ -112,10 +128,9 @@ bool EntrySearcher::searchEntryImpl(const QString& searchString, Entry* entry)
return true;
}
QList<QSharedPointer<EntrySearcher::SearchTerm>> EntrySearcher::parseSearchTerms(const QString& searchString)
void EntrySearcher::parseSearchTerms(const QString& searchString)
{
auto terms = QList<QSharedPointer<SearchTerm>>();
m_searchTerms.clear();
auto results = m_termParser.globalMatch(searchString);
while (results.hasNext()) {
auto result = results.next();
@ -165,8 +180,6 @@ QList<QSharedPointer<EntrySearcher::SearchTerm>> EntrySearcher::parseSearchTerms
}
}
terms.append(term);
m_searchTerms.append(term);
}
return terms;
}