Skip to content

Commit 5562ce6

Browse files
Merge pull request #2818 from Darkheir/fix/query_grammar_regex_between_parentheses
2 parents 09b6ece + a55e406 commit 5562ce6

File tree

3 files changed

+26
-3
lines changed

3 files changed

+26
-3
lines changed

query-grammar/src/query_grammar.rs

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -704,7 +704,11 @@ fn regex(inp: &str) -> IResult<&str, UserInputLeaf> {
704704
many1(alt((preceded(char('\\'), char('/')), none_of("/")))),
705705
char('/'),
706706
),
707-
peek(alt((multispace1, eof))),
707+
peek(alt((
708+
value((), multispace1),
709+
value((), char(')')),
710+
value((), eof),
711+
))),
708712
),
709713
|elements| UserInputLeaf::Regex {
710714
field: None,
@@ -721,8 +725,12 @@ fn regex_infallible(inp: &str) -> JResult<&str, UserInputLeaf> {
721725
opt_i_err(char('/'), "missing delimiter /"),
722726
),
723727
opt_i_err(
724-
peek(alt((multispace1, eof))),
725-
"expected whitespace or end of input",
728+
peek(alt((
729+
value((), multispace1),
730+
value((), char(')')),
731+
value((), eof),
732+
))),
733+
"expected whitespace, closing parenthesis, or end of input",
726734
),
727735
)(inp)
728736
{
@@ -1707,6 +1715,10 @@ mod test {
17071715
test_parse_query_to_ast_helper("foo:(A OR B)", "(?\"foo\":A ?\"foo\":B)");
17081716
test_parse_query_to_ast_helper("foo:(A* OR B*)", "(?\"foo\":A* ?\"foo\":B*)");
17091717
test_parse_query_to_ast_helper("foo:(*A OR *B)", "(?\"foo\":*A ?\"foo\":*B)");
1718+
1719+
// Regexes between parentheses
1720+
test_parse_query_to_ast_helper("foo:(/A.*/)", "\"foo\":/A.*/");
1721+
test_parse_query_to_ast_helper("foo:(/A.*/ OR /B.*/)", "(?\"foo\":/A.*/ ?\"foo\":/B.*/)");
17101722
}
17111723

17121724
#[test]

query-grammar/src/user_input_ast.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ impl UserInputLeaf {
6666
}
6767
UserInputLeaf::Range { field, .. } if field.is_none() => *field = Some(default_field),
6868
UserInputLeaf::Set { field, .. } if field.is_none() => *field = Some(default_field),
69+
UserInputLeaf::Regex { field, .. } if field.is_none() => *field = Some(default_field),
6970
_ => (), // field was already set, do nothing
7071
}
7172
}

src/query/query_parser/query_parser.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2068,6 +2068,16 @@ mod test {
20682068
format!("Regex(Field(0), {:#?})", expected_regex).as_str(),
20692069
false,
20702070
);
2071+
let expected_regex2 = tantivy_fst::Regex::new(r".*a").unwrap();
2072+
test_parse_query_to_logical_ast_helper(
2073+
"title:(/.*b/ OR /.*a/)",
2074+
format!(
2075+
"(Regex(Field(0), {:#?}) Regex(Field(0), {:#?}))",
2076+
expected_regex, expected_regex2
2077+
)
2078+
.as_str(),
2079+
false,
2080+
);
20712081

20722082
// Invalid field
20732083
let err = parse_query_to_logical_ast("float:/.*b/", false).unwrap_err();

0 commit comments

Comments
 (0)