2018-07-07 14:43:29 +02:00
|
|
|
use std::cmp::Ordering;
|
|
|
|
use group_by::GroupBy;
|
2018-09-09 11:13:58 +02:00
|
|
|
use crate::Match;
|
|
|
|
use crate::rank::{match_query_index, Document};
|
2018-10-11 14:04:41 +02:00
|
|
|
use crate::rank::criterion::Criterion;
|
2018-07-07 14:43:29 +02:00
|
|
|
|
2018-08-25 13:15:04 +02:00
|
|
|
#[inline]
|
|
|
|
fn sum_matches_attribute_index(matches: &[Match]) -> u32 {
|
|
|
|
// note that GroupBy will never return an empty group
|
|
|
|
// so we can do this assumption safely
|
|
|
|
GroupBy::new(matches, match_query_index).map(|group| unsafe {
|
|
|
|
group.get_unchecked(0).attribute_index
|
|
|
|
}).sum()
|
2018-07-10 21:29:17 +02:00
|
|
|
}
|
2018-07-07 14:43:29 +02:00
|
|
|
|
2018-10-11 14:04:41 +02:00
|
|
|
#[derive(Debug, Clone, Copy)]
|
|
|
|
pub struct SumOfWordsPosition;
|
|
|
|
|
|
|
|
impl Criterion for SumOfWordsPosition {
|
|
|
|
fn evaluate(&self, lhs: &Document, rhs: &Document) -> Ordering {
|
|
|
|
let lhs = sum_matches_attribute_index(&lhs.matches);
|
|
|
|
let rhs = sum_matches_attribute_index(&rhs.matches);
|
2018-08-25 13:15:04 +02:00
|
|
|
|
2018-10-11 14:04:41 +02:00
|
|
|
lhs.cmp(&rhs)
|
|
|
|
}
|
2018-07-07 14:43:29 +02:00
|
|
|
}
|