Skip to content

Commit

Permalink
use slop
Browse files Browse the repository at this point in the history
  • Loading branch information
saroh committed Jun 24, 2022
1 parent df475b1 commit 5060251
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 40 deletions.
36 changes: 16 additions & 20 deletions query-grammar/src/query_grammar.rs
Expand Up @@ -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::<u32>() {
fn slop_val<'a>() -> impl Parser<&'a str, Output = u32> {
let slop =
(char('~'), many1(digit())).and_then(|(_, slop): (_, String)| match slop.parse::<u32>() {
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)
Expand Down Expand Up @@ -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());
Expand Down
6 changes: 3 additions & 3 deletions query-grammar/src/user_input_ast.rs
Expand Up @@ -40,7 +40,7 @@ impl Debug for UserInputLeaf {
pub struct UserInputLiteral {
pub field_name: Option<String>,
pub phrase: String,
pub matching_distance: u32,
pub slop: u32,
}

impl fmt::Debug for UserInputLiteral {
Expand All @@ -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(())
}
Expand Down
6 changes: 3 additions & 3 deletions src/query/query_parser/logical_ast.rs
Expand Up @@ -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(())
}
Expand Down
25 changes: 11 additions & 14 deletions src/query/query_parser/query_parser.rs
Expand Up @@ -169,7 +169,7 @@ fn trim_ast(logical_ast: LogicalAst) -> Option<LogicalAst> {
/// (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 {
Expand Down Expand Up @@ -408,7 +408,7 @@ impl QueryParser {
field: Field,
json_path: &str,
phrase: &str,
matching_distance: u32,
slop: u32,
) -> Result<Vec<LogicalLiteral>, QueryParserError> {
let field_entry = self.schema.get_field_entry(field);
let field_type = field_entry.field_type();
Expand Down Expand Up @@ -465,7 +465,7 @@ impl QueryParser {
field_name,
field,
phrase,
matching_distance,
slop,
&text_analyzer,
index_record_option,
)?
Expand Down Expand Up @@ -631,12 +631,9 @@ impl QueryParser {
self.compute_path_triplets_for_literal(&literal)?;
let mut asts: Vec<LogicalAst> = 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));
Expand Down Expand Up @@ -680,8 +677,8 @@ impl QueryParser {
fn convert_literal_to_query(logical_literal: LogicalLiteral) -> Box<dyn Query> {
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,
Expand All @@ -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<Option<LogicalLiteral>, QueryParserError> {
Expand All @@ -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(
Expand Down Expand Up @@ -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)"#,
Expand Down

0 comments on commit 5060251

Please sign in to comment.