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

32 lines
940 B
Rust
Raw Normal View History

2019-12-11 17:02:10 +01:00
use std::cmp::Ordering;
2019-12-13 11:14:12 +01:00
use crate::{RawDocument, MResult};
use super::{Criterion, Context, ContextMut, prepare_query_distances};
2019-12-11 17:02:10 +01:00
pub struct Words;
impl Criterion for Words {
fn name(&self) -> &str { "words" }
fn prepare<'h, 'p, 'tag, 'txn, 'q, 'r>(
2019-12-11 17:02:10 +01:00
&self,
ctx: ContextMut<'h, 'p, 'tag, 'txn, 'q>,
documents: &mut [RawDocument<'r, 'tag>],
2019-12-13 11:14:12 +01:00
) -> MResult<()>
{
prepare_query_distances(documents, ctx.query_mapping, ctx.postings_lists);
2019-12-13 11:14:12 +01:00
Ok(())
2019-12-11 17:02:10 +01:00
}
fn evaluate(&self, _ctx: &Context, lhs: &RawDocument, rhs: &RawDocument) -> Ordering {
2019-12-11 17:02:10 +01:00
#[inline]
fn number_of_query_words(distances: &[Option<u8>]) -> usize {
distances.iter().cloned().filter(Option::is_some).count()
}
let lhs = number_of_query_words(&lhs.processed_distances);
let rhs = number_of_query_words(&rhs.processed_distances);
lhs.cmp(&rhs).reverse()
}
}