From 7c5c91ef10c21be3c154cf8da91231b5e627bdb1 Mon Sep 17 00:00:00 2001 From: Albert S Date: Fri, 29 Jul 2022 00:53:09 +0200 Subject: [PATCH] gui: search: Avoid double results + minor improvements Avoid double results in search by distinguishing whether a filter was explicitly given. Previously, we could not discern this. Furthermore, if a content search is given, lone words will be considered path searches. If a path search is given, we consider lone words implicit content search filters. This simplifies queries for the user --- gui/mainwindow.cpp | 74 +++++++++++++++++++++++++++++++++++++++------- 1 file changed, 63 insertions(+), 11 deletions(-) diff --git a/gui/mainwindow.cpp b/gui/mainwindow.cpp index 748256d..dbcd0f3 100644 --- a/gui/mainwindow.cpp +++ b/gui/mainwindow.cpp @@ -503,21 +503,74 @@ void MainWindow::lineEditReturnPressed() { SqliteSearch searcher(db); QVector results; - this->contentSearchQuery = LooqsQuery::build(q, TokenType::FILTER_CONTENT_CONTAINS, true); + LooqsQuery tmpQuery = LooqsQuery::build(q, TokenType::WORD, true); - /* We can have a path search in contentsearch too (if given explicitly), so no need to do it twice. - Make sure path results are listed first. */ - bool addContentSearch = this->contentSearchQuery.hasContentSearch(); - bool addPathSearch = !this->contentSearchQuery.hasPathSearch() || !addContentSearch; + LooqsQuery pathsQuery = tmpQuery; + + this->contentSearchQuery = tmpQuery; + this->contentSearchQuery.setTokens({}); + + bool addContentSearch = false; + bool addPathSearch = false; + + auto createFinalTokens = [&tmpQuery](TokenType replacementToken) + { + QVector tokens = tmpQuery.getTokens(); + for(Token &token : tokens) + { + if(token.type == TokenType::WORD) + { + token.type = replacementToken; + } + } + return tokens; + }; + + /* An explicit search, we just pass it on */ + if(!(tmpQuery.getTokensMask() & TokenType::WORD)) + { + if(tmpQuery.hasContentSearch()) + { + this->contentSearchQuery.setTokens(createFinalTokens(TokenType::FILTER_CONTENT_CONTAINS)); + addContentSearch = true; + } + else + { + this->contentSearchQuery.setTokens(createFinalTokens(TokenType::FILTER_PATH_CONTAINS)); + addPathSearch = true; + } + } + /* A path search, and lone words, e. g. p:("docs") invoice */ + else if(tmpQuery.hasPathSearch() && (tmpQuery.getTokensMask() & TokenType::WORD)) + { + this->contentSearchQuery = tmpQuery; + this->contentSearchQuery.setTokens(createFinalTokens(TokenType::FILTER_CONTENT_CONTAINS)); + addContentSearch = true; + addPathSearch = false; + } + /* A content search and lone words, e. g. c:("to be or not") ebooks */ + else if(tmpQuery.hasContentSearch() && (tmpQuery.getTokensMask() & TokenType::WORD)) + { + this->contentSearchQuery = tmpQuery; + this->contentSearchQuery.setTokens(createFinalTokens(TokenType::FILTER_PATH_CONTAINS)); + addContentSearch = true; + addPathSearch = false; + } + /* "Simply lone words, so search both" */ + else if(!tmpQuery.hasPathSearch() && !tmpQuery.hasContentSearch()) + { + this->contentSearchQuery.setTokens(createFinalTokens(TokenType::FILTER_CONTENT_CONTAINS)); + pathsQuery.setTokens(createFinalTokens(TokenType::FILTER_PATH_CONTAINS)); + addContentSearch = true; + addPathSearch = true; + } if(addPathSearch) { - LooqsQuery filesQuery = LooqsQuery::build(q, TokenType::FILTER_PATH_CONTAINS, false); - if(filesQuery.getLimit() == -1) + if(pathsQuery.getLimit() == -1) { - filesQuery.setLimit(1000); + pathsQuery.setLimit(1000); } - - results.append(searcher.search(filesQuery)); + results.append(searcher.search(pathsQuery)); } if(addContentSearch) { @@ -525,7 +578,6 @@ void MainWindow::lineEditReturnPressed() { this->contentSearchQuery.setLimit(1000); } - results.append(searcher.search(this->contentSearchQuery)); } return results;