diff --git a/query-grammar/src/query_grammar.rs b/query-grammar/src/query_grammar.rs index 31b5230da2..e6bbfd6593 100644 --- a/query-grammar/src/query_grammar.rs +++ b/query-grammar/src/query_grammar.rs @@ -124,35 +124,31 @@ fn term_val<'a>() -> impl Parser<&'a str, Output = String> { } fn term_query<'a>() -> impl Parser<&'a str, Output = UserInputLiteral> { - (field_name(), term_val(), matching_distance_val()).map( - |(field_name, phrase, matching_distance)| UserInputLiteral { - field_name: Some(field_name), - phrase, - matching_distance, - }, - ) + (field_name(), term_val(), slop_val()).map(|(field_name, phrase, slop)| UserInputLiteral { + field_name: Some(field_name), + phrase, + slop, + }) } -fn matching_distance_val<'a>() -> impl Parser<&'a str, Output = u32> { - let matching_distance = (char('~'), many1(digit())).and_then(|(_, distance): (_, String)| { - match distance.parse::() { +fn slop_val<'a>() -> impl Parser<&'a str, Output = u32> { + let slop = + (char('~'), many1(digit())).and_then(|(_, slop): (_, String)| match slop.parse::() { Ok(d) => Ok(d), _ => Err(StringStreamError::UnexpectedParse), - } - }); - optional(matching_distance).map(|distance| match distance { + }); + optional(slop).map(|slop| match slop { Some(d) => d, _ => 0, }) } fn literal<'a>() -> impl Parser<&'a str, Output = UserInputLeaf> { - let term_default_field = - (term_val(), matching_distance_val()).map(|(phrase, matching_distance)| UserInputLiteral { - field_name: None, - phrase, - matching_distance, - }); + let term_default_field = (term_val(), slop_val()).map(|(phrase, slop)| UserInputLiteral { + field_name: None, + phrase, + slop, + }); attempt(term_query()) .or(term_default_field) @@ -737,7 +733,7 @@ mod test { } #[test] - fn test_matching_distance() { + fn test_slop() { assert!(parse_to_ast().parse("\"a b\"~").is_err()); assert!(parse_to_ast().parse("foo:\"a b\"~").is_err()); assert!(parse_to_ast().parse("\"a b\"^2~4").is_err()); diff --git a/query-grammar/src/user_input_ast.rs b/query-grammar/src/user_input_ast.rs index b2b1e5ba96..3130ddbbe6 100644 --- a/query-grammar/src/user_input_ast.rs +++ b/query-grammar/src/user_input_ast.rs @@ -40,7 +40,7 @@ impl Debug for UserInputLeaf { pub struct UserInputLiteral { pub field_name: Option, pub phrase: String, - pub matching_distance: u32, + pub slop: u32, } impl fmt::Debug for UserInputLiteral { @@ -49,8 +49,8 @@ impl fmt::Debug for UserInputLiteral { write!(formatter, "\"{}\":", field)?; } write!(formatter, "\"{}\"", self.phrase)?; - if self.matching_distance > 0 { - write!(formatter, "~{}", self.matching_distance)?; + if self.slop > 0 { + write!(formatter, "~{}", self.slop)?; } Ok(()) } diff --git a/src/query/query_parser/logical_ast.rs b/src/query/query_parser/logical_ast.rs index bade14a41b..2eb75e675a 100644 --- a/src/query/query_parser/logical_ast.rs +++ b/src/query/query_parser/logical_ast.rs @@ -74,10 +74,10 @@ impl fmt::Debug for LogicalLiteral { fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error> { match *self { LogicalLiteral::Term(ref term) => write!(formatter, "{:?}", term), - LogicalLiteral::Phrase(ref terms, distance) => { + LogicalLiteral::Phrase(ref terms, slop) => { write!(formatter, "\"{:?}\"", terms)?; - if distance > 0 { - write!(formatter, "~{:?}", distance) + if slop > 0 { + write!(formatter, "~{:?}", slop) } else { Ok(()) } diff --git a/src/query/query_parser/query_parser.rs b/src/query/query_parser/query_parser.rs index a120049d0e..281d964be7 100644 --- a/src/query/query_parser/query_parser.rs +++ b/src/query/query_parser/query_parser.rs @@ -169,7 +169,7 @@ fn trim_ast(logical_ast: LogicalAst) -> Option { /// (See [`set_boost(...)`](#method.set_field_boost) ). Typically you may want to boost a title /// field. /// -/// Phrase terms support the `~` distance operator which allows to set the phrase's matching +/// Phrase terms support the `~` slop operator which allows to set the phrase's matching /// distance in words. `"big wolf"~1 will return documents containing the phrase `"big bad wolf"`. #[derive(Clone)] pub struct QueryParser { @@ -408,7 +408,7 @@ impl QueryParser { field: Field, json_path: &str, phrase: &str, - matching_distance: u32, + slop: u32, ) -> Result, QueryParserError> { let field_entry = self.schema.get_field_entry(field); let field_type = field_entry.field_type(); @@ -465,7 +465,7 @@ impl QueryParser { field_name, field, phrase, - matching_distance, + slop, &text_analyzer, index_record_option, )? @@ -631,12 +631,9 @@ impl QueryParser { self.compute_path_triplets_for_literal(&literal)?; let mut asts: Vec = Vec::new(); for (field, json_path, phrase) in term_phrases { - for ast in self.compute_logical_ast_for_leaf( - field, - json_path, - phrase, - literal.matching_distance, - )? { + for ast in + self.compute_logical_ast_for_leaf(field, json_path, phrase, literal.slop)? + { // Apply some field specific boost defined at the query parser level. let boost = self.field_boost(field); asts.push(LogicalAst::Leaf(Box::new(ast)).boost(boost)); @@ -680,8 +677,8 @@ impl QueryParser { fn convert_literal_to_query(logical_literal: LogicalLiteral) -> Box { match logical_literal { LogicalLiteral::Term(term) => Box::new(TermQuery::new(term, IndexRecordOption::WithFreqs)), - LogicalLiteral::Phrase(term_with_offsets, distance) => Box::new( - PhraseQuery::new_with_offset_and_slop(term_with_offsets, distance), + LogicalLiteral::Phrase(term_with_offsets, slop) => Box::new( + PhraseQuery::new_with_offset_and_slop(term_with_offsets, slop), ), LogicalLiteral::Range { field, @@ -699,7 +696,7 @@ fn generate_literals_for_str( field_name: &str, field: Field, phrase: &str, - matching_distance: u32, + slop: u32, text_analyzer: &TextAnalyzer, index_record_option: IndexRecordOption, ) -> Result, QueryParserError> { @@ -721,7 +718,7 @@ fn generate_literals_for_str( field_name.to_string(), )); } - Ok(Some(LogicalLiteral::Phrase(terms, matching_distance))) + Ok(Some(LogicalLiteral::Phrase(terms, slop))) } fn generate_literals_for_json_object( @@ -1506,7 +1503,7 @@ mod test { } #[test] - pub fn test_phrase_matching_distance() { + pub fn test_phrase_slop() { test_parse_query_to_logical_ast_helper( "\"a b\"~2", r#"("[(0, Term(type=Str, field=0, "a")), (1, Term(type=Str, field=0, "b"))]"~2 "[(0, Term(type=Str, field=1, "a")), (1, Term(type=Str, field=1, "b"))]"~2)"#,