Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enforcing "NOT" and "-" queries consistency in UserInputAst #1609

Merged
merged 2 commits into from
May 12, 2023
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions query-grammar/src/query_grammar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -374,6 +374,25 @@ pub fn parse_to_ast<'a>() -> impl Parser<&'a str, Output = UserInputAst> {
spaces()
.with(optional(ast()).skip(eof()))
.map(|opt_ast| opt_ast.unwrap_or_else(UserInputAst::empty_query))
.map(|ast| rewrite_ast(ast))
}

/// Removes unnecessary children clauses in AST
///
/// Motivated by [issue #1433](https://github.com/quickwit-oss/tantivy/issues/1433)
fn rewrite_ast(input: UserInputAst) -> UserInputAst {
if let UserInputAst::Clause(terms) = input {
UserInputAst::Clause(terms.into_iter().map(rewrite_ast_clause).collect())
bazhenov marked this conversation as resolved.
Show resolved Hide resolved
} else {
input
}
}

fn rewrite_ast_clause(input: (Option<Occur>, UserInputAst)) -> (Option<Occur>, UserInputAst) {
match input {
(None, UserInputAst::Clause(mut clauses)) if clauses.len() == 1 => clauses.remove(0),
bazhenov marked this conversation as resolved.
Show resolved Hide resolved
other => other,
}
}

#[cfg(test)]
Expand Down Expand Up @@ -749,4 +768,10 @@ mod test {
test_parse_query_to_ast_helper("foo:\"a b\"~300", "\"foo\":\"a b\"~300");
test_parse_query_to_ast_helper("\"a b\"~300^2", "(\"a b\"~300)^2");
}

#[test]
fn test_not_queries_are_consistent() {
test_parse_query_to_ast_helper("tata -toto", "(*\"tata\" -\"toto\")");
test_parse_query_to_ast_helper("tata NOT toto", "(*\"tata\" -\"toto\")");
}
}