Skip to content

Commit 9c7e904

Browse files
committed
allow slop in both directions
allow slop in both directions so "big wolf"~3 can also match "wolf big" This also fixes #1934, when the docsets were reordered by size and didn't match the terms.
1 parent ba309e1 commit 9c7e904

File tree

2 files changed

+122
-129
lines changed

2 files changed

+122
-129
lines changed

src/query/phrase_query/mod.rs

+27-2
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,30 @@ pub mod tests {
207207
Ok(())
208208
}
209209

210+
#[test]
211+
pub fn test_phrase_score_with_slop_bug() -> crate::Result<()> {
212+
let index = create_index(&["asdf asdf Captain Subject Wendy", "Captain"])?;
213+
let schema = index.schema();
214+
let text_field = schema.get_field("text").unwrap();
215+
let searcher = index.reader().unwrap().searcher();
216+
let test_query = |texts: Vec<&str>| {
217+
let terms: Vec<Term> = texts
218+
.iter()
219+
.map(|text| Term::from_field_text(text_field, text))
220+
.collect();
221+
let mut phrase_query = PhraseQuery::new(terms);
222+
phrase_query.set_slop(1);
223+
searcher
224+
.search(&phrase_query, &TEST_COLLECTOR_WITH_SCORE)
225+
.expect("search should succeed")
226+
.scores()
227+
.to_vec()
228+
};
229+
let scores = test_query(vec!["captain", "wendy"]);
230+
assert_eq!(scores.len(), 1);
231+
Ok(())
232+
}
233+
210234
#[test]
211235
pub fn test_phrase_score_with_slop_size() -> crate::Result<()> {
212236
let index = create_index(&["a b e c", "a e e e c", "a e e e e c"])?;
@@ -237,7 +261,7 @@ pub mod tests {
237261
let index = create_index(&[
238262
"a e b e c",
239263
"a e e e e e b e e e e c",
240-
"a c b",
264+
"a c b", // also matches
241265
"a c e b e",
242266
"a e c b",
243267
"a e b c",
@@ -261,7 +285,8 @@ pub mod tests {
261285
let scores = test_query(vec!["a", "b", "c"]);
262286
// The first and last matches.
263287
assert_nearly_equals!(scores[0], 0.23091172);
264-
assert_nearly_equals!(scores[1], 0.25024384);
288+
assert_nearly_equals!(scores[1], 0.27310878);
289+
assert_nearly_equals!(scores[3], 0.25024384);
265290
Ok(())
266291
}
267292

0 commit comments

Comments
 (0)