diff --git a/cli/commandlist.cpp b/cli/commandlist.cpp index 31e0ff6..fa1fbd2 100644 --- a/cli/commandlist.cpp +++ b/cli/commandlist.cpp @@ -24,7 +24,7 @@ int CommandList::handle(QStringList arguments) QStringList files = parser.positionalArguments(); QString queryStrings = files.join(' '); - auto results = dbService->search(LooqsQuery::build(queryStrings)); + auto results = dbService->search(LooqsQuery::build(queryStrings, TokenType::FILTER_PATH_CONTAINS, false)); for(SearchResult &result : results) { diff --git a/cli/commandsearch.cpp b/cli/commandsearch.cpp index 4350171..e7ed114 100644 --- a/cli/commandsearch.cpp +++ b/cli/commandsearch.cpp @@ -16,7 +16,7 @@ int CommandSearch::handle(QStringList arguments) QStringList files = parser.positionalArguments(); QString queryStrings = files.join(' '); - LooqsQuery query = LooqsQuery::build(queryStrings); + LooqsQuery query = LooqsQuery::build(queryStrings, TokenType::FILTER_PATH_CONTAINS, false); bool reverse = parser.isSet("reverse"); if(reverse) { diff --git a/gui/mainwindow.cpp b/gui/mainwindow.cpp index 7049457..c55cbd4 100644 --- a/gui/mainwindow.cpp +++ b/gui/mainwindow.cpp @@ -166,8 +166,13 @@ void MainWindow::lineEditReturnPressed() [&, q]() { SqliteSearch searcher(db); - this->currentQuery = LooqsQuery::build(q); - return searcher.search(this->currentQuery); + this->contentSearchQuery = LooqsQuery::build(q, TokenType::FILTER_CONTENT_CONTAINS, true); + + LooqsQuery filesQuery = LooqsQuery::build(q, TokenType::FILTER_PATH_CONTAINS, false); + QVector results; + results.append(searcher.search(filesQuery)); + results.append(searcher.search(this->contentSearchQuery)); + return results; }); searchWatcher.setFuture(searchFuture); } @@ -243,7 +248,7 @@ void MainWindow::makePdfPreview(int page) QVector wordsToHighlight; QRegularExpression extractor(R"#("([^"]*)"|(\w+))#"); - for(const Token &token : this->currentQuery.getTokens()) + for(const Token &token : this->contentSearchQuery.getTokens()) { if(token.type == FILTER_CONTENT_CONTAINS) { diff --git a/gui/mainwindow.h b/gui/mainwindow.h index 40eb12f..053274d 100644 --- a/gui/mainwindow.h +++ b/gui/mainwindow.h @@ -45,7 +45,7 @@ class MainWindow : public QMainWindow unsigned int processedPdfPreviews; void handleSearchResults(const QVector &results); void handleSearchError(QString error); - LooqsQuery currentQuery; + LooqsQuery contentSearchQuery; int pdfPreviewsPerPage; void createSearchResutlMenu(QMenu &menu, const QFileInfo &fileInfo); void ipcDocOpen(QString path, int num); diff --git a/shared/looqsquery.cpp b/shared/looqsquery.cpp index 15b53ae..6cb4567 100644 --- a/shared/looqsquery.cpp +++ b/shared/looqsquery.cpp @@ -157,15 +157,15 @@ void LooqsQuery::addToken(Token t) * thus, "Downloads zip" becomes essentailly "path.contains:(Downloads) AND path.contains:(zip)" * * TODO: It's a bit ugly still*/ -LooqsQuery LooqsQuery::build(QString expression) +LooqsQuery LooqsQuery::build(QString expression, TokenType loneWordsTokenType, bool mergeLoneWords) { if(!checkParanthesis(expression)) { throw LooqsGeneralException("Invalid paranthesis"); } + QStringList loneWords; LooqsQuery result; - // TODO: merge lonewords QRegularExpression rx("((?(\\.|\\w)+):(?\\((?[^\\)]+)\\)|([\\w,])+)|(?AND|OR)" "|(?!)|(?\\(|\\))|(?\\w+))"); QRegularExpressionMatchIterator i = rx.globalMatch(expression); @@ -233,7 +233,14 @@ LooqsQuery LooqsQuery::build(QString expression) if(loneword != "") { - result.addToken(Token(FILTER_PATH_CONTAINS, loneword)); + if(mergeLoneWords) + { + loneWords.append(loneword); + } + else + { + result.addToken(Token(loneWordsTokenType, loneword)); + } } if(filtername != "") @@ -292,6 +299,11 @@ LooqsQuery LooqsQuery::build(QString expression) } } + if(mergeLoneWords) + { + result.addToken(Token(loneWordsTokenType, loneWords.join(' '))); + } + bool contentsearch = (result.getTokensMask() & FILTER_CONTENT) == FILTER_CONTENT; bool sortsForContent = std::any_of(result.sortConditions.begin(), result.sortConditions.end(), [](SortCondition c) { return c.field == CONTENT_TEXT; }); diff --git a/shared/looqsquery.h b/shared/looqsquery.h index da6c9c7..c0a0e76 100644 --- a/shared/looqsquery.h +++ b/shared/looqsquery.h @@ -54,7 +54,7 @@ class LooqsQuery } void addSortCondition(SortCondition sc); static bool checkParanthesis(QString query); - static LooqsQuery build(QString query); + static LooqsQuery build(QString query, TokenType loneWordsTokenType, bool mergeLoneWords); }; #endif // LOOQSQUERY_H