2018-07-07 14:43:29 +02:00
|
|
|
use std::cmp::Ordering;
|
2018-12-07 17:59:03 +01:00
|
|
|
use std::ops::Deref;
|
2018-12-07 14:41:06 +01:00
|
|
|
|
2018-12-07 17:59:03 +01:00
|
|
|
use rocksdb::DB;
|
2019-01-30 16:30:27 +01:00
|
|
|
use slice_group_by::GroupBy;
|
2018-12-07 14:41:06 +01:00
|
|
|
|
2019-01-06 21:27:41 +01:00
|
|
|
use crate::rank::{match_query_index, Document};
|
2018-10-11 14:04:41 +02:00
|
|
|
use crate::rank::criterion::Criterion;
|
2018-12-07 14:41:06 +01:00
|
|
|
use crate::database::DatabaseView;
|
|
|
|
use crate::Match;
|
2018-07-07 14:43:29 +02:00
|
|
|
|
2018-08-25 13:15:04 +02:00
|
|
|
#[inline]
|
2019-01-06 11:41:47 +01:00
|
|
|
fn contains_exact(matches: &&[Match]) -> bool {
|
2018-08-25 13:15:04 +02:00
|
|
|
matches.iter().any(|m| m.is_exact)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
2019-01-06 21:27:41 +01:00
|
|
|
fn number_exact_matches(matches: &[Match]) -> usize {
|
2019-01-30 16:30:27 +01:00
|
|
|
matches.linear_group_by(match_query_index).filter(contains_exact).count()
|
2018-08-25 13:15:04 +02:00
|
|
|
}
|
|
|
|
|
2018-10-11 14:04:41 +02:00
|
|
|
#[derive(Debug, Clone, Copy)]
|
|
|
|
pub struct Exact;
|
|
|
|
|
2018-12-07 17:59:03 +01:00
|
|
|
impl<D> Criterion<D> for Exact
|
|
|
|
where D: Deref<Target=DB>
|
|
|
|
{
|
|
|
|
fn evaluate(&self, lhs: &Document, rhs: &Document, _: &DatabaseView<D>) -> Ordering {
|
2018-10-11 14:04:41 +02:00
|
|
|
let lhs = number_exact_matches(&lhs.matches);
|
|
|
|
let rhs = number_exact_matches(&rhs.matches);
|
2018-07-07 14:43:29 +02:00
|
|
|
|
2018-10-11 14:04:41 +02:00
|
|
|
lhs.cmp(&rhs).reverse()
|
|
|
|
}
|
2018-07-07 14:43:29 +02:00
|
|
|
}
|