Skip to content

Commit

Permalink
Trying to fix #609 (#616)
Browse files Browse the repository at this point in the history
  • Loading branch information
fulmicoton authored Aug 6, 2019
1 parent 754b55e commit 143f78e
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 17 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ Tantivy 0.11.0
=====================

- Added f64 field. Internally reuse u64 code the same way i64 does (@fdb-hiroshima)
- Closes #609. Better handling of hyphens in query parser.

Tantivy 0.10.1
=====================
Expand Down
5 changes: 2 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "tantivy"
version = "0.10.1"
version = "0.11.0"
authors = ["Paul Masurel <[email protected]>"]
license = "MIT"
categories = ["database-implementations", "data-structures"]
Expand Down Expand Up @@ -86,7 +86,6 @@ travis-ci = { repository = "tantivy-search/tantivy" }
[dev-dependencies.fail]
features = ["failpoints"]


# Following the "fail" crate best practises, we isolate
# tests that define specific behavior in fail check points
# in a different binary.
Expand All @@ -97,4 +96,4 @@ features = ["failpoints"]
[[test]]
name = "failpoints"
path = "tests/failpoints/mod.rs"
required-features = ["fail/failpoints"]
required-features = ["fail/failpoints"]
39 changes: 26 additions & 13 deletions src/query/query_parser/query_grammar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,19 @@ parser! {
parser! {
fn word[I]()(I) -> String
where [I: Stream<Item = char>] {
many1(satisfy(|c: char| c.is_alphanumeric() || c=='.'))
.and_then(|s: String| {
match s.as_str() {
"OR" => Err(StreamErrorFor::<I>::unexpected_static_message("OR")),
"AND" => Err(StreamErrorFor::<I>::unexpected_static_message("AND")),
"NOT" => Err(StreamErrorFor::<I>::unexpected_static_message("NOT")),
_ => Ok(s)
}
})
(
satisfy(|c: char| c.is_alphanumeric()),
many(satisfy(|c: char| !c.is_whitespace() && ![':', '{', '}', '"', '[', ']', '(',')'].contains(&c)))
)
.map(|(s1, s2): (char, String)| format!("{}{}", s1, s2))
.and_then(|s: String| {
match s.as_str() {
"OR" => Err(StreamErrorFor::<I>::unexpected_static_message("OR")),
"AND" => Err(StreamErrorFor::<I>::unexpected_static_message("AND")),
"NOT" => Err(StreamErrorFor::<I>::unexpected_static_message("NOT")),
_ => Ok(s)
}
})
}
}

Expand Down Expand Up @@ -115,9 +119,7 @@ parser! {
)
)
.or(attempt(
range().map(UserInputAST::from)
)
)
range().map(UserInputAST::from)))
.or(literal().map(|leaf| UserInputAST::Leaf(Box::new(leaf))))
}
}
Expand Down Expand Up @@ -225,6 +227,13 @@ mod test {
assert!(parse_to_ast().parse(query).is_err());
}

#[test]
fn test_parse_query_to_ast_hyphen() {
test_parse_query_to_ast_helper("\"www-form-encoded\"", "\"www-form-encoded\"");
test_parse_query_to_ast_helper("www-form-encoded", "\"www-form-encoded\"");
test_parse_query_to_ast_helper("www-form-encoded", "\"www-form-encoded\"");
}

#[test]
fn test_parse_query_to_ast_not_op() {
assert_eq!(
Expand Down Expand Up @@ -272,13 +281,17 @@ mod test {
test_parse_query_to_ast_helper("-abc:toto", "-(abc:\"toto\")");
test_parse_query_to_ast_helper("abc:a b", "(abc:\"a\" \"b\")");
test_parse_query_to_ast_helper("abc:\"a b\"", "abc:\"a b\"");
test_is_parse_err("abc + ");
}

#[test]
fn test_parse_query_to_ast_range() {
test_parse_query_to_ast_helper("foo:[1 TO 5]", "foo:[\"1\" TO \"5\"]");
test_parse_query_to_ast_helper("[1 TO 5]", "[\"1\" TO \"5\"]");
test_parse_query_to_ast_helper("foo:{a TO z}", "foo:{\"a\" TO \"z\"}");
test_parse_query_to_ast_helper("foo:[1 TO toto}", "foo:[\"1\" TO \"toto\"}");
test_parse_query_to_ast_helper("foo:[* TO toto}", "foo:[\"*\" TO \"toto\"}");
test_parse_query_to_ast_helper("foo:[1 TO *}", "foo:[\"1\" TO \"*\"}");
test_parse_query_to_ast_helper("foo:[1.1 TO *}", "foo:[\"1.1\" TO \"*\"}");
test_is_parse_err("abc + ");
}
}
21 changes: 20 additions & 1 deletion src/query/query_parser/query_parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -690,7 +690,7 @@ mod test {
}

#[test]
pub fn test_parse_query_to_ast_disjunction() {
pub fn test_parse_query_to_ast_single_term() {
test_parse_query_to_logical_ast_helper(
"title:toto",
"Term([0, 0, 0, 0, 116, 111, 116, 111])",
Expand All @@ -714,6 +714,10 @@ mod test {
.unwrap(),
QueryParserError::AllButQueryForbidden
);
}

#[test]
pub fn test_parse_query_to_ast_two_terms() {
test_parse_query_to_logical_ast_helper(
"title:a b",
"(Term([0, 0, 0, 0, 97]) (Term([0, 0, 0, 0, 98]) \
Expand All @@ -726,6 +730,10 @@ mod test {
(1, Term([0, 0, 0, 0, 98]))]\"",
false,
);
}

#[test]
pub fn test_parse_query_to_ast_ranges() {
test_parse_query_to_logical_ast_helper(
"title:[a TO b]",
"(Included(Term([0, 0, 0, 0, 97])) TO \
Expand Down Expand Up @@ -893,4 +901,15 @@ mod test {
true,
);
}

#[test]
pub fn test_query_parser_hyphen() {
test_parse_query_to_logical_ast_helper(
"title:www-form-encoded",
"\"[(0, Term([0, 0, 0, 0, 119, 119, 119])), \
(1, Term([0, 0, 0, 0, 102, 111, 114, 109])), \
(2, Term([0, 0, 0, 0, 101, 110, 99, 111, 100, 101, 100]))]\"",
false,
);
}
}

0 comments on commit 143f78e

Please sign in to comment.