meilisearch/src/rank/criterion/sum_of_words_position.rs

34 lines
904 B
Rust
Raw Normal View History

use std::cmp::Ordering;
use std::ops::Deref;
use rocksdb::DB;
use group_by::GroupBy;
use crate::database::DatabaseView;
use crate::rank::{match_query_index, Document};
2018-10-11 14:04:41 +02:00
use crate::rank::criterion::Criterion;
use crate::Match;
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-10-11 14:04:41 +02:00
#[derive(Debug, Clone, Copy)]
pub struct SumOfWordsPosition;
impl<D> Criterion<D> for SumOfWordsPosition
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 = 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)
}
}