fix phrase prefix query (#2043)

* fix phrase prefix query

it would fail spectacularly when no doc in the segment would match the phrase part of the query

* clippy
This commit is contained in:
trinity-1686a
2023-05-22 12:36:20 +02:00
committed by GitHub
parent d7e97331e5
commit 6564e0c467
2 changed files with 19 additions and 2 deletions

View File

@@ -141,7 +141,7 @@ impl<TPostings: Postings> PhrasePrefixScorer<TPostings> {
suffix_offset: (max_offset - suffix_pos) as u32,
phrase_count: 0,
};
if !phrase_prefix_scorer.matches_prefix() {
if phrase_prefix_scorer.doc() != TERMINATED && !phrase_prefix_scorer.matches_prefix() {
phrase_prefix_scorer.advance();
}
phrase_prefix_scorer
@@ -182,7 +182,6 @@ impl<TPostings: Postings> DocSet for PhrasePrefixScorer<TPostings> {
}
fn seek(&mut self, target: DocId) -> DocId {
self.phrase_scorer.seek(target);
let doc = self.phrase_scorer.seek(target);
if doc == TERMINATED || self.matches_prefix() {
return doc;

View File

@@ -258,4 +258,22 @@ mod tests {
assert_eq!(phrase_scorer.advance(), TERMINATED);
Ok(())
}
#[test]
pub fn test_phrase_no_match() -> crate::Result<()> {
let index = create_index(&["aa dd", "aa aa bb c dd aa bb cc aa dc", " aa bb cd"])?;
let schema = index.schema();
let text_field = schema.get_field("text").unwrap();
let searcher = index.reader()?.searcher();
let phrase_query = PhrasePrefixQuery::new(vec![
Term::from_field_text(text_field, "aa"),
Term::from_field_text(text_field, "cc"),
Term::from_field_text(text_field, "d"),
]);
let enable_scoring = EnableScoring::enabled_from_searcher(&searcher);
let weight = phrase_query.weight(enable_scoring).unwrap();
let mut phrase_scorer = weight.scorer(searcher.segment_reader(0u32), 1.0)?;
assert_eq!(phrase_scorer.advance(), TERMINATED);
Ok(())
}
}