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

32 lines
949 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" }
2019-12-13 11:14:12 +01:00
fn prepare<'h, 'p, 'tag, 'txn, 'q, 'a, 'r>(
2019-12-11 17:02:10 +01:00
&self,
2019-12-13 11:14:12 +01:00
ctx: ContextMut<'h, 'p, 'tag, 'txn, 'q, 'a>,
documents: &mut [RawDocument<'r, 'tag>],
2019-12-13 11:14:12 +01:00
) -> MResult<()>
{
prepare_query_distances(documents, ctx.query_enhancer, ctx.postings_lists);
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()
}
}