MeiliSearch/meilisearch-core/src/criterion/words_position.rs

44 lines
1.2 KiB
Rust
Raw Normal View History

2019-12-11 17:02:10 +01:00
use std::cmp::Ordering;
use slice_group_by::GroupBy;
use crate::RawDocument;
use crate::bucket_sort::SimpleMatch;
use super::{Criterion, Context, ContextMut, prepare_raw_matches};
2019-12-11 17:02:10 +01:00
pub struct WordsPosition;
impl Criterion for WordsPosition {
fn name(&self) -> &str { "words position" }
fn prepare<'p, 'tag, 'txn, 'q, 'a, 'r>(
2019-12-11 17:02:10 +01:00
&self,
ctx: ContextMut<'p, 'tag, 'txn, 'q, 'a>,
documents: &mut [RawDocument<'r, 'tag>],
2019-12-11 17:02:10 +01:00
) {
prepare_raw_matches(documents, ctx.postings_lists, ctx.query_enhancer, ctx.automatons);
2019-12-11 17:02:10 +01:00
}
fn evaluate<'p, 'tag, 'txn, 'q, 'a, 'r>(
2019-12-11 17:02:10 +01:00
&self,
ctx: &Context<'p, 'tag, 'txn, 'q, 'a>,
lhs: &RawDocument<'r, 'tag>,
rhs: &RawDocument<'r, 'tag>,
2019-12-11 17:02:10 +01:00
) -> Ordering
{
#[inline]
fn sum_words_position(matches: &[SimpleMatch]) -> usize {
let mut sum_words_position = 0;
for group in matches.linear_group_by_key(|bm| bm.query_index) {
sum_words_position += group[0].word_index as usize;
}
sum_words_position
}
let lhs = sum_words_position(&lhs.processed_matches);
let rhs = sum_words_position(&rhs.processed_matches);
lhs.cmp(&rhs)
}
}