2019-12-12 00:02:10 +08:00
|
|
|
use std::cmp::{Ordering, Reverse};
|
2019-10-02 23:34:32 +08:00
|
|
|
use slice_group_by::GroupBy;
|
2019-12-12 00:02:10 +08:00
|
|
|
use crate::RawDocument;
|
2019-12-12 18:33:39 +08:00
|
|
|
use crate::bucket_sort::BareMatch;
|
|
|
|
use super::{Criterion, Context, ContextMut};
|
2019-10-02 23:34:32 +08:00
|
|
|
|
|
|
|
pub struct Exact;
|
|
|
|
|
|
|
|
impl Criterion for Exact {
|
2019-12-12 00:02:10 +08:00
|
|
|
fn name(&self) -> &str { "exact" }
|
|
|
|
|
2019-12-12 18:33:39 +08:00
|
|
|
fn prepare(&self, _ctx: ContextMut, documents: &mut [RawDocument]) {
|
2019-12-12 00:02:10 +08:00
|
|
|
for document in documents {
|
|
|
|
document.raw_matches.sort_unstable_by_key(|bm| (bm.query_index, Reverse(bm.is_exact)));
|
|
|
|
}
|
2019-10-15 00:48:54 +08:00
|
|
|
}
|
|
|
|
|
2019-12-12 18:33:39 +08:00
|
|
|
fn evaluate(&self, _ctx: &Context, lhs: &RawDocument, rhs: &RawDocument) -> Ordering {
|
2019-12-12 00:02:10 +08:00
|
|
|
#[inline]
|
|
|
|
fn sum_exact_query_words(matches: &[BareMatch]) -> usize {
|
|
|
|
let mut sum_exact_query_words = 0;
|
|
|
|
|
|
|
|
for group in matches.linear_group_by_key(|bm| bm.query_index) {
|
|
|
|
sum_exact_query_words += group[0].is_exact as usize;
|
|
|
|
}
|
2019-10-15 00:48:54 +08:00
|
|
|
|
2019-12-12 00:02:10 +08:00
|
|
|
sum_exact_query_words
|
|
|
|
}
|
2019-10-15 00:48:54 +08:00
|
|
|
|
2019-12-12 00:02:10 +08:00
|
|
|
let lhs = sum_exact_query_words(&lhs.raw_matches);
|
|
|
|
let rhs = sum_exact_query_words(&rhs.raw_matches);
|
2019-10-02 23:34:32 +08:00
|
|
|
|
2019-12-12 00:02:10 +08:00
|
|
|
lhs.cmp(&rhs).reverse()
|
2019-10-02 23:34:32 +08:00
|
|
|
}
|
|
|
|
}
|