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
This commit is contained in:
parent
076c3c4c7f
commit
7c5c91ef10
@ -503,21 +503,74 @@ void MainWindow::lineEditReturnPressed()
|
|||||||
{
|
{
|
||||||
SqliteSearch searcher(db);
|
SqliteSearch searcher(db);
|
||||||
QVector<SearchResult> results;
|
QVector<SearchResult> 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.
|
LooqsQuery pathsQuery = tmpQuery;
|
||||||
Make sure path results are listed first. */
|
|
||||||
bool addContentSearch = this->contentSearchQuery.hasContentSearch();
|
this->contentSearchQuery = tmpQuery;
|
||||||
bool addPathSearch = !this->contentSearchQuery.hasPathSearch() || !addContentSearch;
|
this->contentSearchQuery.setTokens({});
|
||||||
|
|
||||||
|
bool addContentSearch = false;
|
||||||
|
bool addPathSearch = false;
|
||||||
|
|
||||||
|
auto createFinalTokens = [&tmpQuery](TokenType replacementToken)
|
||||||
|
{
|
||||||
|
QVector<Token> 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)
|
if(addPathSearch)
|
||||||
{
|
{
|
||||||
LooqsQuery filesQuery = LooqsQuery::build(q, TokenType::FILTER_PATH_CONTAINS, false);
|
if(pathsQuery.getLimit() == -1)
|
||||||
if(filesQuery.getLimit() == -1)
|
|
||||||
{
|
{
|
||||||
filesQuery.setLimit(1000);
|
pathsQuery.setLimit(1000);
|
||||||
}
|
}
|
||||||
|
results.append(searcher.search(pathsQuery));
|
||||||
results.append(searcher.search(filesQuery));
|
|
||||||
}
|
}
|
||||||
if(addContentSearch)
|
if(addContentSearch)
|
||||||
{
|
{
|
||||||
@ -525,7 +578,6 @@ void MainWindow::lineEditReturnPressed()
|
|||||||
{
|
{
|
||||||
this->contentSearchQuery.setLimit(1000);
|
this->contentSearchQuery.setLimit(1000);
|
||||||
}
|
}
|
||||||
|
|
||||||
results.append(searcher.search(this->contentSearchQuery));
|
results.append(searcher.search(this->contentSearchQuery));
|
||||||
}
|
}
|
||||||
return results;
|
return results;
|
||||||
|
Loading…
Reference in New Issue
Block a user