feat: Introduce the revisited SortBy criterion

This commit is contained in:
Clément Renault 2019-02-02 14:42:12 +01:00
parent 2e905bac08
commit 5efbc5ceb3
No known key found for this signature in database
GPG Key ID: 0151CDAB43460DAE
2 changed files with 24 additions and 23 deletions

View File

@ -4,7 +4,7 @@ mod words_proximity;
mod sum_of_words_attribute; mod sum_of_words_attribute;
mod sum_of_words_position; mod sum_of_words_position;
mod exact; mod exact;
// mod sort_by; mod sort_by;
mod document_id; mod document_id;
use std::cmp::Ordering; use std::cmp::Ordering;
@ -17,7 +17,7 @@ pub use self::{
sum_of_words_attribute::SumOfWordsAttribute, sum_of_words_attribute::SumOfWordsAttribute,
sum_of_words_position::SumOfWordsPosition, sum_of_words_position::SumOfWordsPosition,
exact::Exact, exact::Exact,
// sort_by::SortBy, sort_by::SortBy,
document_id::DocumentId, document_id::DocumentId,
}; };

View File

@ -24,7 +24,7 @@ use crate::rank::RawDocument;
/// ///
/// # Example /// # Example
/// ///
/// ```no-test /// ```ignore
/// use serde_derive::Deserialize; /// use serde_derive::Deserialize;
/// use meilidb::rank::criterion::*; /// use meilidb::rank::criterion::*;
/// ///
@ -40,39 +40,40 @@ use crate::rank::RawDocument;
/// .add(SumOfWordsAttribute) /// .add(SumOfWordsAttribute)
/// .add(SumOfWordsPosition) /// .add(SumOfWordsPosition)
/// .add(Exact) /// .add(Exact)
/// .add(SortBy::<TimeOnly>::new()) /// .add(SortBy::<TimeOnly>::new(&view))
/// .add(DocumentId); /// .add(DocumentId);
/// ///
/// let criterion = builder.build(); /// let criterion = builder.build();
/// ///
/// ``` /// ```
pub struct SortBy<T> { pub struct SortBy<'a, T, D>
where D: Deref<Target=DB> + Send + Sync,
T: Send + Sync
{
view: &'a DatabaseView<D>,
_phantom: marker::PhantomData<T>, _phantom: marker::PhantomData<T>,
} }
impl<T> SortBy<T> { impl<'a, T, D> SortBy<'a, T, D>
pub fn new() -> Self { where D: Deref<Target=DB> + Send + Sync,
SortBy::default() T: Send + Sync
}
}
impl<T> Default for SortBy<T> {
fn default() -> SortBy<T> {
SortBy { _phantom: marker::PhantomData }
}
}
impl<T, D> Criterion<D> for SortBy<T>
where D: Deref<Target=DB>,
T: DeserializeOwned + Ord,
{ {
fn evaluate(&self, lhs: &RawDocument, rhs: &RawDocument, view: &DatabaseView<D>) -> Ordering { pub fn new(view: &'a DatabaseView<D>) -> Self {
let lhs = match view.document_by_id::<T>(lhs.id) { SortBy { view, _phantom: marker::PhantomData }
}
}
impl<'a, T, D> Criterion for SortBy<'a, T, D>
where D: Deref<Target=DB> + Send + Sync,
T: DeserializeOwned + Ord + Send + Sync,
{
fn evaluate(&self, lhs: &RawDocument, rhs: &RawDocument) -> Ordering {
let lhs = match self.view.document_by_id::<T>(lhs.id) {
Ok(doc) => Some(doc), Ok(doc) => Some(doc),
Err(e) => { eprintln!("{}", e); None }, Err(e) => { eprintln!("{}", e); None },
}; };
let rhs = match view.document_by_id::<T>(rhs.id) { let rhs = match self.view.document_by_id::<T>(rhs.id) {
Ok(doc) => Some(doc), Ok(doc) => Some(doc),
Err(e) => { eprintln!("{}", e); None }, Err(e) => { eprintln!("{}", e); None },
}; };