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-13 18:14:12 +08:00
|
|
|
use crate::{RawDocument, MResult};
|
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-13 18:14:12 +08:00
|
|
|
fn prepare<'h, 'p, 'tag, 'txn, 'q, 'a, 'r>(
|
|
|
|
&self,
|
|
|
|
_ctx: ContextMut<'h, 'p, 'tag, 'txn, 'q, 'a>,
|
|
|
|
documents: &mut [RawDocument<'r, 'tag>],
|
|
|
|
) -> MResult<()>
|
|
|
|
{
|
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-12-13 18:14:12 +08:00
|
|
|
Ok(())
|
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);
|
|
|
|
lhs.cmp(&rhs).reverse()
|
2019-10-02 23:34:32 +08:00
|
|
|
}
|
|
|
|
}
|