Skip to content
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 73 additions & 0 deletions src/query/phrase_query/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ pub(crate) mod tests {

use super::*;
use crate::collector::tests::{TEST_COLLECTOR_WITHOUT_SCORE, TEST_COLLECTOR_WITH_SCORE};
use crate::collector::Count;
use crate::index::Index;
use crate::query::{EnableScoring, QueryParser, Weight};
use crate::schema::{Schema, Term, TEXT};
Expand Down Expand Up @@ -255,6 +256,78 @@ pub(crate) mod tests {
Ok(())
}

#[test]
pub fn test_phrase_slop_above_u8_max() -> crate::Result<()> {
let filler = vec!["w"; 256].join(" ");
let index = create_index(&[format!("a {filler} b {filler} c")])?;
let text_field = index.schema().get_field("text").unwrap();
let searcher = index.reader()?.searcher();

for slop in [255, 256, 300, 511, 512] {
let terms = ["a", "b", "c"]
.iter()
.map(|text| Term::from_field_text(text_field, text))
.collect();
let mut phrase_query = PhraseQuery::new(terms);
phrase_query.set_slop(slop);
let expected = if slop == 512 { 1 } else { 0 };
assert_eq!(
searcher.search(&phrase_query, &Count)?,
expected,
"unscored search at slop {slop}"
);
assert_eq!(
test_query(slop, &index, vec!["a", "b", "c"]).len(),
expected,
"scored search at slop {slop}"
);
}
Ok(())
}

#[test]
pub fn test_phrase_slop_inside_carried_span() -> crate::Result<()> {
let index = create_index(&["a x b x c", "b"])?;
let text_field = index.schema().get_field("text").unwrap();
let searcher = index.reader()?.searcher();
let terms = ["a", "b", "c"]
.iter()
.map(|text| Term::from_field_text(text_field, text))
.collect();
let mut phrase_query = PhraseQuery::new(terms);
phrase_query.set_slop(2);
assert_eq!(searcher.search(&phrase_query, &Count)?, 1);
assert_eq!(test_query(2, &index, vec!["a", "b", "c"]).len(), 1);
Ok(())
}

#[test]
pub fn test_phrase_slop_candidate_expansion_bound() -> crate::Result<()> {
let index = create_index(&["a x b x c x d x e"])?;
let text_field = index.schema().get_field("text").unwrap();
let searcher = index.reader()?.searcher();

for (slop, expected) in [(3, 0), (4, 1)] {
let terms = ["a", "b", "c", "d", "e"]
.iter()
.map(|text| Term::from_field_text(text_field, text))
.collect();
let mut phrase_query = PhraseQuery::new(terms);
phrase_query.set_slop(slop);
assert_eq!(
searcher.search(&phrase_query, &Count)?,
expected,
"unscored search at slop {slop}"
);
assert_eq!(
test_query(slop, &index, vec!["a", "b", "c", "d", "e"]).len(),
expected,
"scored search at slop {slop}"
);
}
Ok(())
}

#[test]
pub fn test_phrase_score_with_slop_ordering() -> crate::Result<()> {
let index = create_index(&[
Expand Down
Loading