meilisearch/meilisearch-core/src/criterion/exact.rs

41 lines
1.2 KiB
Rust
Raw Normal View History

2019-12-12 00:02:10 +08:00
use std::cmp::{Ordering, Reverse};
use slice_group_by::GroupBy;
2019-12-13 18:14:12 +08:00
use crate::{RawDocument, MResult};
use crate::bucket_sort::BareMatch;
use super::{Criterion, Context, ContextMut};
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(())
}
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-12-12 00:02:10 +08:00
sum_exact_query_words
}
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()
}
}