shared: LooqsQuery: Fix logic of implicit AND booleans. Add 'limit:' support
Add implicit AND booleans at the end. This fixes a number of issues in LooqsQuery: (1) A query like a b c p:(something) would fail, because a b c get merged into one word. This happens at the end. lonewords are special and do not become a token immediatly. So previous logic to add implicit ANDs does not apply. (2) Negations were also broken with lonewords. (3) The TokenType enum fields were too narrow to be useful for the bitmask Independent of that, add support for 'limit:'
This commit is contained in:
@ -220,19 +220,8 @@ LooqsQuery LooqsQuery::build(QString expression, TokenType loneWordsTokenType, b
|
||||
{
|
||||
throw LooqsGeneralException("Can't have two negations following each other");
|
||||
}
|
||||
if(!previousWasBool())
|
||||
{
|
||||
result.addToken(Token(BOOL_AND)); // Implicit and, our default operation
|
||||
}
|
||||
result.addToken(Token(NEGATION));
|
||||
}
|
||||
if(!result.tokens.isEmpty() && !previousWasBool() && !previousWas(NEGATION) && !previousWas(BRACKET_OPEN) &&
|
||||
bracket != ")")
|
||||
{
|
||||
// the current token isn't a negation, isn't a boolean. Thus, implicit AND is required
|
||||
result.addToken(Token(BOOL_AND));
|
||||
}
|
||||
|
||||
if(bracket != "")
|
||||
{
|
||||
if(bracket == "(")
|
||||
@ -259,7 +248,7 @@ LooqsQuery LooqsQuery::build(QString expression, TokenType loneWordsTokenType, b
|
||||
|
||||
if(filtername != "")
|
||||
{
|
||||
TokenType tokenType;
|
||||
TokenType tokenType = WORD;
|
||||
QString value = m.captured("innerargs");
|
||||
if(value == "")
|
||||
{
|
||||
@ -294,21 +283,21 @@ LooqsQuery LooqsQuery::build(QString expression, TokenType loneWordsTokenType, b
|
||||
{
|
||||
tokenType = FILTER_CONTENT_PAGE;
|
||||
}
|
||||
else if(filtername ==
|
||||
"sort") // TODO: given this is not really a "filter", this feels slightly misplaced here
|
||||
// TODO: given this is not really a "filter", this feels slightly misplaced here
|
||||
else if(filtername == "sort")
|
||||
{
|
||||
if(!result.sortConditions.empty())
|
||||
{
|
||||
throw LooqsGeneralException("Two sort statements are illegal");
|
||||
}
|
||||
// TODO: hack, since we are not a "filter", we must remove a preceeding (implicit) boolean
|
||||
if((result.tokens.last().type & BOOL) == BOOL)
|
||||
{
|
||||
result.tokens.pop_back();
|
||||
}
|
||||
result.sortConditions = createSortConditions(value);
|
||||
continue;
|
||||
}
|
||||
else if(filtername == "limit")
|
||||
{
|
||||
result.limit = value.toInt();
|
||||
continue;
|
||||
}
|
||||
else
|
||||
{
|
||||
throw LooqsGeneralException("Unknown filter provided!");
|
||||
@ -326,6 +315,26 @@ LooqsQuery LooqsQuery::build(QString expression, TokenType loneWordsTokenType, b
|
||||
}
|
||||
}
|
||||
|
||||
/* Add our default implicit AND boolean condition where appropriate */
|
||||
QVector<Token> newTokens;
|
||||
|
||||
TokenType prevType = BOOL_AND;
|
||||
int needsBoolean = FILTER_CONTENT | FILTER_PATH | NEGATION;
|
||||
for(Token &t : result.tokens)
|
||||
{
|
||||
if(t.type == BRACKET_OPEN || t.type & needsBoolean)
|
||||
{
|
||||
if(!((prevType & BOOL) == BOOL) && !((prevType & NEGATION) == NEGATION) &&
|
||||
!((prevType & BRACKET_OPEN) == BRACKET_OPEN))
|
||||
{
|
||||
newTokens.append(Token(BOOL_AND));
|
||||
}
|
||||
}
|
||||
prevType = t.type;
|
||||
newTokens.append(t);
|
||||
}
|
||||
result.tokens = newTokens;
|
||||
|
||||
bool contentsearch = result.hasContentSearch();
|
||||
bool sortsForContent = std::any_of(result.sortConditions.begin(), result.sortConditions.end(),
|
||||
[](SortCondition c) { return c.field == CONTENT_TEXT; });
|
||||
|
Reference in New Issue
Block a user