diff --git a/src/lib/component/controller/SearchController.cpp b/src/lib/component/controller/SearchController.cpp index 635295dc..6ae4973f 100644 --- a/src/lib/component/controller/SearchController.cpp +++ b/src/lib/component/controller/SearchController.cpp @@ -71,8 +71,16 @@ void SearchController::handleMessage(MessageSearchAutocomplete* message) { TRACE("search autocomplete"); + SearchView* view = getView(); + + // Don't autocomplete if autocompletion request is not up-to-date anymore + if (message->query != view->getQuery()) + { + return; + } + LOG_INFO("autocomplete string: \"" + message->query + "\""); - getView()->setAutocompletionList(m_storageAccess->getAutocompletionMatches(message->query)); + view->setAutocompletionList(m_storageAccess->getAutocompletionMatches(message->query)); } void SearchController::handleMessage(MessageSearchFullText* message) diff --git a/src/lib/component/view/SearchView.h b/src/lib/component/view/SearchView.h index 574c01ab..31939b57 100644 --- a/src/lib/component/view/SearchView.h +++ b/src/lib/component/view/SearchView.h @@ -15,6 +15,8 @@ public: virtual std::string getName() const; + virtual std::string getQuery() const = 0; + virtual void setMatches(const std::vector& matches) = 0; virtual void setFocus() = 0; diff --git a/src/lib/data/PersistentStorage.cpp b/src/lib/data/PersistentStorage.cpp index 4aa057bf..212633a8 100644 --- a/src/lib/data/PersistentStorage.cpp +++ b/src/lib/data/PersistentStorage.cpp @@ -503,9 +503,11 @@ std::vector PersistentStorage::getAutocompletionMatches(const std:: // search in indices size_t maxResultsCount = 100; + size_t maxBestScoredResultsLength = 100; + std::vector results; utility::append(results, m_commandIndex.search(query, 0)); - utility::append(results, m_elementIndex.search(query, maxResultsCount, 100)); + utility::append(results, m_elementIndex.search(query, maxResultsCount, maxBestScoredResultsLength)); utility::append(results, m_fileIndex.search(query, 20)); // fetch StorageNodes for node ids @@ -556,6 +558,7 @@ std::vector PersistentStorage::getAutocompletionMatches(const std:: } match.name = result.text; + match.text = result.text; match.indices = result.indices; match.score = result.score; @@ -585,7 +588,9 @@ std::vector PersistentStorage::getAutocompletionMatches(const std:: // rescore match if (idx && match.indices.size()) { - SearchResult newResult = SearchIndex::rescoreText(match.name, match.text, match.indices, match.score); + SearchResult newResult = + SearchIndex::rescoreText(match.name, match.text, match.indices, match.score, maxBestScoredResultsLength); + match.score = newResult.score; match.indices = newResult.indices; } diff --git a/src/lib/data/search/SearchIndex.cpp b/src/lib/data/search/SearchIndex.cpp index 79afebc5..44813a2c 100644 --- a/src/lib/data/search/SearchIndex.cpp +++ b/src/lib/data/search/SearchIndex.cpp @@ -464,6 +464,11 @@ SearchResult SearchIndex::rescoreText( textIndices.push_back(idx - (fulltext.size() - text.size())); } } + // ignore rescoring if result is too long + else if (maxBestScoredLength > 0 && fulltext.size() > maxBestScoredLength * 2) + { + return result; + } // try if match is within text else { diff --git a/src/lib/data/search/SearchIndex.h b/src/lib/data/search/SearchIndex.h index 902b1efb..c08ed5bf 100644 --- a/src/lib/data/search/SearchIndex.h +++ b/src/lib/data/search/SearchIndex.h @@ -78,7 +78,7 @@ public: const std::string& text, const std::vector& indices, int score, - size_t maxBestScoredLength = 0); + size_t maxBestScoredLength); private: std::vector> m_nodes; diff --git a/src/lib_gui/qt/element/QtSearchBar.cpp b/src/lib_gui/qt/element/QtSearchBar.cpp index 36e4a16a..bd99920c 100644 --- a/src/lib_gui/qt/element/QtSearchBar.cpp +++ b/src/lib_gui/qt/element/QtSearchBar.cpp @@ -66,6 +66,11 @@ QSize QtSearchBar::sizeHint() const return QSize(400, 100); } +QString QtSearchBar::query() const +{ + return m_searchBox->text(); +} + void QtSearchBar::setMatches(const std::vector& matches) { m_searchBox->setMatches(matches); diff --git a/src/lib_gui/qt/element/QtSearchBar.h b/src/lib_gui/qt/element/QtSearchBar.h index 32f66729..b9046add 100644 --- a/src/lib_gui/qt/element/QtSearchBar.h +++ b/src/lib_gui/qt/element/QtSearchBar.h @@ -22,6 +22,8 @@ public: virtual QSize sizeHint() const; + QString query() const; + void setMatches(const std::vector& matches); void setFocus(); void findFulltext(); diff --git a/src/lib_gui/qt/element/QtSmartSearchBox.cpp b/src/lib_gui/qt/element/QtSmartSearchBox.cpp index 22caa824..ad76cf05 100644 --- a/src/lib_gui/qt/element/QtSmartSearchBox.cpp +++ b/src/lib_gui/qt/element/QtSmartSearchBox.cpp @@ -895,7 +895,7 @@ void QtSmartSearchBox::clearLineEdit() void QtSmartSearchBox::requestAutoCompletions() { - if (text().size()) + if (text().size() && !text().startsWith(SearchMatch::FULLTEXT_SEARCH_CHARACTER)) { MessageSearchAutocomplete(text().toStdString()).dispatch(); } diff --git a/src/lib_gui/qt/view/QtSearchView.cpp b/src/lib_gui/qt/view/QtSearchView.cpp index c4f8217c..42482caf 100644 --- a/src/lib_gui/qt/view/QtSearchView.cpp +++ b/src/lib_gui/qt/view/QtSearchView.cpp @@ -37,6 +37,11 @@ void QtSearchView::refreshView() m_refreshViewFunctor(); } +std::string QtSearchView::getQuery() const +{ + return m_widget->query().toStdString(); +} + void QtSearchView::setMatches(const std::vector& matches) { m_setMatchesFunctor(matches); diff --git a/src/lib_gui/qt/view/QtSearchView.h b/src/lib_gui/qt/view/QtSearchView.h index 8085f17c..3db91596 100644 --- a/src/lib_gui/qt/view/QtSearchView.h +++ b/src/lib_gui/qt/view/QtSearchView.h @@ -19,6 +19,7 @@ public: virtual void refreshView(); // SearchView implementation + virtual std::string getQuery() const; virtual void setMatches(const std::vector& matches); virtual void setFocus(); virtual void findFulltext();